Skip to content

Commit 12b0ab2

Browse files
[llvm-exegesis] Switch from MCJIT to LLJIT (#72838)
This patch switches from using MCJIT to LLJIT as MCJIT is going to be deprecated soon. Fixes #41764.
1 parent 6ba6039 commit 12b0ab2

File tree

3 files changed

+36
-54
lines changed

3 files changed

+36
-54
lines changed

llvm/tools/llvm-exegesis/lib/Assembler.cpp

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
#include "llvm/CodeGen/TargetInstrInfo.h"
2222
#include "llvm/CodeGen/TargetPassConfig.h"
2323
#include "llvm/CodeGen/TargetSubtargetInfo.h"
24-
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
24+
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
2525
#include "llvm/IR/BasicBlock.h"
2626
#include "llvm/IR/Instructions.h"
2727
#include "llvm/IR/LegacyPassManager.h"
2828
#include "llvm/MC/MCInstrInfo.h"
29+
#include "llvm/Object/SymbolSize.h"
2930
#include "llvm/Support/Alignment.h"
3031
#include "llvm/Support/MemoryBuffer.h"
3132
#include "llvm/Support/raw_ostream.h"
@@ -105,7 +106,7 @@ MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionName,
105106
FunctionType *FunctionType =
106107
FunctionType::get(ReturnType, {MemParamType}, false);
107108
Function *const F = Function::Create(
108-
FunctionType, GlobalValue::InternalLinkage, FunctionName, Module);
109+
FunctionType, GlobalValue::ExternalLinkage, FunctionName, Module);
109110
BasicBlock *BB = BasicBlock::Create(Module->getContext(), "", F);
110111
new UnreachableInst(Module->getContext(), BB);
111112
return MMI->getOrCreateMachineFunction(*F);
@@ -324,66 +325,48 @@ object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename) {
324325
return cantFail(object::ObjectFile::createObjectFile(Filename));
325326
}
326327

327-
namespace {
328-
329-
// Implementation of this class relies on the fact that a single object with a
330-
// single function will be loaded into memory.
331-
class TrackingSectionMemoryManager : public SectionMemoryManager {
332-
public:
333-
explicit TrackingSectionMemoryManager(uintptr_t *CodeSize)
334-
: CodeSize(CodeSize) {}
335-
336-
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
337-
unsigned SectionID,
338-
StringRef SectionName) override {
339-
*CodeSize = Size;
340-
return SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID,
341-
SectionName);
342-
}
343-
344-
private:
345-
uintptr_t *const CodeSize = nullptr;
346-
};
347-
348-
} // namespace
349-
350328
Expected<ExecutableFunction> ExecutableFunction::create(
351329
std::unique_ptr<LLVMTargetMachine> TM,
352330
object::OwningBinary<object::ObjectFile> &&ObjectFileHolder) {
353331
assert(ObjectFileHolder.getBinary() && "cannot create object file");
354332
std::unique_ptr<LLVMContext> Ctx = std::make_unique<LLVMContext>();
355-
// Initializing the execution engine.
356-
// We need to use the JIT EngineKind to be able to add an object file.
357-
LLVMLinkInMCJIT();
358-
uintptr_t CodeSize = 0;
359-
std::string Error;
360-
std::unique_ptr<ExecutionEngine> EE(
361-
EngineBuilder(createModule(Ctx, TM->createDataLayout()))
362-
.setErrorStr(&Error)
363-
.setMCPU(TM->getTargetCPU())
364-
.setEngineKind(EngineKind::JIT)
365-
.setMCJITMemoryManager(
366-
std::make_unique<TrackingSectionMemoryManager>(&CodeSize))
367-
.create(TM.release()));
368-
if (!EE)
369-
return make_error<StringError>(Twine(Error), inconvertibleErrorCode());
370-
// Adding the generated object file containing the assembled function.
371-
// The ExecutionEngine makes sure the object file is copied into an
372-
// executable page.
373-
EE->addObjectFile(std::move(ObjectFileHolder));
374-
// Fetching function bytes.
375-
const uint64_t FunctionAddress = EE->getFunctionAddress(FunctionID);
333+
334+
auto SymbolSizes = object::computeSymbolSizes(*ObjectFileHolder.getBinary());
335+
// Get the size of the function that we want to call into (with the name of
336+
// FunctionID). This should always be the third symbol returned by
337+
// calculateSymbolSizes.
338+
assert(SymbolSizes.size() == 3);
339+
assert(cantFail(std::get<0>(SymbolSizes[2]).getName()) == FunctionID);
340+
uintptr_t CodeSize = std::get<1>(SymbolSizes[2]);
341+
342+
auto EJITOrErr = orc::LLJITBuilder().create();
343+
if (!EJITOrErr)
344+
return EJITOrErr.takeError();
345+
346+
auto EJIT = std::move(*EJITOrErr);
347+
348+
if (auto ObjErr =
349+
EJIT->addObjectFile(std::get<1>(ObjectFileHolder.takeBinary())))
350+
return ObjErr;
351+
352+
auto FunctionAddressOrErr = EJIT->lookup(FunctionID);
353+
if (!FunctionAddressOrErr)
354+
return FunctionAddressOrErr.takeError();
355+
356+
const uint64_t FunctionAddress = FunctionAddressOrErr->getValue();
357+
376358
assert(isAligned(kFunctionAlignment, FunctionAddress) &&
377359
"function is not properly aligned");
360+
378361
StringRef FBytes =
379362
StringRef(reinterpret_cast<const char *>(FunctionAddress), CodeSize);
380-
return ExecutableFunction(std::move(Ctx), std::move(EE), FBytes);
363+
return ExecutableFunction(std::move(Ctx), std::move(EJIT), FBytes);
381364
}
382365

