2015-10-07 01:24:35 +02:00
|
|
|
//===- MCJITTestBase.h - Common base class for MCJIT Unit tests -*- C++ -*-===//
|
2012-10-04 22:29:44 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-10-04 22:29:44 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements common functionality required by the MCJIT unit tests,
|
|
|
|
// as well as logic to skip tests on unsupported architectures and operating
|
|
|
|
// systems.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H
|
|
|
|
#define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H
|
2012-10-04 22:29:44 +02:00
|
|
|
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "MCJITTestAPICommon.h"
|
2012-10-04 22:29:44 +02:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2012-11-27 20:42:02 +01:00
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2019-01-13 17:09:28 +01:00
|
|
|
#include "llvm/IR/Type.h"
|
2012-10-04 22:29:44 +02:00
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-06-28 22:37:20 +02:00
|
|
|
/// Helper class that can build very simple Modules
|
|
|
|
class TrivialModuleBuilder {
|
2012-10-04 22:29:44 +02:00
|
|
|
protected:
|
2013-06-28 22:37:20 +02:00
|
|
|
LLVMContext Context;
|
|
|
|
IRBuilder<> Builder;
|
|
|
|
std::string BuilderTriple;
|
2012-10-04 22:29:44 +02:00
|
|
|
|
2013-06-28 22:37:20 +02:00
|
|
|
TrivialModuleBuilder(const std::string &Triple)
|
|
|
|
: Builder(Context), BuilderTriple(Triple) {}
|
2012-10-04 22:29:44 +02:00
|
|
|
|
2013-06-28 22:37:20 +02:00
|
|
|
Module *createEmptyModule(StringRef Name = StringRef()) {
|
2012-10-04 22:29:44 +02:00
|
|
|
Module * M = new Module(Name, Context);
|
2013-06-28 22:37:20 +02:00
|
|
|
M->setTargetTriple(Triple::normalize(BuilderTriple));
|
2012-10-04 22:29:44 +02:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *startFunction(Module *M, FunctionType *FT, StringRef Name) {
|
|
|
|
Function *Result =
|
|
|
|
Function::Create(FT, GlobalValue::ExternalLinkage, Name, M);
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
|
|
|
|
Builder.SetInsertPoint(BB);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void endFunctionWithRet(Function *Func, Value *RetValue) {
|
|
|
|
Builder.CreateRet(RetValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a simple function that invokes Callee and takes the same arguments:
|
|
|
|
// int Caller(...) { return Callee(...); }
|
|
|
|
Function *insertSimpleCallFunction(Module *M, Function *Callee) {
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *Result = startFunction(M, Callee->getFunctionType(), "caller");
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
SmallVector<Value*, 1> CallArgs;
|
|
|
|
|
2015-10-20 20:30:20 +02:00
|
|
|
for (Argument &A : Result->args())
|
|
|
|
CallArgs.push_back(&A);
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
|
|
|
|
Builder.CreateRet(ReturnCode);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a function named 'main' that returns a uint32_t:
|
|
|
|
// int32_t main() { return X; }
|
|
|
|
// where X is given by returnCode
|
|
|
|
Function *insertMainFunction(Module *M, uint32_t returnCode) {
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *Result = startFunction(
|
|
|
|
M, FunctionType::get(Type::getInt32Ty(Context), {}, false), "main");
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
|
|
|
|
endFunctionWithRet(Result, ReturnVal);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a function
|
|
|
|
// int32_t add(int32_t a, int32_t b) { return a + b; }
|
|
|
|
// in the current module and returns a pointer to it.
|
|
|
|
Function *insertAddFunction(Module *M, StringRef Name = "add") {
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *Result = startFunction(
|
|
|
|
M,
|
|
|
|
FunctionType::get(
|
|
|
|
Type::getInt32Ty(Context),
|
|
|
|
{Type::getInt32Ty(Context), Type::getInt32Ty(Context)}, false),
|
|
|
|
Name);
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
Function::arg_iterator args = Result->arg_begin();
|
2015-10-20 20:30:20 +02:00
|
|
|
Value *Arg1 = &*args;
|
|
|
|
Value *Arg2 = &*++args;
|
2012-10-04 22:29:44 +02:00
|
|
|
Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
|
|
|
|
|
|
|
|
endFunctionWithRet(Result, AddResult);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2014-10-23 01:18:42 +02:00
|
|
|
// Inserts a declaration to a function defined elsewhere
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *insertExternalReferenceToFunction(Module *M, FunctionType *FTy,
|
|
|
|
StringRef Name) {
|
|
|
|
Function *Result =
|
|
|
|
Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
|
2012-10-04 22:29:44 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts an declaration to a function defined elsewhere
|
|
|
|
Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
|
|
|
|
Function *Result = Function::Create(Func->getFunctionType(),
|
2013-10-01 03:48:36 +02:00
|
|
|
GlobalValue::ExternalLinkage,
|
2012-10-04 22:29:44 +02:00
|
|
|
Func->getName(), M);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a global variable of type int32
|
2013-10-01 03:48:36 +02:00
|
|
|
// FIXME: make this a template function to support any type
|
2012-10-04 22:29:44 +02:00
|
|
|
GlobalVariable *insertGlobalInt32(Module *M,
|
|
|
|
StringRef name,
|
|
|
|
int32_t InitialValue) {
|
2019-01-13 17:09:28 +01:00
|
|
|
Type *GlobalTy = Type::getInt32Ty(Context);
|
2012-10-04 22:29:44 +02:00
|
|
|
Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
|
|
|
|
GlobalVariable *Global = new GlobalVariable(*M,
|
|
|
|
GlobalTy,
|
|
|
|
false,
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
IV,
|
|
|
|
name);
|
|
|
|
return Global;
|
|
|
|
}
|
2013-10-01 03:48:36 +02:00
|
|
|
|
|
|
|
// Inserts a function
|
|
|
|
// int32_t recursive_add(int32_t num) {
|
|
|
|
// if (num == 0) {
|
|
|
|
// return num;
|
|
|
|
// } else {
|
|
|
|
// int32_t recursive_param = num - 1;
|
|
|
|
// return num + Helper(recursive_param);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// NOTE: if Helper is left as the default parameter, Helper == recursive_add.
|
|
|
|
Function *insertAccumulateFunction(Module *M,
|
2015-10-07 01:24:35 +02:00
|
|
|
Function *Helper = nullptr,
|
|
|
|
StringRef Name = "accumulate") {
|
2019-01-13 17:09:28 +01:00
|
|
|
Function *Result =
|
|
|
|
startFunction(M,
|
|
|
|
FunctionType::get(Type::getInt32Ty(Context),
|
|
|
|
{Type::getInt32Ty(Context)}, false),
|
|
|
|
Name);
|
2015-10-07 01:24:35 +02:00
|
|
|
if (!Helper)
|
2013-10-01 03:48:36 +02:00
|
|
|
Helper = Result;
|
|
|
|
|
|
|
|
BasicBlock *BaseCase = BasicBlock::Create(Context, "", Result);
|
|
|
|
BasicBlock *RecursiveCase = BasicBlock::Create(Context, "", Result);
|
|
|
|
|
|
|
|
// if (num == 0)
|
2015-10-20 20:30:20 +02:00
|
|
|
Value *Param = &*Result->arg_begin();
|
2013-10-01 03:48:36 +02:00
|
|
|
Value *Zero = ConstantInt::get(Context, APInt(32, 0));
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpEQ(Param, Zero),
|
|
|
|
BaseCase, RecursiveCase);
|
|
|
|
|
|
|
|
// return num;
|
|
|
|
Builder.SetInsertPoint(BaseCase);
|
|
|
|
Builder.CreateRet(Param);
|
|
|
|
|
|
|
|
// int32_t recursive_param = num - 1;
|
|
|
|
// return Helper(recursive_param);
|
|
|
|
Builder.SetInsertPoint(RecursiveCase);
|
|
|
|
Value *One = ConstantInt::get(Context, APInt(32, 1));
|
|
|
|
Value *RecursiveParam = Builder.CreateSub(Param, One);
|
|
|
|
Value *RecursiveReturn = Builder.CreateCall(Helper, RecursiveParam);
|
|
|
|
Value *Accumulator = Builder.CreateAdd(Param, RecursiveReturn);
|
|
|
|
Builder.CreateRet(Accumulator);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates Modules A and B:
|
|
|
|
// Module A { Extern FB1, Function FA which calls FB1 },
|
|
|
|
// Module B { Extern FA, Function FB1, Function FB2 which calls FA },
|
2014-03-06 06:51:42 +01:00
|
|
|
void createCrossModuleRecursiveCase(std::unique_ptr<Module> &A, Function *&FA,
|
|
|
|
std::unique_ptr<Module> &B,
|
|
|
|
Function *&FB1, Function *&FB2) {
|
2013-10-01 03:48:36 +02:00
|
|
|
// Define FB1 in B.
|
|
|
|
B.reset(createEmptyModule("B"));
|
2015-10-07 01:24:35 +02:00
|
|
|
FB1 = insertAccumulateFunction(B.get(), nullptr, "FB1");
|
2013-10-01 03:48:36 +02:00
|
|
|
|
|
|
|
// Declare FB1 in A (as an external).
|
|
|
|
A.reset(createEmptyModule("A"));
|
|
|
|
Function *FB1Extern = insertExternalReferenceToFunction(A.get(), FB1);
|
|
|
|
|
|
|
|
// Define FA in A (with a call to FB1).
|
|
|
|
FA = insertAccumulateFunction(A.get(), FB1Extern, "FA");
|
|
|
|
|
|
|
|
// Declare FA in B (as an external)
|
|
|
|
Function *FAExtern = insertExternalReferenceToFunction(B.get(), FA);
|
|
|
|
|
|
|
|
// Define FB2 in B (with a call to FA)
|
|
|
|
FB2 = insertAccumulateFunction(B.get(), FAExtern, "FB2");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module A { Function FA },
|
|
|
|
// Module B { Extern FA, Function FB which calls FA },
|
|
|
|
// Module C { Extern FB, Function FC which calls FB },
|
2014-03-06 06:51:42 +01:00
|
|
|
void
|
|
|
|
createThreeModuleChainedCallsCase(std::unique_ptr<Module> &A, Function *&FA,
|
|
|
|
std::unique_ptr<Module> &B, Function *&FB,
|
|
|
|
std::unique_ptr<Module> &C, Function *&FC) {
|
2013-10-01 03:48:36 +02:00
|
|
|
A.reset(createEmptyModule("A"));
|
|
|
|
FA = insertAddFunction(A.get());
|
|
|
|
|
|
|
|
B.reset(createEmptyModule("B"));
|
|
|
|
Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
|
2019-01-13 17:09:28 +01:00
|
|
|
FB = insertSimpleCallFunction(B.get(), FAExtern_in_B);
|
2013-10-01 03:48:36 +02:00
|
|
|
|
|
|
|
C.reset(createEmptyModule("C"));
|
|
|
|
Function *FBExtern_in_C = insertExternalReferenceToFunction(C.get(), FB);
|
2019-01-13 17:09:28 +01:00
|
|
|
FC = insertSimpleCallFunction(C.get(), FBExtern_in_C);
|
2013-10-01 03:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Module A { Function FA },
|
|
|
|
// Populates Modules A and B:
|
|
|
|
// Module B { Function FB }
|
2014-03-06 06:51:42 +01:00
|
|
|
void createTwoModuleCase(std::unique_ptr<Module> &A, Function *&FA,
|
|
|
|
std::unique_ptr<Module> &B, Function *&FB) {
|
2013-10-01 03:48:36 +02:00
|
|
|
A.reset(createEmptyModule("A"));
|
|
|
|
FA = insertAddFunction(A.get());
|
|
|
|
|
|
|
|
B.reset(createEmptyModule("B"));
|
|
|
|
FB = insertAddFunction(B.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module A { Function FA },
|
|
|
|
// Module B { Extern FA, Function FB which calls FA }
|
2014-03-06 06:51:42 +01:00
|
|
|
void createTwoModuleExternCase(std::unique_ptr<Module> &A, Function *&FA,
|
|
|
|
std::unique_ptr<Module> &B, Function *&FB) {
|
2013-10-01 03:48:36 +02:00
|
|
|
A.reset(createEmptyModule("A"));
|
|
|
|
FA = insertAddFunction(A.get());
|
|
|
|
|
|
|
|
B.reset(createEmptyModule("B"));
|
|
|
|
Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
|
2019-01-13 17:09:28 +01:00
|
|
|
FB = insertSimpleCallFunction(B.get(), FAExtern_in_B);
|
2013-10-01 03:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Module A { Function FA },
|
|
|
|
// Module B { Extern FA, Function FB which calls FA },
|
|
|
|
// Module C { Extern FB, Function FC which calls FA },
|
2014-03-06 06:51:42 +01:00
|
|
|
void createThreeModuleCase(std::unique_ptr<Module> &A, Function *&FA,
|
|
|
|
std::unique_ptr<Module> &B, Function *&FB,
|
|
|
|
std::unique_ptr<Module> &C, Function *&FC) {
|
2013-10-01 03:48:36 +02:00
|
|
|
A.reset(createEmptyModule("A"));
|
|
|
|
FA = insertAddFunction(A.get());
|
|
|
|
|
|
|
|
B.reset(createEmptyModule("B"));
|
|
|
|
Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
|
2019-01-13 17:09:28 +01:00
|
|
|
FB = insertSimpleCallFunction(B.get(), FAExtern_in_B);
|
2013-10-01 03:48:36 +02:00
|
|
|
|
|
|
|
C.reset(createEmptyModule("C"));
|
|
|
|
Function *FAExtern_in_C = insertExternalReferenceToFunction(C.get(), FA);
|
2019-01-13 17:09:28 +01:00
|
|
|
FC = insertSimpleCallFunction(C.get(), FAExtern_in_C);
|
2013-10-01 03:48:36 +02:00
|
|
|
}
|
2013-06-28 22:37:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder {
|
|
|
|
protected:
|
|
|
|
MCJITTestBase()
|
2017-08-03 04:16:21 +02:00
|
|
|
: TrivialModuleBuilder(HostTriple), OptLevel(CodeGenOpt::None),
|
|
|
|
CodeModel(CodeModel::Small), MArch(""), MM(new SectionMemoryManager) {
|
2013-06-28 22:37:20 +02:00
|
|
|
// The architectures below are known to be compatible with MCJIT as they
|
|
|
|
// are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
|
|
|
|
// kept in sync.
|
|
|
|
SupportedArchs.push_back(Triple::aarch64);
|
|
|
|
SupportedArchs.push_back(Triple::arm);
|
2013-07-24 03:58:40 +02:00
|
|
|
SupportedArchs.push_back(Triple::mips);
|
|
|
|
SupportedArchs.push_back(Triple::mipsel);
|
2015-05-28 15:48:41 +02:00
|
|
|
SupportedArchs.push_back(Triple::mips64);
|
|
|
|
SupportedArchs.push_back(Triple::mips64el);
|
2013-06-28 22:37:20 +02:00
|
|
|
SupportedArchs.push_back(Triple::x86);
|
|
|
|
SupportedArchs.push_back(Triple::x86_64);
|
|
|
|
|
|
|
|
// Some architectures have sub-architectures in which tests will fail, like
|
|
|
|
// ARM. These two vectors will define if they do have sub-archs (to avoid
|
|
|
|
// extra work for those who don't), and if so, if they are listed to work
|
|
|
|
HasSubArchs.push_back(Triple::arm);
|
|
|
|
SupportedSubArchs.push_back("armv6");
|
|
|
|
SupportedSubArchs.push_back("armv7");
|
|
|
|
|
2014-04-01 01:42:23 +02:00
|
|
|
UnsupportedEnvironments.push_back(Triple::Cygnus);
|
2013-06-28 22:37:20 +02:00
|
|
|
}
|
2012-10-04 22:29:44 +02:00
|
|
|
|
2014-08-19 06:04:25 +02:00
|
|
|
void createJIT(std::unique_ptr<Module> M) {
|
2012-10-04 22:29:44 +02:00
|
|
|
|
|
|
|
// Due to the EngineBuilder constructor, it is required to have a Module
|
|
|
|
// in order to construct an ExecutionEngine (i.e. MCJIT)
|
|
|
|
assert(M != 0 && "a non-null Module must be provided to create MCJIT");
|
|
|
|
|
2014-08-19 06:04:25 +02:00
|
|
|
EngineBuilder EB(std::move(M));
|
2012-10-04 22:29:44 +02:00
|
|
|
std::string Error;
|
|
|
|
TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
|
2014-12-03 01:51:19 +01:00
|
|
|
.setMCJITMemoryManager(std::move(MM))
|
2012-10-04 22:29:44 +02:00
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setOptLevel(CodeGenOpt::None)
|
|
|
|
.setMArch(MArch)
|
|
|
|
.setMCPU(sys::getHostCPUName())
|
|
|
|
//.setMAttrs(MAttrs)
|
|
|
|
.create());
|
|
|
|
// At this point, we cannot modify the module any more.
|
|
|
|
assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenOpt::Level OptLevel;
|
|
|
|
CodeModel::Model CodeModel;
|
|
|
|
StringRef MArch;
|
|
|
|
SmallVector<std::string, 1> MAttrs;
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<ExecutionEngine> TheJIT;
|
2014-12-03 01:51:19 +01:00
|
|
|
std::unique_ptr<RTDyldMemoryManager> MM;
|
2012-10-04 22:29:44 +02:00
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<Module> M;
|
2012-10-04 22:29:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
|
2015-10-07 01:24:35 +02:00
|
|
|
#endif // LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H
|