Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Delete Default and JITDefault code models
Browse files Browse the repository at this point in the history
IMHO it is an antipattern to have a enum value that is Default.

At any given piece of code it is not clear if we have to handle
Default or if has already been mapped to a concrete value. In this
case in particular, only the target can do the mapping and it is nice
to make sure it is always done.

This deletes the two default enum values of CodeModel and uses an
explicit Optional<CodeModel> when it is possible that it is
unspecified.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@309911 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Aug 3, 2017
1 parent 65d41d8 commit 9aafb85
Show file tree
Hide file tree
Showing 64 changed files with 420 additions and 468 deletions.
28 changes: 14 additions & 14 deletions include/llvm/CodeGen/CommandFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ TMModel("thread-model",
clEnumValN(ThreadModel::Single, "single",
"Single thread model")));

cl::opt<llvm::CodeModel::Model>
CMModel("code-model",
cl::desc("Choose code model"),
cl::init(CodeModel::Default),
cl::values(clEnumValN(CodeModel::Default, "default",
"Target default code model"),
clEnumValN(CodeModel::Small, "small",
"Small code model"),
clEnumValN(CodeModel::Kernel, "kernel",
"Kernel code model"),
clEnumValN(CodeModel::Medium, "medium",
"Medium code model"),
clEnumValN(CodeModel::Large, "large",
"Large code model")));
cl::opt<llvm::CodeModel::Model> CMModel(
"code-model", cl::desc("Choose code model"),
cl::values(clEnumValN(CodeModel::Small, "small", "Small code model"),
clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
clEnumValN(CodeModel::Large, "large", "Large code model")));

static inline Optional<CodeModel::Model> getCodeModel() {
if (CMModel.getNumOccurrences()) {
CodeModel::Model M = CMModel;
return M;
}
return None;
}

