Skip to content

Commit 529b6fc

Browse files
authored
[clang-repl] Fix destructor for interpreter for the cuda negation case (#138091)
Check this error for more context (https://github.com/compiler-research/CppInterOp/actions/runs/14749797085/job/41407625681?pr=491#step:10:531) This fails with ``` * thread #1, name = 'CppInterOpTests', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0x55500356d6d3) * frame #0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99 frame #1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830 frame #2: 0x00007fffee20917a libclangCppInterOp.so.21.0gitstd::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 58 frame #3: 0x00007fffee224796 libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 838 frame #4: 0x00007fffee22494d libclangCppInterOp.so.21.0gitclang::CompilerInstance::~CompilerInstance() + 13 frame #5: 0x00007fffed95ec62 libclangCppInterOp.so.21.0gitclang::IncrementalCUDADeviceParser::~IncrementalCUDADeviceParser() + 98 frame #6: 0x00007fffed9551b6 libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 102 frame #7: 0x00007fffed95598d libclangCppInterOp.so.21.0gitclang::Interpreter::~Interpreter() + 13 frame #8: 0x00007fffed9181e7 libclangCppInterOp.so.21.0gitcompat::createClangInterpreter(std::vector<char const*, std::allocator<char const*>>&) + 2919 ``` Problem : 1) The destructor currently handles no clearance for the DeviceParser and the DeviceAct. We currently only have this https://github.com/llvm/llvm-project/blob/976493822443c52a71ed3c67aaca9a555b20c55d/clang/lib/Interpreter/Interpreter.cpp#L416-L419 2) The ownership for DeviceCI currently is present in IncrementalCudaDeviceParser. But this should be similar to how the combination for hostCI, hostAction and hostParser are managed by the Interpreter. As on master the DeviceAct and DeviceParser are managed by the Interpreter but not DeviceCI. This is problematic because : IncrementalParser holds a Sema& which points into the DeviceCI. On master, DeviceCI is destroyed before the base class ~IncrementalParser() runs, causing Parser::reset() to access a dangling Sema (and as Sema holds a reference to Preprocessor which owns PragmaNamespace) we see this ``` * frame #0: 0x00007fffee41cfe3 libclangCppInterOp.so.21.0gitclang::PragmaNamespace::~PragmaNamespace() + 99 frame #1: 0x00007fffee435666 libclangCppInterOp.so.21.0gitclang::Preprocessor::~Preprocessor() + 3830 ```
1 parent 0f6b671 commit 529b6fc

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

clang/include/clang/Interpreter/Interpreter.h

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class Interpreter {
116116
/// Compiler instance performing the incremental compilation.
117117
std::unique_ptr<CompilerInstance> CI;
118118

119+
/// An optional compiler instance for CUDA offloading
120+
std::unique_ptr<CompilerInstance> DeviceCI;
121+
119122
protected:
120123
// Derived classes can use an extended interface of the Interpreter.
121124
Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,

clang/lib/Interpreter/DeviceOffload.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
namespace clang {
2626

2727
IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
28-
std::unique_ptr<CompilerInstance> DeviceInstance,
29-
CompilerInstance &HostInstance,
28+
CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
3029
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
3130
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
32-
: IncrementalParser(*DeviceInstance, Err), PTUs(PTUs), VFS(FS),
31+
: IncrementalParser(DeviceInstance, Err), PTUs(PTUs), VFS(FS),
3332
CodeGenOpts(HostInstance.getCodeGenOpts()),
34-
TargetOpts(DeviceInstance->getTargetOpts()) {
33+
TargetOpts(DeviceInstance.getTargetOpts()) {
3534
if (Err)
3635
return;
3736
StringRef Arch = TargetOpts.CPU;
@@ -41,7 +40,6 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
4140
llvm::inconvertibleErrorCode()));
4241
return;
4342
}
44-
DeviceCI = std::move(DeviceInstance);
4543
}
4644

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

clang/lib/Interpreter/DeviceOffload.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
2828

2929
public:
3030
IncrementalCUDADeviceParser(
31-
std::unique_ptr<CompilerInstance> DeviceInstance,
32-
CompilerInstance &HostInstance,
31+
CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
3332
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
3433
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
3534

@@ -42,7 +41,6 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
4241
~IncrementalCUDADeviceParser();
4342

4443
protected:
45-
std::unique_ptr<CompilerInstance> DeviceCI;
4644
int SMVersion;
4745
llvm::SmallString<1024> PTXCode;
4846
llvm::SmallVector<char, 1024> FatbinContent;

clang/lib/Interpreter/Interpreter.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
416416
Interpreter::~Interpreter() {
417417
IncrParser.reset();
418418
Act->FinalizeAction();
419+
if (DeviceParser)
420+
DeviceParser.reset();
421+
if (DeviceAct)
422+
DeviceAct->FinalizeAction();
419423
if (IncrExecutor) {
420424
if (llvm::Error Err = IncrExecutor->cleanUp())
421425
llvm::report_fatal_error(
@@ -501,8 +505,11 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
501505

502506
DCI->ExecuteAction(*Interp->DeviceAct);
503507

508+
Interp->DeviceCI = std::move(DCI);
509+
504510
auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
505-
std::move(DCI), *Interp->getCompilerInstance(), IMVFS, Err, Interp->PTUs);
511+
*Interp->DeviceCI, *Interp->getCompilerInstance(), IMVFS, Err,
512+
Interp->PTUs);
506513

507514
if (Err)
508515
return std::move(Err);

0 commit comments

Comments
 (0)