Skip to content

Commit 44cd370

Browse files
committed
move codegen related operations to incrementalaction clas
1 parent 1d773de commit 44cd370

File tree

9 files changed

+94
-73
lines changed

9 files changed

+94
-73
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class Interpreter {
111111
/// Compiler instance performing the incremental compilation.
112112
std::unique_ptr<CompilerInstance> CI;
113113

114+
/// An optional compiler instance for CUDA offloading
115+
std::unique_ptr<CompilerInstance> DeviceCI;
116+
114117
protected:
115118
// Derived classes can use an extended interface of the Interpreter.
116119
Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
namespace clang {
2626

2727
IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
28-
std::unique_ptr<CompilerInstance> DeviceInstance,
29-
CompilerInstance &HostInstance, IncrementalAction *DeviceAct,
28+
CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
29+
IncrementalAction *DeviceAct,
3030
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
3131
llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs)
32-
: IncrementalParser(*DeviceInstance, DeviceAct, Err, PTUs), VFS(FS),
32+
: IncrementalParser(DeviceInstance, DeviceAct, Err, PTUs), VFS(FS),
3333
CodeGenOpts(HostInstance.getCodeGenOpts()),
34-
TargetOpts(DeviceInstance->getTargetOpts()) {
34+
TargetOpts(DeviceInstance.getTargetOpts()) {
3535
if (Err)
3636
return;
3737
StringRef Arch = TargetOpts.CPU;
@@ -41,7 +41,6 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
4141
llvm::inconvertibleErrorCode()));
4242
return;
4343
}
44-
DeviceCI = std::move(DeviceInstance);
4544
}
4645

4746
llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
2828

2929
public:
3030
IncrementalCUDADeviceParser(
31-
std::unique_ptr<CompilerInstance> DeviceInstance,
32-
CompilerInstance &HostInstance, IncrementalAction *DeviceAct,
31+
CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
32+
IncrementalAction *DeviceAct,
3333
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
3434
llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
3535

@@ -42,7 +42,6 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
4242
~IncrementalCUDADeviceParser();
4343

4444
protected:
45-
std::unique_ptr<CompilerInstance> DeviceCI;
4645
int SMVersion;
4746
llvm::SmallString<1024> PTXCode;
4847
llvm::SmallVector<char, 1024> FatbinContent;

clang/lib/Interpreter/IncrementalAction.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
#include "clang/AST/ASTConsumer.h"
1212
#include "clang/CodeGen/CodeGenAction.h"
13+
#include "clang/CodeGen/ModuleBuilder.h"
1314
#include "clang/Frontend/CompilerInstance.h"
1415
#include "clang/Frontend/FrontendOptions.h"
1516
#include "clang/FrontendTool/Utils.h"
1617
#include "clang/Interpreter/Interpreter.h"
18+
#include "clang/Lex/PreprocessorOptions.h"
1719
#include "clang/Sema/Sema.h"
20+
#include "llvm/IR/Module.h"
1821
#include "llvm/Support/Error.h"
1922
#include "llvm/Support/ErrorHandling.h"
2023

@@ -50,7 +53,7 @@ IncrementalAction::IncrementalAction(CompilerInstance &CI,
5053
}
5154
return Act;
5255
}()),
53-
Interp(I), Consumer(std::move(Consumer)) {}
56+
Interp(I), CI(CI), Consumer(std::move(Consumer)) {}
5457

5558
std::unique_ptr<ASTConsumer>
5659
IncrementalAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
@@ -83,6 +86,45 @@ void IncrementalAction::FinalizeAction() {
8386
EndSourceFile();
8487
}
8588

