clang 24.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
21
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ExecutionEngine/JITSymbol.h"
24#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
25#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
26#include "llvm/Support/Error.h"
27#include <cstdint>
28#include <memory>
29#include <vector>
30
31namespace llvm {
32namespace orc {
33class ThreadSafeContext;
34} // namespace orc
35} // namespace llvm
36
37namespace clang {
38
39namespace driver {
40class Compilation;
41} // namespace driver
42
44class CXXRecordDecl;
45class Decl;
48
49/// The device language to offload to. \c Detect selects the vendor of the
50/// device available on the machine.
51enum class OffloadType { Detect, CUDA, HIP };
52
53/// Create a pre-configured \c CompilerInstance for incremental processing.
55 using DriverCompilationFn = llvm::Error(const driver::Compilation &);
56
57public:
59
60 void SetCompilerArgs(const std::vector<const char *> &Args) {
61 UserArgs = Args;
62 }
63
64 void SetTargetTriple(std::string TT) { TargetTriple = TT; }
65
66 // General C++
68
69 // Offload options
70 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
71
72 // The SDK path (CUDA or ROCm) of the device language being offloaded to.
73 void SetDeviceSDK(llvm::StringRef Path) { DeviceSDKPath = Path; }
74
75 // Hand over the compilation.
79
84
85private:
87 create(std::string TT, std::vector<const char *> &ClangArgv);
88
90 createOffload(bool device, OffloadType Type = OffloadType::Detect);
91
92 std::vector<const char *> UserArgs;
93 std::optional<std::string> TargetTriple;
94
95 llvm::StringRef OffloadArch;
96 llvm::StringRef DeviceSDKPath;
97
98 std::string OffloadCUID;
99
100 std::optional<std::function<DriverCompilationFn>> CompilationCB;
101};
102
103class IncrementalAction;
104class InProcessPrintingASTConsumer;
105
106/// Provides top-level interfaces for incremental compilation and execution.
108 friend class Value;
109 friend InProcessPrintingASTConsumer;
110
111 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
112 /// Long-lived, incremental parsing action.
113 std::unique_ptr<IncrementalAction> Act;
114 std::unique_ptr<IncrementalParser> IncrParser;
115 std::unique_ptr<IncrementalExecutor> IncrExecutor;
116
117 // An optional parser for CUDA offloading
118 std::unique_ptr<IncrementalDeviceParser> DeviceParser;
119
120 // An optional action for CUDA offloading
121 std::unique_ptr<IncrementalAction> DeviceAct;
122
123 /// List containing information about each incrementally parsed piece of code.
124 std::list<PartialTranslationUnit> PTUs;
125
126 unsigned InitPTUSize = 0;
127
128 // This member holds the last result of the value printing. It's a class
129 // member because we might want to access it after more inputs. If no value
130 // printing happens, it's in an invalid state.
131 Value LastValue;
132
133 /// Compiler instance performing the incremental compilation.
134 std::unique_ptr<CompilerInstance> CI;
135
136 /// An optional compiler instance for CUDA offloading
137 std::unique_ptr<CompilerInstance> DeviceCI;
138
139protected:
140 // Derived classes can use an extended interface of the Interpreter.
141 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
142 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr,
143 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr);
144
145 // Create the internal IncrementalExecutor, or re-create it after calling
146 // ResetExecutor().
147 llvm::Error CreateExecutor();
148
149 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
150 // JIT engine. In particular, it doesn't run cleanup or destructors.
151 void ResetExecutor() { IncrExecutor.reset(); }
152
153public:
154 virtual ~Interpreter();
156 create(std::unique_ptr<CompilerInstance> CI,
157 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr);
159 createWithDevice(std::unique_ptr<CompilerInstance> CI,
160 std::unique_ptr<CompilerInstance> DCI,
162
163 const ASTContext &getASTContext() const;
168
170 llvm::Error Execute(PartialTranslationUnit &T);
171 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
172
173 /// Set the optimization level and size used to emit subsequently parsed
174 /// input. \p OptSize mirrors \c CodeGenOptions::OptimizeSize (1 for -Os,
175 /// 2 for -Oz).
176 void setOptLevel(unsigned OptLevel, unsigned OptSize = 0);
177
178 /// Undo N previous incremental inputs.
179 llvm::Error Undo(unsigned N = 1);
180
181 /// Link a dynamic library
182 llvm::Error LoadDynamicLibrary(const char *name);
183
184 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
185 /// the CodeGenModule's internal mangling cache to avoid recomputing the
186 /// mangled name.
188
189 /// \returns the \c ExecutorAddr of a given name as written in the IR.
191 getSymbolAddress(llvm::StringRef IRName) const;
192
193 /// \returns the \c ExecutorAddr of a given name as written in the object
194 /// file.
196 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
197
199 return *IncrExecutorBuilder;
200 }
201
202private:
203 size_t getEffectivePTUSize() const;
204 void markUserCodeStart();
205
206 // A cache for the compiled destructors used to for de-allocation of managed
207 // clang::Values.
208 mutable llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
209
210 std::array<Expr *, 4> ValuePrintingInfo = {0};
211
212 std::unique_ptr<IncrementalExecutorBuilder> IncrExecutorBuilder;
213
214 /// @}
215 /// @name Value and pretty printing support
216 /// @{
217
218 std::string ValueDataToString(const Value &V) const;
219 std::string ValueTypeToString(const Value &V) const;
220
221 llvm::Expected<Expr *> convertExprToValue(Expr *E);
222
223 // When we deallocate clang::Value we need to run the destructor of the type.
224 // This function forces emission of the needed dtor.
226 CompileDtorCall(CXXRecordDecl *CXXRD) const;
227};
228} // namespace clang
229
230#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:245
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
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
void SetCompilerArgs(const std::vector< const char * > &Args)
Definition Interpreter.h:60
void SetDriverCompilationCallback(std::function< DriverCompilationFn > C)
Definition Interpreter.h:76
void SetTargetTriple(std::string TT)
Definition Interpreter.h:64
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateHost(OffloadType Type)
void SetOffloadArch(llvm::StringRef Arch)
Definition Interpreter.h:70
void SetDeviceSDK(llvm::StringRef Path)
Definition Interpreter.h:73
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateDevice(OffloadType Type)
Common configuration and interface for incremental executor builders.
Provides support for incremental compilation.
const IncrementalExecutorBuilder & getIncrementalExecutorBuilder() const
llvm::Expected< IncrementalExecutor & > getExecutionEngine()
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V=nullptr)
static llvm::Expected< std::unique_ptr< Interpreter > > create(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< IncrementalExecutorBuilder > IEB=nullptr)
llvm::Error CreateExecutor()
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddress(GlobalDecl GD) const
void setOptLevel(unsigned OptLevel, unsigned OptSize=0)
Set the optimization level and size used to emit subsequently parsed input.
llvm::Error LoadDynamicLibrary(const char *name)
Link a dynamic library.
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
Interpreter(std::unique_ptr< CompilerInstance > Instance, llvm::Error &Err, std::unique_ptr< IncrementalExecutorBuilder > IEB=nullptr, std::unique_ptr< clang::ASTConsumer > Consumer=nullptr)
llvm::Error Undo(unsigned N=1)
Undo N previous incremental inputs.
const CompilerInstance * getCompilerInstance() const
static llvm::Expected< std::unique_ptr< Interpreter > > createWithDevice(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< CompilerInstance > DCI, OffloadType Type=OffloadType::Detect)
const ASTContext & getASTContext() const
friend class Value
llvm::Error Execute(PartialTranslationUnit &T)
A processor an offloading action can target.
Definition OffloadArch.h:36
The base class of the type hierarchy.
Definition TypeBase.h:1879
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:46
Top level wrappers for InstallAPI frontend operations.
OffloadType
The device language to offload to.
Definition Interpreter.h:51
@ Parse
Parse the block; this code is always used.
Definition Parser.h:137
const FunctionProtoType * T
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
int const char * function
Definition c++config.h:31
The class keeps track of various objects created as part of processing incremental inputs.