Skip to content

Commit c730d87

Browse files
clementvalGeorgeARM
authored andcommitted
[flang][cuda] Only copy global that have effective use (llvm#137890)
1 parent 9c679fa commit c730d87

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

flang/lib/Optimizer/Transforms/CUFDeviceGlobal.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ static void processAddrOfOp(fir::AddrOfOp addrOfOp,
3232
mlir::SymbolTable &symbolTable,
3333
llvm::DenseSet<fir::GlobalOp> &candidates,
3434
bool recurseInGlobal) {
35+
36+
// Check if there is a real use of the global.
37+
if (addrOfOp.getOperation()->hasOneUse()) {
38+
mlir::OpOperand &addrUse = *addrOfOp.getOperation()->getUses().begin();
39+
if (mlir::isa<fir::DeclareOp>(addrUse.getOwner()) &&
40+
addrUse.getOwner()->use_empty())
41+
return;
42+
}
43+
3544
if (auto globalOp = symbolTable.lookup<fir::GlobalOp>(
3645
addrOfOp.getSymbol().getRootReference().getValue())) {
3746
// TO DO: limit candidates to non-scalars. Scalars appear to have been

flang/test/Fir/CUDA/cuda-implicit-device-global.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,16 @@ // attributes(global) subroutine kernel4()
329329
// CHECK-LABEL: fir.global internal @_QFkernel4Ea : i32
330330
// CHECK-LABEL: gpu.module @cuda_device_mod
331331
// CHECK: fir.global internal @_QFkernel4Ea : i32
332+
333+
// -----
334+
335+
fir.global @_QMiso_c_bindingECc_alert constant : !fir.char<1>
336+
func.func @_QMcudafor_lib_internalsPfoo() attributes {cuf.proc_attr = #cuf.cuda_proc<global>} {
337+
%19 = fir.address_of(@_QMiso_c_bindingECc_alert) : !fir.ref<!fir.char<1>>
338+
%c1 = arith.constant 1 : index
339+
%20 = fir.declare %19 typeparams %c1 {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QMiso_c_bindingECc_alert"} : (!fir.ref<!fir.char<1>>, index) -> !fir.ref<!fir.char<1>>
340+
return
341+
}
342+
343+
// CHECK-LABEL: gpu.module @cuda_device_mod
344+
// CHECK-NOT: _QMiso_c_bindingECc_alert

0 commit comments

Comments
 (0)