1 //===- MCJITTest.cpp - Unit tests for the MCJIT -----------------*- C++ -*-===//
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 bool needsToReserveAllocationSpace() override { return true; }
91 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
92 uintptr_t DataSizeRO, uint32_t RODataAlign,
93 uintptr_t DataSizeRW, uint32_t RWDataAlign) override {
94 ReservedCodeSize = CodeSize;
95 ReservedDataSizeRO = DataSizeRO;
96 ReservedDataSizeRW = DataSizeRW;
99 void useSpace(uintptr_t* UsedSize, uintptr_t Size, unsigned Alignment) {
100 uintptr_t AlignedSize = (Size + Alignment - 1) / Alignment * Alignment;
101 uintptr_t AlignedBegin = (*UsedSize + Alignment - 1) / Alignment * Alignment;
102 *UsedSize = AlignedBegin + AlignedSize;
105 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
106 unsigned SectionID, StringRef SectionName,
107 bool IsReadOnly) override {
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,
115 StringRef SectionName) override {
116 useSpace(&UsedCodeSize, Size, Alignment);
117 return SectionMemoryManager::allocateCodeSection(Size, Alignment,
118 SectionID, SectionName);
122 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
125 // The architectures below are known to be compatible with MCJIT as they
126 // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
128 SupportedArchs.push_back(Triple::aarch64);
129 SupportedArchs.push_back(Triple::arm);
130 SupportedArchs.push_back(Triple::mips);
131 SupportedArchs.push_back(Triple::mips64);
132 SupportedArchs.push_back(Triple::mips64el);
133 SupportedArchs.push_back(Triple::x86);
134 SupportedArchs.push_back(Triple::x86_64);
136 // Some architectures have sub-architectures in which tests will fail, like
137 // ARM. These two vectors will define if they do have sub-archs (to avoid
138 // extra work for those who don't), and if so, if they are listed to work
139 HasSubArchs.push_back(Triple::arm);
140 SupportedSubArchs.push_back("armv6");
141 SupportedSubArchs.push_back("armv7");
143 // The operating systems below are known to be sufficiently incompatible
144 // that they will fail the MCJIT C API tests.
145 UnsupportedEnvironments.push_back(Triple::Cygnus);
148 void SetUp() override {
149 didCallAllocateCodeSection = false;
150 didAllocateCompactUnwindSection = false;
151 didCallYield = false;
158 void TearDown() override {
160 LLVMDisposeExecutionEngine(Engine);
162 LLVMDisposeModule(Module);
165 void buildSimpleFunction() {
166 Module = LLVMModuleCreateWithName("simple_module");
168 LLVMSetTarget(Module, HostTriple.c_str());
170 Function = LLVMAddFunction(Module, "simple_function",
171 LLVMFunctionType(LLVMInt32Type(), nullptr,0, 0));
172 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
174 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
175 LLVMBuilderRef builder = LLVMCreateBuilder();
176 LLVMPositionBuilderAtEnd(builder, entry);
177 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
179 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
180 LLVMDisposeMessage(Error);
182 LLVMDisposeBuilder(builder);
185 void buildFunctionThatUsesStackmap() {
186 Module = LLVMModuleCreateWithName("simple_module");
188 LLVMSetTarget(Module, HostTriple.c_str());
190 LLVMTypeRef stackmapParamTypes[] = { LLVMInt64Type(), LLVMInt32Type() };
191 LLVMValueRef stackmap = LLVMAddFunction(
192 Module, "llvm.experimental.stackmap",
193 LLVMFunctionType(LLVMVoidType(), stackmapParamTypes, 2, 1));
194 LLVMSetLinkage(stackmap, LLVMExternalLinkage);
196 Function = LLVMAddFunction(Module, "simple_function",
197 LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
199 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
200 LLVMBuilderRef builder = LLVMCreateBuilder();
201 LLVMPositionBuilderAtEnd(builder, entry);
202 LLVMValueRef stackmapArgs[] = {
203 LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 5, 0),
204 LLVMConstInt(LLVMInt32Type(), 42, 0)
206 LLVMBuildCall(builder, stackmap, stackmapArgs, 3, "");
207 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
209 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
210 LLVMDisposeMessage(Error);
212 LLVMDisposeBuilder(builder);
215 void buildModuleWithCodeAndData() {
216 Module = LLVMModuleCreateWithName("simple_module");
218 LLVMSetTarget(Module, HostTriple.c_str());
220 // build a global int32 variable initialized to 42.
221 LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "intVal");
222 LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
225 Function = LLVMAddFunction(Module, "getGlobal",
226 LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0));
227 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
229 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "entry");
230 LLVMBuilderRef Builder = LLVMCreateBuilder();
231 LLVMPositionBuilderAtEnd(Builder, Entry);
233 LLVMValueRef IntVal = LLVMBuildLoad(Builder, GlobalVar, "intVal");
234 LLVMBuildRet(Builder, IntVal);
236 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
237 LLVMDisposeMessage(Error);
239 LLVMDisposeBuilder(Builder);
243 LLVMTypeRef ParamTypes[] = { LLVMInt32Type() };
244 Function2 = LLVMAddFunction(
245 Module, "setGlobal", LLVMFunctionType(LLVMVoidType(), ParamTypes, 1, 0));
246 LLVMSetFunctionCallConv(Function2, LLVMCCallConv);
248 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function2, "entry");
249 LLVMBuilderRef Builder = LLVMCreateBuilder();
250 LLVMPositionBuilderAtEnd(Builder, Entry);
252 LLVMValueRef Arg = LLVMGetParam(Function2, 0);
253 LLVMBuildStore(Builder, Arg, GlobalVar);
254 LLVMBuildRetVoid(Builder);
256 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
257 LLVMDisposeMessage(Error);
259 LLVMDisposeBuilder(Builder);
263 void buildMCJITOptions() {
264 LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
265 Options.OptLevel = 2;
267 // Just ensure that this field still exists.
268 Options.NoFramePointerElim = false;
271 void useRoundTripSectionMemoryManager() {
272 Options.MCJMM = LLVMCreateSimpleMCJITMemoryManager(
273 new SectionMemoryManager(),
274 roundTripAllocateCodeSection,
275 roundTripAllocateDataSection,
276 roundTripFinalizeMemory,
280 void buildMCJITEngine() {
282 0, LLVMCreateMCJITCompilerForModule(&Engine, Module, &Options,
283 sizeof(Options), &Error));
286 void buildAndRunPasses() {
287 LLVMPassManagerRef pass = LLVMCreatePassManager();
288 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), pass);
289 LLVMAddConstantPropagationPass(pass);
290 LLVMAddInstructionCombiningPass(pass);
291 LLVMRunPassManager(pass, Module);
292 LLVMDisposePassManager(pass);
295 void buildAndRunOptPasses() {
296 LLVMPassManagerBuilderRef passBuilder;
298 passBuilder = LLVMPassManagerBuilderCreate();
299 LLVMPassManagerBuilderSetOptLevel(passBuilder, 2);
300 LLVMPassManagerBuilderSetSizeLevel(passBuilder, 0);
302 LLVMPassManagerRef functionPasses =
303 LLVMCreateFunctionPassManagerForModule(Module);
304 LLVMPassManagerRef modulePasses =
305 LLVMCreatePassManager();
307 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), modulePasses);
309 LLVMPassManagerBuilderPopulateFunctionPassManager(passBuilder,
311 LLVMPassManagerBuilderPopulateModulePassManager(passBuilder, modulePasses);
313 LLVMPassManagerBuilderDispose(passBuilder);
315 LLVMInitializeFunctionPassManager(functionPasses);
316 for (LLVMValueRef value = LLVMGetFirstFunction(Module);
317 value; value = LLVMGetNextFunction(value))
318 LLVMRunFunctionPassManager(functionPasses, value);
319 LLVMFinalizeFunctionPassManager(functionPasses);
321 LLVMRunPassManager(modulePasses, Module);
323 LLVMDisposePassManager(functionPasses);
324 LLVMDisposePassManager(modulePasses);
327 LLVMModuleRef Module;
328 LLVMValueRef Function;
329 LLVMValueRef Function2;
330 LLVMMCJITCompilerOptions Options;
331 LLVMExecutionEngineRef Engine;
334 } // end anonymous namespace
336 TEST_F(MCJITCAPITest, simple_function) {
337 SKIP_UNSUPPORTED_PLATFORM;
339 buildSimpleFunction();
344 auto *functionPointer = reinterpret_cast<int (*)()>(
345 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
347 EXPECT_EQ(42, functionPointer());
350 TEST_F(MCJITCAPITest, gva) {
351 SKIP_UNSUPPORTED_PLATFORM;
353 Module = LLVMModuleCreateWithName("simple_module");
354 LLVMSetTarget(Module, HostTriple.c_str());
355 LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "simple_value");
356 LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
362 uint64_t raw = LLVMGetGlobalValueAddress(Engine, "simple_value");
363 int32_t *usable = (int32_t *) raw;
365 EXPECT_EQ(42, *usable);
368 TEST_F(MCJITCAPITest, gfa) {
369 SKIP_UNSUPPORTED_PLATFORM;
371 buildSimpleFunction();
376 uint64_t raw = LLVMGetFunctionAddress(Engine, "simple_function");
377 int (*usable)() = (int (*)()) raw;
379 EXPECT_EQ(42, usable());
382 TEST_F(MCJITCAPITest, custom_memory_manager) {
383 SKIP_UNSUPPORTED_PLATFORM;
385 buildSimpleFunction();
387 useRoundTripSectionMemoryManager();
391 auto *functionPointer = reinterpret_cast<int (*)()>(
392 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
394 EXPECT_EQ(42, functionPointer());
395 EXPECT_TRUE(didCallAllocateCodeSection);
398 TEST_F(MCJITCAPITest, stackmap_creates_compact_unwind_on_darwin) {
399 SKIP_UNSUPPORTED_PLATFORM;
401 // This test is also not supported on non-x86 platforms.
402 if (Triple(HostTriple).getArch() != Triple::x86_64)
405 buildFunctionThatUsesStackmap();
407 useRoundTripSectionMemoryManager();
409 buildAndRunOptPasses();
411 auto *functionPointer = reinterpret_cast<int (*)()>(
412 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
414 EXPECT_EQ(42, functionPointer());
415 EXPECT_TRUE(didCallAllocateCodeSection);
417 // Up to this point, the test is specific only to X86-64. But this next
418 // expectation is only valid on Darwin because it assumes that unwind
419 // data is made available only through compact_unwind. It would be
420 // worthwhile to extend this to handle non-Darwin platforms, in which
421 // case you'd want to look for an eh_frame or something.
423 // FIXME: Currently, MCJIT relies on a configure-time check to determine which
424 // sections to emit. The JIT client should have runtime control over this.
426 Triple(HostTriple).getOS() != Triple::Darwin ||
427 Triple(HostTriple).isMacOSXVersionLT(10, 7) ||
428 didAllocateCompactUnwindSection);
431 TEST_F(MCJITCAPITest, reserve_allocation_space) {
432 SKIP_UNSUPPORTED_PLATFORM;
434 TestReserveAllocationSpaceMemoryManager* MM = new TestReserveAllocationSpaceMemoryManager();
436 buildModuleWithCodeAndData();
438 Options.MCJMM = wrap(MM);
442 auto GetGlobalFct = reinterpret_cast<int (*)()>(
443 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
445 auto SetGlobalFct = reinterpret_cast<void (*)(int)>(
446 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function2)));
449 EXPECT_EQ(789, GetGlobalFct());
450 EXPECT_LE(MM->UsedCodeSize, MM->ReservedCodeSize);
451 EXPECT_LE(MM->UsedDataSizeRO, MM->ReservedDataSizeRO);
452 EXPECT_LE(MM->UsedDataSizeRW, MM->ReservedDataSizeRW);
453 EXPECT_TRUE(MM->UsedCodeSize > 0);
454 EXPECT_TRUE(MM->UsedDataSizeRW > 0);
457 TEST_F(MCJITCAPITest, yield) {
458 SKIP_UNSUPPORTED_PLATFORM;
460 buildSimpleFunction();
463 LLVMContextRef C = LLVMGetGlobalContext();
464 LLVMContextSetYieldCallback(C, yield, nullptr);
467 auto *functionPointer = reinterpret_cast<int (*)()>(
468 reinterpret_cast<uintptr_t>(LLVMGetPointerToGlobal(Engine, Function)));
470 EXPECT_EQ(42, functionPointer());
471 EXPECT_TRUE(didCallYield);
474 static int localTestFunc() {
478 TEST_F(MCJITCAPITest, addGlobalMapping) {
479 SKIP_UNSUPPORTED_PLATFORM;
481 Module = LLVMModuleCreateWithName("testModule");
482 LLVMSetTarget(Module, HostTriple.c_str());
483 LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0);
484 LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType);
486 Function = LLVMAddFunction(Module, "test_fn", FunctionType);
487 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "");
488 LLVMBuilderRef Builder = LLVMCreateBuilder();
489 LLVMPositionBuilderAtEnd(Builder, Entry);
490 LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, nullptr, 0, "");
491 LLVMBuildRet(Builder, RetVal);
492 LLVMDisposeBuilder(Builder);
494 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
495 LLVMDisposeMessage(Error);
500 LLVMAddGlobalMapping(
502 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(&localTestFunc)));
506 uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn");
507 int (*usable)() = (int (*)()) raw;
509 EXPECT_EQ(42, usable());