Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / ExecutionEngine / Orc / OrcTestCommon.h
blobce7da76c9653a32aba24a57e64c672833c57b022
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.
11 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
15 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
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"
30 #include <memory>
32 namespace llvm {
34 namespace orc {
35 // CoreAPIsStandardTest that saves a bunch of boilerplate by providing the
36 // following:
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:
49 ~CoreAPIsBasedStandardTest() {
50 if (auto Err = ES.endSession())
51 ES.reportError(std::move(Err));
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 constexpr ExecutorAddr FooAddr{1};
63 static constexpr ExecutorAddr BarAddr{2};
64 static constexpr ExecutorAddr BazAddr{3};
65 static constexpr ExecutorAddr QuxAddr{4};
66 ExecutorSymbolDef FooSym{FooAddr, JITSymbolFlags::Exported};
67 ExecutorSymbolDef BarSym{BarAddr, JITSymbolFlags::Exported};
68 ExecutorSymbolDef BazSym{BazAddr, JITSymbolFlags::Exported};
69 ExecutorSymbolDef QuxSym{QuxAddr, JITSymbolFlags::Exported};
72 } // end namespace orc
74 class OrcNativeTarget {
75 public:
76 static void initialize() {
77 if (!NativeTargetInitialized) {
78 InitializeNativeTarget();
79 InitializeNativeTargetAsmParser();
80 InitializeNativeTargetAsmPrinter();
81 NativeTargetInitialized = true;
85 private:
86 static bool NativeTargetInitialized;
89 class SimpleMaterializationUnit : public orc::MaterializationUnit {
90 public:
91 using MaterializeFunction =
92 std::function<void(std::unique_ptr<orc::MaterializationResponsibility>)>;
93 using DiscardFunction =
94 std::function<void(const orc::JITDylib &, orc::SymbolStringPtr)>;
95 using DestructorFunction = std::function<void()>;
97 SimpleMaterializationUnit(
98 orc::SymbolFlagsMap SymbolFlags, MaterializeFunction Materialize,
99 orc::SymbolStringPtr InitSym = nullptr,
100 DiscardFunction Discard = DiscardFunction(),
101 DestructorFunction Destructor = DestructorFunction())
102 : MaterializationUnit(
103 Interface(std::move(SymbolFlags), std::move(InitSym))),
104 Materialize(std::move(Materialize)), Discard(std::move(Discard)),
105 Destructor(std::move(Destructor)) {}
107 ~SimpleMaterializationUnit() override {
108 if (Destructor)
109 Destructor();
112 StringRef getName() const override { return "<Simple>"; }
114 void
115 materialize(std::unique_ptr<orc::MaterializationResponsibility> R) override {
116 Materialize(std::move(R));
119 void discard(const orc::JITDylib &JD,
120 const orc::SymbolStringPtr &Name) override {
121 if (Discard)
122 Discard(JD, std::move(Name));
123 else
124 llvm_unreachable("Discard not supported");
127 private:
128 MaterializeFunction Materialize;
129 DiscardFunction Discard;
130 DestructorFunction Destructor;
133 class ModuleBuilder {
134 public:
135 ModuleBuilder(LLVMContext &Context, StringRef Triple,
136 StringRef Name);
138 Function *createFunctionDecl(FunctionType *FTy, StringRef Name) {
139 return Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M.get());
142 Module* getModule() { return M.get(); }
143 const Module* getModule() const { return M.get(); }
144 std::unique_ptr<Module> takeModule() { return std::move(M); }
146 private:
147 std::unique_ptr<Module> M;
150 // Dummy struct type.
151 struct DummyStruct {
152 int X[256];
155 inline StructType *getDummyStructTy(LLVMContext &Context) {
156 return StructType::get(ArrayType::get(Type::getInt32Ty(Context), 256));
159 } // namespace llvm
161 #endif