Skip to content

Commit de2e270

Browse files
authored
[flang][cuda] Materialize box when src or dst are rebox (#116494)
1 parent 1c4caec commit de2e270

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ struct CUFDataTransferOpConversion
654654
loc, builder);
655655
}
656656
auto materializeBoxIfNeeded = [&](mlir::Value val) -> mlir::Value {
657-
if (mlir::isa<fir::EmboxOp>(val.getDefiningOp())) {
657+
if (mlir::isa<fir::EmboxOp, fir::ReboxOp>(val.getDefiningOp())) {
658658
// Materialize the box to memory to be able to call the runtime.
659659
mlir::Value box = builder.createTemporary(loc, val.getType());
660660
builder.create<fir::StoreOp>(loc, val, box);

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,51 @@ func.func @_QPlogical_cst() {
466466
// CHECK: %[[BOX_NONE:.*]] = fir.convert %[[DESC]] : (!fir.ref<!fir.box<!fir.logical<4>>>) -> !fir.ref<!fir.box<none>>
467467
// CHECK: fir.call @_FortranACUFDataTransferCstDesc(%{{.*}}, %[[BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
468468

469+
func.func @_QPcallkernel(%arg0: !fir.box<!fir.array<?x?xcomplex<f32>>> {fir.bindc_name = "a"}, %arg1: !fir.ref<f32> {fir.bindc_name = "b"}, %arg2: !fir.ref<f32> {fir.bindc_name = "c"}) {
470+
%c0_i64 = arith.constant 0 : i64
471+
%c1_i32 = arith.constant 1 : i32
472+
%c0_i32 = arith.constant 0 : i32
473+
%c1 = arith.constant 1 : index
474+
%c0 = arith.constant 0 : index
475+
%0 = fir.dummy_scope : !fir.dscope
476+
%1 = fir.declare %arg0 dummy_scope %0 {uniq_name = "_QFcallkernelEa"} : (!fir.box<!fir.array<?x?xcomplex<f32>>>, !fir.dscope) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
477+
%2 = fir.rebox %1 : (!fir.box<!fir.array<?x?xcomplex<f32>>>) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
478+
%3 = cuf.alloc !fir.box<!fir.heap<!fir.array<?x?xcomplex<f32>>>> {bindc_name = "adev", data_attr = #cuf.cuda<device>, uniq_name = "_QFcallkernelEadev"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xcomplex<f32>>>>>
479+
%7 = fir.declare %3 {data_attr = #cuf.cuda<device>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFcallkernelEadev"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xcomplex<f32>>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xcomplex<f32>>>>>
480+
%8 = fir.declare %arg1 dummy_scope %0 {uniq_name = "_QFcallkernelEb"} : (!fir.ref<f32>, !fir.dscope) -> !fir.ref<f32>
481+
%9 = fir.declare %arg2 dummy_scope %0 {uniq_name = "_QFcallkernelEc"} : (!fir.ref<f32>, !fir.dscope) -> !fir.ref<f32>
482+
%10 = fir.alloca i32 {bindc_name = "m", uniq_name = "_QFcallkernelEm"}
483+
%11 = fir.declare %10 {uniq_name = "_QFcallkernelEm"} : (!fir.ref<i32>) -> !fir.ref<i32>
484+
%12 = fir.alloca i32 {bindc_name = "n", uniq_name = "_QFcallkernelEn"}
485+
%13 = fir.declare %12 {uniq_name = "_QFcallkernelEn"} : (!fir.ref<i32>) -> !fir.ref<i32>
486+
%14:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.array<?x?xcomplex<f32>>>, index) -> (index, index, index)
487+
%15 = fir.convert %14#1 : (index) -> i32
488+
fir.store %15 to %13 : !fir.ref<i32>
489+
%16:3 = fir.box_dims %2, %c1 : (!fir.box<!fir.array<?x?xcomplex<f32>>>, index) -> (index, index, index)
490+
%27 = fir.load %13 : !fir.ref<i32>
491+
%28 = fir.convert %27 : (i32) -> index
492+
%29 = arith.cmpi sgt, %28, %c0 : index
493+
%30 = arith.select %29, %28, %c0 : index
494+
%31 = fir.load %11 : !fir.ref<i32>
495+
%32 = fir.convert %31 : (i32) -> index
496+
%33 = arith.cmpi sgt, %32, %c0 : index
497+
%34 = arith.select %33, %32, %c0 : index
498+
%35 = fir.shape %30, %34 : (index, index) -> !fir.shape<2>
499+
%36 = fir.undefined index
500+
%37 = fir.slice %c1, %28, %c1, %c1, %32, %c1 : (index, index, index, index, index, index) -> !fir.slice<2>
501+
%38 = fir.rebox %2 [%37] : (!fir.box<!fir.array<?x?xcomplex<f32>>>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
502+
cuf.data_transfer %38 to %7 {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.box<!fir.array<?x?xcomplex<f32>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xcomplex<f32>>>>>
503+
return
504+
}
505+
506+
// CHECK-LABEL: func.func @_QPcallkernel(
507+
// CHECK-SAME: %[[ARG0:.*]]: !fir.box<!fir.array<?x?xcomplex<f32>>> {fir.bindc_name = "a"}
508+
// CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.box<!fir.array<?x?xcomplex<f32>>>
509+
// CHECK: %[[DECL_ARG0:.*]] = fir.declare %[[ARG0]] dummy_scope %{{.*}} {uniq_name = "_QFcallkernelEa"} : (!fir.box<!fir.array<?x?xcomplex<f32>>>, !fir.dscope) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
510+
// CHECK: %[[REBOX0:.*]] = fir.rebox %[[DECL_ARG0]] : (!fir.box<!fir.array<?x?xcomplex<f32>>>) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
511+
// CHECK: %[[REBOX1:.*]] = fir.rebox %[[REBOX0]] [%{{.*}}] : (!fir.box<!fir.array<?x?xcomplex<f32>>>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xcomplex<f32>>>
512+
// CHECK: fir.store %[[REBOX1]] to %[[ALLOCA]] : !fir.ref<!fir.box<!fir.array<?x?xcomplex<f32>>>>
513+
// CHECK: %[[BOX_NONE:.*]] = fir.convert %[[ALLOCA]] : (!fir.ref<!fir.box<!fir.array<?x?xcomplex<f32>>>>) -> !fir.ref<!fir.box<none>>
514+
// CHECK: fir.call @_FortranACUFDataTransferDescDesc(%{{.*}}, %[[BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.ref<!fir.box<none>>, i32, !fir.ref<i8>, i32) -> none
515+
469516
} // end of module

0 commit comments

Comments
 (0)