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 // CUDA specific
73 void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
74
75 // Hand over the compilation.
79
82
83private:
85 create(std::string TT, std::vector<const char *> &ClangArgv);
86
88
89 std::vector<const char *> UserArgs;
90 std::optional<std::string> TargetTriple;
91
92 llvm::StringRef OffloadArch;
93 llvm::StringRef CudaSDKPath;
94
95 std::optional<std::function<DriverCompilationFn>> CompilationCB;
96};
97
98class IncrementalAction;
99class InProcessPrintingASTConsumer;
100
101/// Provides top-level interfaces for incremental compilation and execution.
103 friend class Value;
104 friend InProcessPrintingASTConsumer;
105
106 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
107 /// Long-lived, incremental parsing action.
108 std::unique_ptr<IncrementalAction> Act;
109 std::unique_ptr<IncrementalParser> IncrParser;
110 std::unique_ptr<IncrementalExecutor> IncrExecutor;
111
112 // An optional parser for CUDA offloading
113 std::unique_ptr<IncrementalDeviceParser> DeviceParser;
114
115 // An optional action for CUDA offloading
116 std::unique_ptr<IncrementalAction> DeviceAct;
117
118 /// List containing information about each incrementally parsed piece of code.
119 std::list<PartialTranslationUnit> PTUs;
120
121 unsigned InitPTUSize = 0;
122
123 // This member holds the last result of the value printing. It's a class
124 // member because we might want to access it after more inputs. If no value
125 // printing happens, it's in an invalid state.
126 Value LastValue;
127
128 /// Compiler instance performing the incremental compilation.
129 std::unique_ptr<CompilerInstance> CI;
130
131 /// An optional compiler instance for CUDA offloading
132 std::unique_ptr<CompilerInstance> DeviceCI;
133
134protected:
135 // Derived classes can use an extended interface of the Interpreter.
136 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
137 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr,
138 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr);
139
140 // Create the internal IncrementalExecutor, or re-create it after calling
141 // ResetExecutor().
142 llvm::Error CreateExecutor();
143
144 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
145 // JIT engine. In particular, it doesn't run cleanup or destructors.
146 void ResetExecutor() { IncrExecutor.reset(); }
147
148public:
149 virtual ~Interpreter();
151 create(std::unique_ptr<CompilerInstance> CI,
152 std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr);
154 createWithDevice(std::unique_ptr<CompilerInstance> CI,
155 std::unique_ptr<CompilerInstance> DCI,
158 createWithCUDA(std::unique_ptr<CompilerInstance> CI,
159 std::unique_ptr<CompilerInstance> DCI);
160
161 const ASTContext &getASTContext() const;
166
168 llvm::Error Execute(PartialTranslationUnit &T);
169 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
170
171 /// Undo N previous incremental inputs.
172 llvm::Error Undo(unsigned N = 1);
173
174 /// Link a dynamic library
175 llvm::Error LoadDynamicLibrary(const char *name);
176
177 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
178 /// the CodeGenModule's internal mangling cache to avoid recomputing the
179 /// mangled name.
181
182 /// \returns the \c ExecutorAddr of a given name as written in the IR.
184 getSymbolAddress(llvm::StringRef IRName) const;
185
186 /// \returns the \c ExecutorAddr of a given name as written in the object
187 /// file.
189 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
190
192 return *IncrExecutorBuilder;
193 }
194
195private:
196 size_t getEffectivePTUSize() const;
197 void markUserCodeStart();
198
199 // A cache for the compiled destructors used to for de-allocation of managed
200 // clang::Values.
201 mutable llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
202
203 std::array<Expr *, 4> ValuePrintingInfo = {0};
204
205 std::unique_ptr<IncrementalExecutorBuilder> IncrExecutorBuilder;
206
207 /// @}
208 /// @name Value and pretty printing support
209 /// @{
210
211 std::string ValueDataToString(const Value &V) const;
212 std::string ValueTypeToString(const Value &V) const;
213
214 llvm::Expected<Expr *> convertExprToValue(Expr *E);
215
216 // When we deallocate clang::Value we need to run the destructor of the type.
217 // This function forces emission of the needed dtor.
219 CompileDtorCall(CXXRecordDecl *CXXRD) const;
220};
221} // namespace clang
222
223#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
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: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()
void SetCudaSDK(llvm::StringRef path)
Definition Interpreter.h:73
void SetOffloadArch(llvm::StringRef Arch)
Definition Interpreter.h:70
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.
static llvm::Expected< std::unique_ptr< Interpreter > > createWithCUDA(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< CompilerInstance > DCI)
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: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.
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.