clang 22.0.0git
Interpreter.h
Go to the documentation of this file.
1//===--- Interpreter.h - Incremental Compilation and Execution---*- 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// This file defines the component which performs incremental code
10// compilation and execution.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_INTERPRETER_INTERPRETER_H
15#define LLVM_CLANG_INTERPRETER_INTERPRETER_H
16
20
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ExecutionEngine/JITSymbol.h"
23#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
24#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
25#include "llvm/Support/Error.h"
26#include <cstdint>
27#include <memory>
28#include <vector>
29
30namespace llvm {
31namespace orc {
32class LLJIT;
33class LLJITBuilder;
34class ThreadSafeContext;
35} // namespace orc
36} // namespace llvm
37
38namespace clang {
39
40namespace driver {
41class ToolChain;
42} // namespace driver
43
45class CXXRecordDecl;
46class Decl;
50
51/// Create a pre-configured \c CompilerInstance for incremental processing.
53public:
55
56 void SetCompilerArgs(const std::vector<const char *> &Args) {
57 UserArgs = Args;
58 }
59
60 void SetTargetTriple(std::string TT) { TargetTriple = TT; }
61
62 // General C++
64
65 // Offload options
66 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
67
68 // CUDA specific
69 void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
70
73
74private:
76 create(std::string TT, std::vector<const char *> &ClangArgv);
77
79
80 std::vector<const char *> UserArgs;
81 std::optional<std::string> TargetTriple;
82
83 llvm::StringRef OffloadArch;
84 llvm::StringRef CudaSDKPath;
85};
86
87class IncrementalAction;
88class InProcessPrintingASTConsumer;
89
90/// Provides top-level interfaces for incremental compilation and execution.
92 friend class Value;
93 friend InProcessPrintingASTConsumer;
94
95 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
96 /// Long-lived, incremental parsing action.
97 std::unique_ptr<IncrementalAction> Act;
98 std::unique_ptr<IncrementalParser> IncrParser;
99 std::unique_ptr<IncrementalExecutor> IncrExecutor;
100
101 // An optional parser for CUDA offloading
102 std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
103
104 // An optional action for CUDA offloading
105 std::unique_ptr<IncrementalAction> DeviceAct;
106
107 /// List containing information about each incrementally parsed piece of code.
108 std::list<PartialTranslationUnit> PTUs;
109
110 unsigned InitPTUSize = 0;
111
112 // This member holds the last result of the value printing. It's a class
113 // member because we might want to access it after more inputs. If no value
114 // printing happens, it's in an invalid state.
115 Value LastValue;
116
117 /// Compiler instance performing the incremental compilation.
118 std::unique_ptr<CompilerInstance> CI;
119
120 /// An optional compiler instance for CUDA offloading
121 std::unique_ptr<CompilerInstance> DeviceCI;
122
123public:
124 struct JITConfig {
125 /// Indicates whether out-of-process JIT execution is enabled.
126 bool IsOutOfProcess = false;
127 /// Path to the out-of-process JIT executor.
128 std::string OOPExecutor = "";
129 std::string OOPExecutorConnect = "";
130 /// Indicates whether to use shared memory for communication.
131 bool UseSharedMemory = false;
132 /// Representing the slab allocation size for memory management in kb.
133 unsigned SlabAllocateSize = 0;
134 /// Path to the ORC runtime library.
135 std::string OrcRuntimePath = "";
136 /// PID of the out-of-process JIT executor.
137 uint32_t ExecutorPID = 0;
138 /// Custom lambda to be executed inside child process/executor
139 std::function<void()> CustomizeFork = nullptr;
140 /// An optional code model to provide to the JITTargetMachineBuilder
141 std::optional<llvm::CodeModel::Model> CM = std::nullopt;
142
147 };
148
149protected:
150 // Derived classes can use an extended interface of the Interpreter.
151 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
152 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr,
153 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr,
154 JITConfig Config = JITConfig());
155
156 // Create the internal IncrementalExecutor, or re-create it after calling
157 // ResetExecutor().
158 llvm::Error CreateExecutor(JITConfig Config = JITConfig());
159
160 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
161 // JIT engine. In particular, it doesn't run cleanup or destructors.
162 void ResetExecutor();
163
164public:
165 virtual ~Interpreter();
167 create(std::unique_ptr<CompilerInstance> CI, JITConfig Config = {});
169 createWithCUDA(std::unique_ptr<CompilerInstance> CI,
170 std::unique_ptr<CompilerInstance> DCI);
172 createLLJITBuilder(std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC,
173 llvm::StringRef OrcRuntimePath);
174 static llvm::Expected<
175 std::pair<std::unique_ptr<llvm::orc::LLJITBuilder>, uint32_t>>
176 outOfProcessJITBuilder(JITConfig Config);
178 getOrcRuntimePath(const driver::ToolChain &TC);
179
180 const ASTContext &getASTContext() const;
182 const CompilerInstance *getCompilerInstance() const;
183 CompilerInstance *getCompilerInstance();
185
187 llvm::Error Execute(PartialTranslationUnit &T);
188 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
189
190 /// Undo N previous incremental inputs.
191 llvm::Error Undo(unsigned N = 1);
192
193 /// Link a dynamic library
194 llvm::Error LoadDynamicLibrary(const char *name);
195
196 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
197 /// the CodeGenModule's internal mangling cache to avoid recomputing the
198 /// mangled name.
200
201 /// \returns the \c ExecutorAddr of a given name as written in the IR.
203 getSymbolAddress(llvm::StringRef IRName) const;
204
205 /// \returns the \c ExecutorAddr of a given name as written in the object
206 /// file.
208 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
209
210 uint32_t getOutOfProcessExecutorPID() const;
211
212private:
213 size_t getEffectivePTUSize() const;
214 void markUserCodeStart();
215
216 // A cache for the compiled destructors used to for de-allocation of managed
217 // clang::Values.
218 mutable llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
219
220 std::array<Expr *, 4> ValuePrintingInfo = {0};
221
222 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder;
223
224 /// @}
225 /// @name Value and pretty printing support
226 /// @{
227
228 std::string ValueDataToString(const Value &V) const;
229 std::string ValueTypeToString(const Value &V) const;
230
231 llvm::Expected<Expr *> convertExprToValue(Expr *E);
232
233 // When we deallocate clang::Value we need to run the destructor of the type.
234 // This function forces emission of the needed dtor.
235 llvm::Expected<llvm::orc::ExecutorAddr>
236 CompileDtorCall(CXXRecordDecl *CXXRD) const;
237};
238} // namespace clang
239
240#endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
#define V(N, I)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:188
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaHost()
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaDevice()
void SetCompilerArgs(const std::vector< const char * > &Args)
Definition Interpreter.h:56
void SetTargetTriple(std::string TT)
Definition Interpreter.h:60
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
void SetCudaSDK(llvm::StringRef path)
Definition Interpreter.h:69
void SetOffloadArch(llvm::StringRef Arch)
Definition Interpreter.h:66
Provides support for incremental compilation.
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V=nullptr)
uint32_t getOutOfProcessExecutorPID() const
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddress(GlobalDecl GD) const
static llvm::Expected< std::pair< std::unique_ptr< llvm::orc::LLJITBuilder >, uint32_t > > outOfProcessJITBuilder(JITConfig Config)
llvm::Error LoadDynamicLibrary(const char *name)
Link a dynamic library.
static llvm::Expected< std::unique_ptr< Interpreter > > createWithCUDA(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< CompilerInstance > DCI)
llvm::Error CreateExecutor(JITConfig Config=JITConfig())
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
llvm::Error Undo(unsigned N=1)
Undo N previous incremental inputs.
const CompilerInstance * getCompilerInstance() const
static llvm::Expected< std::unique_ptr< llvm::orc::LLJITBuilder > > createLLJITBuilder(std::unique_ptr< llvm::orc::ExecutorProcessControl > EPC, llvm::StringRef OrcRuntimePath)
static llvm::Expected< std::string > getOrcRuntimePath(const driver::ToolChain &TC)
const ASTContext & getASTContext() const
friend class Value
Definition Interpreter.h:92
llvm::Expected< llvm::orc::LLJIT & > getExecutionEngine()
static llvm::Expected< std::unique_ptr< Interpreter > > create(std::unique_ptr< CompilerInstance > CI, JITConfig Config={})
llvm::Error Execute(PartialTranslationUnit &T)
Interpreter(std::unique_ptr< CompilerInstance > Instance, llvm::Error &Err, std::unique_ptr< llvm::orc::LLJITBuilder > JITBuilder=nullptr, std::unique_ptr< clang::ASTConsumer > Consumer=nullptr, JITConfig Config=JITConfig())
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of β€˜parallel’, 'serial',...
@ Parse
Parse the block; this code is always used.
Definition Parser.h:137
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
int const char * function
Definition c++config.h:31
#define false
Definition stdbool.h:26
uint32_t ExecutorPID
PID of the out-of-process JIT executor.
std::function< void()> CustomizeFork
Custom lambda to be executed inside child process/executor.
bool IsOutOfProcess
Indicates whether out-of-process JIT execution is enabled.
std::string OrcRuntimePath
Path to the ORC runtime library.
std::optional< llvm::CodeModel::Model > CM
An optional code model to provide to the JITTargetMachineBuilder.
unsigned SlabAllocateSize
Representing the slab allocation size for memory management in kb.
bool UseSharedMemory
Indicates whether to use shared memory for communication.
std::string OOPExecutor
Path to the out-of-process JIT executor.