Skip to content

Commit 4001a79

Browse files
authored
Remove LLVMExtCreateMCJITCompilerForModule from llvm_ext.cc (#13966)
1 parent 1f592ec commit 4001a79

File tree

5 files changed

+13
-78
lines changed

5 files changed

+13
-78
lines changed

src/compiler/crystal/codegen/target.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class Crystal::Codegen::Target
195195

196196
target = LLVM::Target.from_triple(self.to_s)
197197
machine = target.create_target_machine(self.to_s, cpu: cpu, features: features, opt_level: opt_level, code_model: code_model).not_nil!
198-
# We need to disable global isel until https://reviews.llvm.org/D80898 is released,
198+
# FIXME: We need to disable global isel until https://reviews.llvm.org/D80898 is released,
199199
# or we fixed generating values for 0 sized types.
200200
# When removing this, also remove it from the ABI specs and jit compiler.
201201
# See https://github.com/crystal-lang/crystal/issues/9297#issuecomment-636512270

src/llvm/ext/llvm_ext.cc

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <llvm/IR/DIBuilder.h>
22
#include <llvm/IR/IRBuilder.h>
33
#include <llvm/IR/DebugLoc.h>
4-
#include <llvm/ExecutionEngine/ExecutionEngine.h>
5-
#include <llvm/ExecutionEngine/RTDyldMemoryManager.h>
4+
#include <llvm/Target/TargetMachine.h>
5+
#include <llvm-c/TargetMachine.h>
66

77
using namespace llvm;
88

@@ -15,8 +15,6 @@ using namespace llvm;
1515
#define LLVM_VERSION_LE(major, minor) \
1616
(LLVM_VERSION_MAJOR < (major) || LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR <= (minor))
1717

18-
#include <llvm/Target/CodeGenCWrappers.h>
19-
2018
#if LLVM_VERSION_GE(16, 0)
2119
#define makeArrayRef ArrayRef
2220
#endif
@@ -89,75 +87,4 @@ void LLVMExtTargetMachineEnableGlobalIsel(LLVMTargetMachineRef T, LLVMBool Enabl
8987
unwrap(T)->setGlobalISel(Enable);
9088
}
9189

92-
// Copy paste of https://github.com/llvm/llvm-project/blob/dace8224f38a31636a02fe9c2af742222831f70c/llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp#L160-L214
93-
// but with a parameter to set global isel state
94-
LLVMBool LLVMExtCreateMCJITCompilerForModule(
95-
LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
96-
LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
97-
LLVMBool EnableGlobalISel,
98-
char **OutError) {
99-
LLVMMCJITCompilerOptions options;
100-
// If the user passed a larger sized options struct, then they were compiled
101-
// against a newer LLVM. Tell them that something is wrong.
102-
if (SizeOfPassedOptions > sizeof(options)) {
103-
*OutError = strdup(
104-
"Refusing to use options struct that is larger than my own; assuming "
105-
"LLVM library mismatch.");
106-
return 1;
107-
}
108-
109-
110-
// Defend against the user having an old version of the API by ensuring that
111-
// any fields they didn't see are cleared. We must defend against fields being
112-
// set to the bitwise equivalent of zero, and assume that this means "do the
113-
// default" as if that option hadn't been available.
114-
LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
115-
memcpy(&options, PassedOptions, SizeOfPassedOptions);
116-
117-
118-
TargetOptions targetOptions;
119-
targetOptions.EnableFastISel = options.EnableFastISel;
120-
targetOptions.EnableGlobalISel = EnableGlobalISel;
121-
std::unique_ptr<Module> Mod(unwrap(M));
122-
123-
if (Mod)
124-
// Set function attribute "frame-pointer" based on
125-
// NoFramePointerElim.
126-
for (auto &F : *Mod) {
127-
auto Attrs = F.getAttributes();
128-
StringRef Value = options.NoFramePointerElim ? "all" : "none";
129-
#if LLVM_VERSION_GE(14, 0)
130-
Attrs = Attrs.addFnAttribute(F.getContext(), "frame-pointer", Value);
131-
#else
132-
Attrs = Attrs.addAttribute(F.getContext(), AttributeList::FunctionIndex,
133-
"frame-pointer", Value);
134-
#endif
135-
F.setAttributes(Attrs);
136-
}
137-
138-
139-
std::string Error;
140-
EngineBuilder builder(std::move(Mod));
141-
builder.setEngineKind(EngineKind::JIT)
142-
.setErrorStr(&Error)
143-
.setOptLevel((CodeGenOpt::Level)options.OptLevel)
144-
.setTargetOptions(targetOptions);
145-
bool JIT;
146-
if (auto CM = unwrap(options.CodeModel, JIT))
147-
builder.setCodeModel(*CM);
148-
if (options.MCJMM)
149-
builder.setMCJITMemoryManager(
150-
std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
151-
152-
TargetMachine* tm = builder.selectTarget();
153-
tm->setGlobalISel(EnableGlobalISel);
154-
155-
if (ExecutionEngine *JIT = builder.create(tm)) {
156-
*OutJIT = wrap(JIT);
157-
return 0;
158-
}
159-
*OutError = strdup(Error.c_str());
160-
return 1;
161-
}
162-
16390
} // extern "C"

src/llvm/jit_compiler.cr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ class LLVM::JITCompiler
55
mod.take_ownership { raise "Can't create two JIT compilers for the same module" }
66

77
# if LibLLVM.create_jit_compiler_for_module(out @unwrap, mod, 3, out error) != 0
8-
if LibLLVMExt.create_mc_jit_compiler_for_module(out @unwrap, mod, nil, 0, false, out error) != 0
8+
if LibLLVM.create_mc_jit_compiler_for_module(out @unwrap, mod, nil, 0, out error) != 0
99
raise LLVM.string_and_dispose(error)
1010
end
1111

12+
# FIXME: We need to disable global isel until https://reviews.llvm.org/D80898 is released,
13+
# or we fixed generating values for 0 sized types.
14+
# When removing this, also remove it from the ABI specs and Crystal::Codegen::Target.
15+
# See https://github.com/crystal-lang/crystal/issues/9297#issuecomment-636512270
16+
# for background info
17+
target_machine = LibLLVM.get_execution_engine_target_machine(@unwrap)
18+
LibLLVMExt.target_machine_enable_global_isel(target_machine, false)
19+
1220
@finalized = false
1321
end
1422

src/llvm/lib_llvm/execution_engine.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ lib LibLLVM
2828
fun create_mc_jit_compiler_for_module = LLVMCreateMCJITCompilerForModule(out_jit : ExecutionEngineRef*, m : ModuleRef, options : MCJITCompilerOptions*, size_of_options : SizeT, out_error : Char**) : Bool
2929
fun dispose_execution_engine = LLVMDisposeExecutionEngine(ee : ExecutionEngineRef)
3030
fun run_function = LLVMRunFunction(ee : ExecutionEngineRef, f : ValueRef, num_args : UInt, args : GenericValueRef*) : GenericValueRef
31+
fun get_execution_engine_target_machine = LLVMGetExecutionEngineTargetMachine(ee : ExecutionEngineRef) : TargetMachineRef
3132
fun get_pointer_to_global = LLVMGetPointerToGlobal(ee : ExecutionEngineRef, global : ValueRef) : Void*
3233
end

src/llvm/lib_llvm_ext.cr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ lib LibLLVMExt
3333
name : LibC::Char*) : LibLLVM::ValueRef
3434

3535
fun target_machine_enable_global_isel = LLVMExtTargetMachineEnableGlobalIsel(machine : LibLLVM::TargetMachineRef, enable : Bool)
36-
fun create_mc_jit_compiler_for_module = LLVMExtCreateMCJITCompilerForModule(jit : LibLLVM::ExecutionEngineRef*, m : LibLLVM::ModuleRef, options : LibLLVM::MCJITCompilerOptions*, options_length : UInt32, enable_global_isel : Bool, error : UInt8**) : Int32
3736
end

0 commit comments

Comments
 (0)