[PATCH] D113560: [fir] Add !fir.char type conversion

Diana Picus via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 10 04:15:36 PST 2021


rovka created this revision.
rovka added reviewers: clementval, awarzynski, jeanPerier, schweitz, kiranchandramohan, AlexisPerry.
rovka added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
rovka requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch is part of the upstreaming effort from fir-dev.

Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113560

Files:
  flang/lib/Optimizer/CodeGen/TypeConverter.h
  flang/test/Fir/types-to-llvm.fir


Index: flang/test/Fir/types-to-llvm.fir
===================================================================
--- flang/test/Fir/types-to-llvm.fir
+++ flang/test/Fir/types-to-llvm.fir
@@ -62,6 +62,24 @@
 
 // -----
 
+// 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>)
Index: flang/lib/Optimizer/CodeGen/TypeConverter.h
===================================================================
--- flang/lib/Optimizer/CodeGen/TypeConverter.h
+++ flang/lib/Optimizer/CodeGen/TypeConverter.h
@@ -37,6 +37,8 @@
 
     // 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 @@
                                                /*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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113560.386118.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211110/97af093f/attachment.bin>


More information about the llvm-commits mailing list