Skip to content

Commit 98daf22

Browse files
authored
[flang][cuda] Materialize the box in memory when src is emboxed (#116289)
1 parent 17bc738 commit 98daf22

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,14 @@ struct CUFDataTransferOpConversion
641641
mlir::Value dst = op.getDst();
642642
mlir::Value src = op.getSrc();
643643

644-
if (!mlir::isa<fir::BaseBoxType>(srcTy))
644+
if (!mlir::isa<fir::BaseBoxType>(srcTy)) {
645645
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;
651+
}
646652

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

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,43 @@ func.func @_QPdevice_addr_conv() {
385385
// CHECK: fir.embox %[[DEV_ADDR_CONV]](%{{.*}}) : (!fir.ref<!fir.array<4xf32>>, !fir.shape<1>) -> !fir.box<!fir.array<4xf32>>
386386
// CHECK: fir.call @_FortranACUFDataTransferDescDescNoRealloc
387387

388+
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"}) {
389+
%c0_i64 = arith.constant 0 : i64
390+
%c1_i32 = arith.constant 1 : i32
391+
%c0_i32 = arith.constant 0 : i32
392+
%c1 = arith.constant 1 : index
393+
%c0 = arith.constant 0 : index
394+
%0 = fir.dummy_scope : !fir.dscope
395+
%1 = fir.declare %arg2 dummy_scope %0 {uniq_name = "_QFdevmulEwb"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32>
396+
%2 = cuf.alloc !fir.box<!fir.heap<!fir.array<?x?xf32>>> {bindc_name = "bdev", data_attr = #cuf.cuda<device>, uniq_name = "_QFdevmulEbdev"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>
397+
%6 = fir.declare %2 {data_attr = #cuf.cuda<device>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFdevmulEbdev"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>
398+
%7 = fir.declare %arg1 dummy_scope %0 {uniq_name = "_QFdevmulEwa"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32>
399+
%8 = fir.load %1 : !fir.ref<i32>
400+
%9 = fir.convert %8 : (i32) -> index
401+
%12 = fir.shape %c1, %9 : (index, index) -> !fir.shape<2>
402+
%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+
404+
%24 = fir.load %7 : !fir.ref<i32>
405+
%25 = fir.convert %24 : (i32) -> index
406+
%26 = arith.cmpi sgt, %25, %c0 : index
407+
%27 = arith.select %26, %25, %c0 : index
408+
%28 = fir.load %1 : !fir.ref<i32>
409+
%29 = fir.convert %28 : (i32) -> index
410+
%30 = arith.cmpi sgt, %29, %c0 : index
411+
%31 = arith.select %30, %29, %c0 : index
412+
%32 = fir.shape %27, %31 : (index, index) -> !fir.shape<2>
413+
%33 = fir.undefined index
414+
%34 = fir.slice %c1, %25, %c1, %c1, %29, %c1 : (index, index, index, index, index, index) -> !fir.slice<2>
415+
%35 = fir.embox %13(%12) [%34] : (!fir.ref<!fir.array<1x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
416+
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>>>>
417+
return
418+
}
419+
420+
// 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>>
422+
// 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>>
425+
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%{{.*}}, %[[SRC]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
426+
388427
} // end of module

0 commit comments

Comments
 (0)