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
49enum class OffloadType { CUDA, HIP };
50
51/// Create a pre-configured \c CompilerInstance for incremental processing.
53 using DriverCompilationFn = llvm::Error(const driver::Compilation &);
54
55public:
57
58 void SetCompilerArgs(const std::vector<const char *> &Args) {
59 UserArgs = Args;
60 }
61
62 void SetTargetTriple(std::string TT) { TargetTriple = TT; }
63
64 // General C++
66
67 // Offload options
68 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
69
70 void SetDeviceSDK(OffloadType Type, llvm::StringRef Path) {
72 RocmSDKPath = Path;
73 else
74 CudaSDKPath = Path;
75 }
76
77 // Retained for compatibility with existing CUDA callers.
78 void SetCudaSDK(llvm::StringRef Path) {
80 }
81
82 // Hand over the compilation.
86
91
92 // Retained for compatibility with existing CUDA callers.
99
100private:
102 create(std::string TT, std::vector<const char *> &ClangArgv);
103
105 createOffload(OffloadType Type, bool device);
106
107 std::vector<const char *> UserArgs;
108 std::optional<std::string> TargetTriple;
109
110 llvm::StringRef OffloadArch;
111 llvm::StringRef RocmSDKPath;
112 llvm::StringRef CudaSDKPath;
113
114 std::string OffloadCUID;
115
116 std::optional<std::function<DriverCompilationFn>> CompilationCB;
117};
118
119class IncrementalAction;
120class InProcessPrintingASTConsumer;
121
122/// Provides top-level interfaces for incremental compilation and execution.
124 friend class Value;
125 friend InProcessPrintingASTConsumer;
126
127 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
128 /// Long-lived, incremental parsing action.
129 std::unique_ptr<IncrementalAction> Act;
130 std::unique_ptr<IncrementalParser> IncrParser;
131 std::unique_ptr<IncrementalExecutor> IncrExecutor;
132
133 // An optional parser for CUDA offloading
134 std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
135
136 // An optional action for CUDA offloading
137 std::unique_ptr<IncrementalAction> DeviceAct;
138
139 /// List containing information about each incrementally parsed piece of code.
140 std::list<PartialTranslationUnit> PTUs;
141
142 unsigned InitPTUSize = 0;
143
144 // This member holds the last result of the value printing. It's a class
145 // member because we might want to access it after more inputs. If no value
146 // printing happens, it's in an invalid state.
147 Value LastValue;
148
149 /// Compiler instance performing the incremental compilation.
150 std::unique_ptr<CompilerInstance> CI;
151
152 /// An optional compiler instance for CUDA offloading
153 std::unique_ptr<CompilerInstance> DeviceCI;
154
155protected:
156 // Derived classes can use an extended interface of the Interpreter.
157 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
158 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr,
159 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr);
160
161 // Create the internal IncrementalExecutor, or re-create it after calling
162 // ResetExecutor().
163 llvm::Error CreateExecutor();
164
165 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
166 // JIT engine. In particular, it doesn't run cleanup or destructors.
167 void ResetExecutor() { IncrExecutor.reset(); }
168
169public:
170 virtual ~Interpreter();
172 create(std::unique_ptr<CompilerInstance> CI,
173 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr);
175 createWithDevice(OffloadType Type, std::unique_ptr<CompilerInstance> CI,
176 std::unique_ptr<CompilerInstance> DCI);
177
178 const ASTContext &getASTContext() const;
183
185 llvm::Error Execute(PartialTranslationUnit &T);
186 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
187
188 /// Undo N previous incremental inputs.
189 llvm::Error Undo(unsigned N = 1);
190
191 /// Link a dynamic library
192 llvm::Error LoadDynamicLibrary(const char *name);
193
194 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
195 /// the CodeGenModule's internal mangling cache to avoid recomputing the
196 /// mangled name.
198
199 /// \returns the \c ExecutorAddr of a given name as written in the IR.
201 getSymbolAddress(llvm::StringRef IRName) const;
202
203 /// \returns the \c ExecutorAddr of a given name as written in the object
204 /// file.
206 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
207
209 return *IncrExecutorBuilder;
210 }
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<IncrementalExecutorBuilder> IncrExecutorBuilder;
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.
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:239
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
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaHost()
Definition Interpreter.h:93
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaDevice()
Definition Interpreter.h:96
void SetCompilerArgs(const std::vector< const char * > &Args)
Definition Interpreter.h:58
void SetCudaSDK(llvm::StringRef Path)
Definition Interpreter.h:78
void SetDriverCompilationCallback(std::function< DriverCompilationFn > C)
Definition Interpreter.h:83
void SetTargetTriple(std::string TT)
Definition Interpreter.h:62
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateHost(OffloadType Type)
void SetDeviceSDK(OffloadType Type, llvm::StringRef Path)
Definition Interpreter.h:70
void SetOffloadArch(llvm::StringRef Arch)
Definition Interpreter.h:68
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateDevice(OffloadType Type)
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
llvm::Error LoadDynamicLibrary(const char *name)
Link a dynamic library.
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
static llvm::Expected< std::unique_ptr< Interpreter > > createWithDevice(OffloadType Type, std::unique_ptr< CompilerInstance > CI, std::unique_ptr< CompilerInstance > DCI)
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
const ASTContext & getASTContext() const
friend class Value
llvm::Error Execute(PartialTranslationUnit &T)
A processor an offloading action can target.
Definition OffloadArch.h:32
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.
@ 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.