Skip to content

Commit b582596

Browse files
authored
[flang][cuda] Materialize box when needed (#117810)
Materialize the box when the src comes from a embox or rebox operation. This was done in the case of transfer to a descriptor but not when transferring from a descriptor.
1 parent fad5ed6 commit b582596

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ struct CUFDataTransferOpConversion
668668
return mlir::success();
669669
}
670670

671+
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
672+
if (mlir::isa<fir::EmboxOp, fir::ReboxOp>(val.getDefiningOp())) {
673+
// Materialize the box to memory to be able to call the runtime.
674+
mlir::Value box = builder.createTemporary(loc, val.getType());
675+
builder.create<fir::StoreOp>(loc, val, box);
676+
return box;
677+
}
678+
return val;
679+
};
680+
671681
// Conversion of data transfer involving at least one descriptor.
672682
if (mlir::isa<fir::BaseBoxType>(dstTy)) {
673683
// Transfer to a descriptor.
@@ -685,15 +695,7 @@ struct CUFDataTransferOpConversion
685695
func = fir::runtime::getRuntimeFunc<mkRTKey(CUFDataTransferCstDesc)>(
686696
loc, builder);
687697
}
688-
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
689-
if (mlir::isa<fir::EmboxOp, fir::ReboxOp>(val.getDefiningOp())) {
690-
// Materialize the box to memory to be able to call the runtime.
691-
mlir::Value box = builder.createTemporary(loc, val.getType());
692-
builder.create<fir::StoreOp>(loc, val, box);
693-
return box;
694-
}
695-
return val;
696-
};
698+
697699
src = materializeBoxIfNeeded(src);
698700
dst = materializeBoxIfNeeded(dst);
699701

@@ -708,6 +710,7 @@ struct CUFDataTransferOpConversion
708710
} else {
709711
// Transfer from a descriptor.
710712
mlir::Value dst = emboxDst(rewriter, op, symtab);
713+
mlir::Value src = materializeBoxIfNeeded(op.getSrc());
711714

712715
mlir::func::FuncOp func = fir::runtime::getRuntimeFunc<mkRTKey(
713716
CUFDataTransferDescDescNoRealloc)>(loc, builder);
@@ -716,9 +719,8 @@ struct CUFDataTransferOpConversion
716719
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
717720
mlir::Value sourceLine =
718721
fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
719-
llvm::SmallVector<mlir::Value> args{
720-
fir::runtime::createArguments(builder, loc, fTy, dst, op.getSrc(),
721-
modeValue, sourceFile, sourceLine)};
722+
llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(
723+
builder, loc, fTy, dst, src, modeValue, sourceFile, sourceLine)};
722724
builder.create<fir::CallOp>(loc, func, args);
723725
rewriter.eraseOp(op);
724726
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,27 @@ func.func @_QPsrc_cst() {
553553
// CHECK: %[[CONV:.*]] = fir.convert %[[ALLOCA]] : (!fir.ref<f32>) -> !fir.llvm_ptr<i8>
554554
// CHECK: fir.call @_FortranACUFDataTransferPtrPtr(%{{.*}}, %[[CONV]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.llvm_ptr<i8>, !fir.llvm_ptr<i8>, i64, i32, !fir.ref<i8>, i32) -> none
555555

556+
func.func @_QPchecksums(%arg0: !fir.box<!fir.array<?xf64>> {cuf.data_attr = #cuf.cuda<device>, fir.bindc_name = "a"}, %arg1: !fir.ref<i32> {fir.bindc_name = "n"}) {
557+
%c0 = arith.constant 0 : index
558+
%0 = fir.dummy_scope : !fir.dscope
559+
%1 = fir.declare %arg0 dummy_scope %0 {data_attr = #cuf.cuda<device>, uniq_name = "_QFchecksumsEa"} : (!fir.box<!fir.array<?xf64>>, !fir.dscope) -> !fir.box<!fir.array<?xf64>>
560+
%2 = fir.rebox %1 : (!fir.box<!fir.array<?xf64>>) -> !fir.box<!fir.array<?xf64>>
561+
%3 = fir.declare %arg1 dummy_scope %0 {uniq_name = "_QFchecksumsEn"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32>
562+
%4 = fir.load %3 : !fir.ref<i32>
563+
%5 = fir.convert %4 : (i32) -> index
564+
%6 = arith.cmpi sgt, %5, %c0 : index
565+
%7 = arith.select %6, %5, %c0 : index
566+
%8 = fir.alloca !fir.array<?xf64>, %7 {bindc_name = "hosttmp", uniq_name = "_QFchecksumsEhosttmp"}
567+
%9 = fir.shape %7 : (index) -> !fir.shape<1>
568+
%10 = fir.declare %8(%9) {uniq_name = "_QFchecksumsEhosttmp"} : (!fir.ref<!fir.array<?xf64>>, !fir.shape<1>) -> !fir.ref<!fir.array<?xf64>>
569+
%11 = fir.embox %10(%9) : (!fir.ref<!fir.array<?xf64>>, !fir.shape<1>) -> !fir.box<!fir.array<?xf64>>
570+
cuf.data_transfer %2 to %10, %9 : !fir.shape<1> {transfer_kind = #cuf.cuda_transfer<device_host>} : !fir.box<!fir.array<?xf64>>, !fir.ref<!fir.array<?xf64>>
571+
return
572+
}
573+
574+
// CHECK-LABEL: func.func @_QPchecksums
575+
// CHECK: %[[DST:.*]] = fir.convert %{{.*}} : (!fir.ref<!fir.box<!fir.array<?xf64>>>) -> !fir.ref<!fir.box<none>>
576+
// CHECK: %[[SRC:.*]] = fir.convert %{{.*}} : (!fir.ref<!fir.box<!fir.array<?xf64>>>) -> !fir.ref<!fir.box<none>>
577+
// CHECK: fir.call @_FortranACUFDataTransferDescDescNoRealloc(%[[DST]], %[[SRC]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
578+
556579
} // end of module

0 commit comments

Comments
 (0)