clang 22.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/ExecutorProcessControl.h"
24#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
25#include "llvm/Support/Error.h"
26#include <memory>
27#include <vector>
28
29namespace llvm {
30namespace orc {
31class LLJIT;
32class LLJITBuilder;
33class ThreadSafeContext;
34} // namespace orc
35} // namespace llvm
36
37namespace clang {
38
39class CompilerInstance;
40class CXXRecordDecl;
41class Decl;
42class IncrementalExecutor;
43class IncrementalParser;
44class IncrementalCUDADeviceParser;
45
46/// Create a pre-configured \c CompilerInstance for incremental processing.
48public:
50
51 void SetCompilerArgs(const std::vector<const char *> &Args) {
52 UserArgs = Args;
53 }
54
55 void SetTargetTriple(std::string TT) { TargetTriple = TT; }
56
57 // General C++
59
60 // Offload options
61 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
62
63 // CUDA specific
64 void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
65
68
69private:
71 create(std::string TT, std::vector<const char *> &ClangArgv);
72
74
75 std::vector<const char *> UserArgs;
76 std::optional<std::string> TargetTriple;
77
78 llvm::StringRef OffloadArch;
79 llvm::StringRef CudaSDKPath;
80};
81
82class IncrementalAction;
83class InProcessPrintingASTConsumer;
84
85/// Provides top-level interfaces for incremental compilation and execution.
87 friend class Value;
89
90 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
91 /// Long-lived, incremental parsing action.
92 std::unique_ptr<IncrementalAction> Act;
93 std::unique_ptr<IncrementalParser> IncrParser;
94 std::unique_ptr<IncrementalExecutor> IncrExecutor;
95
96 // An optional parser for CUDA offloading
97 std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
98
99 // An optional action for CUDA offloading
100 std::unique_ptr<IncrementalAction> DeviceAct;
101
102 /// List containing information about each incrementally parsed piece of code.
103 std::list<PartialTranslationUnit> PTUs;
104
105 unsigned InitPTUSize = 0;
106
107 // This member holds the last result of the value printing. It's a class
108 // member because we might want to access it after more inputs. If no value
109 // printing happens, it's in an invalid state.
110 Value LastValue;
111
112 /// Compiler instance performing the incremental compilation.
113 std::unique_ptr<CompilerInstance> CI;
114
115 /// An optional compiler instance for CUDA offloading
116 std::unique_ptr<CompilerInstance> DeviceCI;
117
118protected:
119 // Derived classes can use an extended interface of the Interpreter.
120 Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
121 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr,
122 std::unique_ptr<clang::ASTConsumer> Consumer = nullptr);
123
124 // Create the internal IncrementalExecutor, or re-create it after calling
125 // ResetExecutor().
126 llvm::Error CreateExecutor();
127
128 // Delete the internal IncrementalExecutor. This causes a hard shutdown of the
129 // JIT engine. In particular, it doesn't run cleanup or destructors.
130 void ResetExecutor();
131
132public:
133 virtual ~Interpreter();
135 create(std::unique_ptr<CompilerInstance> CI,
136 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr);
138 createWithCUDA(std::unique_ptr<CompilerInstance> CI,
139 std::unique_ptr<CompilerInstance> DCI);
141 createLLJITBuilder(std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC,
142 llvm::StringRef OrcRuntimePath);
143 const ASTContext &getASTContext() const;
148
150 llvm::Error Execute(PartialTranslationUnit &T);
151 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
152
153 /// Undo N previous incremental inputs.
154 llvm::Error Undo(unsigned N = 1);
155
156 /// Link a dynamic library
157 llvm::Error LoadDynamicLibrary(const char *name);
158
159 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
160 /// the CodeGenModule's internal mangling cache to avoid recomputing the
161 /// mangled name.
163
164 /// \returns the \c ExecutorAddr of a given name as written in the IR.
166 getSymbolAddress(llvm::StringRef IRName) const;
167
168 /// \returns the \c ExecutorAddr of a given name as written in the object
169 /// file.
171 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
172
173private:
174 size_t getEffectivePTUSize() const;
175 void markUserCodeStart();
176
177 // A cache for the compiled destructors used to for de-allocation of managed
178 // clang::Values.
179 mutable llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
180
181 std::array<Expr *, 4> ValuePrintingInfo = {0};
182
183 std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder;
184
185 /// @}
186 /// @name Value and pretty printing support
187 /// @{
188
189 std::string ValueDataToString(const Value &V) const;
190 std::string ValueTypeToString(const Value &V) const;
191
192 llvm::Expected<Expr *> convertExprToValue(Expr *E);
193
194 // When we deallocate clang::Value we need to run the destructor of the type.
195 // This function forces emission of the needed dtor.
197 CompileDtorCall(CXXRecordDecl *CXXRD) const;
198};
199} // namespace clang
200
201#endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
#define V(N, I)
Definition: ASTContext.h:3597
Expr * E
OffloadArch Arch
Definition: OffloadArch.cpp:10
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
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
This represents one expression.
Definition: Expr.h:112
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:57
Create a pre-configured CompilerInstance for incremental processing.
Definition: Interpreter.h:47
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:51
void SetTargetTriple(std::string TT)
Definition: Interpreter.h:55
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
void SetCudaSDK(llvm::StringRef path)
Definition: Interpreter.h:64
void SetOffloadArch(llvm::StringRef Arch)
Definition: Interpreter.h:61
Provides top-level interfaces for incremental compilation and execution.
Definition: Interpreter.h:86
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V=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)
virtual ~Interpreter()
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
llvm::Error Undo(unsigned N=1)
Undo N previous incremental inputs.
const CompilerInstance * getCompilerInstance() const
static llvm::Expected< std::unique_ptr< llvm::orc::LLJITBuilder > > createLLJITBuilder(std::unique_ptr< llvm::orc::ExecutorProcessControl > EPC, llvm::StringRef OrcRuntimePath)
static llvm::Expected< std::unique_ptr< Interpreter > > create(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< llvm::orc::LLJITBuilder > JITBuilder=nullptr)
const ASTContext & getASTContext() const
llvm::Expected< llvm::orc::LLJIT & > getExecutionEngine()
llvm::Error Execute(PartialTranslationUnit &T)
The JSON file list parser is used to communicate input to InstallAPI.
OffloadArch
Definition: OffloadArch.h:18
@ Parse
Parse the block; this code is always used.
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.