From 07ee4c05179a27a419fe0f3f17bc14f88c66b6e5 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Thu, 9 Nov 2023 16:26:55 +0800 Subject: [PATCH] Remove `LLVMExtCreateMCJITCompilerForModule` from `llvm_ext.cc` --- src/llvm/ext/llvm_ext.cc | 74 +-------------------------- src/llvm/jit_compiler.cr | 10 +++- src/llvm/lib_llvm/execution_engine.cr | 1 + src/llvm/lib_llvm_ext.cr | 1 - 4 files changed, 11 insertions(+), 75 deletions(-) diff --git a/src/llvm/ext/llvm_ext.cc b/src/llvm/ext/llvm_ext.cc index 513a156a838a..fa4abd68e2fa 100644 --- a/src/llvm/ext/llvm_ext.cc +++ b/src/llvm/ext/llvm_ext.cc @@ -1,8 +1,7 @@ #include #include #include -#include -#include +#include using namespace llvm; @@ -89,75 +88,4 @@ void LLVMExtTargetMachineEnableGlobalIsel(LLVMTargetMachineRef T, LLVMBool Enabl unwrap(T)->setGlobalISel(Enable); } -// Copy paste of https://github.com/llvm/llvm-project/blob/dace8224f38a31636a02fe9c2af742222831f70c/llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp#L160-L214 -// but with a parameter to set global isel state -LLVMBool LLVMExtCreateMCJITCompilerForModule( - LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, - LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, - LLVMBool EnableGlobalISel, - char **OutError) { - LLVMMCJITCompilerOptions options; - // If the user passed a larger sized options struct, then they were compiled - // against a newer LLVM. Tell them that something is wrong. - if (SizeOfPassedOptions > sizeof(options)) { - *OutError = strdup( - "Refusing to use options struct that is larger than my own; assuming " - "LLVM library mismatch."); - return 1; - } - - - // Defend against the user having an old version of the API by ensuring that - // any fields they didn't see are cleared. We must defend against fields being - // set to the bitwise equivalent of zero, and assume that this means "do the - // default" as if that option hadn't been available. - LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); - memcpy(&options, PassedOptions, SizeOfPassedOptions); - - - TargetOptions targetOptions; - targetOptions.EnableFastISel = options.EnableFastISel; - targetOptions.EnableGlobalISel = EnableGlobalISel; - std::unique_ptr Mod(unwrap(M)); - - if (Mod) - // Set function attribute "frame-pointer" based on - // NoFramePointerElim. - for (auto &F : *Mod) { - auto Attrs = F.getAttributes(); - StringRef Value = options.NoFramePointerElim ? "all" : "none"; - #if LLVM_VERSION_GE(14, 0) - Attrs = Attrs.addFnAttribute(F.getContext(), "frame-pointer", Value); - #else - Attrs = Attrs.addAttribute(F.getContext(), AttributeList::FunctionIndex, - "frame-pointer", Value); - #endif - F.setAttributes(Attrs); - } - - - std::string Error; - EngineBuilder builder(std::move(Mod)); - builder.setEngineKind(EngineKind::JIT) - .setErrorStr(&Error) - .setOptLevel((CodeGenOpt::Level)options.OptLevel) - .setTargetOptions(targetOptions); - bool JIT; - if (auto CM = unwrap(options.CodeModel, JIT)) - builder.setCodeModel(*CM); - if (options.MCJMM) - builder.setMCJITMemoryManager( - std::unique_ptr(unwrap(options.MCJMM))); - - TargetMachine* tm = builder.selectTarget(); - tm->setGlobalISel(EnableGlobalISel); - - if (ExecutionEngine *JIT = builder.create(tm)) { - *OutJIT = wrap(JIT); - return 0; - } - *OutError = strdup(Error.c_str()); - return 1; -} - } // extern "C" diff --git a/src/llvm/jit_compiler.cr b/src/llvm/jit_compiler.cr index ee0c92803102..f307ffc55c58 100644 --- a/src/llvm/jit_compiler.cr +++ b/src/llvm/jit_compiler.cr @@ -5,10 +5,18 @@ class LLVM::JITCompiler mod.take_ownership { raise "Can't create two JIT compilers for the same module" } # if LibLLVM.create_jit_compiler_for_module(out @unwrap, mod, 3, out error) != 0 - if LibLLVMExt.create_mc_jit_compiler_for_module(out @unwrap, mod, nil, 0, false, out error) != 0 + if LibLLVM.create_mc_jit_compiler_for_module(out @unwrap, mod, nil, 0, out error) != 0 raise LLVM.string_and_dispose(error) end + # We need to disable global isel until https://reviews.llvm.org/D80898 is released, + # or we fixed generating values for 0 sized types. + # When removing this, also remove it from the ABI specs and Crystal::Codegen::Target. + # See https://github.com/crystal-lang/crystal/issues/9297#issuecomment-636512270 + # for background info + target_machine = LibLLVM.get_execution_engine_target_machine(@unwrap) + LibLLVMExt.target_machine_enable_global_isel(target_machine, false) + @finalized = false end diff --git a/src/llvm/lib_llvm/execution_engine.cr b/src/llvm/lib_llvm/execution_engine.cr index 1c58b5fcd046..f9de5c10ea39 100644 --- a/src/llvm/lib_llvm/execution_engine.cr +++ b/src/llvm/lib_llvm/execution_engine.cr @@ -28,5 +28,6 @@ lib LibLLVM fun create_mc_jit_compiler_for_module = LLVMCreateMCJITCompilerForModule(out_jit : ExecutionEngineRef*, m : ModuleRef, options : MCJITCompilerOptions*, size_of_options : SizeT, out_error : Char**) : Bool fun dispose_execution_engine = LLVMDisposeExecutionEngine(ee : ExecutionEngineRef) fun run_function = LLVMRunFunction(ee : ExecutionEngineRef, f : ValueRef, num_args : UInt, args : GenericValueRef*) : GenericValueRef + fun get_execution_engine_target_machine = LLVMGetExecutionEngineTargetMachine(ee : ExecutionEngineRef) : TargetMachineRef fun get_pointer_to_global = LLVMGetPointerToGlobal(ee : ExecutionEngineRef, global : ValueRef) : Void* end diff --git a/src/llvm/lib_llvm_ext.cr b/src/llvm/lib_llvm_ext.cr index f9bd71f25bde..6520cae55dfc 100644 --- a/src/llvm/lib_llvm_ext.cr +++ b/src/llvm/lib_llvm_ext.cr @@ -33,5 +33,4 @@ lib LibLLVMExt name : LibC::Char*) : LibLLVM::ValueRef fun target_machine_enable_global_isel = LLVMExtTargetMachineEnableGlobalIsel(machine : LibLLVM::TargetMachineRef, enable : Bool) - 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 end