Skip to content

Commit 3489153

Browse files
committed
[flang][cuda] Materialize box when needed
1 parent 91af15b commit 3489153

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
@@ -665,6 +665,16 @@ struct CUFDataTransferOpConversion
665665
return mlir::success();
666666
}
667667

668+
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
669+
if (mlir::isa<fir::EmboxOp, fir::ReboxOp>(val.getDefiningOp())) {
670+
// Materialize the box to memory to be able to call the runtime.
671+
mlir::Value box = builder.createTemporary(loc, val.getType());
672+
builder.create<fir::StoreOp>(loc, val, box);
673+
return box;
674+
}
675+
return val;
676+
};
677+
668678
// Conversion of data transfer involving at least one descriptor.
669679
if (mlir::isa<fir::BaseBoxType>(dstTy)) {
670680
// Transfer to a descriptor.
@@ -682,15 +692,7 @@ struct CUFDataTransferOpConversion
682692
func = fir::runtime::getRuntimeFunc<mkRTKey(CUFDataTransferCstDesc)>(
683693
loc, builder);
684694
}
685-
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
686-
if (mlir::isa<fir::EmboxOp, fir::ReboxOp>(val.getDefiningOp())) {
687-
// Materialize the box to memory to be able to call the runtime.
688-
mlir::Value box = builder.createTemporary(loc, val.getType());
689-
builder.create<fir::StoreOp>(loc, val, box);
690-
return box;
691-
}
692-
return val;
693-
};
695+
694696
src = materializeBoxIfNeeded(src);
695697
dst = materializeBoxIfNeeded(dst);
696698

@@ -705,6 +707,7 @@ struct CUFDataTransferOpConversion
705707
} else {
706708
// Transfer from a descriptor.
707709
mlir::Value dst = emboxDst(rewriter, op, symtab);
710+
mlir::Value src = materializeBoxIfNeeded(op.getSrc());
708711

709712
mlir::func::FuncOp func = fir::runtime::getRuntimeFunc<mkRTKey(
710713
CUFDataTransferDescDescNoRealloc)>(loc, builder);
@@ -713,9 +716,8 @@ struct CUFDataTransferOpConversion
713716
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
714717
mlir::Value sourceLine =
715718
fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
716-
llvm::SmallVector<mlir::Value> args{
717-
fir::runtime::createArguments(builder, loc, fTy, dst, op.getSrc(),
718-
modeValue, sourceFile, sourceLine)};
719+
llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(
720+
builder, loc, fTy, dst, src, modeValue, sourceFile, sourceLine)};
719721
builder.create<fir::CallOp>(loc, func, args);
720722
rewriter.eraseOp(op);
721723
}

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)