1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
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 // This test suite verifies basic MCJIT functionality when invoked form the C
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Analysis.h"
16 #include "MCJITTestAPICommon.h"
17 #include "llvm-c/Core.h"
18 #include "llvm-c/ExecutionEngine.h"
19 #include "llvm-c/Target.h"
20 #include "llvm-c/Transforms/PassManagerBuilder.h"
21 #include "llvm-c/Transforms/Scalar.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Host.h"
25 #include "gtest/gtest.h"
29 static bool didCallAllocateCodeSection;
30 static bool didAllocateCompactUnwindSection;
31 static bool didCallYield;
33 static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
36 const char *sectionName) {
37 didCallAllocateCodeSection = true;
38 return static_cast<SectionMemoryManager*>(object)->allocateCodeSection(
39 size, alignment, sectionID, sectionName);
42 static uint8_t *roundTripAllocateDataSection(void *object, uintptr_t size,
45 const char *sectionName,
46 LLVMBool isReadOnly) {
47 if (!strcmp(sectionName, "__compact_unwind"))
48 didAllocateCompactUnwindSection = true;
49 return static_cast<SectionMemoryManager*>(object)->allocateDataSection(
50 size, alignment, sectionID, sectionName, isReadOnly);
53 static LLVMBool roundTripFinalizeMemory(void *object, char **errMsg) {
54 std::string errMsgString;
56 static_cast<SectionMemoryManager*>(object)->finalizeMemory(&errMsgString);
58 *errMsg = LLVMCreateMessage(errMsgString.c_str());
64 static void roundTripDestroy(void *object) {
65 delete static_cast<SectionMemoryManager*>(object);
68 static void yield(LLVMContextRef, void *) {
74 // memory manager to test reserve allocation space callback
75 class TestReserveAllocationSpaceMemoryManager: public SectionMemoryManager {
77 uintptr_t ReservedCodeSize;
78 uintptr_t UsedCodeSize;
79 uintptr_t ReservedDataSizeRO;
80 uintptr_t UsedDataSizeRO;
81 uintptr_t ReservedDataSizeRW;
82 uintptr_t UsedDataSizeRW;
84 TestReserveAllocationSpaceMemoryManager() :
85 ReservedCodeSize(0), UsedCodeSize(0), ReservedDataSizeRO(0),
86 UsedDataSizeRO(0), ReservedDataSizeRW(0), UsedDataSizeRW(0) {
89 virtual bool needsToReserveAllocationSpace() {
93 virtual void reserveAllocationSpace(
94 uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) {
95 ReservedCodeSize = CodeSize;
96 ReservedDataSizeRO = DataSizeRO;
97 ReservedDataSizeRW = DataSizeRW;
100 void useSpace(uintptr_t* UsedSize, uintptr_t Size, unsigned Alignment) {
101 uintptr_t AlignedSize = (Size + Alignment - 1) / Alignment * Alignment;
102 uintptr_t AlignedBegin = (*UsedSize + Alignment - 1) / Alignment * Alignment;
103 *UsedSize = AlignedBegin + AlignedSize;
106 virtual uint8_t* allocateDataSection(uintptr_t Size, unsigned Alignment,
107 unsigned SectionID, StringRef SectionName, bool IsReadOnly) {
108 useSpace(IsReadOnly ? &UsedDataSizeRO : &UsedDataSizeRW, Size, Alignment);
109 return SectionMemoryManager::allocateDataSection(Size, Alignment,
110 SectionID, SectionName, IsReadOnly);
113 uint8_t* allocateCodeSection(uintptr_t Size, unsigned Alignment,
114 unsigned SectionID, StringRef SectionName) {
115 useSpace(&UsedCodeSize, Size, Alignment);
116 return SectionMemoryManager::allocateCodeSection(Size, Alignment,
117 SectionID, SectionName);
121 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
124 // The architectures below are known to be compatible with MCJIT as they
125 // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
127 SupportedArchs.push_back(Triple::aarch64);
128 SupportedArchs.push_back(Triple::arm);
129 SupportedArchs.push_back(Triple::mips);
130 SupportedArchs.push_back(Triple::x86);
131 SupportedArchs.push_back(Triple::x86_64);
133 // Some architectures have sub-architectures in which tests will fail, like
134 // ARM. These two vectors will define if they do have sub-archs (to avoid
135 // extra work for those who don't), and if so, if they are listed to work
136 HasSubArchs.push_back(Triple::arm);
137 SupportedSubArchs.push_back("armv6");
138 SupportedSubArchs.push_back("armv7");
140 // The operating systems below are known to be sufficiently incompatible
141 // that they will fail the MCJIT C API tests.
142 UnsupportedOSs.push_back(Triple::Cygwin);
144 UnsupportedEnvironments.push_back(Triple::Cygnus);
147 virtual void SetUp() {
148 didCallAllocateCodeSection = false;
149 didAllocateCompactUnwindSection = false;
150 didCallYield = false;
157 virtual void TearDown() {
159 LLVMDisposeExecutionEngine(Engine);
161 LLVMDisposeModule(Module);
164 void buildSimpleFunction() {
165 Module = LLVMModuleCreateWithName("simple_module");
167 LLVMSetTarget(Module, HostTriple.c_str());
169 Function = LLVMAddFunction(Module, "simple_function",
170 LLVMFunctionType(LLVMInt32Type(), nullptr,0, 0));
171 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
173 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
174 LLVMBuilderRef builder = LLVMCreateBuilder();
175 LLVMPositionBuilderAtEnd(builder, entry);
176 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
178 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
179 LLVMDisposeMessage(Error);
181 LLVMDisposeBuilder(builder);
184 void buildFunctionThatUsesStackmap() {
185 Module = LLVMModuleCreateWithName("simple_module");
187 LLVMSetTarget(Module, HostTriple.c_str());
189 LLVMTypeRef stackmapParamTypes[] = { LLVMInt64Type(), LLVMInt32Type() };
190 LLVMValueRef stackmap = LLVMAddFunction(
191 Module, "llvm.experimental.stackmap",
192 LLVMFunctionType(LLVMVoidType(), stackmapParamTypes, 2, 1));
193 LLVMSetLinkage(stackmap, LLVMExternalLinkage);
195 Function = LLVMAddFunction(Module, "simple_function",
196 LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
198 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
199 LLVMBuilderRef builder = LLVMCreateBuilder();
200 LLVMPositionBuilderAtEnd(builder, entry);
201 LLVMValueRef stackmapArgs[] = {
202 LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 5, 0),
203 LLVMConstInt(LLVMInt32Type(), 42, 0)
205 LLVMBuildCall(builder, stackmap, stackmapArgs, 3, "");
206 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
208 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
209 LLVMDisposeMessage(Error);
211 LLVMDisposeBuilder(builder);
214 void buildModuleWithCodeAndData() {
215 Module = LLVMModuleCreateWithName("simple_module");
217 LLVMSetTarget(Module, HostTriple.c_str());
219 // build a global int32 variable initialized to 42.
220 LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "intVal");
221 LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
224 Function = LLVMAddFunction(Module, "getGlobal",
225 LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
226 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
228 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "entry");
229 LLVMBuilderRef Builder = LLVMCreateBuilder();
230 LLVMPositionBuilderAtEnd(Builder, Entry);
232 LLVMValueRef IntVal = LLVMBuildLoad(Builder, GlobalVar, "intVal");
233 LLVMBuildRet(Builder, IntVal);
235 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
236 LLVMDisposeMessage(Error);
238 LLVMDisposeBuilder(Builder);
242 LLVMTypeRef ParamTypes[] = { LLVMInt32Type() };
243 Function2 = LLVMAddFunction(
244 Module, "setGlobal", LLVMFunctionType(LLVMVoidType(), ParamTypes, 1, 0));
245 LLVMSetFunctionCallConv(Function2, LLVMCCallConv);
247 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function2, "entry");
248 LLVMBuilderRef Builder = LLVMCreateBuilder();
249 LLVMPositionBuilderAtEnd(Builder, Entry);
251 LLVMValueRef Arg = LLVMGetParam(Function2, 0);
252 LLVMBuildStore(Builder, Arg, GlobalVar);
253 LLVMBuildRetVoid(Builder);
255 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
256 LLVMDisposeMessage(Error);
258 LLVMDisposeBuilder(Builder);
262 void buildMCJITOptions() {
263 LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
264 Options.OptLevel = 2;
266 // Just ensure that this field still exists.
267 Options.NoFramePointerElim = false;
270 void useRoundTripSectionMemoryManager() {
271 Options.MCJMM = LLVMCreateSimpleMCJITMemoryManager(
272 new SectionMemoryManager(),
273 roundTripAllocateCodeSection,
274 roundTripAllocateDataSection,
275 roundTripFinalizeMemory,
279 void buildMCJITEngine() {
281 0, LLVMCreateMCJITCompilerForModule(&Engine, Module, &Options,
282 sizeof(Options), &Error));
285 void buildAndRunPasses() {
286 LLVMPassManagerRef pass = LLVMCreatePassManager();
287 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), pass);
288 LLVMAddConstantPropagationPass(pass);
289 LLVMAddInstructionCombiningPass(pass);
290 LLVMRunPassManager(pass, Module);
291 LLVMDisposePassManager(pass);
294 void buildAndRunOptPasses() {
295 LLVMPassManagerBuilderRef passBuilder;
297 passBuilder = LLVMPassManagerBuilderCreate();
298 LLVMPassManagerBuilderSetOptLevel(passBuilder, 2);
299 LLVMPassManagerBuilderSetSizeLevel(passBuilder, 0);
301 LLVMPassManagerRef functionPasses =
302 LLVMCreateFunctionPassManagerForModule(Module);
303 LLVMPassManagerRef modulePasses =
304 LLVMCreatePassManager();
306 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), modulePasses);
308 LLVMPassManagerBuilderPopulateFunctionPassManager(passBuilder,
310 LLVMPassManagerBuilderPopulateModulePassManager(passBuilder, modulePasses);
312 LLVMPassManagerBuilderDispose(passBuilder);
314 LLVMInitializeFunctionPassManager(functionPasses);
315 for (LLVMValueRef value = LLVMGetFirstFunction(Module);
316 value; value = LLVMGetNextFunction(value))
317 LLVMRunFunctionPassManager(functionPasses, value);
318 LLVMFinalizeFunctionPassManager(functionPasses);
320 LLVMRunPassManager(modulePasses, Module);
322 LLVMDisposePassManager(functionPasses);
323 LLVMDisposePassManager(modulePasses);
326 LLVMModuleRef Module;
327 LLVMValueRef Function;
328 LLVMValueRef Function2;
329 LLVMMCJITCompilerOptions Options;
330 LLVMExecutionEngineRef Engine;
333 } // end anonymous namespace
335 TEST_F(MCJITCAPITest, simple_function) {
336 SKIP_UNSUPPORTED_PLATFORM;
338 buildSimpleFunction();
347 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
349 EXPECT_EQ(42, functionPointer.usable());
352 TEST_F(MCJITCAPITest, custom_memory_manager) {
353 SKIP_UNSUPPORTED_PLATFORM;
355 buildSimpleFunction();
357 useRoundTripSectionMemoryManager();
365 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
367 EXPECT_EQ(42, functionPointer.usable());
368 EXPECT_TRUE(didCallAllocateCodeSection);
371 TEST_F(MCJITCAPITest, stackmap_creates_compact_unwind_on_darwin) {
372 SKIP_UNSUPPORTED_PLATFORM;
374 // This test is also not supported on non-x86 platforms.
375 if (Triple(HostTriple).getArch() != Triple::x86_64)
378 buildFunctionThatUsesStackmap();
380 useRoundTripSectionMemoryManager();
382 buildAndRunOptPasses();
388 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
390 EXPECT_EQ(42, functionPointer.usable());
391 EXPECT_TRUE(didCallAllocateCodeSection);
393 // Up to this point, the test is specific only to X86-64. But this next
394 // expectation is only valid on Darwin because it assumes that unwind
395 // data is made available only through compact_unwind. It would be
396 // worthwhile to extend this to handle non-Darwin platforms, in which
397 // case you'd want to look for an eh_frame or something.
399 // FIXME: Currently, MCJIT relies on a configure-time check to determine which
400 // sections to emit. The JIT client should have runtime control over this.
402 Triple(HostTriple).getOS() != Triple::Darwin ||
403 Triple(HostTriple).isMacOSXVersionLT(10, 7) ||
404 didAllocateCompactUnwindSection);
407 TEST_F(MCJITCAPITest, reserve_allocation_space) {
408 SKIP_UNSUPPORTED_PLATFORM;
410 TestReserveAllocationSpaceMemoryManager* MM = new TestReserveAllocationSpaceMemoryManager();
412 buildModuleWithCodeAndData();
414 Options.MCJMM = wrap(MM);
422 GetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function);
428 SetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function2);
430 SetGlobalFct.usable(789);
431 EXPECT_EQ(789, GetGlobalFct.usable());
432 EXPECT_LE(MM->UsedCodeSize, MM->ReservedCodeSize);
433 EXPECT_LE(MM->UsedDataSizeRO, MM->ReservedDataSizeRO);
434 EXPECT_LE(MM->UsedDataSizeRW, MM->ReservedDataSizeRW);
435 EXPECT_TRUE(MM->UsedCodeSize > 0);
436 EXPECT_TRUE(MM->UsedDataSizeRW > 0);
439 TEST_F(MCJITCAPITest, yield) {
440 SKIP_UNSUPPORTED_PLATFORM;
442 buildSimpleFunction();
445 LLVMContextRef C = LLVMGetGlobalContext();
446 LLVMContextSetYieldCallback(C, yield, nullptr);
453 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
455 EXPECT_EQ(42, functionPointer.usable());
456 EXPECT_TRUE(didCallYield);