Mercurial > hg > CbC > CbC_llvm
comparison unittests/ExecutionEngine/Orc/OrcCAPITest.cpp @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 803732b1fca8 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
1 //===--------------- OrcCAPITest.cpp - Unit tests Orc C API ---------------===// | 1 //===--------------- OrcCAPITest.cpp - Unit tests Orc C API ---------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 | 8 |
10 #include "OrcTestCommon.h" | 9 #include "OrcTestCommon.h" |
11 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" | 10 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
25 | 24 |
26 class OrcCAPIExecutionTest : public testing::Test, public OrcExecutionTest { | 25 class OrcCAPIExecutionTest : public testing::Test, public OrcExecutionTest { |
27 protected: | 26 protected: |
28 std::unique_ptr<Module> createTestModule(const Triple &TT) { | 27 std::unique_ptr<Module> createTestModule(const Triple &TT) { |
29 ModuleBuilder MB(Context, TT.str(), ""); | 28 ModuleBuilder MB(Context, TT.str(), ""); |
30 Function *TestFunc = MB.createFunctionDecl<int()>("testFunc"); | 29 Type *IntTy = Type::getScalarTy<int>(Context); |
31 Function *Main = MB.createFunctionDecl<int(int, char*[])>("main"); | 30 Function *TestFunc = |
31 MB.createFunctionDecl(FunctionType::get(IntTy, {}, false), "testFunc"); | |
32 Function *Main = MB.createFunctionDecl( | |
33 FunctionType::get( | |
34 IntTy, | |
35 {IntTy, Type::getInt8PtrTy(Context)->getPointerTo()}, | |
36 false), | |
37 "main"); | |
32 | 38 |
33 Main->getBasicBlockList().push_back(BasicBlock::Create(Context)); | 39 Main->getBasicBlockList().push_back(BasicBlock::Create(Context)); |
34 IRBuilder<> B(&Main->back()); | 40 IRBuilder<> B(&Main->back()); |
35 Value* Result = B.CreateCall(TestFunc); | 41 Value* Result = B.CreateCall(TestFunc); |
36 B.CreateRet(Result); | 42 B.CreateRet(Result); |
37 | 43 |
38 return MB.takeModule(); | 44 return MB.takeModule(); |
39 } | 45 } |
40 | 46 |
41 std::shared_ptr<object::OwningBinary<object::ObjectFile>> | 47 std::unique_ptr<MemoryBuffer> createTestObject() { |
42 createTestObject() { | |
43 orc::SimpleCompiler IRCompiler(*TM); | 48 orc::SimpleCompiler IRCompiler(*TM); |
44 auto M = createTestModule(TM->getTargetTriple()); | 49 auto M = createTestModule(TM->getTargetTriple()); |
45 M->setDataLayout(TM->createDataLayout()); | 50 M->setDataLayout(TM->createDataLayout()); |
46 return std::make_shared<object::OwningBinary<object::ObjectFile>>( | 51 return IRCompiler(*M); |
47 IRCompiler(*M)); | |
48 } | 52 } |
49 | 53 |
50 typedef int (*MainFnTy)(); | 54 typedef int (*MainFnTy)(); |
51 | 55 |
52 static int myTestFuncImpl() { | 56 static int myTestFuncImpl() { |
73 static LLVMOrcTargetAddress myCompileCallback(LLVMOrcJITStackRef JITStack, | 77 static LLVMOrcTargetAddress myCompileCallback(LLVMOrcJITStackRef JITStack, |
74 void *Ctx) { | 78 void *Ctx) { |
75 CompileContext *CCtx = static_cast<CompileContext*>(Ctx); | 79 CompileContext *CCtx = static_cast<CompileContext*>(Ctx); |
76 auto *ET = CCtx->APIExecTest; | 80 auto *ET = CCtx->APIExecTest; |
77 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple()); | 81 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple()); |
78 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release())); | 82 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, wrap(CCtx->M.release()), |
79 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, SM, myResolver, nullptr); | 83 myResolver, nullptr); |
80 LLVMOrcDisposeSharedModuleRef(SM); | |
81 CCtx->Compiled = true; | 84 CCtx->Compiled = true; |
82 LLVMOrcTargetAddress MainAddr; | 85 LLVMOrcTargetAddress MainAddr; |
83 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main"); | 86 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main"); |
84 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr); | 87 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr); |
85 return MainAddr; | 88 return MainAddr; |
87 }; | 90 }; |
88 | 91 |
89 char *OrcCAPIExecutionTest::testFuncName = nullptr; | 92 char *OrcCAPIExecutionTest::testFuncName = nullptr; |
90 | 93 |
91 TEST_F(OrcCAPIExecutionTest, TestEagerIRCompilation) { | 94 TEST_F(OrcCAPIExecutionTest, TestEagerIRCompilation) { |
92 if (!TM) | 95 if (!SupportsJIT) |
93 return; | 96 return; |
94 | 97 |
95 LLVMOrcJITStackRef JIT = | 98 LLVMOrcJITStackRef JIT = |
96 LLVMOrcCreateInstance(wrap(TM.get())); | 99 LLVMOrcCreateInstance(wrap(TM.get())); |
97 | 100 |
98 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple()); | 101 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple()); |
99 | 102 |
100 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); | 103 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); |
101 | 104 |
102 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release())); | |
103 LLVMOrcModuleHandle H; | 105 LLVMOrcModuleHandle H; |
104 LLVMOrcAddEagerlyCompiledIR(JIT, &H, SM, myResolver, nullptr); | 106 LLVMOrcAddEagerlyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr); |
105 LLVMOrcDisposeSharedModuleRef(SM); | 107 |
106 LLVMOrcTargetAddress MainAddr; | 108 // get symbol address searching the entire stack |
107 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main"); | 109 { |
108 MainFnTy MainFn = (MainFnTy)MainAddr; | 110 LLVMOrcTargetAddress MainAddr; |
109 int Result = MainFn(); | 111 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main"); |
110 EXPECT_EQ(Result, 42) | 112 MainFnTy MainFn = (MainFnTy)MainAddr; |
111 << "Eagerly JIT'd code did not return expected result"; | 113 int Result = MainFn(); |
114 EXPECT_EQ(Result, 42) | |
115 << "Eagerly JIT'd code did not return expected result"; | |
116 } | |
117 | |
118 // and then just searching a single handle | |
119 { | |
120 LLVMOrcTargetAddress MainAddr; | |
121 LLVMOrcGetSymbolAddressIn(JIT, &MainAddr, H, "main"); | |
122 MainFnTy MainFn = (MainFnTy)MainAddr; | |
123 int Result = MainFn(); | |
124 EXPECT_EQ(Result, 42) | |
125 << "Eagerly JIT'd code did not return expected result"; | |
126 } | |
112 | 127 |
113 LLVMOrcRemoveModule(JIT, H); | 128 LLVMOrcRemoveModule(JIT, H); |
114 | 129 |
115 LLVMOrcDisposeMangledSymbol(testFuncName); | 130 LLVMOrcDisposeMangledSymbol(testFuncName); |
116 LLVMOrcDisposeInstance(JIT); | 131 LLVMOrcDisposeInstance(JIT); |
117 } | 132 } |
118 | 133 |
119 TEST_F(OrcCAPIExecutionTest, TestLazyIRCompilation) { | 134 TEST_F(OrcCAPIExecutionTest, TestLazyIRCompilation) { |
120 if (!TM) | 135 if (!SupportsIndirection) |
121 return; | 136 return; |
122 | 137 |
123 LLVMOrcJITStackRef JIT = | 138 LLVMOrcJITStackRef JIT = |
124 LLVMOrcCreateInstance(wrap(TM.get())); | 139 LLVMOrcCreateInstance(wrap(TM.get())); |
125 | 140 |
126 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple()); | 141 std::unique_ptr<Module> M = createTestModule(TM->getTargetTriple()); |
127 | 142 |
128 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); | 143 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); |
129 | 144 |
130 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release())); | |
131 LLVMOrcModuleHandle H; | 145 LLVMOrcModuleHandle H; |
132 LLVMOrcAddLazilyCompiledIR(JIT, &H, SM, myResolver, nullptr); | 146 LLVMOrcAddLazilyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr); |
133 LLVMOrcDisposeSharedModuleRef(SM); | |
134 LLVMOrcTargetAddress MainAddr; | 147 LLVMOrcTargetAddress MainAddr; |
135 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main"); | 148 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main"); |
136 MainFnTy MainFn = (MainFnTy)MainAddr; | 149 MainFnTy MainFn = (MainFnTy)MainAddr; |
137 int Result = MainFn(); | 150 int Result = MainFn(); |
138 EXPECT_EQ(Result, 42) | 151 EXPECT_EQ(Result, 42) |
143 LLVMOrcDisposeMangledSymbol(testFuncName); | 156 LLVMOrcDisposeMangledSymbol(testFuncName); |
144 LLVMOrcDisposeInstance(JIT); | 157 LLVMOrcDisposeInstance(JIT); |
145 } | 158 } |
146 | 159 |
147 TEST_F(OrcCAPIExecutionTest, TestAddObjectFile) { | 160 TEST_F(OrcCAPIExecutionTest, TestAddObjectFile) { |
148 if (!TM) | 161 if (!SupportsJIT) |
149 return; | 162 return; |
150 | 163 |
151 std::unique_ptr<MemoryBuffer> ObjBuffer; | 164 auto ObjBuffer = createTestObject(); |
152 { | |
153 auto OwningObj = createTestObject(); | |
154 auto ObjAndBuffer = OwningObj->takeBinary(); | |
155 ObjBuffer = std::move(ObjAndBuffer.second); | |
156 } | |
157 | 165 |
158 LLVMOrcJITStackRef JIT = | 166 LLVMOrcJITStackRef JIT = |
159 LLVMOrcCreateInstance(wrap(TM.get())); | 167 LLVMOrcCreateInstance(wrap(TM.get())); |
160 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); | 168 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc"); |
161 | 169 |
173 LLVMOrcDisposeMangledSymbol(testFuncName); | 181 LLVMOrcDisposeMangledSymbol(testFuncName); |
174 LLVMOrcDisposeInstance(JIT); | 182 LLVMOrcDisposeInstance(JIT); |
175 } | 183 } |
176 | 184 |
177 TEST_F(OrcCAPIExecutionTest, TestDirectCallbacksAPI) { | 185 TEST_F(OrcCAPIExecutionTest, TestDirectCallbacksAPI) { |
178 if (!TM) | 186 if (!SupportsIndirection) |
179 return; | 187 return; |
180 | 188 |
181 LLVMOrcJITStackRef JIT = | 189 LLVMOrcJITStackRef JIT = |
182 LLVMOrcCreateInstance(wrap(TM.get())); | 190 LLVMOrcCreateInstance(wrap(TM.get())); |
183 | 191 |