clang 18.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
17#include "clang/AST/Decl.h"
21
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ExecutionEngine/JITSymbol.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 ThreadSafeContext;
33} // namespace orc
34} // namespace llvm
35
36namespace clang {
37
38class CompilerInstance;
39class IncrementalExecutor;
40class IncrementalParser;
41
42/// Create a pre-configured \c CompilerInstance for incremental processing.
44public:
46
47 void SetCompilerArgs(const std::vector<const char *> &Args) {
48 UserArgs = Args;
49 }
50
51 // General C++
53
54 // Offload options
55 void SetOffloadArch(llvm::StringRef Arch) { OffloadArch = Arch; };
56
57 // CUDA specific
58 void SetCudaSDK(llvm::StringRef path) { CudaSDKPath = path; };
59
62
63private:
65 create(std::vector<const char *> &ClangArgv);
66
68
69 std::vector<const char *> UserArgs;
70
71 llvm::StringRef OffloadArch;
72 llvm::StringRef CudaSDKPath;
73};
74
75/// Provides top-level interfaces for incremental compilation and execution.
77 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
78 std::unique_ptr<IncrementalParser> IncrParser;
79 std::unique_ptr<IncrementalExecutor> IncrExecutor;
80
81 // An optional parser for CUDA offloading
82 std::unique_ptr<IncrementalParser> DeviceParser;
83
84 Interpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err);
85
86 llvm::Error CreateExecutor();
87 unsigned InitPTUSize = 0;
88
89 // This member holds the last result of the value printing. It's a class
90 // member because we might want to access it after more inputs. If no value
91 // printing happens, it's in an invalid state.
92 Value LastValue;
93
94public:
97 create(std::unique_ptr<CompilerInstance> CI);
99 createWithCUDA(std::unique_ptr<CompilerInstance> CI,
100 std::unique_ptr<CompilerInstance> DCI);
101 const ASTContext &getASTContext() const;
105
107 llvm::Error Execute(PartialTranslationUnit &T);
108 llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
110
111 /// Undo N previous incremental inputs.
112 llvm::Error Undo(unsigned N = 1);
113
114 /// Link a dynamic library
115 llvm::Error LoadDynamicLibrary(const char *name);
116
117 /// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
118 /// the CodeGenModule's internal mangling cache to avoid recomputing the
119 /// mangled name.
121
122 /// \returns the \c ExecutorAddr of a given name as written in the IR.
124 getSymbolAddress(llvm::StringRef IRName) const;
125
126 /// \returns the \c ExecutorAddr of a given name as written in the object
127 /// file.
129 getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
130
132
134 return ValuePrintingInfo;
135 }
136
138
139private:
140 size_t getEffectivePTUSize() const;
141
142 bool FindRuntimeInterface();
143
144 llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
145
146 llvm::SmallVector<Expr *, 3> ValuePrintingInfo;
147};
148} // namespace clang
149
150#endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
#define V(N, I)
Definition: ASTContext.h:3241
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
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:110
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
Create a pre-configured CompilerInstance for incremental processing.
Definition: Interpreter.h:43
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaHost()
void SetCompilerArgs(const std::vector< const char * > &Args)
Definition: Interpreter.h:47
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCpp()
void SetCudaSDK(llvm::StringRef path)
Definition: Interpreter.h:58
void SetOffloadArch(llvm::StringRef Arch)
Definition: Interpreter.h:55
llvm::Expected< std::unique_ptr< CompilerInstance > > CreateCudaDevice()
Provides top-level interfaces for incremental compilation and execution.
Definition: Interpreter.h:76
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddress(GlobalDecl GD) const
llvm::Error Execute(PartialTranslationUnit &T)
llvm::Expected< llvm::orc::LLJIT & > getExecutionEngine()
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const
static llvm::Expected< std::unique_ptr< Interpreter > > createWithCUDA(std::unique_ptr< CompilerInstance > CI, std::unique_ptr< CompilerInstance > DCI)
const CompilerInstance * getCompilerInstance() const
llvm::Expected< llvm::orc::ExecutorAddr > CompileDtorCall(CXXRecordDecl *CXXRD)
llvm::Error LoadDynamicLibrary(const char *name)
Link a dynamic library.
const llvm::SmallVectorImpl< Expr * > & getValuePrintingInfo() const
Definition: Interpreter.h:133
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V=nullptr)
Expr * SynthesizeExpr(Expr *E)
static llvm::Expected< std::unique_ptr< Interpreter > > create(std::unique_ptr< CompilerInstance > CI)
llvm::Expected< PartialTranslationUnit & > Parse(llvm::StringRef Code)
llvm::Error Undo(unsigned N=1)
Undo N previous incremental inputs.
const ASTContext & getASTContext() const
YAML serialization mapping.
Definition: Dominators.h:30
The class keeps track of various objects created as part of processing incremental inputs.