Skip to content

Commit 546c2df

Browse files
clementvalLukacma
authored andcommitted
[flang][cuda] Do not convert fir.declare op with no user (llvm#164492)
1 parent 76fa821 commit 546c2df

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ struct DeclareOpConversion : public mlir::OpRewritePattern<fir::DeclareOp> {
454454
mlir::LogicalResult
455455
matchAndRewrite(fir::DeclareOp op,
456456
mlir::PatternRewriter &rewriter) const override {
457+
if (op.getResult().getUsers().empty())
458+
return success();
457459
if (auto addrOfOp = op.getMemref().getDefiningOp<fir::AddrOfOp>()) {
458460
if (auto global = symTab.lookup<fir::GlobalOp>(
459461
addrOfOp.getSymbol().getRootReference().getValue())) {
@@ -963,6 +965,8 @@ class CUFOpConversion : public fir::impl::CUFOpConversionBase<CUFOpConversion> {
963965
}
964966

965967
target.addDynamicallyLegalOp<fir::DeclareOp>([&](fir::DeclareOp op) {
968+
if (op.getResult().getUsers().empty())
969+
return true;
966970
if (inDeviceContext(op))
967971
return true;
968972
if (auto addrOfOp = op.getMemref().getDefiningOp<fir::AddrOfOp>()) {

flang/test/Fir/CUDA/cuda-global-addr.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<f80, dense<128> :
6363

6464
// We cannot call _FortranACUFGetDeviceAddress on a constant global.
6565
// There is no symbol for it and the call would result into an unresolved reference.
66+
// CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "arraysize"}
6667
// CHECK-NOT: fir.call {{.*}}GetDeviceAddress
6768

6869
// -----
@@ -90,3 +91,22 @@ func.func @_QQmain() attributes {fir.bindc_name = "test"} {
9091
// CHECK-NOT: fir.call {{.*}}GetDeviceAddress
9192

9293
}
94+
95+
// -----
96+
97+
// Check that we do not introduce call to _FortranACUFGetDeviceAddress when the
98+
// value has no user.
99+
100+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<f80, dense<128> : vector<2xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<272>, dense<64> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<271>, dense<32> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<270>, dense<32> : vector<4xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<i16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i8, dense<8> : vector<2xi64>>, #dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<"dlti.endianness", "little">, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>>} {
101+
func.func @_QQmain() attributes {fir.bindc_name = "T"} {
102+
%0 = fir.dummy_scope : !fir.dscope
103+
%1 = fir.address_of(@_QMcon2Ezzz) : !fir.ref<i32>
104+
%2 = fir.declare %1 {data_attr = #cuf.cuda<constant>, uniq_name = "_QMcon2Ezzz"} : (!fir.ref<i32>) -> !fir.ref<i32>
105+
return
106+
}
107+
fir.global @_QMcon2Ezzz {data_attr = #cuf.cuda<constant>} : i32
108+
}
109+
110+
// CHECK-LABEL: func.func @_QQmain()
111+
// CHECK: fir.address_of(@_QMcon2Ezzz) : !fir.ref<i32>
112+
// CHECK-NOT: fir.call {{.*}}GetDeviceAddress

0 commit comments

Comments
 (0)