Skip to content

Commit a343b74

Browse files
rovkaschweitzpgijeanPerier
committed
[fir] Add !fir.char type conversion
This patch is part of the upstreaming effort from fir-dev. Differential Revision: https://reviews.llvm.org/D113560 Co-authored-by: Eric Schweitz <[email protected]> Co-authored-by: Jean Perier <[email protected]>
1 parent deafc6f commit a343b74

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

flang/lib/Optimizer/CodeGen/TypeConverter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
3737

3838
// Each conversion should return a value of type mlir::Type.
3939
addConversion([&](BoxType box) { return convertBoxType(box); });
40+
addConversion(
41+
[&](fir::CharacterType charTy) { return convertCharType(charTy); });
4042
addConversion([&](fir::LogicalType boolTy) {
4143
return mlir::IntegerType::get(
4244
&getContext(), kindMapping.getLogicalBitsize(boolTy.getFKind()));
@@ -150,6 +152,18 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
150152
/*isPacked=*/false));
151153
}
152154

155+
unsigned characterBitsize(fir::CharacterType charTy) {
156+
return kindMapping.getCharacterBitsize(charTy.getFKind());
157+
}
158+
159+
// fir.char<n> --> llvm<"ix*"> where ix is scaled by kind mapping
160+
mlir::Type convertCharType(fir::CharacterType charTy) {
161+
auto iTy = mlir::IntegerType::get(&getContext(), characterBitsize(charTy));
162+
if (charTy.getLen() == fir::CharacterType::unknownLen())
163+
return iTy;
164+
return mlir::LLVM::LLVMArrayType::get(iTy, charTy.getLen());
165+
}
166+
153167
// Use the target specifics to figure out how to map complex to LLVM IR. The
154168
// use of complex values in function signatures is handled before conversion
155169
// to LLVM IR dialect here.

flang/test/Fir/types-to-llvm.fir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ func private @foo3(%arg0: !fir.box<!fir.type<derived{f:f32}>>)
6363

6464
// -----
6565

66+
// Test char types `!fir.char<k, n>`
67+
68+
func private @foo0(%arg0: !fir.char<1, 4>, %arg1: !fir.char<1, ?>)
69+
// CHECK-LABEL: foo0
70+
// CHECK-SAME: !llvm.array<4 x i8>
71+
// CHECK-SAME: i8
72+
73+
func private @foo1(%arg0: !fir.char<2, 12>, %arg1: !fir.char<2, ?>)
74+
// CHECK-LABEL: foo1
75+
// CHECK-SAME: !llvm.array<12 x i16>
76+
// CHECK-SAME: i16
77+
78+
func private @foo2(%arg0: !fir.char<4, 8>, %arg1: !fir.char<4, ?>)
79+
// CHECK-LABEL: foo2
80+
// CHECK-SAME: !llvm.array<8 x i32>
81+
// CHECK-SAME: i32
82+
83+
// -----
84+
6685
// Test `!fir.logical<KIND>` conversion.
6786

6887
func private @foo0(%arg0: !fir.logical<1>)

0 commit comments

Comments
 (0)