[flang-commits] [flang] a343b74 - [fir] Add !fir.char type conversion
Diana Picus via flang-commits
flang-commits at lists.llvm.org
Wed Nov 10 04:28:56 PST 2021
Author: Diana Picus
Date: 2021-11-10T12:27:58Z
New Revision: a343b74f8532c8e04e600d5d6e1ba7959f950808
URL: https://github.com/llvm/llvm-project/commit/a343b74f8532c8e04e600d5d6e1ba7959f950808
DIFF: https://github.com/llvm/llvm-project/commit/a343b74f8532c8e04e600d5d6e1ba7959f950808.diff
LOG: [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 <eschweitz at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>
Added:
Modified:
flang/lib/Optimizer/CodeGen/TypeConverter.h
flang/test/Fir/types-to-llvm.fir
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/CodeGen/TypeConverter.h b/flang/lib/Optimizer/CodeGen/TypeConverter.h
index 2f5eb0603e29..b73ece78b631 100644
--- a/flang/lib/Optimizer/CodeGen/TypeConverter.h
+++ b/flang/lib/Optimizer/CodeGen/TypeConverter.h
@@ -37,6 +37,8 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
// Each conversion should return a value of type mlir::Type.
addConversion([&](BoxType box) { return convertBoxType(box); });
+ addConversion(
+ [&](fir::CharacterType charTy) { return convertCharType(charTy); });
addConversion([&](fir::LogicalType boolTy) {
return mlir::IntegerType::get(
&getContext(), kindMapping.getLogicalBitsize(boolTy.getFKind()));
@@ -150,6 +152,18 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
/*isPacked=*/false));
}
+ unsigned characterBitsize(fir::CharacterType charTy) {
+ return kindMapping.getCharacterBitsize(charTy.getFKind());
+ }
+
+ // fir.char<n> --> llvm<"ix*"> where ix is scaled by kind mapping
+ mlir::Type convertCharType(fir::CharacterType charTy) {
+ auto iTy = mlir::IntegerType::get(&getContext(), characterBitsize(charTy));
+ if (charTy.getLen() == fir::CharacterType::unknownLen())
+ return iTy;
+ return mlir::LLVM::LLVMArrayType::get(iTy, charTy.getLen());
+ }
+
// Use the target specifics to figure out how to map complex to LLVM IR. The
// use of complex values in function signatures is handled before conversion
// to LLVM IR dialect here.
diff --git a/flang/test/Fir/types-to-llvm.fir b/flang/test/Fir/types-to-llvm.fir
index 94b1a0b76ba8..921ac355d08f 100644
--- a/flang/test/Fir/types-to-llvm.fir
+++ b/flang/test/Fir/types-to-llvm.fir
@@ -63,6 +63,25 @@ func private @foo3(%arg0: !fir.box<!fir.type<derived{f:f32}>>)
// -----
+// Test char types `!fir.char<k, n>`
+
+func private @foo0(%arg0: !fir.char<1, 4>, %arg1: !fir.char<1, ?>)
+// CHECK-LABEL: foo0
+// CHECK-SAME: !llvm.array<4 x i8>
+// CHECK-SAME: i8
+
+func private @foo1(%arg0: !fir.char<2, 12>, %arg1: !fir.char<2, ?>)
+// CHECK-LABEL: foo1
+// CHECK-SAME: !llvm.array<12 x i16>
+// CHECK-SAME: i16
+
+func private @foo2(%arg0: !fir.char<4, 8>, %arg1: !fir.char<4, ?>)
+// CHECK-LABEL: foo2
+// CHECK-SAME: !llvm.array<8 x i32>
+// CHECK-SAME: i32
+
+// -----
+
// Test `!fir.logical<KIND>` conversion.
func private @foo0(%arg0: !fir.logical<1>)
More information about the flang-commits
mailing list