Skip to content

Commit 2583071

Browse files
authored
[flang][cuda] Compute size of derived type arrays (#115914)
1 parent 4714215 commit 2583071

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,15 @@ struct CUFAllocOpConversion : public mlir::OpRewritePattern<cuf::AllocOp> {
321321
builder.createIntegerConstant(loc, builder.getIndexType(), width);
322322
} else if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(
323323
op.getInType())) {
324-
mlir::Value width = builder.createIntegerConstant(
325-
loc, builder.getIndexType(),
326-
computeWidth(loc, seqTy.getEleTy(), kindMap));
324+
std::size_t size = 0;
325+
if (fir::isa_derived(seqTy.getEleTy())) {
326+
mlir::Type structTy = typeConverter->convertType(seqTy.getEleTy());
327+
size = dl->getTypeSizeInBits(structTy) / 8;
328+
} else {
329+
size = computeWidth(loc, seqTy.getEleTy(), kindMap);
330+
}
331+
mlir::Value width =
332+
builder.createIntegerConstant(loc, builder.getIndexType(), size);
327333
mlir::Value nbElem;
328334
if (fir::sequenceWithNonConstantShape(seqTy)) {
329335
assert(!op.getShape().empty() && "expect shape with dynamic arrays");
@@ -580,8 +586,9 @@ struct CUFDataTransferOpConversion
580586
loc, i64Ty, seqTy.getConstantArraySize());
581587
}
582588
unsigned width = 0;
583-
if (fir::isa_derived(dstTy)) {
584-
mlir::Type structTy = typeConverter->convertType(dstTy);
589+
if (fir::isa_derived(fir::unwrapSequenceType(dstTy))) {
590+
mlir::Type structTy =
591+
typeConverter->convertType(fir::unwrapSequenceType(dstTy));
585592
width = dl->getTypeSizeInBits(structTy) / 8;
586593
} else {
587594
width = computeWidth(loc, dstTy, kindMap);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,23 @@ func.func @_QPtest_type() {
308308
// CHECK-LABEL: func.func @_QPtest_type()
309309
// CHECK: fir.call @_FortranACUFDataTransferPtrPtr(%{{.*}}, %{{.*}}, %c12{{.*}}, %c0{{.*}}, %{{.*}}, %{{.*}}) : (!fir.llvm_ptr<i8>, !fir.llvm_ptr<i8>, i64, i32, !fir.ref<i8>, i32) -> none
310310

311+
func.func @_QPtest_array_type() {
312+
%c10 = arith.constant 10 : index
313+
%0 = cuf.alloc !fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>> {bindc_name = "a", data_attr = #cuf.cuda<device>, uniq_name = "_QFtest_array_typeEa"} -> !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>
314+
%1 = fir.shape %c10 : (index) -> !fir.shape<1>
315+
%2 = fir.declare %0(%1) {data_attr = #cuf.cuda<device>, uniq_name = "_QFtest_array_typeEa"} : (!fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>, !fir.shape<1>) -> !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>
316+
%3 = fir.alloca !fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>> {bindc_name = "b", uniq_name = "_QFtest_array_typeEb"}
317+
%4 = fir.declare %3(%1) {uniq_name = "_QFtest_array_typeEb"} : (!fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>, !fir.shape<1>) -> !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>
318+
cuf.data_transfer %4 to %2 {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>, !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>>
319+
cuf.free %2 : !fir.ref<!fir.array<10x!fir.type<_QMbarTcmplx{id:i32,c:complex<f32>}>>> {data_attr = #cuf.cuda<device>}
320+
return
321+
}
322+
323+
// CHECK-LABEL: func.func @_QPtest_array_type()
324+
// CHECK: %[[BYTES:.*]] = arith.muli %c10{{.*}}, %c12 : index
325+
// CHECK: %[[CONV_BYTES:.*]] = fir.convert %[[BYTES]] : (index) -> i64
326+
// CHECK: fir.call @_FortranACUFMemAlloc(%[[CONV_BYTES]], %c0{{.*}}, %{{.*}}, %{{.*}}) : (i64, i32, !fir.ref<i8>, i32) -> !fir.llvm_ptr<i8>
327+
// CHECK: %[[BYTES:.*]] = arith.muli %c10{{.*}}, %c12{{.*}} : i64
328+
// CHECK: fir.call @_FortranACUFDataTransferPtrPtr(%{{.*}}, %{{.*}}, %[[BYTES]], %c0{{.*}}, %{{.*}}, %{{.*}}) : (!fir.llvm_ptr<i8>, !fir.llvm_ptr<i8>, i64, i32, !fir.ref<i8>, i32) -> none
329+
311330
} // end of module

0 commit comments

Comments
 (0)