Skip to content

Commit 87ca1f6

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

File tree

4 files changed

+75
-43
lines changed

4 files changed

+75
-43
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class Interpreter {
9595
// An optional parser for CUDA offloading
9696
std::unique_ptr<IncrementalParser> DeviceParser;
9797

98+
// An optional action for CUDA offloading
99+
std::unique_ptr<IncrementalAction> DeviceAct;
100+
98101
/// List containing information about each incrementally parsed piece of code.
99102
std::list<PartialTranslationUnit> PTUs;
100103

@@ -175,10 +178,11 @@ class Interpreter {
175178
llvm::Expected<Expr *> ExtractValueFromExpr(Expr *E);
176179
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
177180

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

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

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
2828
std::unique_ptr<CompilerInstance> DeviceInstance,
2929
CompilerInstance &HostInstance,
3030
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
31-
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
31+
llvm::Error &Err, 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)) {
39+
DeviceInstance.release();
4040
Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
4141
"Invalid CUDA architecture",
4242
llvm::inconvertibleErrorCode()));
4343
return;
4444
}
45+
DeviceCI = std::move(DeviceInstance);
4546
}
4647

4748
llvm::Expected<TranslationUnitDecl *>
@@ -50,25 +51,6 @@ IncrementalCUDADeviceParser::Parse(llvm::StringRef Input) {
5051
if (!PTU)
5152
return PTU.takeError();
5253

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-
7254
return PTU;
7355
}
7456

@@ -172,6 +154,19 @@ llvm::Error IncrementalCUDADeviceParser::GenerateFatbinary() {
172154

173155
FatbinContent.append(PTXCode.begin(), PTXCode.end());
174156

157+
auto &PTU = PTUs.back();
158+
159+
std::string FatbinFileName = "/" + PTU.TheModule->getName().str() + ".fatbin";
160+
161+
VFS->addFile(FatbinFileName, 0,
162+
llvm::MemoryBuffer::getMemBuffer(
163+
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
164+
"", false));
165+
166+
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
167+
168+
FatbinContent.clear();
169+
175170
return llvm::Error::success();
176171
}
177172

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class CodeGenOptions;
2424
class TargetOptions;
2525

2626
class IncrementalCUDADeviceParser : public IncrementalParser {
27-
const std::list<PartialTranslationUnit> &PTUs;
27+
std::list<PartialTranslationUnit> &PTUs;
2828

2929
public:
3030
IncrementalCUDADeviceParser(
3131
std::unique_ptr<CompilerInstance> DeviceInstance,
3232
CompilerInstance &HostInstance,
3333
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
34-
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
34+
llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
3535

3636
llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input) override;
3737

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 49 additions & 16 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
@@ -558,8 +574,22 @@ Interpreter::Parse(llvm::StringRef Code) {
558574
// included in the host compilation
559575
if (DeviceParser) {
560576
llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse(Code);
561-
if (auto E = DeviceTU.takeError())
577+
if (auto E = DeviceTU.takeError()) {
562578
return std::move(E);
579+
}
580+
581+
auto *CudaParser =
582+
llvm::cast<IncrementalCUDADeviceParser>(DeviceParser.get());
583+
584+
RegisterPTU(*DeviceTU, nullptr, DeviceAct.get());
585+
586+
llvm::Expected<llvm::StringRef> PTX = CudaParser->GeneratePTX();
587+
if (!PTX)
588+
return PTX.takeError();
589+
590+
llvm::Error Err = CudaParser->GenerateFatbinary();
591+
if (Err)
592+
return std::move(Err);
563593
}
564594

565595
// Tell the interpreter sliently ignore unused expressions since value
@@ -736,9 +766,10 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
736766
return llvm::Error::success();
737767
}
738768

739-
std::unique_ptr<llvm::Module> Interpreter::GenModule() {
769+
std::unique_ptr<llvm::Module>
770+
Interpreter::GenModule(IncrementalAction *Action) {
740771
static unsigned ID = 0;
741-
if (CodeGenerator *CG = getCodeGen()) {
772+
if (CodeGenerator *CG = getCodeGen(Action)) {
742773
// Clang's CodeGen is designed to work with a single llvm::Module. In many
743774
// cases for convenience various CodeGen parts have a reference to the
744775
// llvm::Module (TheModule or Module) which does not change when a new
@@ -760,8 +791,10 @@ std::unique_ptr<llvm::Module> Interpreter::GenModule() {
760791
return nullptr;
761792
}
762793

763-
CodeGenerator *Interpreter::getCodeGen() const {
764-
FrontendAction *WrappedAct = Act->getWrapped();
794+
CodeGenerator *Interpreter::getCodeGen(IncrementalAction *Action) const {
795+
if (!Action)
796+
Action = Act.get();
797+
FrontendAction *WrappedAct = Action->getWrapped();
765798
if (!WrappedAct->hasIRSupport())
766799
return nullptr;
767800
return static_cast<CodeGenAction *>(WrappedAct)->getCodeGenerator();

0 commit comments

Comments
 (0)