Skip to content

Commit 7f97bf4

Browse files
committed
store
1 parent a618d30 commit 7f97bf4

File tree

11 files changed

+345
-353
lines changed

11 files changed

+345
-353
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,26 @@ class IncrementalCompilerBuilder {
7878
llvm::StringRef CudaSDKPath;
7979
};
8080

81-
/// Generate glue code between the Interpreter's built-in runtime and user code.
82-
class RuntimeInterfaceBuilder {
83-
public:
84-
virtual ~RuntimeInterfaceBuilder() = default;
81+
class Interpreter;
82+
/// Provides a callback class allowing to listen to interpreter events and to
83+
/// specialize some operations.
84+
class InterpreterCallbacks {
85+
Interpreter &Interp;
8586

86-
using TransformExprFunction = ExprResult(RuntimeInterfaceBuilder *Builder,
87-
Expr *, ArrayRef<Expr *>);
88-
virtual TransformExprFunction *getPrintValueTransformer() = 0;
87+
public:
88+
InterpreterCallbacks(Interpreter &I) : Interp(I) {}
89+
virtual ~InterpreterCallbacks();
90+
virtual void ProcessingTopLevelStmtDecl(TopLevelStmtDecl *D);
8991
};
9092

9193
/// Provides top-level interfaces for incremental compilation and execution.
9294
class Interpreter {
95+
friend Value;
96+
9397
std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
98+
std::unique_ptr<InterpreterCallbacks> InterpreterCB;
9499
std::unique_ptr<IncrementalParser> IncrParser;
95100
std::unique_ptr<IncrementalExecutor> IncrExecutor;
96-
std::unique_ptr<RuntimeInterfaceBuilder> RuntimeIB;
97101

98102
// An optional parser for CUDA offloading
99103
std::unique_ptr<IncrementalParser> DeviceParser;
@@ -105,10 +109,16 @@ class Interpreter {
105109
// printing happens, it's in an invalid state.
106110
Value LastValue;
107111

108-
// Add a call to an Expr to report its result. We query the function from
109-
// RuntimeInterfaceBuilder once and store it as a function pointer to avoid
110-
// frequent virtual function calls.
111-
RuntimeInterfaceBuilder::TransformExprFunction *AddPrintValueCall = nullptr;
112+
// The cached declaration of std::string used as a return type for the built
113+
// trampoline. This is done in C++ to simplify the memory management for
114+
// user-defined printing functions.
115+
Decl *StdString = nullptr;
116+
117+
// A cache for the compiled destructors used to for de-allocation of managed
118+
// clang::Values.
119+
llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
120+
121+
std::array<Expr *, 4> ValuePrintingInfo = {0};
112122

113123
protected:
114124
// Derived classes can use an extended interface of the Interpreter.
@@ -123,23 +133,8 @@ class Interpreter {
123133
// JIT engine. In particular, it doesn't run cleanup or destructors.
124134
void ResetExecutor();
125135

126-
// Lazily construct the RuntimeInterfaceBuilder. The provided instance will be
127-
// used for the entire lifetime of the interpreter. The default implementation
128-
// targets the in-process __clang_Interpreter runtime. Override this to use a
129-
// custom runtime.
130-
virtual std::unique_ptr<RuntimeInterfaceBuilder> FindRuntimeInterface();
131-
132136
public:
133137
virtual ~Interpreter();
134-
135-
// class SynthesizingCodeRAII {
136-
137-
// };
138-
139-
// SynthesizingCodeRAII EnterCodeSynthesisScope() {
140-
141-
// }
142-
143138
static llvm::Expected<std::unique_ptr<Interpreter>>
144139
create(std::unique_ptr<CompilerInstance> CI);
145140
static llvm::Expected<std::unique_ptr<Interpreter>>
@@ -154,7 +149,6 @@ class Interpreter {
154149
llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Code);
155150
llvm::Error Execute(PartialTranslationUnit &T);
156151
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
157-
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
158152

159153
/// Undo N previous incremental inputs.
160154
llvm::Error Undo(unsigned N = 1);
@@ -176,25 +170,32 @@ class Interpreter {
176170
llvm::Expected<llvm::orc::ExecutorAddr>
177171
getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
178172

179-
enum InterfaceKind { NoAlloc, WithAlloc, CopyArray, NewTag };
180-
181-
const llvm::SmallVectorImpl<Expr *> &getValuePrintingInfo() const {
182-
return ValuePrintingInfo;
173+
InterpreterCallbacks *getInterpreterCallbacks() {
174+
return InterpreterCB.get();
175+
}
176+
const InterpreterCallbacks *getInterpreterCallbacks() const {
177+
return const_cast<Interpreter *>(this)->getInterpreterCallbacks();
178+
}
179+
void setInterpreterCallbacks(std::unique_ptr<InterpreterCallbacks> CB) {
180+
InterpreterCB = std::move(CB);
183181
}
184182

185-
Expr *SynthesizeExpr(Expr *E);
183+
llvm::Expected<Expr *> SynthesizeExpr(Expr *E);
186184

187185
std::unique_ptr<llvm::Module> GenModule();
188186

189187
private:
190188
size_t getEffectivePTUSize() const;
191189
void markUserCodeStart();
192190

193-
llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
191+
std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder;
194192

195-
llvm::SmallVector<Expr *, 4> ValuePrintingInfo;
193+
std::string ValueDataToString(const Value &V);
194+
std::string ValueTypeToString(const Value &V) const;
196195

197-
std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder;
196+
// When we deallocate clang::Value we need to run the destructor of the type.
197+
// This function forces emission of the needed dtor.
198+
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
198199
};
199200
} // namespace clang
200201

clang/include/clang/Interpreter/Value.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class REPL_EXTERNAL_VISIBILITY Value {
119119
~Value();
120120

121121
void printType(llvm::raw_ostream &Out) const;
122-
void printData(llvm::raw_ostream &Out) const;
123-
void print(llvm::raw_ostream &Out) const;
124-
void dump() const;
122+
void printData(llvm::raw_ostream &Out);
123+
void print(llvm::raw_ostream &Out);
124+
void dump();
125125
void clear();
126126

127127
ASTContext &getASTContext();

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
namespace clang {
2525

2626
IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
27-
Interpreter &Interp, std::unique_ptr<CompilerInstance> Instance,
28-
IncrementalParser &HostParser, llvm::LLVMContext &LLVMCtx,
27+
std::unique_ptr<CompilerInstance> Instance, IncrementalParser &HostParser,
28+
llvm::LLVMContext &LLVMCtx,
2929
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
3030
llvm::Error &Err)
31-
: IncrementalParser(Interp, std::move(Instance), LLVMCtx, Err),
31+
: IncrementalParser(std::move(Instance), LLVMCtx, Err,
32+
/*InterpreterCallbacks=*/nullptr),
3233
HostParser(HostParser), VFS(FS) {
3334
if (Err)
3435
return;

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace clang {
2222
class IncrementalCUDADeviceParser : public IncrementalParser {
2323
public:
2424
IncrementalCUDADeviceParser(
25-
Interpreter &Interp, std::unique_ptr<CompilerInstance> Instance,
26-
IncrementalParser &HostParser, llvm::LLVMContext &LLVMCtx,
25+
std::unique_ptr<CompilerInstance> Instance, IncrementalParser &HostParser,
26+
llvm::LLVMContext &LLVMCtx,
2727
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
2828
llvm::Error &Err);
2929

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,24 @@
3232
namespace clang {
3333

3434
class IncrementalASTConsumer final : public ASTConsumer {
35-
Interpreter &Interp;
35+
InterpreterCallbacks *CB;
3636
std::unique_ptr<ASTConsumer> Consumer;
3737

3838
public:
39-
IncrementalASTConsumer(Interpreter &InterpRef, std::unique_ptr<ASTConsumer> C)
40-
: Interp(InterpRef), Consumer(std::move(C)) {}
39+
IncrementalASTConsumer(std::unique_ptr<ASTConsumer> C,
40+
InterpreterCallbacks *CB)
41+
: CB(CB), Consumer(std::move(C)) {}
4142

4243
bool HandleTopLevelDecl(DeclGroupRef DGR) override final {
4344
if (DGR.isNull())
4445
return true;
4546
if (!Consumer)
4647
return true;
4748

48-
for (Decl *D : DGR)
49-
if (auto *TSD = llvm::dyn_cast<TopLevelStmtDecl>(D);
50-
TSD && TSD->isSemiMissing())
51-
TSD->setStmt(Interp.SynthesizeExpr(cast<Expr>(TSD->getStmt())));
49+
if (CB)
50+
for (Decl *D : DGR)
51+
if (auto *TLSD = llvm::dyn_cast<TopLevelStmtDecl>(D))
52+
CB->ProcessingTopLevelStmtDecl(TLSD);
5253

5354
return Consumer->HandleTopLevelDecl(DGR);
5455
}
@@ -199,10 +200,9 @@ CodeGenerator *IncrementalParser::getCodeGen() const {
199200

200201
IncrementalParser::IncrementalParser() {}
201202

202-
IncrementalParser::IncrementalParser(Interpreter &Interp,
203-
std::unique_ptr<CompilerInstance> Instance,
203+
IncrementalParser::IncrementalParser(std::unique_ptr<CompilerInstance> Instance,
204204
llvm::LLVMContext &LLVMCtx,
205-
llvm::Error &Err)
205+
llvm::Error &Err, InterpreterCallbacks *CB)
206206
: CI(std::move(Instance)) {
207207
llvm::ErrorAsOutParameter EAO(&Err);
208208
Act = std::make_unique<IncrementalAction>(*CI, LLVMCtx, Err);
@@ -214,7 +214,7 @@ IncrementalParser::IncrementalParser(Interpreter &Interp,
214214
CachedInCodeGenModule = GenModule();
215215

216216
std::unique_ptr<ASTConsumer> IncrConsumer =
217-
std::make_unique<IncrementalASTConsumer>(Interp, CI->takeASTConsumer());
217+
std::make_unique<IncrementalASTConsumer>(CI->takeASTConsumer(), CB);
218218
CI->setASTConsumer(std::move(IncrConsumer));
219219
Consumer = &CI->getASTConsumer();
220220
P.reset(

clang/lib/Interpreter/IncrementalParser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CompilerInstance;
3434
class Parser;
3535

3636
class IncrementalAction;
37-
class Interpreter;
37+
class InterpreterCallbacks;
3838
/// Provides support for incremental compilation. Keeps track of the state
3939
/// changes between the subsequent incremental input.
4040
///
@@ -66,9 +66,9 @@ class IncrementalParser {
6666
IncrementalParser();
6767

6868
public:
69-
IncrementalParser(Interpreter &Interp,
70-
std::unique_ptr<CompilerInstance> Instance,
71-
llvm::LLVMContext &LLVMCtx, llvm::Error &Err);
69+
IncrementalParser(std::unique_ptr<CompilerInstance> Instance,
70+
llvm::LLVMContext &LLVMCtx, llvm::Error &Err,
71+
InterpreterCallbacks *CB);
7272
virtual ~IncrementalParser();
7373

7474
CompilerInstance *getCI() { return CI.get(); }

0 commit comments

Comments
 (0)