Skip to content

Commit d10dab4

Browse files
committed
Fix cuda flag with clang-repl
1 parent ecbd2d5 commit d10dab4

File tree

4 files changed

+68
-51
lines changed

4 files changed

+68
-51
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CXXRecordDecl;
4141
class Decl;
4242
class IncrementalExecutor;
4343
class IncrementalParser;
44+
class IncrementalCUDADeviceParser;
4445

4546
/// Create a pre-configured \c CompilerInstance for incremental processing.
4647
class IncrementalCompilerBuilder {
@@ -93,7 +94,10 @@ class Interpreter {
9394
std::unique_ptr<IncrementalExecutor> IncrExecutor;
9495

9596
// An optional parser for CUDA offloading
96-
std::unique_ptr<IncrementalParser> DeviceParser;
97+
std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
98+
99+
// An optional action for CUDA offloading
100+
std::unique_ptr<IncrementalAction> DeviceAct;
97101

98102
/// List containing information about each incrementally parsed piece of code.
99103
std::list<PartialTranslationUnit> PTUs;
@@ -175,10 +179,11 @@ class Interpreter {
175179
llvm::Expected<Expr *> ExtractValueFromExpr(Expr *E);
176180
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
177181

178-
CodeGenerator *getCodeGen() const;
179-
std::unique_ptr<llvm::Module> GenModule();
182+
CodeGenerator *getCodeGen(IncrementalAction *Action = nullptr) const;
183+
std::unique_ptr<llvm::Module> GenModule(IncrementalAction *Action = nullptr);
180184
PartialTranslationUnit &RegisterPTU(TranslationUnitDecl *TU,
181-
std::unique_ptr<llvm::Module> M = {});
185+
std::unique_ptr<llvm::Module> M = {},
186+
IncrementalAction *Action = nullptr);
182187

183188
// A cache for the compiled destructors used to for de-allocation of managed
184189
// clang::Values.

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,17 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
3131
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
3232
: IncrementalParser(*DeviceInstance, Err), PTUs(PTUs), VFS(FS),
3333
CodeGenOpts(HostInstance.getCodeGenOpts()),
34-
TargetOpts(HostInstance.getTargetOpts()) {
34+
TargetOpts(DeviceInstance->getTargetOpts()) {
3535
if (Err)
3636
return;
37-
DeviceCI = std::move(DeviceInstance);
3837
StringRef Arch = TargetOpts.CPU;
3938
if (!Arch.starts_with("sm_") || Arch.substr(3).getAsInteger(10, SMVersion)) {
4039
Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
4140
"Invalid CUDA architecture",
4241
llvm::inconvertibleErrorCode()));
4342
return;
4443
}
45-
}
46-
47-
llvm::Expected<TranslationUnitDecl *>
48-
IncrementalCUDADeviceParser::Parse(llvm::StringRef Input) {
49-
auto PTU = IncrementalParser::Parse(Input);
50-
if (!PTU)
51-
return PTU.takeError();
52-
53-
auto PTX = GeneratePTX();
54-
if (!PTX)
55-
return PTX.takeError();
56-
57-
auto Err = GenerateFatbinary();
58-
if (Err)
59-
return std::move(Err);
60-
61-
std::string FatbinFileName =
62-
"/incr_module_" + std::to_string(PTUs.size()) + ".fatbin";
63-
VFS->addFile(FatbinFileName, 0,
64-
llvm::MemoryBuffer::getMemBuffer(
65-
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
66-
"", false));
67-
68-
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
69-
70-
FatbinContent.clear();
71-
72-
return PTU;
44+
DeviceCI = std::move(DeviceInstance);
7345
}
7446

7547
llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {
@@ -172,6 +144,19 @@ llvm::Error IncrementalCUDADeviceParser::GenerateFatbinary() {
172144

173145
FatbinContent.append(PTXCode.begin(), PTXCode.end());
174146

147+
const PartialTranslationUnit &PTU = PTUs.back();
148+
149+
std::string FatbinFileName = "/" + PTU.TheModule->getName().str() + ".fatbin";
150+
151+
VFS->addFile(FatbinFileName, 0,
152+
llvm::MemoryBuffer::getMemBuffer(
153+
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
154+
"", false));
155+
156+
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
157+
158+
FatbinContent.clear();
159+
175160
return llvm::Error::success();
176161
}
177162

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
3333
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
3434
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
3535

36-
llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input) override;
37-
3836
// Generate PTX for the last PTU.
3937
llvm::Expected<llvm::StringRef> GeneratePTX();
4038

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -481,20 +481,34 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
481481
OverlayVFS->pushOverlay(IMVFS);
482482
CI->createFileManager(OverlayVFS);
483483

484-
auto Interp = Interpreter::create(std::move(CI));
485-
if (auto E = Interp.takeError())
486-
return std::move(E);
484+
llvm::Expected<std::unique_ptr<Interpreter>> InterpOrErr =
485+
Interpreter::create(std::move(CI));
486+
if (!InterpOrErr)
487+
return InterpOrErr;
488+
489+
std::unique_ptr<Interpreter> Interp = std::move(*InterpOrErr);
487490

488491
llvm::Error Err = llvm::Error::success();
489-
auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
490-
std::move(DCI), *(*Interp)->getCompilerInstance(), IMVFS, Err,
491-
(*Interp)->PTUs);
492+
llvm::LLVMContext &LLVMCtx = *Interp->TSCtx->getContext();
493+
494+
auto DeviceAct =
495+
std::make_unique<IncrementalAction>(*DCI, LLVMCtx, Err, *Interp);
496+
492497
if (Err)
493498
return std::move(Err);
494499

