[llvm] ddc6ef4 - Revert "[llvm-exegesis] Switch from MCJIT to LLJIT (#72838)"
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 24 12:08:23 PST 2023
Author: Aiden Grossman
Date: 2023-11-24T12:07:50-08:00
New Revision: ddc6ef46418cc419041d0bd85de40d0eb9396848
URL: https://github.com/llvm/llvm-project/commit/ddc6ef46418cc419041d0bd85de40d0eb9396848
DIFF: https://github.com/llvm/llvm-project/commit/ddc6ef46418cc419041d0bd85de40d0eb9396848.diff
LOG: Revert "[llvm-exegesis] Switch from MCJIT to LLJIT (#72838)"
This reverts commit 12b0ab2c9ce8ef813d9eb888860f20daac3336bd.
Currently failing on certain configurations due to a missing
constructor. Pulling out so that I can reproduce the issue and make sure
I fix it before resubmitting.
Added:
Modified:
llvm/tools/llvm-exegesis/lib/Assembler.cpp
llvm/tools/llvm-exegesis/lib/Assembler.h
llvm/tools/llvm-exegesis/lib/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
index 903ecbe47b8da0f..9ff33258e965f7e 100644
--- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp
+++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
@@ -21,12 +21,11 @@
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
-#include "llvm/ExecutionEngine/Orc/LLJIT.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/MC/MCInstrInfo.h"
-#include "llvm/Object/SymbolSize.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
@@ -106,7 +105,7 @@ MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionName,
FunctionType *FunctionType =
FunctionType::get(ReturnType, {MemParamType}, false);
Function *const F = Function::Create(
- FunctionType, GlobalValue::ExternalLinkage, FunctionName, Module);
+ FunctionType, GlobalValue::InternalLinkage, FunctionName, Module);
BasicBlock *BB = BasicBlock::Create(Module->getContext(), "", F);
new UnreachableInst(Module->getContext(), BB);
return MMI->getOrCreateMachineFunction(*F);
@@ -325,48 +324,66 @@ object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename) {
return cantFail(object::ObjectFile::createObjectFile(Filename));
}
+namespace {
+
+// Implementation of this class relies on the fact that a single object with a
+// single function will be loaded into memory.
+class TrackingSectionMemoryManager : public SectionMemoryManager {
+public:
+ explicit TrackingSectionMemoryManager(uintptr_t *CodeSize)
+ : CodeSize(CodeSize) {}
+
+ uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID,
+ StringRef SectionName) override {
+ *CodeSize = Size;
+ return SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID,
+ SectionName);
+ }
+
+private:
+ uintptr_t *const CodeSize = nullptr;
+};
+
+} // namespace
+
Expected<ExecutableFunction> ExecutableFunction::create(
std::unique_ptr<LLVMTargetMachine> TM,
object::OwningBinary<object::ObjectFile> &&ObjectFileHolder) {
assert(ObjectFileHolder.getBinary() && "cannot create object file");
std::unique_ptr<LLVMContext> Ctx = std::make_unique<LLVMContext>();
-
- auto SymbolSizes = object::computeSymbolSizes(*ObjectFileHolder.getBinary());
- // Get the size of the function that we want to call into (with the name of
- // FunctionID). This should always be the third symbol returned by
- // calculateSymbolSizes.
- assert(SymbolSizes.size() == 3);
- assert(cantFail(std::get<0>(SymbolSizes[2]).getName()) == FunctionID);
- uintptr_t CodeSize = std::get<1>(SymbolSizes[2]);
-
- auto EJITOrErr = orc::LLJITBuilder().create();
- if (!EJITOrErr)
- return EJITOrErr.takeError();
-
- auto EJIT = std::move(*EJITOrErr);
-
- if (auto ObjErr =
- EJIT->addObjectFile(std::get<1>(ObjectFileHolder.takeBinary())))
- return ObjErr;
-
- auto FunctionAddressOrErr = EJIT->lookup(FunctionID);
- if (!FunctionAddressOrErr)
- return FunctionAddressOrErr.takeError();
-
- const uint64_t FunctionAddress = FunctionAddressOrErr->getValue();
-
+ // Initializing the execution engine.
+ // We need to use the JIT EngineKind to be able to add an object file.
+ LLVMLinkInMCJIT();
+ uintptr_t CodeSize = 0;
+ std::string Error;
+ std::unique_ptr<ExecutionEngine> EE(
+ EngineBuilder(createModule(Ctx, TM->createDataLayout()))
+ .setErrorStr(&Error)
+ .setMCPU(TM->getTargetCPU())
+ .setEngineKind(EngineKind::JIT)
+ .setMCJITMemoryManager(
+ std::make_unique<TrackingSectionMemoryManager>(&CodeSize))
+ .create(TM.release()));
+ if (!EE)
+ return make_error<StringError>(Twine(Error), inconvertibleErrorCode());
+ // Adding the generated object file containing the assembled function.
+ // The ExecutionEngine makes sure the object file is copied into an
+ // executable page.
+ EE->addObjectFile(std::move(ObjectFileHolder));
+ // Fetching function bytes.
+ const uint64_t FunctionAddress = EE->getFunctionAddress(FunctionID);
assert(isAligned(kFunctionAlignment, FunctionAddress) &&
"function is not properly aligned");
-
StringRef FBytes =
StringRef(reinterpret_cast<const char *>(FunctionAddress), CodeSize);
- return ExecutableFunction(std::move(Ctx), std::move(EJIT), FBytes);
+ return ExecutableFunction(std::move(Ctx), std::move(EE), FBytes);
}
ExecutableFunction::ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
- std::unique_ptr<orc::LLJIT> EJIT,
+ std::unique_ptr<ExecutionEngine> EE,
StringRef FB)
- : FunctionBytes(FB), Context(std::move(Ctx)), ExecJIT(std::move(EJIT)) {}
+ : FunctionBytes(FB), Context(std::move(Ctx)), ExecEngine(std::move(EE)) {}
Error getBenchmarkFunctionBytes(const StringRef InputData,
std::vector<uint8_t> &Bytes) {
diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.h b/llvm/tools/llvm-exegesis/lib/Assembler.h
index abc5aa7be8cfeef..5f1bf8cdfb7ad6c 100644
--- a/llvm/tools/llvm-exegesis/lib/Assembler.h
+++ b/llvm/tools/llvm-exegesis/lib/Assembler.h
@@ -23,7 +23,7 @@
#include "llvm/ADT/BitVector.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
-#include "llvm/ExecutionEngine/Orc/LLJIT.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCInst.h"
@@ -124,10 +124,11 @@ class ExecutableFunction {
private:
ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
- std::unique_ptr<orc::LLJIT> EJIT, StringRef FunctionBytes);
+ std::unique_ptr<ExecutionEngine> EE,
+ StringRef FunctionBytes);
std::unique_ptr<LLVMContext> Context;
- std::unique_ptr<orc::LLJIT> ExecJIT;
+ std::unique_ptr<ExecutionEngine> ExecEngine;
};
// Copies benchmark function's bytes from benchmark object.
diff --git a/llvm/tools/llvm-exegesis/lib/CMakeLists.txt b/llvm/tools/llvm-exegesis/lib/CMakeLists.txt
index 7312811989dd800..92135e6aaf673be 100644
--- a/llvm/tools/llvm-exegesis/lib/CMakeLists.txt
+++ b/llvm/tools/llvm-exegesis/lib/CMakeLists.txt
@@ -29,10 +29,10 @@ set(LLVM_LINK_COMPONENTS
MC
MCA
MCDisassembler
+ MCJIT
MCParser
Object
ObjectYAML
- OrcJIT
RuntimeDyld
Support
TargetParser
More information about the llvm-commits
mailing list