clang 20.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/Shared/ExecutorAddress.h"
24#include "llvm/Support/Error.h"
25#include <memory>
26#include <vector>
27
28namespace llvm {
29namespace orc {
30class LLJIT;
31class LLJITBuilder;
32class ThreadSafeContext;
33} // namespace orc
34} // namespace llvm
35
36namespace clang {
37
38class CompilerInstance;
39class CodeGenerator;
40class CXXRecordDecl;
41class Decl;
42class IncrementalExecutor;
43class IncrementalParser;
44
45/// Create a pre-configured \c CompilerInstance for incremental processing.
47public:
49
50 void SetCompilerArgs(const std::vector<const char *> &Args) {
51 UserArgs = Args;
52 }
53
54 void SetTargetTriple(std::string TT) { TargetTriple = TT; }
55
56 // General C++
58
59 // Offload options
60 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
61
62 // CUDA specific
63 void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
64
67
68private:
70 create(std::string TT, std::vector<const char *> &ClangArgv);
71
73
74 std::vector<const char *> UserArgs;
75 std::optional<std::string> TargetTriple;
76
77 llvm::StringRef OffloadArch;
78 llvm::StringRef CudaSDKPath;
79};
80
81class IncrementalAction;
82class InProcessPrintingASTConsumer;
83
84/// Provides top-level interfaces for incremental compilation and execution.
86 friend class Value;
88
89 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
90 /// Long-lived, incremental parsing action.
91 std::unique_ptr<IncrementalAction> Act;
92 std::unique_ptr<IncrementalParser> IncrParser;
93 std::unique_ptr<IncrementalExecutor> IncrExecutor;
94
95 // An optional parser for CUDA offloading
96 std::unique_ptr<IncrementalParser> DeviceParser;
97
98 /// List containing information about each incrementally parsed piece of code.
99 std::list<PartialTranslationUnit> PTUs;
100
101 unsigned InitPTUSize = 0;
102
103 // This member holds the last result of the value printing. It's a class
104 // member because we might want to access it after more inputs. If no value
105 // printing happens, it's in an invalid state.
106 Value LastValue;
107
108 /// When CodeGen is created the first llvm::Module gets cached in many places
109 /// and we must keep it alive.
110 std::unique_ptr<llvm::Module> CachedInCodeGenModule;
111
112 /// Compiler instance performing the incremental compilation.
113 std::unique_ptr<CompilerInstance> CI;
114
115protected:
116 // Derived classes can use an extended interface of the Interpreter.
117 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
118 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr,
119 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr);
120
121 // Create the internal IncrementalExecutor, or re-create it after calling
122 // ResetExecutor().
123 llvm::Error CreateExecutor();
124
125 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
126 // JIT engine. In particular, it doesn't run cleanup or destructors.
127 void ResetExecutor();
128
129public:
130 virtual ~Interpreter();
132 create(std::unique_ptr<CompilerInstance> CI);
134 createWithCUDA(std::unique_ptr<CompilerInstance> CI,
135 std::unique_ptr<CompilerInstance> DCI);
136 const ASTContext &getASTContext() const;
141
143 llvm::Error Execute(PartialTranslationUnit &T);
144 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
145
146 /// Undo N previous incremental inputs.
147 llvm::Error Undo(unsigned N = 1);
148
149 /// Link a dynamic library
150 llvm::Error LoadDynamicLibrary(const char *name);
151
152 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
153 /// the CodeGenModule's internal mangling cache to avoid recomputing the
154 /// mangled name.
156
157 /// \returns the \c ExecutorAddr of a given name as written in the IR.
159 getSymbolAddress(llvm::StringRef IRName) const;
160
161 /// \returns the \c ExecutorAddr of a given name as written in the object
162 /// file.
164 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
165
167 return ValuePrintingInfo;
168 }
169
171
172private:
173 size_t getEffectivePTUSize() const;
174 void markUserCodeStart();
175 llvm::Expected<Expr *> ExtractValueFromExpr(Expr *E);
177
178 CodeGenerator *getCodeGen() const;
179 std::unique_ptr<llvm::Module> GenModule();
181 std::unique_ptr<llvm::Module> M = {});
182
183 // A cache for the compiled destructors used to for de-allocation of managed
184 // clang::Values.
185 llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
186
187 llvm::SmallVector<Expr *, 4> ValuePrintingInfo;
188
189 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder;
190};
191} // namespace clang
192
193#endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
#define V(N, I)
Definition: ASTContext.h:3443
Expr * E
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
The primary public interface to the Clang code generator.
Definition: ModuleBuilder.h:52
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
This represents one expression.
Definition: Expr.h:110
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
Create a pre-configured CompilerInstance for incremental processing.
Definition: Interpreter.h:46
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:50
void SetTargetTriple(std::string TT)
Definition: Interpreter.h:54
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
void SetCudaSDK(llvm::StringRef path)
Definition: Interpreter.h:63
void SetOffloadArch(llvm::StringRef Arch)
Definition: Interpreter.h:60
Provides top-level interfaces for incremental compilation and execution.
Definition: Interpreter.h:85
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V=nullptr)
llvm::Error CreateExecutor()
static llvm::Expected< std::unique_ptr< Interpreter > > create(std::unique_ptr< CompilerInstance > CI)
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)
virtual ~Interpreter()
llvm::Expected< PartialTranslationUnit & > Parse(llvm::StringRef Code)
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
llvm::Error Undo(unsigned N=1)
Undo N previous incremental inputs.
const llvm::SmallVectorImpl< Expr * > & getValuePrintingInfo() const
Definition: Interpreter.h:166
const CompilerInstance * getCompilerInstance() const
Expr * SynthesizeExpr(Expr *E)
const ASTContext & getASTContext() const
llvm::Expected< llvm::orc::LLJIT & > getExecutionEngine()
llvm::Error Execute(PartialTranslationUnit &T)
The top declaration context.
Definition: Decl.h:84
The JSON file list parser is used to communicate input to InstallAPI.
OffloadArch
Definition: Cuda.h:56
const FunctionProtoType * T
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
The class keeps track of various objects created as part of processing incremental inputs.