Skip to content

Commit d2e14ac

Browse files
authored
Remove llvm::Optional usage (#149)
1 parent 37d7f8c commit d2e14ac

30 files changed

+148
-154
lines changed

lib/passes/Commandline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ CommandLineOptions::CommandLineOptions() {
206206
};
207207
}
208208

209-
llvm::Optional<typeart::config::OptionValue> CommandLineOptions::getValue(std::string_view opt_path) const {
209+
std::optional<typeart::config::OptionValue> CommandLineOptions::getValue(std::string_view opt_path) const {
210210
auto key = llvm::StringRef(opt_path.data());
211211
if (mapping_.count(key) != 0U) {
212212
return mapping_.lookup(key);
213213
}
214-
return llvm::None;
214+
return {};
215215
}
216216

217217
[[maybe_unused]] bool CommandLineOptions::valueSpecified(std::string_view opt_path) const {

lib/passes/Commandline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CommandLineOptions final : public config::Configuration {
3030

3131
public:
3232
CommandLineOptions();
33-
[[nodiscard]] llvm::Optional<config::OptionValue> getValue(std::string_view opt_path) const override;
33+
[[nodiscard]] std::optional<config::OptionValue> getValue(std::string_view opt_path) const override;
3434
[[maybe_unused]] [[nodiscard]] bool valueSpecified(std::string_view opt_path) const;
3535
};
3636

lib/passes/TypeARTConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TypeARTConfiguration::TypeARTConfiguration(std::unique_ptr<file::FileOptions> co
2222
: configuration_options_(std::move(config_options)), commandline_options_(std::move(commandline_options)) {
2323
}
2424

25-
llvm::Optional<OptionValue> TypeARTConfiguration::getValue(std::string_view opt_path) const {
25+
std::optional<OptionValue> TypeARTConfiguration::getValue(std::string_view opt_path) const {
2626
const bool use_cl = prioritize_commandline && commandline_options_->valueSpecified(opt_path);
2727
if (use_cl) {
2828
LOG_DEBUG("Take CL arg for " << opt_path.data())

lib/passes/TypeARTConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TypeARTConfiguration final : public Configuration {
3737
TypeARTConfiguration(std::unique_ptr<file::FileOptions> config_options,
3838
std::unique_ptr<cl::CommandLineOptions> commandline_options);
3939
void prioritizeCommandline(bool do_prioritize);
40-
[[nodiscard]] llvm::Optional<OptionValue> getValue(std::string_view opt_path) const override;
40+
[[nodiscard]] std::optional<OptionValue> getValue(std::string_view opt_path) const override;
4141
[[nodiscard]] OptionValue getValueOr(std::string_view opt_path, OptionValue alt) const override;
4242
[[nodiscard]] OptionValue operator[](std::string_view opt_path) const override;
4343
void emitTypeartFileConfiguration(llvm::raw_ostream& out_stream);

lib/passes/TypeARTPass.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include <cassert>
4040
#include <cstddef>
41+
#include <optional>
4142
#include <sstream>
4243
#include <string>
4344
#include <utility>
@@ -72,7 +73,7 @@ ALWAYS_ENABLED_STATISTIC(NumInstrumentedGlobal, "Number of instrumented globals"
7273

7374
namespace typeart::pass {
7475

75-
llvm::Optional<std::string> get_configuration_file_path() {
76+
std::optional<std::string> get_configuration_file_path() {
7677
if (!cl_typeart_configuration_file.empty()) {
7778
LOG_DEBUG("Using cl::opt for config file " << cl_typeart_configuration_file.getValue());
7879
return cl_typeart_configuration_file.getValue();
@@ -83,7 +84,7 @@ llvm::Optional<std::string> get_configuration_file_path() {
8384
return std::string{config_file};
8485
}
8586
LOG_INFO("No configuration file set.")
86-
return llvm::None;
87+
return {};
8788
}
8889

8990
// Used by LLVM pass manager to identify passes in memory
@@ -109,7 +110,7 @@ bool TypeArtPass::doInitialization(Module& m) {
109110
if (!config_file_path) {
110111
pass_config = std::make_unique<config::cl::CommandLineOptions>();
111112
} else {
112-
auto typeart_config = config::make_typeart_configuration({config_file_path.getValue()});
113+
auto typeart_config = config::make_typeart_configuration({config_file_path.value()});
113114
if (typeart_config) {
114115
{
115116
std::string typeart_conf_str;

lib/passes/analysis/MemOpData.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
#ifndef TYPEART_MEMOPDATA_H
1414
#define TYPEART_MEMOPDATA_H
1515

16-
#include "llvm/ADT/Optional.h"
1716
#include "llvm/ADT/SmallPtrSet.h"
1817
#include "llvm/ADT/SmallVector.h"
1918
#include "llvm/ADT/StringMap.h"
2019

20+
#include <optional>
21+
2122
namespace llvm {
2223
class CallBase;
2324
class BitCastInst;
@@ -45,28 +46,28 @@ enum class MemOpKind : uint8_t {
4546
};
4647

4748
struct MemOps {
48-
inline llvm::Optional<MemOpKind> kind(llvm::StringRef function) const {
49+
inline std::optional<MemOpKind> kind(llvm::StringRef function) const {
4950
if (auto alloc = allocKind(function)) {
5051
return alloc;
5152
}
5253
if (auto dealloc = deallocKind(function)) {
5354
return dealloc;
5455
}
55-
return llvm::None;
56+
return {};
5657
}
5758

58-
inline llvm::Optional<MemOpKind> allocKind(llvm::StringRef function) const {
59+
inline std::optional<MemOpKind> allocKind(llvm::StringRef function) const {
5960
if (auto it = alloc_map.find(function); it != std::end(alloc_map)) {
6061
return {(*it).second};
6162
}
62-
return llvm::None;
63+
return {};
6364
}
6465

65-
inline llvm::Optional<MemOpKind> deallocKind(llvm::StringRef function) const {
66+
inline std::optional<MemOpKind> deallocKind(llvm::StringRef function) const {
6667
if (auto it = dealloc_map.find(function); it != std::end(dealloc_map)) {
6768
return {(*it).second};
6869
}
69-
return llvm::None;
70+
return {};
7071
}
7172

7273
const llvm::StringMap<MemOpKind>& allocs() const {
@@ -129,7 +130,7 @@ struct ArrayCookieData {
129130

130131
struct MallocData {
131132
llvm::CallBase* call{nullptr};
132-
llvm::Optional<ArrayCookieData> array_cookie{llvm::None};
133+
std::optional<ArrayCookieData> array_cookie{};
133134
llvm::BitCastInst* primary{nullptr}; // Non-null if non (void*) cast exists
134135
llvm::SmallPtrSet<llvm::BitCastInst*, 4> bitcasts;
135136
MemOpKind kind;
@@ -138,7 +139,7 @@ struct MallocData {
138139

139140
struct FreeData {
140141
llvm::CallBase* call{nullptr};
141-
llvm::Optional<llvm::GetElementPtrInst*> array_cookie_gep{llvm::None};
142+
std::optional<llvm::GetElementPtrInst*> array_cookie_gep{};
142143
MemOpKind kind;
143144
bool is_invoke{false};
144145
};

lib/passes/analysis/MemOpVisitor.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
#include "support/Logger.h"
1919
#include "support/TypeUtil.h"
2020

21-
#include "llvm/ADT/None.h"
22-
#include "llvm/ADT/Optional.h"
2321
#include "llvm/ADT/STLExtras.h"
2422
#include "llvm/ADT/SmallPtrSet.h"
2523
#include "llvm/ADT/StringRef.h"
24+
2625
#if LLVM_VERSION_MAJOR >= 12
2726
#include "llvm/Analysis/ValueTracking.h" // llvm::findAllocaForValue
2827
#else
@@ -38,6 +37,7 @@
3837
#include "llvm/Support/raw_ostream.h"
3938

4039
#include <cstddef>
40+
#include <optional>
4141

4242
namespace typeart::analysis {
4343

@@ -82,26 +82,26 @@ void MemOpVisitor::visitCallBase(llvm::CallBase& cb) {
8282
if (!collect_heap) {
8383
return;
8484
}
85-
const auto isInSet = [&](const auto& fMap) -> llvm::Optional<MemOpKind> {
85+
const auto isInSet = [&](const auto& fMap) -> std::optional<MemOpKind> {
8686
const auto* f = cb.getCalledFunction();
8787
if (!f) {
8888
// TODO handle calls through, e.g., function pointers? - seems infeasible
8989
// LOG_INFO("Encountered indirect call, skipping.");
90-
return None;
90+
return {};
9191
}
9292
const auto name = f->getName().str();
9393

9494
const auto res = fMap.find(name);
9595
if (res != fMap.end()) {
9696
return {(*res).second};
9797
}
98-
return None;
98+
return {};
9999
};
100100

101101
if (auto alloc_val = isInSet(mem_operations.allocs())) {
102-
visitMallocLike(cb, alloc_val.getValue());
102+
visitMallocLike(cb, alloc_val.value());
103103
} else if (auto dealloc_val = isInSet(mem_operations.deallocs())) {
104-
visitFreeLike(cb, dealloc_val.getValue());
104+
visitFreeLike(cb, dealloc_val.value());
105105
}
106106
}
107107

@@ -216,8 +216,8 @@ llvm::Expected<ArrayCookieData> handlePaddedArrayCookie(const MallocGeps& geps,
216216
return {ArrayCookieData{*cookie_store, array_gep}};
217217
}
218218

219-
llvm::Optional<ArrayCookieData> handleArrayCookie(const MallocGeps& geps, MallocBcasts& bcasts,
220-
BitCastInst*& primary_cast) {
219+
std::optional<ArrayCookieData> handleArrayCookie(const MallocGeps& geps, MallocBcasts& bcasts,
220+
BitCastInst*& primary_cast) {
221221
auto exit_on_error = llvm::ExitOnError{"Array Cookie Detection failed!"};
222222
if (geps.size() == 1) {
223223
return exit_on_error(handleUnpaddedArrayCookie(geps, bcasts, primary_cast));
@@ -232,7 +232,7 @@ llvm::Optional<ArrayCookieData> handleArrayCookie(const MallocGeps& geps, Malloc
232232
LOG_FATAL(err);
233233
exit_on_error({error::make_string_error(err)});
234234
}
235-
return llvm::None;
235+
return {};
236236
}
237237

238238
void MemOpVisitor::visitMallocLike(llvm::CallBase& ci, MemOpKind k) {
@@ -253,12 +253,12 @@ void MemOpVisitor::visitFreeLike(llvm::CallBase& ci, MemOpKind k) {
253253
if (auto f = ci.getCalledFunction()) {
254254
auto dkind = mem_operations.deallocKind(f->getName());
255255
if (dkind) {
256-
kind = dkind.getValue();
256+
kind = dkind.value();
257257
}
258258
}
259259

260260
auto gep = dyn_cast<GetElementPtrInst>(ci.getArgOperand(0));
261-
auto array_cookie_gep = gep != nullptr ? llvm::Optional<llvm::GetElementPtrInst*>{gep} : llvm::None;
261+
auto array_cookie_gep = gep != nullptr ? std::optional<llvm::GetElementPtrInst*>{gep} : std::nullopt;
262262
frees.emplace_back(FreeData{&ci, array_cookie_gep, kind, isa<InvokeInst>(ci)});
263263
}
264264

lib/passes/filter/CGInterface.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "support/Logger.h"
1616
#include "support/Util.h"
1717

18-
#include "llvm/ADT/Optional.h"
1918
#include "llvm/Support/Error.h"
2019
#include "llvm/Support/ErrorOr.h"
2120
#include "llvm/Support/JSON.h"
@@ -137,7 +136,7 @@ void JSONCG::construct_call_information(const std::string& entry_caller, const l
137136
const auto hasBody = caller->get("hasBody");
138137
if (hasBody != nullptr) {
139138
assert(hasBody->kind() == llvm::json::Value::Kind::Boolean && "hasBody must be boolean");
140-
hasBodyMap[entry_caller] = hasBody->getAsBoolean().getValue();
139+
hasBodyMap[entry_caller] = *hasBody->getAsBoolean();
141140
}
142141
const auto calls = caller->getArray("callees");
143142
assert(calls != nullptr && "Json callee information is missing");
@@ -146,9 +145,9 @@ void JSONCG::construct_call_information(const std::string& entry_caller, const l
146145
for (const auto& callee : *calls) {
147146
assert(callee.kind() == llvm::json::Value::Kind::String && "Callees must be strings");
148147
const auto callee_json_string = callee.getAsString();
149-
assert(callee_json_string.hasValue() && "Could not get callee as string");
150-
if (callee_json_string.hasValue()) {
151-
const std::string callee_string = std::string{callee_json_string.getValue()};
148+
assert(callee_json_string && "Could not get callee as string");
149+
if (callee_json_string) {
150+
const std::string callee_string = std::string{*callee_json_string};
152151
directly_called_functions[entry_caller].insert(callee_string);
153152
}
154153
}
@@ -159,9 +158,9 @@ void JSONCG::construct_call_information(const std::string& entry_caller, const l
159158
for (const auto& function : *overridingFunctions) {
160159
assert(function.kind() == llvm::json::Value::Kind::String && "Function names are always strings");
161160
const auto functionStr = function.getAsString();
162-
assert(functionStr.hasValue() && "Retrieving overriding function as String failed");
163-
if (functionStr.hasValue()) {
164-
const std::string functionName = std::string{functionStr.getValue()};
161+
assert(functionStr && "Retrieving overriding function as String failed");
162+
if (functionStr) {
163+
const std::string functionName = std::string{*functionStr};
165164
virtualTargets[entry_caller].insert(functionName);
166165
}
167166
}

lib/passes/filter/FilterBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class BaseFilter : public Filter {
119119
continue;
120120
}
121121

122-
llvm::CallSite c(csite.getValue());
122+
llvm::CallSite c(csite.value());
123123
if (fpath.contains(c)) {
124124
// Avoid recursion:
125125
// TODO a continue may be wrong, if the function itself eventually calls "MPI"?
@@ -151,7 +151,7 @@ class BaseFilter : public Filter {
151151
if (OmpHelper::isOmpExecutor(c)) {
152152
auto outlined = OmpHelper::getMicrotask(c);
153153
if (outlined) {
154-
path2def.push(outlined.getValue());
154+
path2def.push(outlined.value());
155155
}
156156
}
157157
}

lib/passes/filter/FilterUtil.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "support/DefUseChain.h"
2020
#include "support/Logger.h"
2121

22-
#include "llvm/ADT/None.h"
23-
#include "llvm/ADT/Optional.h"
2422
#include "llvm/ADT/STLExtras.h"
2523
#include "llvm/ADT/SmallVector.h"
2624
#include "llvm/ADT/iterator_range.h"
@@ -76,7 +74,7 @@ inline std::pair<llvm::Argument*, int> findArg(CallSite c, const Path& p) {
7674
return {nullptr, -1};
7775
}
7876

79-
Value* in = arg.getValue();
77+
Value* in = arg.value();
8078
const auto arg_pos = llvm::find_if(c.args(), [&in](const Use& arg_use) -> bool { return arg_use.get() == in; });
8179

8280
if (arg_pos == c.arg_end()) {
@@ -90,7 +88,7 @@ inline std::pair<llvm::Argument*, int> findArg(CallSite c, const Path& p) {
9088
if (outlined) {
9189
// Calc the offset of arg in executor to actual arg of the outline function:
9290
auto offset = omp::OmpContext::getArgOffsetToMicrotask(c, arg_num);
93-
Argument* argument = (outlined.getValue()->arg_begin() + offset);
91+
Argument* argument = (outlined.value()->arg_begin() + offset);
9492
return {argument, offset};
9593
}
9694
}

0 commit comments

Comments
 (0)