Skip to content

Commit 012fad9

Browse files
authored
[flang][cuda] Materialize the box in memory when dst is emboxed (#116320)
Similar to #116289 but for the dst.
1 parent e8469f1 commit 012fad9

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,15 +639,20 @@ struct CUFDataTransferOpConversion
639639
loc, builder);
640640
mlir::Value dst = op.getDst();
641641
mlir::Value src = op.getSrc();
642-
643642
if (!mlir::isa<fir::BaseBoxType>(srcTy)) {
644643
src = emboxSrc(rewriter, op, symtab);
645-
} else if (mlir::isa<fir::EmboxOp>(src.getDefiningOp())) {
646-
// Materialize the box to memory to be able to call the runtime.
647-
mlir::Value box = builder.createTemporary(loc, src.getType());
648-
builder.create<fir::StoreOp>(loc, src, box);
649-
src = box;
650644
}
645+
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
646+
if (mlir::isa<fir::EmboxOp>(val.getDefiningOp())) {
647+
// Materialize the box to memory to be able to call the runtime.
648+
mlir::Value box = builder.createTemporary(loc, val.getType());
649+
builder.create<fir::StoreOp>(loc, val, box);
650+
return box;
651+
}
652+
return val;
653+
};
654+
src = materializeBoxIfNeeded(src);
655+
dst = materializeBoxIfNeeded(dst);
651656

652657
auto fTy = func.getFunctionType();
653658
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);

flang/test/Fir/CUDA/cuda-data-transfer.fir

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ func.func @_QPdevmul(%arg0: !fir.ref<!fir.array<1x?xf32>> {fir.bindc_name = "b"}
419419
%9 = fir.convert %8 : (i32) -> index
420420
%12 = fir.shape %c1, %9 : (index, index) -> !fir.shape<2>
421421
%13 = fir.declare %arg0(%12) dummy_scope %0 {uniq_name = "_QFdevmulEb"} : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.dscope) -> !fir.ref<!fir.array<1x?xf32>>
422-
423422
%24 = fir.load %7 : !fir.ref<i32>
424423
%25 = fir.convert %24 : (i32) -> index
425424
%26 = arith.cmpi sgt, %25, %c0 : index
@@ -433,15 +432,20 @@ func.func @_QPdevmul(%arg0: !fir.ref<!fir.array<1x?xf32>> {fir.bindc_name = "b"}
433432
%34 = fir.slice %c1, %25, %c1, %c1, %29, %c1 : (index, index, index, index, index, index) -> !fir.slice<2>
434433
%35 = fir.embox %13(%12) [%34] : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
435434
cuf.data_transfer %35 to %6 {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.box<!fir.array<?x?xf32>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>
435+
cuf.data_transfer %6 to %35 {transfer_kind = #cuf.cuda_transfer<device_host>} : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, !fir.box<!fir.array<?x?xf32>>
436436
return
437437
}
438438

439439
// CHECK-LABEL: func.func @_QPdevmul(%arg0: !fir.ref<!fir.array<1x?xf32>> {fir.bindc_name = "b"}, %arg1: !fir.ref<i32> {fir.bindc_name = "wa"}, %arg2: !fir.ref<i32> {fir.bindc_name = "wb"}) {
440-
// CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
440+
// CHECK: %[[ALLOCA0:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
441+
// CHECK: %[[ALLOCA1:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
441442
// CHECK: %[[EMBOX:.*]] = fir.embox %{{.*}}(%{{.*}}) [%{{.*}}] : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
442-
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
443-
// CHECK: %[[SRC:.*]] = fir.convert %[[ALLOCA]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
443+
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA1]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
444+
// CHECK: %[[SRC:.*]] = fir.convert %[[ALLOCA1]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
444445
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%{{.*}}, %[[SRC]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
446+
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA0]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
447+
// CHECK: %[[DST:.*]] = fir.convert %[[ALLOCA0]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
448+
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%[[DST]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
445449

446450

447451
} // end of module

0 commit comments

Comments
 (0)