| 1 |
#include "llvm/ExecutionEngine/Orc/ReOptimizeLayer.h" |
| 2 |
#include "OrcTestCommon.h" |
| 3 |
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h" |
| 4 |
#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h" |
| 5 |
#include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 6 |
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h" |
| 7 |
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 8 |
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h" |
| 9 |
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" |
| 10 |
#include "llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h" |
| 11 |
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
| 12 |
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" |
| 13 |
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" |
| 14 |
#include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h" |
| 15 |
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h" |
| 16 |
#include "llvm/IR/IRBuilder.h" |
| 17 |
#include "llvm/Support/CodeGen.h" |
| 18 |
#include "llvm/TargetParser/Host.h" |
| 19 |
#include "llvm/Testing/Support/Error.h" |
| 20 |
#include "gtest/gtest.h" |
| 21 |
|
| 22 |
using namespace llvm; |
| 23 |
using namespace llvm::orc; |
| 24 |
using namespace llvm::jitlink; |
| 25 |
|
| 26 |
class ReOptimizeLayerTest : public testing::Test { |
| 27 |
public: |
| 28 |
~ReOptimizeLayerTest() { |
| 29 |
if (ES) |
| 30 |
if (auto Err = ES->endSession()) |
| 31 |
ES->reportError(std::move(Err)); |
| 32 |
} |
| 33 |
|
| 34 |
protected: |
| 35 |
void SetUp() override { |
| 36 |
auto JTMB = JITTargetMachineBuilder::detectHost(); |
| 37 |
// Bail out if we can not detect the host. |
| 38 |
if (!JTMB) { |
| 39 |
consumeError(JTMB.takeError()); |
| 40 |
GTEST_SKIP(); |
| 41 |
} |
| 42 |
|
| 43 |
// COFF-ARM64 is not supported yet |
| 44 |
auto Triple = JTMB->getTargetTriple(); |
| 45 |
if (Triple.isOSBinFormatCOFF() && Triple.isAArch64()) |
| 46 |
GTEST_SKIP(); |
| 47 |
|
| 48 |
// SystemZ is not supported yet. |
| 49 |
if (Triple.isSystemZ()) |
| 50 |
GTEST_SKIP(); |
| 51 |
|
| 52 |
// 32-bit X86 is not supported yet. |
| 53 |
if (Triple.isX86() && Triple.isArch32Bit()) |
| 54 |
GTEST_SKIP(); |
| 55 |
|
| 56 |
if (Triple.isPPC()) |
| 57 |
GTEST_SKIP(); |
| 58 |
|
| 59 |
// RISC-V is not supported yet |
| 60 |
if (Triple.isRISCV()) |
| 61 |
GTEST_SKIP(); |
| 62 |
|
| 63 |
// ARM is not supported yet. |
| 64 |
if (Triple.isARM()) |
| 65 |
GTEST_SKIP(); |
| 66 |
|
| 67 |
auto EPC = SelfExecutorProcessControl::Create(); |
| 68 |
if (!EPC) { |
| 69 |
consumeError(EPC.takeError()); |
| 70 |
GTEST_SKIP(); |
| 71 |
} |
| 72 |
|
| 73 |
auto DLOrErr = JTMB->getDefaultDataLayoutForTarget(); |
| 74 |
if (!DLOrErr) { |
| 75 |
consumeError(DLOrErr.takeError()); |
| 76 |
GTEST_SKIP(); |
| 77 |
} |
| 78 |
|
| 79 |
auto PageSize = sys::Process::getPageSize(); |
| 80 |
if (!PageSize) { |
| 81 |
consumeError(PageSize.takeError()); |
| 82 |
GTEST_SKIP(); |
| 83 |
} |
| 84 |
|
| 85 |
ES = std::make_unique<ExecutionSession>(std::move(*EPC)); |
| 86 |
JD = &ES->createBareJITDylib("main"); |
| 87 |
ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>( |
| 88 |
*ES, std::make_unique<InProcessMemoryManager>(*PageSize)); |
| 89 |
DL = std::make_unique<DataLayout>(std::move(*DLOrErr)); |
| 90 |
|
| 91 |
auto TM = JTMB->createTargetMachine(); |
| 92 |
if (!TM) { |
| 93 |
consumeError(TM.takeError()); |
| 94 |
GTEST_SKIP(); |
| 95 |
} |
| 96 |
auto CompileFunction = |
| 97 |
std::make_unique<TMOwningSimpleCompiler>(std::move(*TM)); |
| 98 |
CompileLayer = std::make_unique<IRCompileLayer>(*ES, *ObjLinkingLayer, |
| 99 |
std::move(CompileFunction)); |
| 100 |
} |
| 101 |
|
| 102 |
Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM) { |
| 103 |
assert(TSM && "Can not add null module"); |
| 104 |
|
| 105 |
TSM.withModuleDo([&](Module &M) { M.setDataLayout(*DL); }); |
| 106 |
|
| 107 |
return ROLayer->add(std::move(RT), std::move(TSM)); |
| 108 |
} |
| 109 |
|
| 110 |
JITDylib *JD{nullptr}; |
| 111 |
std::unique_ptr<ExecutionSession> ES; |
| 112 |
std::unique_ptr<ObjectLinkingLayer> ObjLinkingLayer; |
| 113 |
std::unique_ptr<IRCompileLayer> CompileLayer; |
| 114 |
std::unique_ptr<ReOptimizeLayer> ROLayer; |
| 115 |
std::unique_ptr<DataLayout> DL; |
| 116 |
}; |
| 117 |
|
| 118 |
static Function *createRetFunction(Module *M, StringRef Name, |
| 119 |
uint32_t ReturnCode) { |
| 120 |
Function *Result = Function::Create( |
| 121 |
FunctionType::get(Type::getInt32Ty(M->getContext()), {}, false), |
| 122 |
GlobalValue::ExternalLinkage, Name, M); |
| 123 |
|
| 124 |
BasicBlock *BB = BasicBlock::Create(M->getContext(), Name, Result); |
| 125 |
IRBuilder<> Builder(M->getContext()); |
| 126 |
Builder.SetInsertPoint(BB); |
| 127 |
|
| 128 |
Value *RetValue = ConstantInt::get(M->getContext(), APInt(32, ReturnCode)); |
| 129 |
Builder.CreateRet(RetValue); |
| 130 |
return Result; |
| 131 |
} |
| 132 |
|
| 133 |
TEST_F(ReOptimizeLayerTest, BasicReOptimization) { |
| 134 |
MangleAndInterner Mangle(*ES, *DL); |
| 135 |
|
| 136 |
auto &EPC = ES->getExecutorProcessControl(); |
| 137 |
EXPECT_THAT_ERROR(JD->define(absoluteSymbols( |
| 138 |
{{Mangle("__orc_rt_jit_dispatch"), |
| 139 |
{EPC.getJITDispatchInfo().JITDispatchFunction, |
| 140 |
JITSymbolFlags::Exported}}, |
| 141 |
{Mangle("__orc_rt_jit_dispatch_ctx"), |
| 142 |
{EPC.getJITDispatchInfo().JITDispatchContext, |
| 143 |
JITSymbolFlags::Exported}}, |
| 144 |
{Mangle("__orc_rt_reoptimize_tag"), |
| 145 |
{ExecutorAddr(), JITSymbolFlags::Exported}}})), |
| 146 |
Succeeded()); |
| 147 |
|
| 148 |
auto RM = JITLinkRedirectableSymbolManager::Create(*ObjLinkingLayer); |
| 149 |
EXPECT_THAT_ERROR(RM.takeError(), Succeeded()); |
| 150 |
|
| 151 |
ROLayer = std::make_unique<ReOptimizeLayer>(*ES, *DL, *CompileLayer, **RM); |
| 152 |
ROLayer->setReoptimizeFunc( |
| 153 |
[&](ReOptimizeLayer &Parent, |
| 154 |
ReOptimizeLayer::ReOptMaterializationUnitID MUID, unsigned CurVerison, |
| 155 |
ResourceTrackerSP OldRT, ThreadSafeModule &TSM) { |
| 156 |
TSM.withModuleDo([&](Module &M) { |
| 157 |
for (auto &F : M) { |
| 158 |
if (F.isDeclaration()) |
| 159 |
continue; |
| 160 |
for (auto &B : F) { |
| 161 |
for (auto &I : B) { |
| 162 |
if (ReturnInst *Ret = dyn_cast<ReturnInst>(&I)) { |
| 163 |
Value *RetValue = |
| 164 |
ConstantInt::get(M.getContext(), APInt(32, 53)); |
| 165 |
Ret->setOperand(0, RetValue); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
}); |
| 171 |
return Error::success(); |
| 172 |
}); |
| 173 |
EXPECT_THAT_ERROR(ROLayer->reigsterRuntimeFunctions(*JD), Succeeded()); |
| 174 |
|
| 175 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 176 |
auto M = std::make_unique<Module>("<main>", *Ctx); |
| 177 |
M->setTargetTriple(Triple(sys::getProcessTriple())); |
| 178 |
|
| 179 |
(void)createRetFunction(M.get(), "main", 42); |
| 180 |
|
| 181 |
EXPECT_THAT_ERROR(addIRModule(JD->getDefaultResourceTracker(), |
| 182 |
ThreadSafeModule(std::move(M), std::move(Ctx))), |
| 183 |
Succeeded()); |
| 184 |
|
| 185 |
auto Result = cantFail(ES->lookup({JD}, Mangle("main"))); |
| 186 |
auto FuncPtr = Result.getAddress().toPtr<int (*)()>(); |
| 187 |
for (size_t I = 0; I <= ReOptimizeLayer::CallCountThreshold; I++) |
| 188 |
EXPECT_EQ(FuncPtr(), 42); |
| 189 |
EXPECT_EQ(FuncPtr(), 53); |
| 190 |
} |