383366
ExecutableFunction::ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
384-
std::unique_ptr<ExecutionEngine> EE,
367+
std::unique_ptr<orc::LLJIT> EJIT,
385368
StringRef FB)
386-
: FunctionBytes(FB), Context(std::move(Ctx)), ExecEngine(std::move(EE)) {}
369+
: FunctionBytes(FB), Context(std::move(Ctx)), ExecJIT(std::move(EJIT)) {}
387370

388371
Error getBenchmarkFunctionBytes(const StringRef InputData,
389372
std::vector<uint8_t> &Bytes) {

llvm/tools/llvm-exegesis/lib/Assembler.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "llvm/ADT/BitVector.h"
2424
#include "llvm/CodeGen/MachineFunction.h"
2525
#include "llvm/CodeGen/MachineModuleInfo.h"
26-
#include "llvm/ExecutionEngine/ExecutionEngine.h"
26+
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
2727
#include "llvm/IR/LLVMContext.h"
2828
#include "llvm/IR/Module.h"
2929
#include "llvm/MC/MCInst.h"
@@ -124,11 +124,10 @@ class ExecutableFunction {
124124

125125
private:
126126
ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
127-
std::unique_ptr<ExecutionEngine> EE,
128-
StringRef FunctionBytes);
127+
std::unique_ptr<orc::LLJIT> EJIT, StringRef FunctionBytes);
129128

130129
std::unique_ptr<LLVMContext> Context;
131-
std::unique_ptr<ExecutionEngine> ExecEngine;
130+
std::unique_ptr<orc::LLJIT> ExecJIT;
132131
};
133132

134133
// Copies benchmark function's bytes from benchmark object.

llvm/tools/llvm-exegesis/lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ set(LLVM_LINK_COMPONENTS
2929
MC
3030
MCA
3131
MCDisassembler
32-
MCJIT
3332
MCParser
3433
Object
3534
ObjectYAML
35+
OrcJIT
3636
RuntimeDyld
3737
Support
3838
TargetParser

0 commit comments

Comments
 (0)