Skip to content

Commit ea5de60

Browse files
committed
Revert "Reland "[llvm-exegesis] Switch from MCJIT to LLJIT (#72838)""
This broke tools/llvm-exegesis/X86/latency/dummy-counters.test on Mac, see comment on the PR. > There was an issue with certain configurations failing to build due to a > deleted Error constructor that should be fixed with this relanding. This reverts commit 92b821f.
1 parent 1127656 commit ea5de60

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

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

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
#include "llvm/CodeGen/TargetInstrInfo.h"
2222
#include "llvm/CodeGen/TargetPassConfig.h"
2323
#include "llvm/CodeGen/TargetSubtargetInfo.h"
24-
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
24+
#include "llvm/ExecutionEngine/SectionMemoryManager.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"
3029
#include "llvm/Support/Alignment.h"
3130
#include "llvm/Support/MemoryBuffer.h"
3231
#include "llvm/Support/raw_ostream.h"
@@ -106,7 +105,7 @@ MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionName,
106105
FunctionType *FunctionType =
107106
FunctionType::get(ReturnType, {MemParamType}, false);
108107
Function *const F = Function::Create(
109-
FunctionType, GlobalValue::ExternalLinkage, FunctionName, Module);
108+
FunctionType, GlobalValue::InternalLinkage, FunctionName, Module);
110109
BasicBlock *BB = BasicBlock::Create(Module->getContext(), "", F);
111110
new UnreachableInst(Module->getContext(), BB);
112111
return MMI->getOrCreateMachineFunction(*F);
@@ -325,48 +324,66 @@ object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename) {
325324
return cantFail(object::ObjectFile::createObjectFile(Filename));
326325
}
327326

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+
328350
Expected<ExecutableFunction> ExecutableFunction::create(
329351
std::unique_ptr<LLVMTargetMachine> TM,
330352
object::OwningBinary<object::ObjectFile> &&ObjectFileHolder) {
331353
assert(ObjectFileHolder.getBinary() && "cannot create object file");
332354
std::unique_ptr<LLVMContext> Ctx = std::make_unique<LLVMContext>();
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 std::move(ObjErr);
351-
352-
auto FunctionAddressOrErr = EJIT->lookup(FunctionID);
353-
if (!FunctionAddressOrErr)
354-
return FunctionAddressOrErr.takeError();
355-
356-
const uint64_t FunctionAddress = FunctionAddressOrErr->getValue();
357-
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);
358376
assert(isAligned(kFunctionAlignment, FunctionAddress) &&
359377
"function is not properly aligned");
360-
361378
StringRef FBytes =
362379
StringRef(reinterpret_cast<const char *>(FunctionAddress), CodeSize);
363-
return ExecutableFunction(std::move(Ctx), std::move(EJIT), FBytes);
380+
return ExecutableFunction(std::move(Ctx), std::move(EE), FBytes);
364381
}
365382

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

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

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

Lines changed: 4 additions & 3 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/Orc/LLJIT.h"
26+
#include "llvm/ExecutionEngine/ExecutionEngine.h"
2727
#include "llvm/IR/LLVMContext.h"
2828
#include "llvm/IR/Module.h"
2929
#include "llvm/MC/MCInst.h"
@@ -124,10 +124,11 @@ class ExecutableFunction {
124124

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

129130
std::unique_ptr<LLVMContext> Context;
130-
std::unique_ptr<orc::LLJIT> ExecJIT;
131+
std::unique_ptr<ExecutionEngine> ExecEngine;
131132
};
132133

133134
// 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
3233
MCParser
3334
Object
3435
ObjectYAML
35-
OrcJIT
3636
RuntimeDyld
3737
Support
3838
TargetParser

0 commit comments

Comments
 (0)