cl::opt<llvm::ExceptionHandling>
ExceptionModel("exception-model",
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/ExecutionEngine/ExecutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class EngineBuilder {
std::shared_ptr<JITSymbolResolver> Resolver;
TargetOptions Options;
Optional<Reloc::Model> RelocModel;
CodeModel::Model CMModel;
Optional<CodeModel::Model> CMModel;
std::string MArch;
std::string MCPU;
SmallVector<std::string, 4> MAttrs;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/LTO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Config {
TargetOptions Options;
std::vector<std::string> MAttrs;
Optional<Reloc::Model> RelocModel = Reloc::PIC_;
CodeModel::Model CodeModel = CodeModel::Default;
Optional<CodeModel::Model> CodeModel = None;
CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
TargetMachine::CodeGenFileType CGFileType = TargetMachine::CGFT_ObjectFile;
unsigned OptLevel = 2;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Support/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace llvm {
// Code model types.
namespace CodeModel {
// Sync changes with CodeGenCWrappers.h.
enum Model { Default, JITDefault, Small, Kernel, Medium, Large };
enum Model { Small, Kernel, Medium, Large };
}

namespace PICLevel {
Expand Down
15 changes: 6 additions & 9 deletions include/llvm/Support/CodeGenCWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@

namespace llvm {

inline CodeModel::Model unwrap(LLVMCodeModel Model) {
inline Optional<CodeModel::Model> unwrap(LLVMCodeModel Model, bool &JIT) {
JIT = false;
switch (Model) {
case LLVMCodeModelDefault:
return CodeModel::Default;
case LLVMCodeModelJITDefault:
return CodeModel::JITDefault;
JIT = true;
case LLVMCodeModelDefault:
return None;
case LLVMCodeModelSmall:
return CodeModel::Small;
case LLVMCodeModelKernel:
Expand All @@ -37,15 +38,11 @@ inline CodeModel::Model unwrap(LLVMCodeModel Model) {
case LLVMCodeModelLarge:
return CodeModel::Large;
}
return CodeModel::Default;
return CodeModel::Small;
}

inline LLVMCodeModel wrap(CodeModel::Model Model) {
switch (Model) {
case CodeModel::Default:
return LLVMCodeModelDefault;
case CodeModel::JITDefault:
return LLVMCodeModelJITDefault;
case CodeModel::Small:
return LLVMCodeModelSmall;
case CodeModel::Kernel:
Expand Down
55 changes: 17 additions & 38 deletions include/llvm/Support/TargetRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,16 @@ class Target {

using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
const Triple &TT);
using MCAdjustCodeGenOptsFnTy = void (*)(const Triple &TT, Reloc::Model RM,
CodeModel::Model &CM);

using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
StringRef CPU,
StringRef Features);
using TargetMachineCtorTy = TargetMachine *(*)(
const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
const TargetOptions &Options, Optional<Reloc::Model> RM,
CodeModel::Model CM, CodeGenOpt::Level OL);
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);
// 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 @@ -191,8 +188,6 @@ class Target {
/// registered.
MCAsmInfoCtorFnTy MCAsmInfoCtorFn;

MCAdjustCodeGenOptsFnTy MCAdjustCodeGenOptsFn;

/// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
/// if registered.
MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
Expand Down Expand Up @@ -312,12 +307,6 @@ class Target {
return MCAsmInfoCtorFn(MRI, Triple(TheTriple));
}

void adjustCodeGenOpts(const Triple &TT, Reloc::Model RM,
CodeModel::Model &CM) const {
if (MCAdjustCodeGenOptsFn)
MCAdjustCodeGenOptsFn(TT, RM, CM);
}

/// createMCInstrInfo - Create a MCInstrInfo implementation.
///
MCInstrInfo *createMCInstrInfo() const {
Expand Down Expand Up @@ -365,15 +354,17 @@ class Target {
/// feature set; it should always be provided. Generally this should be
/// either the target triple from the module, or the target triple of the
/// host if that does not exist.
TargetMachine *
createTargetMachine(StringRef TT, StringRef CPU, StringRef Features,
const TargetOptions &Options, Optional<Reloc::Model> RM,
CodeModel::Model CM = CodeModel::Default,
CodeGenOpt::Level OL = CodeGenOpt::Default) const {
TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
StringRef Features,
const TargetOptions &Options,
Optional<Reloc::Model> RM,
Optional<CodeModel::Model> CM = None,
CodeGenOpt::Level OL = CodeGenOpt::Default,
bool JIT = false) const {
if (!TargetMachineCtorFn)
return nullptr;
return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
CM, OL);
CM, OL, JIT);
}

/// createMCAsmBackend - Create a target specific assembly parser.
Expand Down Expand Up @@ -663,11 +654,6 @@ struct TargetRegistry {
T.MCAsmInfoCtorFn = Fn;
}

static void registerMCAdjustCodeGenOpts(Target &T,
Target::MCAdjustCodeGenOptsFnTy Fn) {
T.MCAdjustCodeGenOptsFn = Fn;
}

/// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
/// given target.
///
Expand Down Expand Up @@ -929,12 +915,6 @@ struct RegisterMCAsmInfoFn {
}
};

struct RegisterMCAdjustCodeGenOptsFn {
RegisterMCAdjustCodeGenOptsFn(Target &T, Target::MCAdjustCodeGenOptsFnTy Fn) {
TargetRegistry::registerMCAdjustCodeGenOpts(T, Fn);
}
};

/// RegisterMCInstrInfo - Helper template for registering a target instruction
/// info implementation. This invokes the static "Create" method on the class
/// to actually do the construction. Usage:
Expand Down Expand Up @@ -1080,12 +1060,11 @@ 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,
CodeModel::Model CM, CodeGenOpt::Level OL) {
return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL);
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) {
return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Target/TargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TargetMachine {
std::string TargetFS;

Reloc::Model RM = Reloc::Static;
CodeModel::Model CMModel = CodeModel::Default;
CodeModel::Model CMModel = CodeModel::Small;
CodeGenOpt::Level OptLevel = CodeGenOpt::Default;

/// Contains target specific asm information.
Expand Down
1 change: 0 additions & 1 deletion lib/CodeGen/LLVMTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
Reloc::Model RM, CodeModel::Model CM,
CodeGenOpt::Level OL)
: TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
T.adjustCodeGenOpts(TT, RM, CM);
this->RM = RM;
this->CMModel = CM;
this->OptLevel = OL;
Expand Down
2 changes: 1 addition & 1 deletion lib/ExecutionEngine/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
: M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
CMModel(CodeModel::JITDefault), UseOrcMCJITReplacement(false) {
UseOrcMCJITReplacement(false) {
// IR module verification is enabled by default in debug builds, and disabled
// by default in release builds.
#ifndef NDEBUG
Expand Down
4 changes: 3 additions & 1 deletion lib/ExecutionEngine/ExecutionEngineBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ LLVMBool LLVMCreateMCJITCompilerForModule(
builder.setEngineKind(EngineKind::JIT)
.setErrorStr(&Error)
.setOptLevel((CodeGenOpt::Level)options.OptLevel)
.setCodeModel(unwrap(options.CodeModel))
.setTargetOptions(targetOptions);
bool JIT;
if (Optional<CodeModel::Model> CM = unwrap(options.CodeModel, JIT))
builder.setCodeModel(*CM);
if (options.MCJMM)
builder.setMCJITMemoryManager(
std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
Expand Down
9 changes: 4 additions & 5 deletions lib/ExecutionEngine/TargetSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
}

// Allocate a target...
TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
MCPU, FeaturesStr,
Options,
RelocModel, CMModel,
OptLevel);
TargetMachine *Target =
TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
Options, RelocModel, CMModel, OptLevel,
/*JIT*/ true);
assert(Target && "Could not allocate target machine!");
return Target;
}
5 changes: 4 additions & 1 deletion lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ static void computeCacheKey(
AddUnsigned(*Conf.RelocModel);
else
AddUnsigned(-1);
AddUnsigned(Conf.CodeModel);
if (Conf.CodeModel)
AddUnsigned(*Conf.CodeModel);
else
AddUnsigned(-1);
AddUnsigned(Conf.CGOptLevel);
AddUnsigned(Conf.CGFileType);
AddUnsigned(Conf.OptLevel);
Expand Down
5 changes: 2 additions & 3 deletions lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,8 @@ bool LTOCodeGenerator::determineTarget() {
}

std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
return std::unique_ptr<TargetMachine>(
MArch->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
RelocModel, CodeModel::Default, CGOptLevel));
return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
TripleStr, MCpu, FeatureStr, Options, RelocModel, None, CGOptLevel));
}

// If a linkonce global is present in the MustPreserveSymbols, we need to make
Expand Down
6 changes: 3 additions & 3 deletions lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,9 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
Features.getDefaultSubtargetFeatures(TheTriple);
std::string FeatureStr = Features.getString();

return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
CodeModel::Default, CGOptLevel));
return std::unique_ptr<TargetMachine>(
TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
RelocModel, None, CGOptLevel));
}