495-
(*Interp)->DeviceParser = std::move(DeviceParser);
500+
Interp->DeviceAct = std::move(DeviceAct);
501+
502+
DCI->ExecuteAction(*Interp->DeviceAct);
503+
504+
auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
505+
std::move(DCI), *Interp->getCompilerInstance(), IMVFS, Err, Interp->PTUs);
506+
507+
if (Err)
508+
return std::move(Err);
496509

497-
return Interp;
510+
Interp->DeviceParser = std::move(DeviceParser);
511+
return std::move(Interp);
498512
}
499513

500514
const CompilerInstance *Interpreter::getCompilerInstance() const {
@@ -532,15 +546,17 @@ size_t Interpreter::getEffectivePTUSize() const {
532546

533547
PartialTranslationUnit &
534548
Interpreter::RegisterPTU(TranslationUnitDecl *TU,
535-
std::unique_ptr<llvm::Module> M /*={}*/) {
549+
std::unique_ptr<llvm::Module> M /*={}*/,
550+
IncrementalAction *Action) {
536551
PTUs.emplace_back(PartialTranslationUnit());
537552
PartialTranslationUnit &LastPTU = PTUs.back();
538553
LastPTU.TUPart = TU;
539554

540555
if (!M)
541-
M = GenModule();
556+
M = GenModule(Action);
542557

543-
assert((!getCodeGen() || M) && "Must have a llvm::Module at this point");
558+
assert((!getCodeGen(Action) || M) &&
559+
"Must have a llvm::Module at this point");
544560

545561
LastPTU.TheModule = std::move(M);
546562
LLVM_DEBUG(llvm::dbgs() << "compile-ptu " << PTUs.size() - 1
@@ -560,6 +576,16 @@ Interpreter::Parse(llvm::StringRef Code) {
560576
llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse(Code);
561577
if (auto E = DeviceTU.takeError())
562578
return std::move(E);
579+
580+
RegisterPTU(*DeviceTU, nullptr, DeviceAct.get());
581+
582+
llvm::Expected<llvm::StringRef> PTX = DeviceParser->GeneratePTX();
583+
if (!PTX)
584+
return PTX.takeError();
585+
586+
llvm::Error Err = DeviceParser->GenerateFatbinary();
587+
if (Err)
588+
return std::move(Err);
563589
}
564590

565591
// Tell the interpreter sliently ignore unused expressions since value
@@ -736,9 +762,10 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
736762
return llvm::Error::success();
737763
}
738764

739-
std::unique_ptr<llvm::Module> Interpreter::GenModule() {
765+
std::unique_ptr<llvm::Module>
766+
Interpreter::GenModule(IncrementalAction *Action) {
740767
static unsigned ID = 0;
741-
if (CodeGenerator *CG = getCodeGen()) {
768+
if (CodeGenerator *CG = getCodeGen(Action)) {
742769
// Clang's CodeGen is designed to work with a single llvm::Module. In many
743770
// cases for convenience various CodeGen parts have a reference to the
744771
// llvm::Module (TheModule or Module) which does not change when a new
@@ -760,8 +787,10 @@ std::unique_ptr<llvm::Module> Interpreter::GenModule() {
760787
return nullptr;
761788
}
762789

763-
CodeGenerator *Interpreter::getCodeGen() const {
764-
FrontendAction *WrappedAct = Act->getWrapped();
790+
CodeGenerator *Interpreter::getCodeGen(IncrementalAction *Action) const {
791+
if (!Action)
792+
Action = Act.get();
793+
FrontendAction *WrappedAct = Action->getWrapped();
765794
if (!WrappedAct->hasIRSupport())
766795
return nullptr;
767796
return static_cast<CodeGenAction *>(WrappedAct)->getCodeGenerator();

0 commit comments

Comments
 (0)