89+
void IncrementalAction::CacheCodeGenModule() {
90+
CachedInCodeGenModule = GenModule();
91+
}
92+
93+
llvm::Module *IncrementalAction::getCachedCodeGenModule() const {
94+
return CachedInCodeGenModule.get();
95+
}
96+
97+
std::unique_ptr<llvm::Module> IncrementalAction::GenModule() {
98+
static unsigned ID = 0;
99+
if (CodeGenerator *CG = getCodeGen()) {
100+
// Clang's CodeGen is designed to work with a single llvm::Module. In many
101+
// cases for convenience various CodeGen parts have a reference to the
102+
// llvm::Module (TheModule or Module) which does not change when a new
103+
// module is pushed. However, the execution engine wants to take ownership
104+
// of the module which does not map well to CodeGen's design. To work this
105+
// around we created an empty module to make CodeGen happy. We should make
106+
// sure it always stays empty.
107+
assert(((!CachedInCodeGenModule ||
108+
!CI.getPreprocessorOpts().Includes.empty()) ||
109+
(CachedInCodeGenModule->empty() &&
110+
CachedInCodeGenModule->global_empty() &&
111+
CachedInCodeGenModule->alias_empty() &&
112+
CachedInCodeGenModule->ifunc_empty())) &&
113+
"CodeGen wrote to a readonly module");
114+
std::unique_ptr<llvm::Module> M(CG->ReleaseModule());
115+
CG->StartModule("incr_module_" + std::to_string(ID++), M->getContext());
116+
return M;
117+
}
118+
return nullptr;
119+
}
120+
121+
CodeGenerator *IncrementalAction::getCodeGen() const {
122+
FrontendAction *WrappedAct = getWrapped();
123+
if (!WrappedAct || !WrappedAct->hasIRSupport())
124+
return nullptr;
125+
return static_cast<CodeGenAction *>(WrappedAct)->getCodeGenerator();
126+
}
127+
86128
InProcessPrintingASTConsumer::InProcessPrintingASTConsumer(
87129
std::unique_ptr<ASTConsumer> C, Interpreter &I)
88130
: MultiplexConsumer(std::move(C)), Interp(I) {}

clang/lib/Interpreter/IncrementalAction.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
#include "clang/Frontend/FrontendActions.h"
1313
#include "clang/Frontend/MultiplexConsumer.h"
1414