/**
Expand Down
52 changes: 38 additions & 14 deletions lib/Target/AArch64/AArch64TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,42 @@ static Reloc::Model getEffectiveRelocModel(const Triple &TT,
return *RM;
}

static CodeModel::Model getEffectiveCodeModel(const Triple &TT,
Optional<CodeModel::Model> CM,
bool JIT) {
if (CM) {
if (*CM != CodeModel::Small && *CM != CodeModel::Large) {
if (!TT.isOSFuchsia())
report_fatal_error(
"Only small and large code models are allowed on AArch64");
else if (CM != CodeModel::Kernel)
report_fatal_error(
"Only small, kernel, and large code models are allowed on AArch64");
}
return *CM;
}
// The default MCJIT memory managers make no guarantees about where they can
// find an executable page; JITed code needs to be able to refer to globals
// no matter how far away they are.
if (JIT)
return CodeModel::Large;
return CodeModel::Small;
}

/// Create an AArch64 architecture model.
///
AArch64TargetMachine::AArch64TargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, Optional<Reloc::Model> RM,
CodeModel::Model CM, CodeGenOpt::Level OL, bool LittleEndian)
: LLVMTargetMachine(T, computeDataLayout(TT, Options.MCOptions,
LittleEndian),
TT, CPU, FS, Options,
getEffectiveRelocModel(TT, RM), CM, OL),
TLOF(createTLOF(getTargetTriple())),
isLittle(LittleEndian) {
AArch64TargetMachine::AArch64TargetMachine(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,
bool LittleEndian)
: LLVMTargetMachine(T,
computeDataLayout(TT, Options.MCOptions, LittleEndian),
TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
getEffectiveCodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
initAsmInfo();
}

Expand Down Expand Up @@ -252,16 +276,16 @@ void AArch64leTargetMachine::anchor() { }
AArch64leTargetMachine::AArch64leTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, Optional<Reloc::Model> RM,
CodeModel::Model CM, CodeGenOpt::Level OL)
: AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
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,
CodeModel::Model CM, CodeGenOpt::Level OL)
: AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
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 9aafb85

Please sign in to comment.