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