Skip to content

Commit 1c02399

Browse files
committed
[flang][cuda] Materialize the box in memory when dst is emboxed
1 parent 4f2651c commit 1c02399

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
@@ -640,15 +640,20 @@ struct CUFDataTransferOpConversion
640640
loc, builder);
641641
mlir::Value dst = op.getDst();
642642
mlir::Value src = op.getSrc();
643-
644643
if (!mlir::isa<fir::BaseBoxType>(srcTy)) {
645644
src = emboxSrc(rewriter, op, symtab);
646-
} else if (mlir::isa<fir::EmboxOp>(src.getDefiningOp())) {
647-
// Materialize the box to memory to be able to call the runtime.
648-
mlir::Value box = builder.createTemporary(loc, src.getType());
649-
builder.create<fir::StoreOp>(loc, src, box);
650-
src = box;
651645
}
646+
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
647+
if (mlir::isa<fir::EmboxOp>(val.getDefiningOp())) {
648+
// Materialize the box to memory to be able to call the runtime.
649+
mlir::Value box = builder.createTemporary(loc, val.getType());
650+
builder.create<fir::StoreOp>(loc, val, box);
651+
return box;
652+
}
653+
return val;
654+
};
655+
src = materializeBoxIfNeeded(src);
656+
dst = materializeBoxIfNeeded(dst);
652657

653658
auto fTy = func.getFunctionType();
654659
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
@@ -400,7 +400,6 @@ func.func @_QPdevmul(%arg0: !fir.ref<!fir.array<1x?xf32>> {fir.bindc_name = "b"}
400400
%9 = fir.convert %8 : (i32) -> index
401401
%12 = fir.shape %c1, %9 : (index, index) -> !fir.shape<2>
402402
%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>>
403-
404403
%24 = fir.load %7 : !fir.ref<i32>
405404
%25 = fir.convert %24 : (i32) -> index
406405
%26 = arith.cmpi sgt, %25, %c0 : index
@@ -414,14 +413,19 @@ func.func @_QPdevmul(%arg0: !fir.ref<!fir.array<1x?xf32>> {fir.bindc_name = "b"}
414413
%34 = fir.slice %c1, %25, %c1, %c1, %29, %c1 : (index, index, index, index, index, index) -> !fir.slice<2>
415414
%35 = fir.embox %13(%12) [%34] : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
416415
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>>>>
416+
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>>
417417
return
418418
}
419419

420420
// 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"}) {
421-
// CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
421+
// CHECK: %[[ALLOCA0:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
422+
// CHECK: %[[ALLOCA1:.*]] = fir.alloca !fir.box<!fir.array<?x?xf32>>
422423
// CHECK: %[[EMBOX:.*]] = fir.embox %{{.*}}(%{{.*}}) [%{{.*}}] : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
423-
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
424-
// CHECK: %[[SRC:.*]] = fir.convert %[[ALLOCA]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
424+
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA1]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
425+
// CHECK: %[[SRC:.*]] = fir.convert %[[ALLOCA1]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
425426
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%{{.*}}, %[[SRC]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
427+
// CHECK: fir.store %[[EMBOX]] to %[[ALLOCA0]] : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
428+
// CHECK: %[[DST:.*]] = fir.convert %[[ALLOCA0]] : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
429+
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%[[DST]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
426430

427431
} // end of module

0 commit comments

Comments
 (0)