1 //===--------------- OrcCAPITest.cpp - Unit tests Orc C API ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OrcTestCommon.h"
11 #include "gtest/gtest.h"
12 #include "llvm-c/Core.h"
13 #include "llvm-c/OrcBindings.h"
14 #include "llvm-c/Target.h"
15 #include "llvm-c/TargetMachine.h"
23 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
25 class OrcCAPIExecutionTest : public testing::Test, public OrcExecutionTest {
27 std::unique_ptr<Module> createTestModule(const Triple &TT) {
28 ModuleBuilder MB(getGlobalContext(), TT.str(), "");
29 Function *TestFunc = MB.createFunctionDecl<int()>("testFunc");
30 Function *Main = MB.createFunctionDecl<int(int, char*[])>("main");
32 Main->getBasicBlockList().push_back(BasicBlock::Create(getGlobalContext()));
33 IRBuilder<> B(&Main->back());
34 Value* Result = B.CreateCall(TestFunc);
37 return MB.takeModule();
40 typedef int (*MainFnTy)();
42 static int myTestFuncImpl() {
46 static char *testFuncName;
48 static uint64_t myResolver(const char *Name, void *Ctx) {
49 if (!strncmp(Name, testFuncName, 8))
50 return (uint64_t)&myTestFuncImpl;
54 struct CompileContext {
55 CompileContext() : Compiled(false) { }
57 OrcCAPIExecutionTest* APIExecTest;
58 std::unique_ptr<Module> M;
59 LLVMOrcModuleHandle H;
63 static LLVMOrcTargetAddress myCompileCallback(LLVMOrcJITStackRef JITStack,
65 CompileContext *CCtx = static_cast<CompileContext*>(Ctx);
66 auto *ET = CCtx->APIExecTest;
67 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
68 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, wrap(CCtx->M.get()),
70 CCtx->Compiled = true;
71 LLVMOrcTargetAddress MainAddr = LLVMOrcGetSymbolAddress(JITStack, "main");
72 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr);
77 char *OrcCAPIExecutionTest::testFuncName = nullptr;
79 TEST_F(OrcCAPIExecutionTest, TestEagerIRCompilation) {
83 LLVMOrcJITStackRef JIT =
84 LLVMOrcCreateInstance(wrap(TM.get()));
86 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
88 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
90 LLVMOrcModuleHandle H =
91 LLVMOrcAddEagerlyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
92 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
93 int Result = MainFn();
95 << "Eagerly JIT'd code did not return expected result";
97 LLVMOrcRemoveModule(JIT, H);
99 LLVMOrcDisposeMangledSymbol(testFuncName);
100 LLVMOrcDisposeInstance(JIT);
103 TEST_F(OrcCAPIExecutionTest, TestLazyIRCompilation) {
107 LLVMOrcJITStackRef JIT =
108 LLVMOrcCreateInstance(wrap(TM.get()));
110 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
112 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
114 LLVMOrcModuleHandle H =
115 LLVMOrcAddLazilyCompiledIR(JIT, wrap(M.get()), myResolver, nullptr);
116 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
117 int Result = MainFn();
118 EXPECT_EQ(Result, 42)
119 << "Lazily JIT'd code did not return expected result";
121 LLVMOrcRemoveModule(JIT, H);
123 LLVMOrcDisposeMangledSymbol(testFuncName);
124 LLVMOrcDisposeInstance(JIT);
127 TEST_F(OrcCAPIExecutionTest, TestDirectCallbacksAPI) {
131 LLVMOrcJITStackRef JIT =
132 LLVMOrcCreateInstance(wrap(TM.get()));
134 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
137 C.APIExecTest = this;
138 LLVMOrcCreateIndirectStub(JIT, "foo",
139 LLVMOrcCreateLazyCompileCallback(JIT,
142 MainFnTy FooFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "foo");
143 int Result = FooFn();
144 EXPECT_TRUE(C.Compiled)
145 << "Function wasn't lazily compiled";
146 EXPECT_EQ(Result, 42)
147 << "Direct-callback JIT'd code did not return expected result";
151 EXPECT_FALSE(C.Compiled)
152 << "Direct-callback JIT'd code was JIT'd twice";
154 LLVMOrcRemoveModule(JIT, C.H);
156 LLVMOrcDisposeMangledSymbol(testFuncName);
157 LLVMOrcDisposeInstance(JIT);