Skip to content

Commit

Permalink
Convert Optional<CodeModel> to std::optional<CodeModel>
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Parzyszek committed Dec 3, 2022
1 parent d98c172 commit 8c7c20f
Show file tree
Hide file tree
Showing 67 changed files with 280 additions and 159 deletions.
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
#include "llvm/Transforms/Utils/SymbolRewriter.h"
#include <memory>
#include <optional>
using namespace clang;
using namespace llvm;

Expand Down Expand Up @@ -312,7 +313,7 @@ static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
}
}

static Optional<llvm::CodeModel::Model>
static std::optional<llvm::CodeModel::Model>
getCodeModel(const CodeGenOptions &CodeGenOpts) {
unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
.Case("tiny", llvm::CodeModel::Tiny)
Expand All @@ -324,7 +325,7 @@ getCodeModel(const CodeGenOptions &CodeGenOpts) {
.Default(~0u);
assert(CodeModel != ~0u && "invalid code model!");
if (CodeModel == ~1u)
return None;
return std::nullopt;
return static_cast<llvm::CodeModel::Model>(CodeModel);
}

Expand Down Expand Up @@ -572,7 +573,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
return;
}

Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
std::string FeaturesStr =
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
Expand Down
3 changes: 2 additions & 1 deletion lld/Common/TargetOptionsCommandFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llvm/ADT/Triple.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/Target/TargetOptions.h"
#include <optional>

