Debian Codegraph @

clang/unittests/Interpreter/InterpreterExtensionsTest.cpp

raw
1 //===- unittests/Interpreter/InterpreterExtensionsTest.cpp ----------------===//
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 // Unit tests for Clang's Interpreter library.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "InterpreterTestFixture.h"
14
15 #include "clang/Interpreter/Interpreter.h"
16
17 #include "clang/AST/Expr.h"
18 #include "clang/Frontend/CompilerInstance.h"
19 #include "clang/Sema/Lookup.h"
20 #include "clang/Sema/Sema.h"
21
22 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
23 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
24 #include "llvm/MC/TargetRegistry.h"
25 #include "llvm/Support/Threading.h"
26 #include "llvm/Testing/Support/Error.h"
27
28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
30
31 #include <system_error>
32
33 #if defined(_AIX) || defined(__MVS__)
34 #define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
35 #endif
36
37 using namespace clang;
38 namespace {
39
40 class InterpreterExtensionsTest : public InterpreterTestBase {
41 protected:
42 void SetUp() override {
43 #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
44 GTEST_SKIP();
45 #endif
46 }
47
48 static void SetUpTestSuite() {
49 llvm::InitializeAllTargets();
50 llvm::InitializeAllTargetInfos();
51 llvm::InitializeAllTargetMCs();
52 llvm::InitializeAllAsmPrinters();
53 }
54
55 public:
56 // Some tests require a arm-registered-target
57 static bool IsARMTargetRegistered() {
58 llvm::Triple TT;
59 TT.setArch(llvm::Triple::arm);
60 TT.setVendor(llvm::Triple::UnknownVendor);
61 TT.setOS(llvm::Triple::UnknownOS);
62
63 std::string UnusedErr;
64 return llvm::TargetRegistry::lookupTarget(TT.str(), UnusedErr);
65 }
66 };
67
68 class RecordRuntimeIBMetrics : public Interpreter {
69 struct NoopRuntimeInterfaceBuilder : public RuntimeInterfaceBuilder {
70 NoopRuntimeInterfaceBuilder(Sema &S) : S(S) {}
71
72 TransformExprFunction *getPrintValueTransformer() override {
73 TransformerQueries += 1;
74 return &noop;
75 }
76
77 static ExprResult noop(RuntimeInterfaceBuilder *Builder, Expr *E,
78 ArrayRef<Expr *> FixedArgs) {
79 auto *B = static_cast<NoopRuntimeInterfaceBuilder *>(Builder);
80 B->TransformedExprs += 1;
81 return B->S.ActOnFinishFullExpr(E, /*DiscardedValue=*/false);
82 }
83
84 Sema &S;
85 size_t TransformedExprs = 0;
86 size_t TransformerQueries = 0;
87 };
88
89 public:
90 // Inherit with using wouldn't make it public
91 RecordRuntimeIBMetrics(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err)
92 : Interpreter(std::move(CI), Err) {}
93
94 std::unique_ptr<RuntimeInterfaceBuilder> FindRuntimeInterface() override {
95 assert(RuntimeIBPtr == nullptr && "We create the builder only once");
96 Sema &S = getCompilerInstance()->getSema();
97 auto RuntimeIB = std::make_unique<NoopRuntimeInterfaceBuilder>(S);
98 RuntimeIBPtr = RuntimeIB.get();
99 return RuntimeIB;
100 }
101
102 NoopRuntimeInterfaceBuilder *RuntimeIBPtr = nullptr;
103 };
104
105 TEST_F(InterpreterExtensionsTest, FindRuntimeInterface) {
106 if (!HostSupportsJIT())
107 GTEST_SKIP();
108
109 clang::IncrementalCompilerBuilder CB;
110 llvm::Error ErrOut = llvm::Error::success();
111 RecordRuntimeIBMetrics Interp(cantFail(CB.CreateCpp()), ErrOut);
112 cantFail(std::move(ErrOut));
113 cantFail(Interp.Parse("int a = 1; a"));
114 cantFail(Interp.Parse("int b = 2; b"));
115 cantFail(Interp.Parse("int c = 3; c"));
116 EXPECT_EQ(3U, Interp.RuntimeIBPtr->TransformedExprs);
117 EXPECT_EQ(1U, Interp.RuntimeIBPtr->TransformerQueries);
118 }
119
120 class CustomJBInterpreter : public Interpreter {
121 using CustomJITBuilderCreatorFunction =
122 std::function<llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>>()>;
123 CustomJITBuilderCreatorFunction JBCreator = nullptr;
124
125 public:
126 CustomJBInterpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &ErrOut,
127 std::unique_ptr<llvm::orc::LLJITBuilder> JB)
128 : Interpreter(std::move(CI), ErrOut, std::move(JB)) {}
129
130 ~CustomJBInterpreter() override {
131 // Skip cleanUp() because it would trigger LLJIT default dtors
132 Interpreter::ResetExecutor();
133 }
134
135 llvm::Error CreateExecutor() { return Interpreter::CreateExecutor(); }
136 };
137
138 TEST_F(InterpreterExtensionsTest, DefaultCrossJIT) {
139 if (!IsARMTargetRegistered())
140 GTEST_SKIP();
141
142 IncrementalCompilerBuilder CB;
143 CB.SetTargetTriple("armv6-none-eabi");
144 auto CI = cantFail(CB.CreateCpp());
145 llvm::Error ErrOut = llvm::Error::success();
146 CustomJBInterpreter Interp(std::move(CI), ErrOut, nullptr);
147 cantFail(std::move(ErrOut));
148 }
149
150 TEST_F(InterpreterExtensionsTest, CustomCrossJIT) {
151 if (!IsARMTargetRegistered())
152 GTEST_SKIP();
153
154 std::string TargetTriple = "armv6-none-eabi";
155
156 IncrementalCompilerBuilder CB;
157 CB.SetTargetTriple(TargetTriple);
158 auto CI = cantFail(CB.CreateCpp());
159
160 using namespace llvm::orc;
161 LLJIT *JIT = nullptr;
162 std::vector<std::unique_ptr<llvm::MemoryBuffer>> Objs;
163 auto JTMB = JITTargetMachineBuilder(llvm::Triple(TargetTriple));
164 JTMB.setCPU("cortex-m0plus");
165
166 auto JB = std::make_unique<LLJITBuilder>();
167 JB->setJITTargetMachineBuilder(JTMB);
168 JB->setPlatformSetUp(setUpInactivePlatform);
169 JB->setNotifyCreatedCallback([&](LLJIT &J) {
170 ObjectLayer &ObjLayer = J.getObjLinkingLayer();
171 auto *JITLinkObjLayer = llvm::dyn_cast<ObjectLinkingLayer>(&ObjLayer);
172 JITLinkObjLayer->setReturnObjectBuffer(
173 [&Objs](std::unique_ptr<llvm::MemoryBuffer> MB) {
174 Objs.push_back(std::move(MB));
175 });
176 JIT = &J;
177 return llvm::Error::success();
178 });
179
180 llvm::Error ErrOut = llvm::Error::success();
181 CustomJBInterpreter Interp(std::move(CI), ErrOut, std::move(JB));
182 cantFail(std::move(ErrOut));
183
184 EXPECT_EQ(0U, Objs.size());
185 cantFail(Interp.ParseAndExecute("int a = 1;"));
186 ASSERT_NE(JIT, nullptr); // But it is, because JBCreator was never called
187 ExecutorAddr Addr = cantFail(JIT->lookup("a"));
188 EXPECT_NE(0U, Addr.getValue());
189 EXPECT_EQ(1U, Objs.size());
190 }
191
192 } // end anonymous namespace