Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ def cuf_DataTransferOp : cuf_Op<"data_transfer", []> {

let arguments = (ins Arg<AnyType, "", [MemRead]>:$src,
Arg<AnyRefOrBoxType, "", [MemWrite]>:$dst,
Optional<fir_ShapeType>:$shape,
cuf_DataTransferKindAttr:$transfer_kind);

let assemblyFormat = [{
$src `to` $dst attr-dict `:` type(operands)
$src `to` $dst (`,` $shape^ `:` type($shape) )? attr-dict `:` type($src) `,` type($dst)
}];

let hasVerifier = 1;
Expand Down
15 changes: 9 additions & 6 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4272,18 +4272,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
base = convertOp.getValue();
// Special case if the rhs is a constant.
if (matchPattern(base.getDefiningOp(), mlir::m_Constant())) {
builder.create<cuf::DataTransferOp>(loc, base, lhsVal,
transferKindAttr);
builder.create<cuf::DataTransferOp>(
loc, base, lhsVal, /*shape=*/mlir::Value{}, transferKindAttr);
} else {
auto associate = hlfir::genAssociateExpr(
loc, builder, rhs, rhs.getType(), ".cuf_host_tmp");
builder.create<cuf::DataTransferOp>(loc, associate.getBase(), lhsVal,
/*shape=*/mlir::Value{},
transferKindAttr);
builder.create<hlfir::EndAssociateOp>(loc, associate);
}
} else {
builder.create<cuf::DataTransferOp>(loc, rhsVal, lhsVal,
transferKindAttr);
builder.create<cuf::DataTransferOp>(
loc, rhsVal, lhsVal, /*shape=*/mlir::Value{}, transferKindAttr);
}
return;
}
Expand All @@ -4293,6 +4294,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
auto transferKindAttr = cuf::DataTransferKindAttr::get(
builder.getContext(), cuf::DataTransferKind::DeviceHost);
builder.create<cuf::DataTransferOp>(loc, rhsVal, lhsVal,
/*shape=*/mlir::Value{},
transferKindAttr);
return;
}
Expand All @@ -4303,6 +4305,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
auto transferKindAttr = cuf::DataTransferKindAttr::get(
builder.getContext(), cuf::DataTransferKind::DeviceDevice);
builder.create<cuf::DataTransferOp>(loc, rhsVal, lhsVal,
/*shape=*/mlir::Value{},
transferKindAttr);
return;
}
Expand Down Expand Up @@ -4346,8 +4349,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
addSymbol(sym,
hlfir::translateToExtendedValue(loc, builder, temp).first,
/*forced=*/true);
builder.create<cuf::DataTransferOp>(loc, addr, temp,
transferKindAttr);
builder.create<cuf::DataTransferOp>(
loc, addr, temp, /*shape=*/mlir::Value{}, transferKindAttr);
++nbDeviceResidentObject;
}
}
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ llvm::LogicalResult cuf::AllocateOp::verify() {
llvm::LogicalResult cuf::DataTransferOp::verify() {
mlir::Type srcTy = getSrc().getType();
mlir::Type dstTy = getDst().getType();
if (getShape()) {
if (!fir::isa_ref_type(srcTy) || !fir::isa_ref_type(dstTy))
return emitOpError()
<< "shape can only be specified on data transfer with references";
}
if ((fir::isa_ref_type(srcTy) && fir::isa_ref_type(dstTy)) ||
(fir::isa_box_type(srcTy) && fir::isa_box_type(dstTy)) ||
(fir::isa_ref_type(srcTy) && fir::isa_box_type(dstTy)) ||
Expand Down
31 changes: 31 additions & 0 deletions flang/test/Fir/cuf-invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,34 @@ func.func @_QPsub1() {
cuf.free %0 : !fir.ref<f32> {data_attr = #cuf.cuda<constant>}
return
}

// -----

func.func @_QPsub1(%arg0: !fir.ref<!fir.array<?xf32>> {cuf.data_attr = #cuf.cuda<device>, fir.bindc_name = "adev"}, %arg1: !fir.ref<!fir.array<?xf32>> {fir.bindc_name = "ahost"}, %arg2: !fir.ref<i32> {fir.bindc_name = "n"}, %arg3: !fir.ref<i32> {fir.bindc_name = "m"}) {
%0 = fir.dummy_scope : !fir.dscope
%1:2 = hlfir.declare %arg2 dummy_scope %0 {uniq_name = "_QFsub1En"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
%2:2 = hlfir.declare %arg3 dummy_scope %0 {uniq_name = "_QFsub1Em"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
%3 = fir.load %1#0 : !fir.ref<i32>
%4 = fir.load %2#0 : !fir.ref<i32>
%5 = arith.muli %3, %4 : i32
%6 = fir.convert %5 : (i32) -> i64
%7 = fir.convert %6 : (i64) -> index
%c0 = arith.constant 0 : index
%8 = arith.cmpi sgt, %7, %c0 : index
%9 = arith.select %8, %7, %c0 : index
%10 = fir.shape %9 : (index) -> !fir.shape<1>
%11:2 = hlfir.declare %arg0(%10) dummy_scope %0 {data_attr = #cuf.cuda<device>, uniq_name = "_QFsub1Eadev"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.dscope) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)
%12 = fir.load %1#0 : !fir.ref<i32>
%13 = fir.load %2#0 : !fir.ref<i32>
%14 = arith.muli %12, %13 : i32
%15 = fir.convert %14 : (i32) -> i64
%16 = fir.convert %15 : (i64) -> index
%c0_0 = arith.constant 0 : index
%17 = arith.cmpi sgt, %16, %c0_0 : index
%18 = arith.select %17, %16, %c0_0 : index
%19 = fir.shape %18 : (index) -> !fir.shape<1>
%20:2 = hlfir.declare %arg1(%19) dummy_scope %0 {uniq_name = "_QFsub1Eahost"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.dscope) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)
// expected-error@+1{{'cuf.data_transfer' op shape can only be specified on data transfer with references}}
cuf.data_transfer %20#0 to %11#0, %19 : !fir.shape<1> {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>
return
}