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/OrcBindings.h"
13 #include "llvm-c/Target.h"
14 #include "llvm-c/TargetMachine.h"
22 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef);
24 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)(void);
42 static int myTestFuncImpl(void) {
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;
56 char *OrcCAPIExecutionTest::testFuncName = 0;
58 TEST_F(OrcCAPIExecutionTest, TestEagerIRCompilation) {
59 auto TM = getHostTargetMachineIfSupported();
64 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
66 LLVMOrcJITStackRef JIT =
67 LLVMOrcCreateInstance(wrap(TM.get()), LLVMGetGlobalContext());
69 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
71 LLVMOrcModuleHandle H =
72 LLVMOrcAddEagerlyCompiledIR(JIT, wrap(M.get()), myResolver, 0);
73 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
74 int Result = MainFn();
76 << "Eagerly JIT'd code did not return expected result";
78 LLVMOrcRemoveModule(JIT, H);
80 LLVMOrcDisposeMangledSymbol(testFuncName);
81 LLVMOrcDisposeInstance(JIT);
84 TEST_F(OrcCAPIExecutionTest, TestLazyIRCompilation) {
85 auto TM = getHostTargetMachineIfSupported();
90 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple());
92 LLVMOrcJITStackRef JIT =
93 LLVMOrcCreateInstance(wrap(TM.get()), LLVMGetGlobalContext());
95 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
96 LLVMOrcModuleHandle H =
97 LLVMOrcAddLazilyCompiledIR(JIT, wrap(M.get()), myResolver, 0);
98 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
99 int Result = MainFn();
100 EXPECT_EQ(Result, 42)
101 << "Lazily JIT'd code did not return expected result";
103 LLVMOrcRemoveModule(JIT, H);
105 LLVMOrcDisposeMangledSymbol(testFuncName);
106 LLVMOrcDisposeInstance(JIT);