Skip to content

Commit c405adf

Browse files
committed
Revert "[clang-repl] Simplify the value printing logic to enable out-of-process. (llvm#107737)"
This reverts commit a72d7ee.
1 parent ed8019d commit c405adf

13 files changed

+822
-732
lines changed

clang/include/clang/Frontend/MultiplexConsumer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class MultiplexConsumer : public SemaConsumer {
5353
public:
5454
// Takes ownership of the pointers in C.
5555
MultiplexConsumer(std::vector<std::unique_ptr<ASTConsumer>> C);
56-
MultiplexConsumer(std::unique_ptr<ASTConsumer> C);
5756
~MultiplexConsumer() override;
5857

5958
// ASTConsumer
@@ -81,7 +80,7 @@ class MultiplexConsumer : public SemaConsumer {
8180
void InitializeSema(Sema &S) override;
8281
void ForgetSema() override;
8382

84-
protected:
83+
private:
8584
std::vector<std::unique_ptr<ASTConsumer>> Consumers; // Owns these.
8685
std::unique_ptr<MultiplexASTMutationListener> MutationListener;
8786
std::unique_ptr<MultiplexASTDeserializationListener> DeserializationListener;

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
#ifndef LLVM_CLANG_INTERPRETER_INTERPRETER_H
1515
#define LLVM_CLANG_INTERPRETER_INTERPRETER_H
1616

17+
#include "clang/AST/Decl.h"
1718
#include "clang/AST/GlobalDecl.h"
1819
#include "clang/Interpreter/PartialTranslationUnit.h"
1920
#include "clang/Interpreter/Value.h"
21+
#include "clang/Sema/Ownership.h"
2022

2123
#include "llvm/ADT/DenseMap.h"
2224
#include "llvm/ExecutionEngine/JITSymbol.h"
@@ -36,9 +38,6 @@ class ThreadSafeContext;
3638
namespace clang {
3739

3840
class CompilerInstance;
39-
class CodeGenerator;
40-
class CXXRecordDecl;
41-
class Decl;
4241
class IncrementalExecutor;
4342
class IncrementalParser;
4443

@@ -78,45 +77,42 @@ class IncrementalCompilerBuilder {
7877
llvm::StringRef CudaSDKPath;
7978
};
8079

81-
class IncrementalAction;
82-
class InProcessPrintingASTConsumer;
80+
/// Generate glue code between the Interpreter's built-in runtime and user code.
81+
class RuntimeInterfaceBuilder {
82+
public:
83+
virtual ~RuntimeInterfaceBuilder() = default;
84+
85+
using TransformExprFunction = ExprResult(RuntimeInterfaceBuilder *Builder,
86+
Expr *, ArrayRef<Expr *>);
87+
virtual TransformExprFunction *getPrintValueTransformer() = 0;
88+
};
8389

8490
/// Provides top-level interfaces for incremental compilation and execution.
8591
class Interpreter {
86-
friend class Value;
87-
friend InProcessPrintingASTConsumer;
88-
8992
std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
90-
/// Long-lived, incremental parsing action.
91-
std::unique_ptr<IncrementalAction> Act;
9293
std::unique_ptr<IncrementalParser> IncrParser;
9394
std::unique_ptr<IncrementalExecutor> IncrExecutor;
95+
std::unique_ptr<RuntimeInterfaceBuilder> RuntimeIB;
9496

9597
// An optional parser for CUDA offloading
9698
std::unique_ptr<IncrementalParser> DeviceParser;
9799

98-
/// List containing information about each incrementally parsed piece of code.
99-
std::list<PartialTranslationUnit> PTUs;
100-
101100
unsigned InitPTUSize = 0;
102101

103102
// This member holds the last result of the value printing. It's a class
104103
// member because we might want to access it after more inputs. If no value
105104
// printing happens, it's in an invalid state.
106105
Value LastValue;
107106

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;
107+
// Add a call to an Expr to report its result. We query the function from
108+
// RuntimeInterfaceBuilder once and store it as a function pointer to avoid
109+
// frequent virtual function calls.
110+
RuntimeInterfaceBuilder::TransformExprFunction *AddPrintValueCall = nullptr;
114111

115112
protected:
116113
// 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);
114+
Interpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err,
115+
std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr);
120116

121117
// Create the internal IncrementalExecutor, or re-create it after calling
122118
// ResetExecutor().
@@ -126,8 +122,15 @@ class Interpreter {
126122
// JIT engine. In particular, it doesn't run cleanup or destructors.
127123
void ResetExecutor();
128124

125+
// Lazily construct the RuntimeInterfaceBuilder. The provided instance will be
126+
// used for the entire lifetime of the interpreter. The default implementation
127+
// targets the in-process __clang_Interpreter runtime. Override this to use a
128+
// custom runtime.
129+
virtual std::unique_ptr<RuntimeInterfaceBuilder> FindRuntimeInterface();
130+
129131
public:
130132
virtual ~Interpreter();
133+
131134
static llvm::Expected<std::unique_ptr<Interpreter>>
132135
create(std::unique_ptr<CompilerInstance> CI);
133136
static llvm::Expected<std::unique_ptr<Interpreter>>
@@ -142,6 +145,7 @@ class Interpreter {
142145
llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Code);
143146
llvm::Error Execute(PartialTranslationUnit &T);
144147
llvm::Error ParseAndExecute(llvm::StringRef Code, Value *V = nullptr);
148+
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
145149

146150
/// Undo N previous incremental inputs.
147151
llvm::Error Undo(unsigned N = 1);
@@ -163,6 +167,8 @@ class Interpreter {
163167
llvm::Expected<llvm::orc::ExecutorAddr>
164168
getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
165169

170+
enum InterfaceKind { NoAlloc, WithAlloc, CopyArray, NewTag };
171+
166172
const llvm::SmallVectorImpl<Expr *> &getValuePrintingInfo() const {
167173
return ValuePrintingInfo;
168174
}
@@ -172,15 +178,7 @@ class Interpreter {
172178
private:
173179
size_t getEffectivePTUSize() const;
174180
void markUserCodeStart();
175-
llvm::Expected<Expr *> ExtractValueFromExpr(Expr *E);
176-
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
177-
178-
CodeGenerator *getCodeGen() const;
179-
std::unique_ptr<llvm::Module> GenModule();
180-
PartialTranslationUnit &RegisterPTU(TranslationUnitDecl *TU);
181181

182-
// A cache for the compiled destructors used to for de-allocation of managed
183-
// clang::Values.
184182
llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
185183

186184
llvm::SmallVector<Expr *, 4> ValuePrintingInfo;

clang/lib/Frontend/MultiplexConsumer.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,6 @@ MultiplexConsumer::MultiplexConsumer(
298298
}
299299
}
300300

301-
MultiplexConsumer::MultiplexConsumer(std::unique_ptr<ASTConsumer> C)
302-
: MultiplexConsumer([](std::unique_ptr<ASTConsumer> Consumer) {
303-
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
304-
Consumers.push_back(std::move(Consumer));
305-
return Consumers;
306-
}(std::move(C))) {}
307-
308301
MultiplexConsumer::~MultiplexConsumer() {}
309302

310303
void MultiplexConsumer::Initialize(ASTContext &Context) {

clang/lib/Interpreter/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ add_clang_library(clangInterpreter
2323
IncrementalExecutor.cpp
2424
IncrementalParser.cpp
2525
Interpreter.cpp
26-
InterpreterValuePrinter.cpp
2726
InterpreterUtils.cpp
2827
Value.cpp
2928
${WASM_SRC}

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "clang/Basic/TargetOptions.h"
1616
#include "clang/CodeGen/ModuleBuilder.h"
1717
#include "clang/Frontend/CompilerInstance.h"
18-
#include "clang/Interpreter/PartialTranslationUnit.h"
1918

2019
#include "llvm/IR/LegacyPassManager.h"
2120
#include "llvm/IR/Module.h"
@@ -25,17 +24,15 @@
2524
namespace clang {
2625

2726
IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
28-
std::unique_ptr<CompilerInstance> DeviceInstance,
29-
CompilerInstance &HostInstance,
27+
Interpreter &Interp, std::unique_ptr<CompilerInstance> Instance,
28+
IncrementalParser &HostParser, llvm::LLVMContext &LLVMCtx,
3029
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
31-
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
32-
: IncrementalParser(*DeviceInstance, Err), PTUs(PTUs), VFS(FS),
33-
CodeGenOpts(HostInstance.getCodeGenOpts()),
34-
TargetOpts(HostInstance.getTargetOpts()) {
30+
llvm::Error &Err)
31+
: IncrementalParser(Interp, std::move(Instance), LLVMCtx, Err),
32+
HostParser(HostParser), VFS(FS) {
3533
if (Err)
3634
return;
37-
DeviceCI = std::move(DeviceInstance);
38-
StringRef Arch = TargetOpts.CPU;
35+
StringRef Arch = CI->getTargetOpts().CPU;
3936
if (!Arch.starts_with("sm_") || Arch.substr(3).getAsInteger(10, SMVersion)) {
4037
Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
4138
"Invalid CUDA architecture",
@@ -44,7 +41,7 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
4441
}
4542
}
4643

47-
llvm::Expected<TranslationUnitDecl *>
44+
llvm::Expected<PartialTranslationUnit &>
4845
IncrementalCUDADeviceParser::Parse(llvm::StringRef Input) {
4946
auto PTU = IncrementalParser::Parse(Input);
5047
if (!PTU)
@@ -65,7 +62,7 @@ IncrementalCUDADeviceParser::Parse(llvm::StringRef Input) {
6562
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
6663
"", false));
6764

68-
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
65+
HostParser.getCI()->getCodeGenOpts().CudaGpuBinaryFileName = FatbinFileName;
6966

7067
FatbinContent.clear();
7168

@@ -83,7 +80,7 @@ llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {
8380
std::error_code());
8481
llvm::TargetOptions TO = llvm::TargetOptions();
8582
llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
86-
PTU.TheModule->getTargetTriple(), TargetOpts.CPU, "", TO,
83+
PTU.TheModule->getTargetTriple(), getCI()->getTargetOpts().CPU, "", TO,
8784
llvm::Reloc::Model::PIC_);
8885
PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());
8986

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,19 @@
1818
#include "llvm/Support/VirtualFileSystem.h"
1919

2020
namespace clang {
21-
struct PartialTranslationUnit;
22-
class CompilerInstance;
23-
class CodeGenOptions;
24-
class TargetOptions;
2521

2622
class IncrementalCUDADeviceParser : public IncrementalParser {
27-
const std::list<PartialTranslationUnit> &PTUs;
28-
2923
public:
3024
IncrementalCUDADeviceParser(
31-
std::unique_ptr<CompilerInstance> DeviceInstance,
32-
CompilerInstance &HostInstance,
25+
Interpreter &Interp, std::unique_ptr<CompilerInstance> Instance,
26+
IncrementalParser &HostParser, llvm::LLVMContext &LLVMCtx,
3327
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
34-
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
28+
llvm::Error &Err);
3529

36-
llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input) override;
30+
llvm::Expected<PartialTranslationUnit &>
31+
Parse(llvm::StringRef Input) override;
3732

38-
// Generate PTX for the last PTU.
33+
// Generate PTX for the last PTU
3934
llvm::Expected<llvm::StringRef> GeneratePTX();
4035

4136
// Generate fatbinary contents in memory
@@ -44,13 +39,11 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
4439
~IncrementalCUDADeviceParser();
4540

4641
protected:
47-
std::unique_ptr<CompilerInstance> DeviceCI;
42+
IncrementalParser &HostParser;
4843
int SMVersion;
4944
llvm::SmallString<1024> PTXCode;
5045
llvm::SmallVector<char, 1024> FatbinContent;
5146
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
52-
CodeGenOptions &CodeGenOpts; // Intentionally a reference.
53-
const TargetOptions &TargetOpts;
5447
};
5548

5649
} // namespace clang

clang/lib/Interpreter/IncrementalExecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ IncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
118118
return SymOrErr->getAddress();
119119
}
120120

121-
} // namespace clang
121+
} // end namespace clang

0 commit comments

Comments
 (0)