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/Scalar.h"
21 #include "llvm-c/Transforms/PassManagerBuilder.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/Support/Host.h"
24 #include "gtest/gtest.h"
25 #include "llvm/Support/Debug.h"
29 static bool didCallAllocateCodeSection;
30 static bool didAllocateCompactUnwindSection;
32 static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
35 const char *sectionName) {
36 didCallAllocateCodeSection = true;
37 return static_cast<SectionMemoryManager*>(object)->allocateCodeSection(
38 size, alignment, sectionID, sectionName);
41 static uint8_t *roundTripAllocateDataSection(void *object, uintptr_t size,
44 const char *sectionName,
45 LLVMBool isReadOnly) {
46 if (!strcmp(sectionName, "__compact_unwind"))
47 didAllocateCompactUnwindSection = true;
48 return static_cast<SectionMemoryManager*>(object)->allocateDataSection(
49 size, alignment, sectionID, sectionName, isReadOnly);
52 static LLVMBool roundTripFinalizeMemory(void *object, char **errMsg) {
53 std::string errMsgString;
55 static_cast<SectionMemoryManager*>(object)->finalizeMemory(&errMsgString);
57 *errMsg = LLVMCreateMessage(errMsgString.c_str());
63 static void roundTripDestroy(void *object) {
64 delete static_cast<SectionMemoryManager*>(object);
69 // memory manager to test reserve allocation space callback
70 class TestReserveAllocationSpaceMemoryManager: public SectionMemoryManager {
72 uintptr_t ReservedCodeSize;
73 uintptr_t UsedCodeSize;
74 uintptr_t ReservedDataSizeRO;
75 uintptr_t UsedDataSizeRO;
76 uintptr_t ReservedDataSizeRW;
77 uintptr_t UsedDataSizeRW;
79 TestReserveAllocationSpaceMemoryManager() :
80 ReservedCodeSize(0), UsedCodeSize(0), ReservedDataSizeRO(0),
81 UsedDataSizeRO(0), ReservedDataSizeRW(0), UsedDataSizeRW(0) {
84 virtual bool needsToReserveAllocationSpace() {
88 virtual void reserveAllocationSpace(
89 uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) {
90 ReservedCodeSize = CodeSize;
91 ReservedDataSizeRO = DataSizeRO;
92 ReservedDataSizeRW = DataSizeRW;
95 void useSpace(uintptr_t* UsedSize, uintptr_t Size, unsigned Alignment) {
96 uintptr_t AlignedSize = (Size + Alignment - 1) / Alignment * Alignment;
97 uintptr_t AlignedBegin = (*UsedSize + Alignment - 1) / Alignment * Alignment;
98 *UsedSize = AlignedBegin + AlignedSize;
101 virtual uint8_t* allocateDataSection(uintptr_t Size, unsigned Alignment,
102 unsigned SectionID, StringRef SectionName, bool IsReadOnly) {
103 useSpace(IsReadOnly ? &UsedDataSizeRO : &UsedDataSizeRW, Size, Alignment);
104 return SectionMemoryManager::allocateDataSection(Size, Alignment,
105 SectionID, SectionName, IsReadOnly);
108 uint8_t* allocateCodeSection(uintptr_t Size, unsigned Alignment,
109 unsigned SectionID, StringRef SectionName) {
110 useSpace(&UsedCodeSize, Size, Alignment);
111 return SectionMemoryManager::allocateCodeSection(Size, Alignment,
112 SectionID, SectionName);
116 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
119 // The architectures below are known to be compatible with MCJIT as they
120 // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
122 SupportedArchs.push_back(Triple::aarch64);
123 SupportedArchs.push_back(Triple::arm);
124 SupportedArchs.push_back(Triple::mips);
125 SupportedArchs.push_back(Triple::x86);
126 SupportedArchs.push_back(Triple::x86_64);
128 // Some architectures have sub-architectures in which tests will fail, like
129 // ARM. These two vectors will define if they do have sub-archs (to avoid
130 // extra work for those who don't), and if so, if they are listed to work
131 HasSubArchs.push_back(Triple::arm);
132 SupportedSubArchs.push_back("armv6");
133 SupportedSubArchs.push_back("armv7");
135 // The operating systems below are known to be sufficiently incompatible
136 // that they will fail the MCJIT C API tests.
137 UnsupportedOSs.push_back(Triple::Cygwin);
140 virtual void SetUp() {
141 didCallAllocateCodeSection = false;
142 didAllocateCompactUnwindSection = false;
149 virtual void TearDown() {
151 LLVMDisposeExecutionEngine(Engine);
153 LLVMDisposeModule(Module);
156 void buildSimpleFunction() {
157 Module = LLVMModuleCreateWithName("simple_module");
159 LLVMSetTarget(Module, HostTriple.c_str());
161 Function = LLVMAddFunction(
162 Module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
163 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
165 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
166 LLVMBuilderRef builder = LLVMCreateBuilder();
167 LLVMPositionBuilderAtEnd(builder, entry);
168 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
170 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
171 LLVMDisposeMessage(Error);
173 LLVMDisposeBuilder(builder);
176 void buildFunctionThatUsesStackmap() {
177 Module = LLVMModuleCreateWithName("simple_module");
179 LLVMSetTarget(Module, HostTriple.c_str());
181 LLVMTypeRef stackmapParamTypes[] = { LLVMInt64Type(), LLVMInt32Type() };
182 LLVMValueRef stackmap = LLVMAddFunction(
183 Module, "llvm.experimental.stackmap",
184 LLVMFunctionType(LLVMVoidType(), stackmapParamTypes, 2, 1));
185 LLVMSetLinkage(stackmap, LLVMExternalLinkage);
187 Function = LLVMAddFunction(
188 Module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
190 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(Function, "entry");
191 LLVMBuilderRef builder = LLVMCreateBuilder();
192 LLVMPositionBuilderAtEnd(builder, entry);
193 LLVMValueRef stackmapArgs[] = {
194 LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 5, 0),
195 LLVMConstInt(LLVMInt32Type(), 42, 0)
197 LLVMBuildCall(builder, stackmap, stackmapArgs, 3, "");
198 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
200 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
201 LLVMDisposeMessage(Error);
203 LLVMDisposeBuilder(builder);
206 void buildModuleWithCodeAndData() {
207 Module = LLVMModuleCreateWithName("simple_module");
209 LLVMSetTarget(Module, HostTriple.c_str());
211 // build a global int32 variable initialized to 42.
212 LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "intVal");
213 LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0));
216 Function = LLVMAddFunction(
217 Module, "getGlobal", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
218 LLVMSetFunctionCallConv(Function, LLVMCCallConv);
220 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "entry");
221 LLVMBuilderRef Builder = LLVMCreateBuilder();
222 LLVMPositionBuilderAtEnd(Builder, Entry);
224 LLVMValueRef IntVal = LLVMBuildLoad(Builder, GlobalVar, "intVal");
225 LLVMBuildRet(Builder, IntVal);
227 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
228 LLVMDisposeMessage(Error);
230 LLVMDisposeBuilder(Builder);
234 LLVMTypeRef ParamTypes[] = { LLVMInt32Type() };
235 Function2 = LLVMAddFunction(
236 Module, "setGlobal", LLVMFunctionType(LLVMVoidType(), ParamTypes, 1, 0));
237 LLVMSetFunctionCallConv(Function2, LLVMCCallConv);
239 LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function2, "entry");
240 LLVMBuilderRef Builder = LLVMCreateBuilder();
241 LLVMPositionBuilderAtEnd(Builder, Entry);
243 LLVMValueRef Arg = LLVMGetParam(Function2, 0);
244 LLVMBuildStore(Builder, Arg, GlobalVar);
245 LLVMBuildRetVoid(Builder);
247 LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
248 LLVMDisposeMessage(Error);
250 LLVMDisposeBuilder(Builder);
254 void buildMCJITOptions() {
255 LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
256 Options.OptLevel = 2;
258 // Just ensure that this field still exists.
259 Options.NoFramePointerElim = false;
262 void useRoundTripSectionMemoryManager() {
263 Options.MCJMM = LLVMCreateSimpleMCJITMemoryManager(
264 new SectionMemoryManager(),
265 roundTripAllocateCodeSection,
266 roundTripAllocateDataSection,
267 roundTripFinalizeMemory,
271 void buildMCJITEngine() {
273 0, LLVMCreateMCJITCompilerForModule(&Engine, Module, &Options,
274 sizeof(Options), &Error));
277 void buildAndRunPasses() {
278 LLVMPassManagerRef pass = LLVMCreatePassManager();
279 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), pass);
280 LLVMAddConstantPropagationPass(pass);
281 LLVMAddInstructionCombiningPass(pass);
282 LLVMRunPassManager(pass, Module);
283 LLVMDisposePassManager(pass);
286 void buildAndRunOptPasses() {
287 LLVMPassManagerBuilderRef passBuilder;
289 passBuilder = LLVMPassManagerBuilderCreate();
290 LLVMPassManagerBuilderSetOptLevel(passBuilder, 2);
291 LLVMPassManagerBuilderSetSizeLevel(passBuilder, 0);
293 LLVMPassManagerRef functionPasses =
294 LLVMCreateFunctionPassManagerForModule(Module);
295 LLVMPassManagerRef modulePasses =
296 LLVMCreatePassManager();
298 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(Engine), modulePasses);
300 LLVMPassManagerBuilderPopulateFunctionPassManager(passBuilder,
302 LLVMPassManagerBuilderPopulateModulePassManager(passBuilder, modulePasses);
304 LLVMPassManagerBuilderDispose(passBuilder);
306 LLVMInitializeFunctionPassManager(functionPasses);
307 for (LLVMValueRef value = LLVMGetFirstFunction(Module);
308 value; value = LLVMGetNextFunction(value))
309 LLVMRunFunctionPassManager(functionPasses, value);
310 LLVMFinalizeFunctionPassManager(functionPasses);
312 LLVMRunPassManager(modulePasses, Module);
314 LLVMDisposePassManager(functionPasses);
315 LLVMDisposePassManager(modulePasses);
318 LLVMModuleRef Module;
319 LLVMValueRef Function;
320 LLVMValueRef Function2;
321 LLVMMCJITCompilerOptions Options;
322 LLVMExecutionEngineRef Engine;
325 } // end anonymous namespace
327 TEST_F(MCJITCAPITest, simple_function) {
328 SKIP_UNSUPPORTED_PLATFORM;
330 buildSimpleFunction();
339 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
341 EXPECT_EQ(42, functionPointer.usable());
344 TEST_F(MCJITCAPITest, custom_memory_manager) {
345 SKIP_UNSUPPORTED_PLATFORM;
347 buildSimpleFunction();
349 useRoundTripSectionMemoryManager();
357 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
359 EXPECT_EQ(42, functionPointer.usable());
360 EXPECT_TRUE(didCallAllocateCodeSection);
363 TEST_F(MCJITCAPITest, stackmap_creates_compact_unwind_on_darwin) {
364 SKIP_UNSUPPORTED_PLATFORM;
366 buildFunctionThatUsesStackmap();
368 useRoundTripSectionMemoryManager();
370 buildAndRunOptPasses();
376 functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);
378 EXPECT_EQ(42, functionPointer.usable());
379 EXPECT_TRUE(didCallAllocateCodeSection);
382 Triple(HostTriple).getOS() != Triple::Darwin ||
383 didAllocateCompactUnwindSection);
386 TEST_F(MCJITCAPITest, reserve_allocation_space) {
387 SKIP_UNSUPPORTED_PLATFORM;
389 TestReserveAllocationSpaceMemoryManager* MM = new TestReserveAllocationSpaceMemoryManager();
391 buildModuleWithCodeAndData();
393 Options.MCJMM = wrap(MM);
401 GetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function);
407 SetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function2);
409 SetGlobalFct.usable(789);
410 EXPECT_EQ(789, GetGlobalFct.usable());
411 EXPECT_LE(MM->UsedCodeSize, MM->ReservedCodeSize);
412 EXPECT_LE(MM->UsedDataSizeRO, MM->ReservedDataSizeRO);
413 EXPECT_LE(MM->UsedDataSizeRW, MM->ReservedDataSizeRW);
414 EXPECT_TRUE(MM->UsedCodeSize > 0);
415 EXPECT_TRUE(MM->UsedDataSizeRW > 0);