15+
namespace llvm {
16+
class Module;
17+
}
18+
1519
namespace clang {
1620

1721
class Interpreter;
22+
class CodeGenerator;
1823

1924
/// A custom action enabling the incremental processing functionality.
2025
///
@@ -29,8 +34,13 @@ class IncrementalAction : public WrapperFrontendAction {
2934
private:
3035
bool IsTerminating = false;
3136
Interpreter &Interp;
37+
CompilerInstance &CI;
3238
std::unique_ptr<ASTConsumer> Consumer;
3339

40+
/// When CodeGen is created the first llvm::Module gets cached in many places
41+
/// and we must keep it alive.
42+
std::unique_ptr<llvm::Module> CachedInCodeGenModule;
43+
3444
public:
3545
IncrementalAction(CompilerInstance &CI, llvm::LLVMContext &LLVMCtx,
3646
llvm::Error &Err, Interpreter &I,
@@ -52,6 +62,18 @@ class IncrementalAction : public WrapperFrontendAction {
5262
void EndSourceFile() override;
5363

5464
void FinalizeAction();
65+
66+
/// Cache the current CodeGen module to preserve internal references.
67+
void CacheCodeGenModule();
68+
69+
/// Access the cached CodeGen module.
70+
llvm::Module *getCachedCodeGenModule() const;
71+
72+
/// Access the current code generator.
73+
CodeGenerator *getCodeGen() const;
74+
75+
/// Generate an LLVM module for the most recent parsed input.
76+
std::unique_ptr<llvm::Module> GenModule();
5577
};
5678

5779
class InProcessPrintingASTConsumer final : public MultiplexConsumer {

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
#include "IncrementalAction.h"
1515

1616
#include "clang/AST/DeclContextInternals.h"
17-
#include "clang/CodeGen/CodeGenAction.h"
18-
#include "clang/CodeGen/ModuleBuilder.h"
1917
#include "clang/Frontend/CompilerInstance.h"
20-
#include "clang/Frontend/FrontendAction.h"
2118
#include "clang/Interpreter/PartialTranslationUnit.h"
22-
#include "clang/Lex/PreprocessorOptions.h"
2319
#include "clang/Parse/Parser.h"
2420
#include "clang/Sema/Sema.h"
2521
#include "llvm/IR/Module.h"
@@ -37,10 +33,10 @@ namespace clang {
3733
IncrementalParser::IncrementalParser(CompilerInstance &Instance,
3834
IncrementalAction *Act, llvm::Error &Err,
3935
std::list<PartialTranslationUnit> &PTUs)
40-
: S(Instance.getSema()), CI(Instance), Act(Act), PTUs(PTUs) {
36+
: S(Instance.getSema()), Act(Act), PTUs(PTUs) {
4137
llvm::ErrorAsOutParameter EAO(&Err);
4238
Consumer = &S.getASTConsumer();
43-
P = std::make_unique<Parser>(S.getPreprocessor(), S, /*SkipBodies=*/false);
39+
P.reset(new Parser(S.getPreprocessor(), S, /*SkipBodies=*/false));
4440
P->Initialize();
4541
}
4642

@@ -202,9 +198,9 @@ IncrementalParser::RegisterPTU(TranslationUnitDecl *TU,
202198
LastPTU.TUPart = TU;
203199

204200
if (!M)
205-
M = GenModule();
201+
M = Act->GenModule();
206202

207-
assert((!getCodeGen() || M) && "Must have a llvm::Module at this point");
203+
assert((!Act->getCodeGen() || M) && "Must have a llvm::Module at this point");
208204

209205
LastPTU.TheModule = std::move(M);
210206
LLVM_DEBUG(llvm::dbgs() << "compile-ptu " << PTUs.size() - 1
@@ -215,36 +211,4 @@ IncrementalParser::RegisterPTU(TranslationUnitDecl *TU,
215211
LLVM_DEBUG(llvm::dbgs() << "]\n");
216212
return LastPTU;
217213
}
218-
219-
std::unique_ptr<llvm::Module> IncrementalParser::GenModule() {
220-
static unsigned ID = 0;
221-
if (CodeGenerator *CG = getCodeGen()) {
222-
// Clang's CodeGen is designed to work with a single llvm::Module. In many
223-
// cases for convenience various CodeGen parts have a reference to the
224-
// llvm::Module (TheModule or Module) which does not change when a new
225-
// module is pushed. However, the execution engine wants to take ownership
226-
// of the module which does not map well to CodeGen's design. To work this
227-
// around we created an empty module to make CodeGen happy. We should make
228-
// sure it always stays empty.
229-
assert(((!CachedInCodeGenModule ||
230-
!CI.getPreprocessorOpts().Includes.empty()) ||
231-
(CachedInCodeGenModule->empty() &&
232-
CachedInCodeGenModule->global_empty() &&
233-
CachedInCodeGenModule->alias_empty() &&
234-
CachedInCodeGenModule->ifunc_empty())) &&
235-
"CodeGen wrote to a readonly module");
236-
std::unique_ptr<llvm::Module> M(CG->ReleaseModule());
237-
CG->StartModule("incr_module_" + std::to_string(ID++), M->getContext());
238-
return M;
239-
}
240-
return nullptr;
241-
}
242-
243-
CodeGenerator *IncrementalParser::getCodeGen() const {
244-
FrontendAction *WrappedAct = Act->getWrapped();
245-
if (!WrappedAct->hasIRSupport())
246-
return nullptr;
247-
return static_cast<CodeGenAction *>(WrappedAct)->getCodeGenerator();
248-
}
249-
250214
} // end namespace clang

clang/lib/Interpreter/IncrementalParser.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Module;
2525

2626
namespace clang {
2727
class ASTConsumer;
28-
class CodeGenerator;
2928
class CompilerInstance;
3029
class Parser;
3130
class Sema;
@@ -50,9 +49,6 @@ class IncrementalParser {
5049
/// Counts the number of direct user input lines that have been parsed.
5150
unsigned InputCount = 0;
5251

53-
/// The CompilerInstance used during parsing.
54-
CompilerInstance &CI;
55-
5652
/// The FrontendAction used during incremental parsing.
5753
IncrementalAction *Act = nullptr;
5854

@@ -63,23 +59,13 @@ class IncrementalParser {
6359
llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
6460
virtual ~IncrementalParser();
6561

66-
/// When CodeGen is created the first llvm::Module gets cached in many places
67-
/// and we must keep it alive.
68-
std::unique_ptr<llvm::Module> CachedInCodeGenModule;
69-
7062
/// Parses incremental input by creating an in-memory file.
7163
///\returns a \c PartialTranslationUnit which holds information about the
7264
/// \c TranslationUnitDecl.
7365
virtual llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input);
7466

7567
void CleanUpPTU(TranslationUnitDecl *MostRecentTU);
7668

77-
/// Access the current code generator.
78-
CodeGenerator *getCodeGen() const;
79-
80-
/// Generate an LLVM module for the most recent parsed input.
81-
std::unique_ptr<llvm::Module> GenModule();
82-
8369
/// Register a PTU produced by Parse.
8470
PartialTranslationUnit &RegisterPTU(TranslationUnitDecl *TU,
8571
std::unique_ptr<llvm::Module> M = {});

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
270270
if (ErrOut)
271271
return;
272272

273-
if (IncrParser->getCodeGen()) {
274-
IncrParser->CachedInCodeGenModule = IncrParser->GenModule();
273+
if (Act->getCodeGen()) {
274+
Act->CacheCodeGenModule();
275275
// The initial PTU is filled by `-include` or by CUDA includes
276276
// automatically.
277277
if (!CI->getPreprocessorOpts().Includes.empty()) {
278278
// We can't really directly pass the CachedInCodeGenModule to the Jit
279279
// because it will steal it, causing dangling references as explained in
280280
// Interpreter::Execute
281-
auto M = llvm::CloneModule(*IncrParser->CachedInCodeGenModule);
281+
auto M = llvm::CloneModule(*Act->getCachedCodeGenModule());
282282
ASTContext &C = CI->getASTContext();
283283
IncrParser->RegisterPTU(C.getTranslationUnitDecl(), std::move(M));
284284
}
@@ -289,7 +289,7 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
289289
}
290290

291291
// Not all frontends support code-generation, e.g. ast-dump actions don't
292-
if (IncrParser->getCodeGen()) {
292+
if (Act->getCodeGen()) {
293293
// Process the PTUs that came from initialization. For example -include will
294294
// give us a header that's processed at initialization of the preprocessor.
295295
for (PartialTranslationUnit &PTU : PTUs)
@@ -303,6 +303,10 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
303303
Interpreter::~Interpreter() {
304304
IncrParser.reset();
305305
Act->FinalizeAction();
306+
if (DeviceParser)
307+
DeviceParser.reset();
308+
if (DeviceAct)
309+
DeviceAct->FinalizeAction();
306310
if (IncrExecutor) {
307311
if (llvm::Error Err = IncrExecutor->cleanUp())
308312
llvm::report_fatal_error(
@@ -388,9 +392,11 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
388392

389393
DCI->ExecuteAction(*Interp->DeviceAct);
390394

395+
Interp->DeviceCI = std::move(DCI);
396+
391397
auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
392-
std::move(DCI), *Interp->getCompilerInstance(), Interp->DeviceAct.get(),
393-
IMVFS, Err, Interp->PTUs);
398+
*Interp->DeviceCI, *Interp->getCompilerInstance(),
399+
Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs);
394400

395401
if (Err)
396402
return std::move(Err);
@@ -479,7 +485,7 @@ llvm::Error Interpreter::CreateExecutor() {
479485
return llvm::make_error<llvm::StringError>("Operation failed. "
480486
"Execution engine exists",
481487
std::error_code());
482-
if (!IncrParser->getCodeGen())
488+
if (!Act->getCodeGen())
483489
return llvm::make_error<llvm::StringError>("Operation failed. "
484490
"No code generator available",
485491
std::error_code());
@@ -559,7 +565,7 @@ Interpreter::getSymbolAddress(GlobalDecl GD) const {
559565
return llvm::make_error<llvm::StringError>("Operation failed. "
560566
"No execution engine",
561567
std::error_code());
562-
llvm::StringRef MangledName = IncrParser->getCodeGen()->GetMangledName(GD);
568+
llvm::StringRef MangledName = Act->getCodeGen()->GetMangledName(GD);
563569
return getSymbolAddress(MangledName);
564570
}
565571

clang/lib/Interpreter/InterpreterValuePrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "IncrementalParser.h"
13+
#include "IncrementalAction.h"
1414
#include "InterpreterUtils.h"
1515
#include "clang/AST/ASTContext.h"
1616
#include "clang/AST/PrettyPrinter.h"
@@ -45,7 +45,7 @@ Interpreter::CompileDtorCall(CXXRecordDecl *CXXRD) {
4545
getCompilerInstance()->getSema().LookupDestructor(CXXRD);
4646

4747
llvm::StringRef Name =
48-
IncrParser->getCodeGen()->GetMangledName(GlobalDecl(DtorRD, Dtor_Base));
48+
Act->getCodeGen()->GetMangledName(GlobalDecl(DtorRD, Dtor_Base));
4949
auto AddrOrErr = getSymbolAddress(Name);
5050
if (!AddrOrErr)
5151
return AddrOrErr.takeError();

0 commit comments

Comments
 (0)