Skip to content

Commit 0f941f6

Browse files
authored
[flang][cuda] Add support to allocate scalar character types (llvm#169550)
Add support for character declared like: ``` subroutine sub1() character*4, device :: b end subroutine ```
1 parent 3694798 commit 0f941f6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

flang/lib/Optimizer/Transforms/CUFOpConversion.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ struct CUFAllocOpConversion : public mlir::OpRewritePattern<cuf::AllocOp> {
331331
std::size_t structSize = dl->getTypeSizeInBits(structTy) / 8;
332332
bytes = builder.createIntegerConstant(loc, builder.getIndexType(),
333333
structSize);
334+
} else if (fir::isa_char(op.getInType())) {
335+
mlir::Type charTy = typeConverter->convertType(op.getInType());
336+
std::size_t charSize = dl->getTypeSizeInBits(charTy) / 8;
337+
bytes = builder.createIntegerConstant(loc, builder.getIndexType(),
338+
charSize);
334339
} else {
335340
mlir::emitError(loc, "unsupported type in cuf.alloc\n");
336341
}

flang/test/Fir/CUDA/cuda-alloc-free.fir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,24 @@ func.func @_QQalloc_char() attributes {fir.bindc_name = "alloc_char"} {
9494
// CHECK: %[[BYTES_CONV:.*]] = fir.convert %[[BYTES]] : (index) -> i64
9595
// CHECK: fir.call @_FortranACUFMemAlloc(%[[BYTES_CONV]], %c0{{.*}}, %{{.*}}, %{{.*}}) {cuf.data_attr = #cuf.cuda<device>} : (i64, i32, !fir.ref<i8>, i32) -> !fir.llvm_ptr<i8>
9696

97+
98+
func.func @_QQalloc_char2() {
99+
%c4 = arith.constant 4 : index
100+
%1 = cuf.alloc !fir.char<1,4>(%c4 : index) {bindc_name = "b", data_attr = #cuf.cuda<device>, uniq_name = "_QFsub1Eb"} -> !fir.ref<!fir.char<1,4>>
101+
%2 = cuf.alloc !fir.char<2,4>(%c4 : index) {bindc_name = "c", data_attr = #cuf.cuda<device>, uniq_name = "_QFsub1Ec"} -> !fir.ref<!fir.char<2,4>>
102+
%c10 = arith.constant 4 : index
103+
%3 = cuf.alloc !fir.char<4,10>(%c10 : index) {bindc_name = "d", data_attr = #cuf.cuda<device>, uniq_name = "_QFsub1Ed"} -> !fir.ref<!fir.char<4,10>>
104+
return
105+
}
106+
107+
// CHECK-LABEL: func.func @_QQalloc_char2()
108+
// CHECK: %[[BYTES_4:.*]] = fir.convert %c4{{.*}} : (index) -> i64
109+
// CHECK: %{{.*}} = fir.call @_FortranACUFMemAlloc(%[[BYTES_4]], %{{.*}}, %{{.*}}, %{{.*}}) {cuf.data_attr = #cuf.cuda<device>} : (i64, i32, !fir.ref<i8>, i32) -> !fir.llvm_ptr<i8>
110+
111+
// CHECK: %[[BYTES_8:.*]] = fir.convert %c8{{.*}} : (index) -> i64
112+
// CHECK: %{{.*}} = fir.call @_FortranACUFMemAlloc(%[[BYTES_8]], %{{.*}}, %{{.*}}, %{{.*}}) {cuf.data_attr = #cuf.cuda<device>} : (i64, i32, !fir.ref<i8>, i32) -> !fir.llvm_ptr<i8>
113+
114+
// CHECK: %[[BYTES_40:.*]] = fir.convert %c40{{.*}} : (index) -> i64
115+
// CHECK: %{{.*}} = fir.call @_FortranACUFMemAlloc(%[[BYTES_40]], %{{.*}}, %{{.*}}, %{{.*}}) {cuf.data_attr = #cuf.cuda<device>} : (i64, i32, !fir.ref<i8>, i32) -> !fir.llvm_ptr<i8>
116+
97117
} // end module

0 commit comments

Comments
 (0)