JITTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. //===- JITTest.cpp - Unit tests for the JIT -------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/BasicBlock.h"
  10. #include "llvm/Constant.h"
  11. #include "llvm/Constants.h"
  12. #include "llvm/DerivedTypes.h"
  13. #include "llvm/Function.h"
  14. #include "llvm/GlobalValue.h"
  15. #include "llvm/GlobalVariable.h"
  16. #include "llvm/IRBuilder.h"
  17. #include "llvm/LLVMContext.h"
  18. #include "llvm/Module.h"
  19. #include "llvm/Type.h"
  20. #include "llvm/ADT/OwningPtr.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/Assembly/Parser.h"
  23. #include "llvm/Bitcode/ReaderWriter.h"
  24. #include "llvm/ExecutionEngine/JIT.h"
  25. #include "llvm/ExecutionEngine/JITMemoryManager.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include "llvm/Support/SourceMgr.h"
  28. #include "llvm/Support/TargetSelect.h"
  29. #include "llvm/Support/TypeBuilder.h"
  30. #include "gtest/gtest.h"
  31. #include <vector>
  32. using namespace llvm;
  33. namespace {
  34. Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
  35. std::vector<Type*> params;
  36. FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
  37. params, false);
  38. Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
  39. BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
  40. IRBuilder<> builder(Entry);
  41. Value *Load = builder.CreateLoad(G);
  42. Type *GTy = G->getType()->getElementType();
  43. Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
  44. builder.CreateStore(Add, G);
  45. builder.CreateRet(Add);
  46. return F;
  47. }
  48. std::string DumpFunction(const Function *F) {
  49. std::string Result;
  50. raw_string_ostream(Result) << "" << *F;
  51. return Result;
  52. }
  53. class RecordingJITMemoryManager : public JITMemoryManager {
  54. const OwningPtr<JITMemoryManager> Base;
  55. public:
  56. RecordingJITMemoryManager()
  57. : Base(JITMemoryManager::CreateDefaultMemManager()) {
  58. stubsAllocated = 0;
  59. }
  60. virtual void *getPointerToNamedFunction(const std::string &Name,
  61. bool AbortOnFailure = true) {
  62. return Base->getPointerToNamedFunction(Name, AbortOnFailure);
  63. }
  64. virtual void setMemoryWritable() { Base->setMemoryWritable(); }
  65. virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
  66. virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
  67. virtual void AllocateGOT() { Base->AllocateGOT(); }
  68. virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); }
  69. struct StartFunctionBodyCall {
  70. StartFunctionBodyCall(uint8_t *Result, const Function *F,
  71. uintptr_t ActualSize, uintptr_t ActualSizeResult)
  72. : Result(Result), F(F), F_dump(DumpFunction(F)),
  73. ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
  74. uint8_t *Result;
  75. const Function *F;
  76. std::string F_dump;
  77. uintptr_t ActualSize;
  78. uintptr_t ActualSizeResult;
  79. };
  80. std::vector<StartFunctionBodyCall> startFunctionBodyCalls;
  81. virtual uint8_t *startFunctionBody(const Function *F,
  82. uintptr_t &ActualSize) {
  83. uintptr_t InitialActualSize = ActualSize;
  84. uint8_t *Result = Base->startFunctionBody(F, ActualSize);
  85. startFunctionBodyCalls.push_back(
  86. StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize));
  87. return Result;
  88. }
  89. int stubsAllocated;
  90. virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
  91. unsigned Alignment) {
  92. stubsAllocated++;
  93. return Base->allocateStub(F, StubSize, Alignment);
  94. }
  95. struct EndFunctionBodyCall {
  96. EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart,
  97. uint8_t *FunctionEnd)
  98. : F(F), F_dump(DumpFunction(F)),
  99. FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {}
  100. const Function *F;
  101. std::string F_dump;
  102. uint8_t *FunctionStart;
  103. uint8_t *FunctionEnd;
  104. };
  105. std::vector<EndFunctionBodyCall> endFunctionBodyCalls;
  106. virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
  107. uint8_t *FunctionEnd) {
  108. endFunctionBodyCalls.push_back(
  109. EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
  110. Base->endFunctionBody(F, FunctionStart, FunctionEnd);
  111. }
  112. virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  113. unsigned SectionID) {
  114. return Base->allocateDataSection(Size, Alignment, SectionID);
  115. }
  116. virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  117. unsigned SectionID) {
  118. return Base->allocateCodeSection(Size, Alignment, SectionID);
  119. }
  120. virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
  121. return Base->allocateSpace(Size, Alignment);
  122. }
  123. virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
  124. return Base->allocateGlobal(Size, Alignment);
  125. }
  126. struct DeallocateFunctionBodyCall {
  127. DeallocateFunctionBodyCall(const void *Body) : Body(Body) {}
  128. const void *Body;
  129. };
  130. std::vector<DeallocateFunctionBodyCall> deallocateFunctionBodyCalls;
  131. virtual void deallocateFunctionBody(void *Body) {
  132. deallocateFunctionBodyCalls.push_back(DeallocateFunctionBodyCall(Body));
  133. Base->deallocateFunctionBody(Body);
  134. }
  135. struct DeallocateExceptionTableCall {
  136. DeallocateExceptionTableCall(const void *ET) : ET(ET) {}
  137. const void *ET;
  138. };
  139. std::vector<DeallocateExceptionTableCall> deallocateExceptionTableCalls;
  140. virtual void deallocateExceptionTable(void *ET) {
  141. deallocateExceptionTableCalls.push_back(DeallocateExceptionTableCall(ET));
  142. Base->deallocateExceptionTable(ET);
  143. }
  144. struct StartExceptionTableCall {
  145. StartExceptionTableCall(uint8_t *Result, const Function *F,
  146. uintptr_t ActualSize, uintptr_t ActualSizeResult)
  147. : Result(Result), F(F), F_dump(DumpFunction(F)),
  148. ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
  149. uint8_t *Result;
  150. const Function *F;
  151. std::string F_dump;
  152. uintptr_t ActualSize;
  153. uintptr_t ActualSizeResult;
  154. };
  155. std::vector<StartExceptionTableCall> startExceptionTableCalls;
  156. virtual uint8_t* startExceptionTable(const Function* F,
  157. uintptr_t &ActualSize) {
  158. uintptr_t InitialActualSize = ActualSize;
  159. uint8_t *Result = Base->startExceptionTable(F, ActualSize);
  160. startExceptionTableCalls.push_back(
  161. StartExceptionTableCall(Result, F, InitialActualSize, ActualSize));
  162. return Result;
  163. }
  164. struct EndExceptionTableCall {
  165. EndExceptionTableCall(const Function *F, uint8_t *TableStart,
  166. uint8_t *TableEnd, uint8_t* FrameRegister)
  167. : F(F), F_dump(DumpFunction(F)),
  168. TableStart(TableStart), TableEnd(TableEnd),
  169. FrameRegister(FrameRegister) {}
  170. const Function *F;
  171. std::string F_dump;
  172. uint8_t *TableStart;
  173. uint8_t *TableEnd;
  174. uint8_t *FrameRegister;
  175. };
  176. std::vector<EndExceptionTableCall> endExceptionTableCalls;
  177. virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
  178. uint8_t *TableEnd, uint8_t* FrameRegister) {
  179. endExceptionTableCalls.push_back(
  180. EndExceptionTableCall(F, TableStart, TableEnd, FrameRegister));
  181. return Base->endExceptionTable(F, TableStart, TableEnd, FrameRegister);
  182. }
  183. };
  184. bool LoadAssemblyInto(Module *M, const char *assembly) {
  185. SMDiagnostic Error;
  186. bool success =
  187. NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
  188. std::string errMsg;
  189. raw_string_ostream os(errMsg);
  190. Error.print("", os);
  191. EXPECT_TRUE(success) << os.str();
  192. return success;
  193. }
  194. class JITTest : public testing::Test {
  195. protected:
  196. virtual void SetUp() {
  197. M = new Module("<main>", Context);
  198. RJMM = new RecordingJITMemoryManager;
  199. RJMM->setPoisonMemory(true);
  200. std::string Error;
  201. TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
  202. .setJITMemoryManager(RJMM)
  203. .setErrorStr(&Error).create());
  204. ASSERT_TRUE(TheJIT.get() != NULL) << Error;
  205. }
  206. void LoadAssembly(const char *assembly) {
  207. LoadAssemblyInto(M, assembly);
  208. }
  209. LLVMContext Context;
  210. Module *M; // Owned by ExecutionEngine.
  211. RecordingJITMemoryManager *RJMM;
  212. OwningPtr<ExecutionEngine> TheJIT;
  213. };
  214. // Regression test for a bug. The JIT used to allocate globals inside the same
  215. // memory block used for the function, and when the function code was freed,
  216. // the global was left in the same place. This test allocates a function
  217. // that uses and global, deallocates it, and then makes sure that the global
  218. // stays alive after that.
  219. TEST(JIT, GlobalInFunction) {
  220. LLVMContext context;
  221. Module *M = new Module("<main>", context);
  222. JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
  223. // Tell the memory manager to poison freed memory so that accessing freed
  224. // memory is more easily tested.
  225. MemMgr->setPoisonMemory(true);
  226. std::string Error;
  227. OwningPtr<ExecutionEngine> JIT(EngineBuilder(M)
  228. .setEngineKind(EngineKind::JIT)
  229. .setErrorStr(&Error)
  230. .setJITMemoryManager(MemMgr)
  231. // The next line enables the fix:
  232. .setAllocateGVsWithCode(false)
  233. .create());
  234. ASSERT_EQ(Error, "");
  235. // Create a global variable.
  236. Type *GTy = Type::getInt32Ty(context);
  237. GlobalVariable *G = new GlobalVariable(
  238. *M,
  239. GTy,
  240. false, // Not constant.
  241. GlobalValue::InternalLinkage,
  242. Constant::getNullValue(GTy),
  243. "myglobal");
  244. // Make a function that points to a global.
  245. Function *F1 = makeReturnGlobal("F1", G, M);
  246. // Get the pointer to the native code to force it to JIT the function and
  247. // allocate space for the global.
  248. void (*F1Ptr)() =
  249. reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
  250. // Since F1 was codegen'd, a pointer to G should be available.
  251. int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
  252. ASSERT_NE((int32_t*)NULL, GPtr);
  253. EXPECT_EQ(0, *GPtr);
  254. // F1() should increment G.
  255. F1Ptr();
  256. EXPECT_EQ(1, *GPtr);
  257. // Make a second function identical to the first, referring to the same
  258. // global.
  259. Function *F2 = makeReturnGlobal("F2", G, M);
  260. void (*F2Ptr)() =
  261. reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
  262. // F2() should increment G.
  263. F2Ptr();
  264. EXPECT_EQ(2, *GPtr);
  265. // Deallocate F1.
  266. JIT->freeMachineCodeForFunction(F1);
  267. // F2() should *still* increment G.
  268. F2Ptr();
  269. EXPECT_EQ(3, *GPtr);
  270. }
  271. int PlusOne(int arg) {
  272. return arg + 1;
  273. }
  274. // ARM tests disabled pending fix for PR10783.
  275. #if !defined(__arm__)
  276. TEST_F(JITTest, FarCallToKnownFunction) {
  277. // x86-64 can only make direct calls to functions within 32 bits of
  278. // the current PC. To call anything farther away, we have to load
  279. // the address into a register and call through the register. The
  280. // current JIT does this by allocating a stub for any far call.
  281. // There was a bug in which the JIT tried to emit a direct call when
  282. // the target was already in the JIT's global mappings and lazy
  283. // compilation was disabled.
  284. Function *KnownFunction = Function::Create(
  285. TypeBuilder<int(int), false>::get(Context),
  286. GlobalValue::ExternalLinkage, "known", M);
  287. TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
  288. // int test() { return known(7); }
  289. Function *TestFunction = Function::Create(
  290. TypeBuilder<int(), false>::get(Context),
  291. GlobalValue::ExternalLinkage, "test", M);
  292. BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
  293. IRBuilder<> Builder(Entry);
  294. Value *result = Builder.CreateCall(
  295. KnownFunction,
  296. ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
  297. Builder.CreateRet(result);
  298. TheJIT->DisableLazyCompilation(true);
  299. int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
  300. (intptr_t)TheJIT->getPointerToFunction(TestFunction));
  301. // This used to crash in trying to call PlusOne().
  302. EXPECT_EQ(8, TestFunctionPtr());
  303. }
  304. // Test a function C which calls A and B which call each other.
  305. TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
  306. TheJIT->DisableLazyCompilation(true);
  307. FunctionType *Func1Ty =
  308. cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
  309. std::vector<Type*> arg_types;
  310. arg_types.push_back(Type::getInt1Ty(Context));
  311. FunctionType *FuncTy = FunctionType::get(
  312. Type::getVoidTy(Context), arg_types, false);
  313. Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
  314. "func1", M);
  315. Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
  316. "func2", M);
  317. Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
  318. "func3", M);
  319. BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
  320. BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
  321. BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
  322. BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
  323. BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
  324. BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
  325. BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
  326. // Make Func1 call Func2(0) and Func3(0).
  327. IRBuilder<> Builder(Block1);
  328. Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
  329. Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
  330. Builder.CreateRetVoid();
  331. // void Func2(bool b) { if (b) { Func3(false); return; } return; }
  332. Builder.SetInsertPoint(Block2);
  333. Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
  334. Builder.SetInsertPoint(True2);
  335. Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
  336. Builder.CreateRetVoid();
  337. Builder.SetInsertPoint(False2);
  338. Builder.CreateRetVoid();
  339. // void Func3(bool b) { if (b) { Func2(false); return; } return; }
  340. Builder.SetInsertPoint(Block3);
  341. Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
  342. Builder.SetInsertPoint(True3);
  343. Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
  344. Builder.CreateRetVoid();
  345. Builder.SetInsertPoint(False3);
  346. Builder.CreateRetVoid();
  347. // Compile the function to native code
  348. void (*F1Ptr)() =
  349. reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
  350. F1Ptr();
  351. }
  352. // Regression test for PR5162. This used to trigger an AssertingVH inside the
  353. // JIT's Function to stub mapping.
  354. TEST_F(JITTest, NonLazyLeaksNoStubs) {
  355. TheJIT->DisableLazyCompilation(true);
  356. // Create two functions with a single basic block each.
  357. FunctionType *FuncTy =
  358. cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
  359. Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
  360. "func1", M);
  361. Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
  362. "func2", M);
  363. BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
  364. BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
  365. // The first function calls the second and returns the result
  366. IRBuilder<> Builder(Block1);
  367. Value *Result = Builder.CreateCall(Func2);
  368. Builder.CreateRet(Result);
  369. // The second function just returns a constant
  370. Builder.SetInsertPoint(Block2);
  371. Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
  372. // Compile the function to native code
  373. (void)TheJIT->getPointerToFunction(Func1);
  374. // Free the JIT state for the functions
  375. TheJIT->freeMachineCodeForFunction(Func1);
  376. TheJIT->freeMachineCodeForFunction(Func2);
  377. // Delete the first function (and show that is has no users)
  378. EXPECT_EQ(Func1->getNumUses(), 0u);
  379. Func1->eraseFromParent();
  380. // Delete the second function (and show that it has no users - it had one,
  381. // func1 but that's gone now)
  382. EXPECT_EQ(Func2->getNumUses(), 0u);
  383. Func2->eraseFromParent();
  384. }
  385. TEST_F(JITTest, ModuleDeletion) {
  386. TheJIT->DisableLazyCompilation(false);
  387. LoadAssembly("define void @main() { "
  388. " call i32 @computeVal() "
  389. " ret void "
  390. "} "
  391. " "
  392. "define internal i32 @computeVal() { "
  393. " ret i32 0 "
  394. "} ");
  395. Function *func = M->getFunction("main");
  396. TheJIT->getPointerToFunction(func);
  397. TheJIT->removeModule(M);
  398. delete M;
  399. SmallPtrSet<const void*, 2> FunctionsDeallocated;
  400. for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
  401. i != e; ++i) {
  402. FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
  403. }
  404. for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
  405. EXPECT_TRUE(FunctionsDeallocated.count(
  406. RJMM->startFunctionBodyCalls[i].Result))
  407. << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
  408. }
  409. EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
  410. RJMM->deallocateFunctionBodyCalls.size());
  411. SmallPtrSet<const void*, 2> ExceptionTablesDeallocated;
  412. unsigned NumTablesDeallocated = 0;
  413. for (unsigned i = 0, e = RJMM->deallocateExceptionTableCalls.size();
  414. i != e; ++i) {
  415. ExceptionTablesDeallocated.insert(
  416. RJMM->deallocateExceptionTableCalls[i].ET);
  417. if (RJMM->deallocateExceptionTableCalls[i].ET != NULL) {
  418. // If JITEmitDebugInfo is off, we'll "deallocate" NULL, which doesn't
  419. // appear in startExceptionTableCalls.
  420. NumTablesDeallocated++;
  421. }
  422. }
  423. for (unsigned i = 0, e = RJMM->startExceptionTableCalls.size(); i != e; ++i) {
  424. EXPECT_TRUE(ExceptionTablesDeallocated.count(
  425. RJMM->startExceptionTableCalls[i].Result))
  426. << "Function's exception table leaked: \n"
  427. << RJMM->startExceptionTableCalls[i].F_dump;
  428. }
  429. EXPECT_EQ(RJMM->startExceptionTableCalls.size(),
  430. NumTablesDeallocated);
  431. }
  432. #endif // !defined(__arm__)
  433. // ARM, MIPS and PPC still emit stubs for calls since the target may be
  434. // too far away to call directly. This #if can probably be removed when
  435. // http://llvm.org/PR5201 is fixed.
  436. #if !defined(__arm__) && !defined(__mips__) && \
  437. !defined(__powerpc__) && !defined(__ppc__)
  438. typedef int (*FooPtr) ();
  439. TEST_F(JITTest, NoStubs) {
  440. LoadAssembly("define void @bar() {"
  441. "entry: "
  442. "ret void"
  443. "}"
  444. " "
  445. "define i32 @foo() {"
  446. "entry:"
  447. "call void @bar()"
  448. "ret i32 undef"
  449. "}"
  450. " "
  451. "define i32 @main() {"
  452. "entry:"
  453. "%0 = call i32 @foo()"
  454. "call void @bar()"
  455. "ret i32 undef"
  456. "}");
  457. Function *foo = M->getFunction("foo");
  458. uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
  459. FooPtr ptr = (FooPtr)(tmp);
  460. (ptr)();
  461. // We should now allocate no more stubs, we have the code to foo
  462. // and the existing stub for bar.
  463. int stubsBefore = RJMM->stubsAllocated;
  464. Function *func = M->getFunction("main");
  465. TheJIT->getPointerToFunction(func);
  466. Function *bar = M->getFunction("bar");
  467. TheJIT->getPointerToFunction(bar);
  468. ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
  469. }
  470. #endif // !ARM && !PPC
  471. TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
  472. TheJIT->DisableLazyCompilation(true);
  473. LoadAssembly("define i8()* @get_foo_addr() { "
  474. " ret i8()* @foo "
  475. "} "
  476. " "
  477. "define i8 @foo() { "
  478. " ret i8 42 "
  479. "} ");
  480. Function *F_get_foo_addr = M->getFunction("get_foo_addr");
  481. typedef char(*fooT)();
  482. fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>(
  483. (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr));
  484. fooT foo_addr = get_foo_addr();
  485. // Now free get_foo_addr. This should not free the machine code for foo or
  486. // any call stub returned as foo's canonical address.
  487. TheJIT->freeMachineCodeForFunction(F_get_foo_addr);
  488. // Check by calling the reported address of foo.
  489. EXPECT_EQ(42, foo_addr());
  490. // The reported address should also be the same as the result of a subsequent
  491. // getPointerToFunction(foo).
  492. #if 0
  493. // Fails until PR5126 is fixed:
  494. Function *F_foo = M->getFunction("foo");
  495. fooT foo = reinterpret_cast<fooT>(
  496. (intptr_t)TheJIT->getPointerToFunction(F_foo));
  497. EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr);
  498. #endif
  499. }
  500. // ARM and MIPS do not have an implementation
  501. // of replaceMachineCodeForFunction(), so recompileAndRelinkFunction
  502. // doesn't work.
  503. #if !defined(__arm__) && !defined(__mips__)
  504. TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
  505. Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
  506. GlobalValue::ExternalLinkage, "test", M);
  507. BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
  508. IRBuilder<> Builder(Entry);
  509. Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1);
  510. Builder.CreateRet(Val);
  511. TheJIT->DisableLazyCompilation(true);
  512. // Compile the function once, and make sure it works.
  513. int (*OrigFPtr)() = reinterpret_cast<int(*)()>(
  514. (intptr_t)TheJIT->recompileAndRelinkFunction(F));
  515. EXPECT_EQ(1, OrigFPtr());
  516. // Now change the function to return a different value.
  517. Entry->eraseFromParent();
  518. BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F);
  519. Builder.SetInsertPoint(NewEntry);
  520. Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2);
  521. Builder.CreateRet(Val);
  522. // Recompile it, which should produce a new function pointer _and_ update the
  523. // old one.
  524. int (*NewFPtr)() = reinterpret_cast<int(*)()>(
  525. (intptr_t)TheJIT->recompileAndRelinkFunction(F));
  526. EXPECT_EQ(2, NewFPtr())
  527. << "The new pointer should call the new version of the function";
  528. EXPECT_EQ(2, OrigFPtr())
  529. << "The old pointer's target should now jump to the new version";
  530. }
  531. #endif // !defined(__arm__)
  532. } // anonymous namespace
  533. // This variable is intentionally defined differently in the statically-compiled
  534. // program from the IR input to the JIT to assert that the JIT doesn't use its
  535. // definition.
  536. extern "C" int32_t JITTest_AvailableExternallyGlobal;
  537. int32_t JITTest_AvailableExternallyGlobal = 42;
  538. namespace {
  539. TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
  540. TheJIT->DisableLazyCompilation(true);
  541. LoadAssembly("@JITTest_AvailableExternallyGlobal = "
  542. " available_externally global i32 7 "
  543. " "
  544. "define i32 @loader() { "
  545. " %result = load i32* @JITTest_AvailableExternallyGlobal "
  546. " ret i32 %result "
  547. "} ");
  548. Function *loaderIR = M->getFunction("loader");
  549. int32_t (*loader)() = reinterpret_cast<int32_t(*)()>(
  550. (intptr_t)TheJIT->getPointerToFunction(loaderIR));
  551. EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
  552. << " not 7 from the IR version.";
  553. }
  554. } // anonymous namespace
  555. // This function is intentionally defined differently in the statically-compiled
  556. // program from the IR input to the JIT to assert that the JIT doesn't use its
  557. // definition.
  558. extern "C" int32_t JITTest_AvailableExternallyFunction() {
  559. return 42;
  560. }
  561. namespace {
  562. // ARM tests disabled pending fix for PR10783.
  563. #if !defined(__arm__)
  564. TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
  565. TheJIT->DisableLazyCompilation(true);
  566. LoadAssembly("define available_externally i32 "
  567. " @JITTest_AvailableExternallyFunction() { "
  568. " ret i32 7 "
  569. "} "
  570. " "
  571. "define i32 @func() { "
  572. " %result = tail call i32 "
  573. " @JITTest_AvailableExternallyFunction() "
  574. " ret i32 %result "
  575. "} ");
  576. Function *funcIR = M->getFunction("func");
  577. int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
  578. (intptr_t)TheJIT->getPointerToFunction(funcIR));
  579. EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
  580. << " not 7 from the IR version.";
  581. }
  582. TEST_F(JITTest, EscapedLazyStubStillCallable) {
  583. TheJIT->DisableLazyCompilation(false);
  584. LoadAssembly("define internal i32 @stubbed() { "
  585. " ret i32 42 "
  586. "} "
  587. " "
  588. "define i32()* @get_stub() { "
  589. " ret i32()* @stubbed "
  590. "} ");
  591. typedef int32_t(*StubTy)();
  592. // Call get_stub() to get the address of @stubbed without actually JITting it.
  593. Function *get_stubIR = M->getFunction("get_stub");
  594. StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>(
  595. (intptr_t)TheJIT->getPointerToFunction(get_stubIR));
  596. StubTy stubbed = get_stub();
  597. // Now get_stubIR is the only reference to stubbed's stub.
  598. get_stubIR->eraseFromParent();
  599. // Now there are no references inside the JIT, but we've got a pointer outside
  600. // it. The stub should be callable and return the right value.
  601. EXPECT_EQ(42, stubbed());
  602. }
  603. // Converts the LLVM assembly to bitcode and returns it in a std::string. An
  604. // empty string indicates an error.
  605. std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
  606. Module TempModule("TempModule", Context);
  607. if (!LoadAssemblyInto(&TempModule, Assembly)) {
  608. return "";
  609. }
  610. std::string Result;
  611. raw_string_ostream OS(Result);
  612. WriteBitcodeToFile(&TempModule, OS);
  613. OS.flush();
  614. return Result;
  615. }
  616. // Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode'
  617. // lazily. The associated Module (owned by the ExecutionEngine) is returned in
  618. // M. Both will be NULL on an error. Bitcode must live at least as long as the
  619. // ExecutionEngine.
  620. ExecutionEngine *getJITFromBitcode(
  621. LLVMContext &Context, const std::string &Bitcode, Module *&M) {
  622. // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
  623. MemoryBuffer *BitcodeBuffer =
  624. MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
  625. std::string errMsg;
  626. M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
  627. if (M == NULL) {
  628. ADD_FAILURE() << errMsg;
  629. delete BitcodeBuffer;
  630. return NULL;
  631. }
  632. ExecutionEngine *TheJIT = EngineBuilder(M)
  633. .setEngineKind(EngineKind::JIT)
  634. .setErrorStr(&errMsg)
  635. .create();
  636. if (TheJIT == NULL) {
  637. ADD_FAILURE() << errMsg;
  638. delete M;
  639. M = NULL;
  640. return NULL;
  641. }
  642. return TheJIT;
  643. }
  644. TEST(LazyLoadedJITTest, MaterializableAvailableExternallyFunctionIsntCompiled) {
  645. LLVMContext Context;
  646. const std::string Bitcode =
  647. AssembleToBitcode(Context,
  648. "define available_externally i32 "
  649. " @JITTest_AvailableExternallyFunction() { "
  650. " ret i32 7 "
  651. "} "
  652. " "
  653. "define i32 @func() { "
  654. " %result = tail call i32 "
  655. " @JITTest_AvailableExternallyFunction() "
  656. " ret i32 %result "
  657. "} ");
  658. ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
  659. Module *M;
  660. OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
  661. ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
  662. TheJIT->DisableLazyCompilation(true);
  663. Function *funcIR = M->getFunction("func");
  664. Function *availableFunctionIR =
  665. M->getFunction("JITTest_AvailableExternallyFunction");
  666. // Double-check that the available_externally function is still unmaterialized
  667. // when getPointerToFunction needs to find out if it's available_externally.
  668. EXPECT_TRUE(availableFunctionIR->isMaterializable());
  669. int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
  670. (intptr_t)TheJIT->getPointerToFunction(funcIR));
  671. EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
  672. << " not 7 from the IR version.";
  673. }
  674. TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
  675. LLVMContext Context;
  676. const std::string Bitcode =
  677. AssembleToBitcode(Context,
  678. "define i32 @recur1(i32 %a) { "
  679. " %zero = icmp eq i32 %a, 0 "
  680. " br i1 %zero, label %done, label %notdone "
  681. "done: "
  682. " ret i32 3 "
  683. "notdone: "
  684. " %am1 = sub i32 %a, 1 "
  685. " %result = call i32 @recur2(i32 %am1) "
  686. " ret i32 %result "
  687. "} "
  688. " "
  689. "define i32 @recur2(i32 %b) { "
  690. " %result = call i32 @recur1(i32 %b) "
  691. " ret i32 %result "
  692. "} ");
  693. ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
  694. Module *M;
  695. OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
  696. ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
  697. TheJIT->DisableLazyCompilation(true);
  698. Function *recur1IR = M->getFunction("recur1");
  699. Function *recur2IR = M->getFunction("recur2");
  700. EXPECT_TRUE(recur1IR->isMaterializable());
  701. EXPECT_TRUE(recur2IR->isMaterializable());
  702. int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>(
  703. (intptr_t)TheJIT->getPointerToFunction(recur1IR));
  704. EXPECT_EQ(3, recur1(4));
  705. }
  706. #endif // !defined(__arm__)
  707. // This code is copied from JITEventListenerTest, but it only runs once for all
  708. // the tests in this directory. Everything seems fine, but that's strange
  709. // behavior.
  710. class JITEnvironment : public testing::Environment {
  711. virtual void SetUp() {
  712. // Required to create a JIT.
  713. InitializeNativeTarget();
  714. }
  715. };
  716. testing::Environment* const jit_env =
  717. testing::AddGlobalTestEnvironment(new JITEnvironment);
  718. }