1 //===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common utilities for the Orc unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12
13
14 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
15 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
16
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/Core.h"
19 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
20 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/TargetRegistry.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/TargetSelect.h"
28 #include "gtest/gtest.h"
29
30 #include <memory>
31
32 namespace llvm {
33
34 namespace orc {
35 // CoreAPIsStandardTest that saves a bunch of boilerplate by providing the
36 // following:
37 //
38 // (1) ES -- An ExecutionSession
39 // (2) Foo, Bar, Baz, Qux -- SymbolStringPtrs for strings "foo", "bar", "baz",
40 // and "qux" respectively.
41 // (3) FooAddr, BarAddr, BazAddr, QuxAddr -- Dummy addresses. Guaranteed
42 // distinct and non-null.
43 // (4) FooSym, BarSym, BazSym, QuxSym -- JITEvaluatedSymbols with FooAddr,
44 // BarAddr, BazAddr, and QuxAddr respectively. All with default strong,
45 // linkage and non-hidden visibility.
46 // (5) V -- A JITDylib associated with ES.
47 class CoreAPIsBasedStandardTest : public testing::Test {
48 public:
~CoreAPIsBasedStandardTest()49 ~CoreAPIsBasedStandardTest() {
50 if (auto Err = ES.endSession())
51 ES.reportError(std::move(Err));
52 }
53
54 protected:
55 std::shared_ptr<SymbolStringPool> SSP = std::make_shared<SymbolStringPool>();
56 ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>(SSP)};
57 JITDylib &JD = ES.createBareJITDylib("JD");
58 SymbolStringPtr Foo = ES.intern("foo");
59 SymbolStringPtr Bar = ES.intern("bar");
60 SymbolStringPtr Baz = ES.intern("baz");
61 SymbolStringPtr Qux = ES.intern("qux");
62 static const JITTargetAddress FooAddr = 1U;
63 static const JITTargetAddress BarAddr = 2U;
64 static const JITTargetAddress BazAddr = 3U;
65 static const JITTargetAddress QuxAddr = 4U;
66 JITEvaluatedSymbol FooSym =
67 JITEvaluatedSymbol(FooAddr, JITSymbolFlags::Exported);
68 JITEvaluatedSymbol BarSym =
69 JITEvaluatedSymbol(BarAddr, JITSymbolFlags::Exported);
70 JITEvaluatedSymbol BazSym =
71 JITEvaluatedSymbol(BazAddr, JITSymbolFlags::Exported);
72 JITEvaluatedSymbol QuxSym =
73 JITEvaluatedSymbol(QuxAddr, JITSymbolFlags::Exported);
74 };
75
76 } // end namespace orc
77
78 class OrcNativeTarget {
79 public:
initialize()80 static void initialize() {
81 if (!NativeTargetInitialized) {
82 InitializeNativeTarget();
83 InitializeNativeTargetAsmParser();
84 InitializeNativeTargetAsmPrinter();
85 NativeTargetInitialized = true;
86 }
87 }
88
89 private:
90 static bool NativeTargetInitialized;
91 };
92
93 class SimpleMaterializationUnit : public orc::MaterializationUnit {
94 public:
95 using MaterializeFunction =
96 std::function<void(std::unique_ptr<orc::MaterializationResponsibility>)>;
97 using DiscardFunction =
98 std::function<void(const orc::JITDylib &, orc::SymbolStringPtr)>;
99 using DestructorFunction = std::function<void()>;
100
101 SimpleMaterializationUnit(
102 orc::SymbolFlagsMap SymbolFlags, MaterializeFunction Materialize,
103 orc::SymbolStringPtr InitSym = nullptr,
104 DiscardFunction Discard = DiscardFunction(),
105 DestructorFunction Destructor = DestructorFunction())
MaterializationUnit(Interface (std::move (SymbolFlags),std::move (InitSym)))106 : MaterializationUnit(
107 Interface(std::move(SymbolFlags), std::move(InitSym))),
108 Materialize(std::move(Materialize)), Discard(std::move(Discard)),
109 Destructor(std::move(Destructor)) {}
110
~SimpleMaterializationUnit()111 ~SimpleMaterializationUnit() override {
112 if (Destructor)
113 Destructor();
114 }
115
getName()116 StringRef getName() const override { return "<Simple>"; }
117
118 void
materialize(std::unique_ptr<orc::MaterializationResponsibility> R)119 materialize(std::unique_ptr<orc::MaterializationResponsibility> R) override {
120 Materialize(std::move(R));
121 }
122
discard(const orc::JITDylib & JD,const orc::SymbolStringPtr & Name)123 void discard(const orc::JITDylib &JD,
124 const orc::SymbolStringPtr &Name) override {
125 if (Discard)
126 Discard(JD, std::move(Name));
127 else
128 llvm_unreachable("Discard not supported");
129 }
130
131 private:
132 MaterializeFunction Materialize;
133 DiscardFunction Discard;
134 DestructorFunction Destructor;
135 };
136
137 class ModuleBuilder {
138 public:
139 ModuleBuilder(LLVMContext &Context, StringRef Triple,
140 StringRef Name);
141
createFunctionDecl(FunctionType * FTy,StringRef Name)142 Function *createFunctionDecl(FunctionType *FTy, StringRef Name) {
143 return Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M.get());
144 }
145
getModule()146 Module* getModule() { return M.get(); }
getModule()147 const Module* getModule() const { return M.get(); }
takeModule()148 std::unique_ptr<Module> takeModule() { return std::move(M); }
149
150 private:
151 std::unique_ptr<Module> M;
152 };
153
154 // Dummy struct type.
155 struct DummyStruct {
156 int X[256];
157 };
158
getDummyStructTy(LLVMContext & Context)159 inline StructType *getDummyStructTy(LLVMContext &Context) {
160 return StructType::get(ArrayType::get(Type::getInt32Ty(Context), 256));
161 }
162
163 } // namespace llvm
164
165 #endif
166