llvm::TargetOptions lld::initTargetOptionsFromCodeGenFlags() {
return llvm::codegen::InitTargetOptionsFromCodeGenFlags(llvm::Triple());
Expand All @@ -19,7 +20,7 @@ llvm::Optional<llvm::Reloc::Model> lld::getRelocModelFromCMModel() {
return llvm::codegen::getExplicitRelocModel();
}

llvm::Optional<llvm::CodeModel::Model> lld::getCodeModelFromCMModel() {
std::optional<llvm::CodeModel::Model> lld::getCodeModelFromCMModel() {
return llvm::codegen::getExplicitCodeModel();
}

Expand Down
3 changes: 2 additions & 1 deletion lld/include/lld/Common/TargetOptionsCommandFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#include "llvm/ADT/Optional.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Target/TargetOptions.h"
#include <optional>

namespace lld {
llvm::TargetOptions initTargetOptionsFromCodeGenFlags();
llvm::Optional<llvm::Reloc::Model> getRelocModelFromCMModel();
llvm::Optional<llvm::CodeModel::Model> getCodeModelFromCMModel();
std::optional<llvm::CodeModel::Model> getCodeModelFromCMModel();
std::string getCPUStr();
std::vector<std::string> getMAttrs();
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/CodeGen/CommandFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Target/TargetOptions.h"
#include <optional>
#include <string>
#include <vector>

Expand All @@ -42,7 +43,7 @@ Optional<Reloc::Model> getExplicitRelocModel();
ThreadModel::Model getThreadModel();

CodeModel::Model getCodeModel();
Optional<CodeModel::Model> getExplicitCodeModel();
std::optional<CodeModel::Model> getExplicitCodeModel();

llvm::ExceptionHandling getExceptionModel();

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/ExecutionEngine/ExecutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -541,7 +542,7 @@ class EngineBuilder {
std::shared_ptr<LegacyJITSymbolResolver> Resolver;
TargetOptions Options;
Optional<Reloc::Model> RelocModel;
Optional<CodeModel::Model> CMModel;
std::optional<CodeModel::Model> CMModel;
std::string MArch;
std::string MCPU;
SmallVector<std::string, 4> MAttrs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -92,13 +93,13 @@ class JITTargetMachineBuilder {
const Optional<Reloc::Model> &getRelocationModel() const { return RM; }

/// Set the code model.
JITTargetMachineBuilder &setCodeModel(Optional<CodeModel::Model> CM) {
JITTargetMachineBuilder &setCodeModel(std::optional<CodeModel::Model> CM) {
this->CM = std::move(CM);
return *this;
}

/// Get the code model.
const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
const std::optional<CodeModel::Model> &getCodeModel() const { return CM; }

/// Set the LLVM CodeGen optimization level.
JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOpt::Level OptLevel) {
Expand Down Expand Up @@ -151,7 +152,7 @@ class JITTargetMachineBuilder {
SubtargetFeatures Features;
TargetOptions Options;
Optional<Reloc::Model> RM;
Optional<CodeModel::Model> CM;
std::optional<CodeModel::Model> CM;
CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
};

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <cstdint>
#include <iterator>
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -863,7 +864,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
/// @{

/// Returns the code model (tiny, small, kernel, medium or large model)
Optional<CodeModel::Model> getCodeModel() const;
std::optional<CodeModel::Model> getCodeModel() const;

/// Set the code model (tiny, small, kernel, medium or large)
void setCodeModel(CodeModel::Model CL);
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/LTO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/Target/TargetOptions.h"

#include <functional>
#include <optional>

namespace llvm {

Expand Down Expand Up @@ -52,7 +53,7 @@ struct Config {
/// For adding passes that run right before codegen.
std::function<void(legacy::PassManager &)> PreCodeGenPassesHook;
Optional<Reloc::Model> RelocModel = Reloc::PIC_;
Optional<CodeModel::Model> CodeModel = std::nullopt;
std::optional<CodeModel::Model> CodeModel = std::nullopt;
CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
CodeGenFileType CGFileType = CGFT_ObjectFile;
unsigned OptLevel = 2;
Expand Down
15 changes: 9 additions & 6 deletions llvm/include/llvm/MC/TargetRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <cstddef>
#include <iterator>
#include <memory>
#include <optional>
#include <string>

namespace llvm {
Expand Down Expand Up @@ -167,7 +168,7 @@ class Target {
using TargetMachineCtorTy = TargetMachine
*(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
const TargetOptions &Options, Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
// If it weren't for layering issues (this header is in llvm/Support, but
// depends on MC?) this should take the Streamer by value rather than rvalue
// reference.
Expand Down Expand Up @@ -481,7 +482,7 @@ class Target {
TargetMachine *
createTargetMachine(StringRef TT, StringRef CPU, StringRef Features,
const TargetOptions &Options, Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM = std::nullopt,
std::optional<CodeModel::Model> CM = std::nullopt,
CodeGenOpt::Level OL = CodeGenOpt::Default,
bool JIT = false) const {
if (!TargetMachineCtorFn)
Expand Down Expand Up @@ -1358,10 +1359,12 @@ template <class TargetMachineImpl> struct RegisterTargetMachine {
}

private:
static TargetMachine *
Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
static TargetMachine *Allocator(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
Optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOpt::Level OL, bool JIT) {
return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
}
};
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Target/CodeGenCWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#include "llvm/ADT/Optional.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/ErrorHandling.h"
#include <optional>

namespace llvm {

inline Optional<CodeModel::Model> unwrap(LLVMCodeModel Model, bool &JIT) {
inline std::optional<CodeModel::Model> unwrap(LLVMCodeModel Model, bool &JIT) {
JIT = false;
switch (Model) {
case LLVMCodeModelJITDefault:
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/Target/TargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Support/PGOOptions.h"
#include "llvm/Target/CGPassBuilderOption.h"
#include "llvm/Target/TargetOptions.h"
#include <optional>
#include <string>
#include <utility>

Expand Down Expand Up @@ -497,8 +498,9 @@ class LLVMTargetMachine : public TargetMachine {
/// CM does not have a value. The tiny and kernel models will produce
/// an error, so targets that support them or require more complex codemodel
/// selection logic should implement and call their own getEffectiveCodeModel.
inline CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
CodeModel::Model Default) {
inline CodeModel::Model
getEffectiveCodeModel(std::optional<CodeModel::Model> CM,
CodeModel::Model Default) {
if (CM) {
// By default, targets do not support the tiny and kernel models.
if (*CM == CodeModel::Tiny)
Expand Down
14 changes: 13 additions & 1 deletion llvm/lib/CodeGen/CommandFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include <optional>

using namespace llvm;

Expand Down Expand Up @@ -50,12 +51,23 @@ using namespace llvm;
return None; \
}

// Temporary macro for incremental transition to std::optional.
#define CGSTDOPT_EXP(TY, NAME) \
CGOPT(TY, NAME) \
std::optional<TY> codegen::getExplicit##NAME() { \
if (NAME##View->getNumOccurrences()) { \
TY res = *NAME##View; \
return res; \
} \
return std::nullopt; \
}

CGOPT(std::string, MArch)
CGOPT(std::string, MCPU)
CGLIST(std::string, MAttrs)
CGOPT_EXP(Reloc::Model, RelocModel)
CGOPT(ThreadModel::Model, ThreadModel)
CGOPT_EXP(CodeModel::Model, CodeModel)
CGSTDOPT_EXP(CodeModel::Model, CodeModel)
CGOPT(ExceptionHandling, ExceptionModel)
CGOPT_EXP(CodeGenFileType, FileType)
CGOPT(FramePointerKind, FramePointerUsage)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/Target/CodeGenCWrappers.h"
#include "llvm/Target/TargetOptions.h"
#include <cstring>
#include <optional>

using namespace llvm;

Expand Down Expand Up @@ -199,7 +200,7 @@ LLVMBool LLVMCreateMCJITCompilerForModule(
.setOptLevel((CodeGenOpt::Level)options.OptLevel)
.setTargetOptions(targetOptions);
bool JIT;
if (Optional<CodeModel::Model> CM = unwrap(options.CodeModel, JIT))
if (std::optional<CodeModel::Model> CM = unwrap(options.CodeModel, JIT))
builder.setCodeModel(*CM);
if (options.MCJMM)
builder.setMCJITMemoryManager(
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ void Module::setPIELevel(PIELevel::Level PL) {
addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
}

Optional<CodeModel::Model> Module::getCodeModel() const {
std::optional<CodeModel::Model> Module::getCodeModel() const {
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));

if (!Val)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/Transforms/Utils/SplitModule.h"
#include <optional>

using namespace llvm;
using namespace lto;
Expand Down Expand Up @@ -214,7 +215,7 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
RelocModel =
M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;

Optional<CodeModel::Model> CodeModel;
std::optional<CodeModel::Model> CodeModel;
if (Conf.CodeModel)
CodeModel = *Conf.CodeModel;
else
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ static Reloc::Model getEffectiveRelocModel(const Triple &TT,
}

static CodeModel::Model
getEffectiveAArch64CodeModel(const Triple &TT, Optional<CodeModel::Model> CM,
bool JIT) {
getEffectiveAArch64CodeModel(const Triple &TT,
std::optional<CodeModel::Model> CM, bool JIT) {
if (CM) {
if (*CM != CodeModel::Small && *CM != CodeModel::Tiny &&
*CM != CodeModel::Large) {
Expand All @@ -316,7 +316,7 @@ AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM,
std::optional<CodeModel::Model> CM,
CodeGenOpt::Level OL, bool JIT,
bool LittleEndian)
: LLVMTargetMachine(T,
Expand Down Expand Up @@ -455,15 +455,15 @@ void AArch64leTargetMachine::anchor() { }
AArch64leTargetMachine::AArch64leTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
: AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}

void AArch64beTargetMachine::anchor() { }

AArch64beTargetMachine::AArch64beTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
: AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}

namespace {
Expand Down
Loading

0 comments on commit 8c7c20f

Please sign in to comment.