Skip to content

Commit c378780

Browse files
clementvalyuxuanchen1997
authored andcommitted
[flang][cuda] Avoid temporary when RHS is a logical constant (#99078)
Summary: Enhance the detection of constant on the RHS for logical cases so we don't create a temporary. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251681
1 parent 0b655a2 commit c378780

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,9 +4230,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
42304230
auto transferKindAttr = cuf::DataTransferKindAttr::get(
42314231
builder.getContext(), cuf::DataTransferKind::HostDevice);
42324232
if (!rhs.isVariable()) {
4233+
mlir::Value base = rhs;
4234+
if (auto convertOp =
4235+
mlir::dyn_cast<fir::ConvertOp>(rhs.getDefiningOp()))
4236+
base = convertOp.getValue();
42334237
// Special case if the rhs is a constant.
4234-
if (matchPattern(rhs.getDefiningOp(), mlir::m_Constant())) {
4235-
builder.create<cuf::DataTransferOp>(loc, rhs, lhsVal,
4238+
if (matchPattern(base.getDefiningOp(), mlir::m_Constant())) {
4239+
builder.create<cuf::DataTransferOp>(loc, base, lhsVal,
42364240
transferKindAttr);
42374241
} else {
42384242
auto associate = hlfir::genAssociateExpr(

flang/test/Lower/CUDA/cuda-data-transfer.cuf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,12 @@ end subroutine
265265
! CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<?xi32>, %14#1 {bindc_name = ".tmp", uniq_name = ""}
266266
! CHECK: cuf.data_transfer
267267
! CHECK: fir.freemem %[[TEMP]] : !fir.heap<!fir.array<?xi32>>
268+
269+
subroutine sub14()
270+
logical(4), device :: log(10)
271+
log = .true.
272+
end subroutine
273+
274+
! CHECK-LABEL: func.func @_QPsub14()
275+
! CHECK: %[[TRUE:.*]] = arith.constant true
276+
! CHECK: cuf.data_transfer %[[TRUE]] to %{{.*}}#0 {transfer_kind = #cuf.cuda_transfer<host_device>} : i1, !fir.ref<!fir.array<10x!fir.logical<4>>>

0 commit comments

Comments
 (0)