Skip to content

Commit

Permalink
codegen: remove jl_data_layout and jl_TargetMachine (#43802)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi authored Mar 3, 2022
1 parent 8eb872c commit 82dc130
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 140 deletions.
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ $(BUILDDIR)/jltypes.o $(BUILDDIR)/jltypes.dbg.obj: $(SRCDIR)/builtin_proto.h
$(build_shlibdir)/libllvmcalltest.$(SHLIB_EXT): $(SRCDIR)/codegen_shared.h $(BUILDDIR)/julia_version.h
$(BUILDDIR)/llvm-alloc-helpers.o $(BUILDDIR)/llvm-alloc-helpers.dbg.obj: $(SRCDIR)/codegen_shared.h $(SRCDIR)/llvm-pass-helpers.h $(SRCDIR)/llvm-alloc-helpers.h
$(BUILDDIR)/llvm-alloc-opt.o $(BUILDDIR)/llvm-alloc-opt.dbg.obj: $(SRCDIR)/codegen_shared.h $(SRCDIR)/llvm-pass-helpers.h $(SRCDIR)/llvm-alloc-helpers.h
$(BUILDDIR)/llvm-cpufeatures.o $(BUILDDIR)/llvm-cpufeatures.dbg.obj: $(SRCDIR)/jitlayers.h
$(BUILDDIR)/llvm-final-gc-lowering.o $(BUILDDIR)/llvm-final-gc-lowering.dbg.obj: $(SRCDIR)/llvm-pass-helpers.h
$(BUILDDIR)/llvm-gc-invariant-verifier.o $(BUILDDIR)/llvm-gc-invariant-verifier.dbg.obj: $(SRCDIR)/codegen_shared.h
$(BUILDDIR)/llvm-julia-licm.o $(BUILDDIR)/llvm-julia-licm.dbg.obj: $(SRCDIR)/codegen_shared.h $(SRCDIR)/llvm-alloc-helpers.h $(SRCDIR)/llvm-pass-helpers.h
Expand Down
22 changes: 10 additions & 12 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ void jl_dump_native_impl(void *native_code,
// We don't want to use MCJIT's target machine because
// it uses the large code model and we may potentially
// want less optimizations there.
Triple TheTriple = Triple(jl_TargetMachine->getTargetTriple());
Triple TheTriple = Triple(jl_ExecutionEngine->getTargetTriple());
// make sure to emit the native object format, even if FORCE_ELF was set in codegen
#if defined(_OS_WINDOWS_)
TheTriple.setObjectFormat(Triple::COFF);
Expand All @@ -454,11 +454,11 @@ void jl_dump_native_impl(void *native_code,
TheTriple.setOS(llvm::Triple::MacOSX);
#endif
std::unique_ptr<TargetMachine> TM(
jl_TargetMachine->getTarget().createTargetMachine(
jl_ExecutionEngine->getTargetMachine().getTarget().createTargetMachine(
TheTriple.getTriple(),
jl_TargetMachine->getTargetCPU(),
jl_TargetMachine->getTargetFeatureString(),
jl_TargetMachine->Options,
jl_ExecutionEngine->getTargetMachine().getTargetCPU(),
jl_ExecutionEngine->getTargetMachine().getTargetFeatureString(),
jl_ExecutionEngine->getTargetMachine().Options,
#if defined(_OS_LINUX_) || defined(_OS_FREEBSD_)
Reloc::PIC_,
#else
Expand Down Expand Up @@ -509,9 +509,7 @@ void jl_dump_native_impl(void *native_code,

// Reset the target triple to make sure it matches the new target machine
data->M->setTargetTriple(TM->getTargetTriple().str());
DataLayout DL = TM->createDataLayout();
DL.reset(DL.getStringRepresentation() + "-ni:10:11:12:13");
data->M->setDataLayout(DL);
data->M->setDataLayout(create_jl_data_layout(*TM));
Type *T_size;
if (sizeof(size_t) == 8)
T_size = Type::getInt64Ty(Context);
Expand Down Expand Up @@ -850,9 +848,9 @@ class JuliaPipeline : public Pass {
(void)jl_init_llvm();
PMTopLevelManager *TPM = Stack.top()->getTopLevelManager();
TPMAdapter Adapter(TPM);
addTargetPasses(&Adapter, jl_TargetMachine);
addTargetPasses(&Adapter, &jl_ExecutionEngine->getTargetMachine());
addOptimizationPasses(&Adapter, OptLevel);
addMachinePasses(&Adapter, jl_TargetMachine, OptLevel);
addMachinePasses(&Adapter, &jl_ExecutionEngine->getTargetMachine(), OptLevel);
}
JuliaPipeline() : Pass(PT_PassManager, ID) {}
Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const override {
Expand Down Expand Up @@ -979,9 +977,9 @@ void *jl_get_llvmf_defn_impl(jl_method_instance_t *mi, LLVMContextRef ctxt, size
static legacy::PassManager *PM;
if (!PM) {
PM = new legacy::PassManager();
addTargetPasses(PM, jl_TargetMachine);
addTargetPasses(PM, &jl_ExecutionEngine->getTargetMachine());
addOptimizationPasses(PM, jl_options.opt_level);
addMachinePasses(PM, jl_TargetMachine, jl_options.opt_level);
addMachinePasses(PM, &jl_ExecutionEngine->getTargetMachine(), jl_options.opt_level);
}

// get the source code for this function
Expand Down
113 changes: 10 additions & 103 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include "llvm-version.h"
#include "platform.h"
#if defined(_OS_WINDOWS_)
// use ELF because RuntimeDyld COFF i686 support didn't exist
// use ELF because RuntimeDyld COFF X86_64 doesn't seem to work (fails to generate function pointers)?
#define FORCE_ELF
#endif
#if defined(_CPU_X86_)
#define JL_NEED_FLOATTEMP_VAR 1
#endif
Expand Down Expand Up @@ -211,8 +206,6 @@ extern void _chkstk(void);
bool imaging_mode = false;

// shared llvm state
TargetMachine *jl_TargetMachine;
static DataLayout &jl_data_layout = *(new DataLayout(""));
#define jl_Module ctx.f->getParent()
#define jl_builderModule(builder) (builder).GetInsertBlock()->getParent()->getParent()
#define prepare_call(Callee) prepare_call_in(jl_Module, (Callee))
Expand Down Expand Up @@ -1901,8 +1894,9 @@ static jl_cgval_t convert_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_
return jl_cgval_t(v, typ, new_tindex);
}

static void jl_setup_module(Module *m, const jl_cgparams_t *params = &jl_default_cgparams)
Module *_jl_create_llvm_module(StringRef name, LLVMContext &context, const jl_cgparams_t *params, const DataLayout &DL, const Triple &triple)
{
Module *m = new Module(name, context);
// Some linkers (*cough* OS X) don't understand DWARF v4, so we use v2 in
// imaging mode. The structure of v4 is slightly nicer for debugging JIT
// code.
Expand All @@ -1917,8 +1911,8 @@ static void jl_setup_module(Module *m, const jl_cgparams_t *params = &jl_default
if (!m->getModuleFlag("Debug Info Version"))
m->addModuleFlag(llvm::Module::Warning, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
m->setDataLayout(jl_data_layout);
m->setTargetTriple(jl_TargetMachine->getTargetTriple().str());
m->setDataLayout(DL);
m->setTargetTriple(triple.str());

#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_) && JL_LLVM_VERSION >= 130000
// tell Win32 to assume the stack is always 16-byte aligned,
Expand All @@ -1929,18 +1923,12 @@ static void jl_setup_module(Module *m, const jl_cgparams_t *params = &jl_default
#if defined(JL_DEBUG_BUILD) && JL_LLVM_VERSION >= 130000
m->setStackProtectorGuard("global");
#endif
return m;
}

Module *_jl_create_llvm_module(StringRef name, LLVMContext &context, const jl_cgparams_t *params)
{
Module *M = new Module(name, context);
jl_setup_module(M, params);
return M;
}

Module *jl_create_llvm_module(StringRef name, LLVMContext &context)
Module *jl_create_llvm_module(StringRef name, LLVMContext &ctx, const DataLayout *DL, const Triple *triple)
{
return _jl_create_llvm_module(name, context, &jl_default_cgparams);
return _jl_create_llvm_module(name, ctx, &jl_default_cgparams, DL ? *DL : jl_ExecutionEngine->getDataLayout(), triple ? *triple : jl_ExecutionEngine->getTargetTriple());
}

static void jl_init_function(Function *F)
Expand Down Expand Up @@ -6450,7 +6438,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
declarations.specFunctionObject = funcName.str();

// allocate Function declarations and wrapper objects
Module *M = _jl_create_llvm_module(ctx.name, ctx.builder.getContext(), ctx.params);
Module *M = _jl_create_llvm_module(ctx.name, ctx.builder.getContext(), ctx.params, jl_ExecutionEngine->getDataLayout(), jl_ExecutionEngine->getTargetTriple());
jl_returninfo_t returninfo = {};
Function *f = NULL;
bool has_sret = false;
Expand Down Expand Up @@ -8248,88 +8236,7 @@ extern "C" void jl_init_llvm(void)
if (clopt && clopt->getNumOccurrences() == 0)
cl::ProvidePositionalOption(clopt, "4", 1);

TargetOptions options = TargetOptions();
//options.PrintMachineCode = true; //Print machine code produced during JIT compiling
#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_) && JL_LLVM_VERSION < 130000
// tell Win32 to assume the stack is always 16-byte aligned,
// and to ensure that it is 16-byte aligned for out-going calls,
// to ensure compatibility with GCC codes
// In LLVM 13 and onwards this has turned into a module option
options.StackAlignmentOverride = 16;
#endif
#if defined(JL_DEBUG_BUILD) && JL_LLVM_VERSION < 130000
// LLVM defaults to tls stack guard, which causes issues with Julia's tls implementation
options.StackProtectorGuard = StackProtectorGuards::Global;
#endif
Triple TheTriple(sys::getProcessTriple());
#if defined(FORCE_ELF)
TheTriple.setObjectFormat(Triple::ELF);
#endif
uint32_t target_flags = 0;
auto target = jl_get_llvm_target(imaging_mode, target_flags);
auto &TheCPU = target.first;
SmallVector<std::string, 10> targetFeatures(target.second.begin(), target.second.end());
std::string errorstr;
const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, errorstr);
if (!TheTarget)
jl_errorf("%s", errorstr.c_str());
if (jl_processor_print_help || (target_flags & JL_TARGET_UNKNOWN_NAME)) {
std::unique_ptr<MCSubtargetInfo> MSTI(
TheTarget->createMCSubtargetInfo(TheTriple.str(), "", ""));
if (!MSTI->isCPUStringValid(TheCPU))
jl_errorf("Invalid CPU name \"%s\".", TheCPU.c_str());
if (jl_processor_print_help) {
// This is the only way I can find to print the help message once.
// It'll be nice if we can iterate through the features and print our own help
// message...
MSTI->setDefaultFeatures("help", "", "");
}
}
// Package up features to be passed to target/subtarget
std::string FeaturesStr;
if (!targetFeatures.empty()) {
SubtargetFeatures Features;
for (unsigned i = 0; i != targetFeatures.size(); ++i)
Features.AddFeature(targetFeatures[i]);
FeaturesStr = Features.getString();
}
// Allocate a target...
Optional<CodeModel::Model> codemodel =
#if defined(JL_USE_JITLINK)
// JITLink can patch up relocations between far objects so we can use the
// small code model – which is good, as the large code model is unmaintained
// on MachO/AArch64.
CodeModel::Small;
#elif defined(_P64)
// Make sure we are using the large code model on 64bit
// Let LLVM pick a default suitable for jitting on 32bit
CodeModel::Large;
#else
None;
#endif
auto optlevel = CodeGenOptLevelFor(jl_options.opt_level);
jl_TargetMachine = TheTarget->createTargetMachine(
TheTriple.getTriple(), TheCPU, FeaturesStr,
options,
Reloc::Static, // Generate simpler code for JIT
codemodel,
optlevel,
true // JIT
);
assert(jl_TargetMachine && "Failed to select target machine -"
" Is the LLVM backend for this CPU enabled?");
#if (!defined(_CPU_ARM_) && !defined(_CPU_PPC64_))
// FastISel seems to be buggy for ARM. Ref #13321
if (jl_options.opt_level < 2)
jl_TargetMachine->setFastISel(true);
#endif

jl_ExecutionEngine = new JuliaOJIT(*jl_TargetMachine, new LLVMContext());

// Mark our address spaces as non-integral
jl_data_layout = jl_ExecutionEngine->getDataLayout();
std::string DL = jl_data_layout.getStringRepresentation() + "-ni:10:11:12:13";
jl_data_layout.reset(DL);
jl_ExecutionEngine = new JuliaOJIT(new LLVMContext());

// Register GDB event listener
#if defined(JL_DEBUG_BUILD)
Expand Down Expand Up @@ -8396,7 +8303,7 @@ extern "C" JL_DLLEXPORT void jl_init_codegen_impl(void)
jl_init_jit();
init_jit_functions();

Module *m = _jl_create_llvm_module("julia", *jl_ExecutionEngine->getContext().getContext(), &jl_default_cgparams);
Module *m = _jl_create_llvm_module("julia", *jl_ExecutionEngine->getContext().getContext(), &jl_default_cgparams, jl_ExecutionEngine->getDataLayout(), jl_ExecutionEngine->getTargetTriple());
init_julia_llvm_env(m);

jl_init_intrinsic_functions_codegen();
Expand Down
4 changes: 2 additions & 2 deletions src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ jl_value_t *jl_dump_function_asm_impl(void *F, char raw_mc, const char* asm_vari
if (f != &f2 && !f->isDeclaration())
f2.deleteBody();
}
LLVMTargetMachine *TM = static_cast<LLVMTargetMachine*>(jl_TargetMachine);
LLVMTargetMachine *TM = static_cast<LLVMTargetMachine*>(&jl_ExecutionEngine->getTargetMachine());
legacy::PassManager PM;
addTargetPasses(&PM, TM);
if (raw_mc) {
Expand All @@ -1226,7 +1226,7 @@ jl_value_t *jl_dump_function_asm_impl(void *F, char raw_mc, const char* asm_vari
if (!strcmp(asm_variant, "intel"))
OutputAsmDialect = 1;
MCInstPrinter *InstPrinter = TM->getTarget().createMCInstPrinter(
TM->getTargetTriple(), OutputAsmDialect, MAI, MII, MRI);
jl_ExecutionEngine->getTargetTriple(), OutputAsmDialect, MAI, MII, MRI);
std::unique_ptr<MCAsmBackend> MAB(TM->getTarget().createMCAsmBackend(
STI, MRI, TM->Options.MCOptions));
std::unique_ptr<MCCodeEmitter> MCE;
Expand Down
Loading

0 comments on commit 82dc130

Please sign in to comment.