Skip to content

[CIR][CUDA] Fix destructor behaviour #1422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ bool CIRGenModule::MayBeEmittedEagerly(const ValueDecl *global) {
if (fd) {
// Implicit template instantiations may change linkage if they are later
// explicitly instantiated, so they should not be emitted eagerly.
// TODO(cir): do we care?
assert(fd->getTemplateSpecializationKind() != TSK_ImplicitInstantiation &&
"not implemented");
if (fd->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
return false;

assert(!fd->isTemplated() && "Templates NYI");
}
const auto *vd = dyn_cast<VarDecl>(global);
Expand Down
23 changes: 17 additions & 6 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) {
cir::GlobalLinkageKind::InternalLinkage);

// Move over the initialzation code of the ctor region.
auto &block = op.getCtorRegion().front();
mlir::Block *entryBB = f.addEntryBlock();
entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
block.begin(), std::prev(block.end()));
if (!op.getCtorRegion().empty()) {
auto &block = op.getCtorRegion().front();
entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
block.begin(), std::prev(block.end()));
}

// Register the destructor call with __cxa_atexit
auto &dtorRegion = op.getDtorRegion();
Expand Down Expand Up @@ -294,9 +296,18 @@ FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) {

// Replace cir.yield with cir.return
builder.setInsertionPointToEnd(entryBB);
auto &yieldOp = block.getOperations().back();
assert(isa<YieldOp>(yieldOp));
builder.create<ReturnOp>(yieldOp.getLoc());
mlir::Operation *yieldOp = nullptr;
if (!op.getCtorRegion().empty()) {
auto &block = op.getCtorRegion().front();
yieldOp = &block.getOperations().back();
} else {
assert(!dtorRegion.empty());
auto &block = dtorRegion.front();
yieldOp = &block.getOperations().back();
}

assert(isa<YieldOp>(*yieldOp));
builder.create<ReturnOp>(yieldOp->getLoc());
return f;
}

Expand Down
27 changes: 27 additions & 0 deletions clang/test/CIR/CodeGen/CUDA/destructor.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "../Inputs/cuda.h"

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
// RUN: -x cuda -emit-cir -target-sdk-version=12.3 \
// RUN: %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
// RUN: -fcuda-is-device -emit-cir -target-sdk-version=12.3 \
// RUN: %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-DEVICE --input-file=%t.cir %s

// Make sure we do emit device-side kernel even if it's only referenced
// by the destructor of a variable not present on device.
template<typename T> __global__ void f(T) {}
template<typename T> struct A {
~A() { f<<<1, 1>>>(T()); }
};

// CIR-DEVICE: cir.func @_Z1fIiEvT_

// CIR-HOST: cir.func {{.*}} @_ZN1AIiED2Ev{{.*}} {
// CIR-HOST: cir.call @__cudaPushCallConfiguration
// CIR-HOST: cir.call @_Z16__device_stub__fIiEvT_
// CIR-HOST: }

A<int> a;
Loading