[flang-commits] [PATCH] D154072: [flang] Support CHARACTER(4) pointer targets

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Jun 29 05:34:17 PDT 2023


jeanPerier created this revision.
jeanPerier added reviewers: PeteSteinfeld, vzakhari.
jeanPerier added a project: Flang.
Herald added subscribers: sunshaoce, bzcheeseman, mehdi_amini, rriddle, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.
Herald added a subscriber: stephenneuendorffer.

fir.rebox is emitting an llvm.sdiv to compute the character length
given the byte size from the input descriptor.
Inside a fir.global, this is not needed given the target length must
be accessible via the type, and it caused MLIR to fail LLVM IR
code generation (and crash).

Use the input type length when available instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154072

Files:
  flang/lib/Optimizer/CodeGen/CodeGen.cpp
  flang/test/Fir/rebox-global.fir


Index: flang/test/Fir/rebox-global.fir
===================================================================
--- flang/test/Fir/rebox-global.fir
+++ flang/test/Fir/rebox-global.fir
@@ -10,3 +10,14 @@
   fir.has_value %2 : !fir.box<!fir.ptr<i32>>
 }
 // CHECK: @p = global { ptr, i64, i32, i8, i8, i8, i8 } { ptr @x, {{.*}}, i32 {{.*}}, i8 0, i8 9, i8 1, i8 0 }
+
+// Test that sdiv is not generated inside fir.global.
+fir.global @char4 target : !fir.char<4,10>
+fir.global @pointer_char4_init : !fir.box<!fir.ptr<!fir.char<4,10>>> {
+  %0 = fir.address_of(@char4) : !fir.ref<!fir.char<4,10>>
+  %1 = fir.embox %0 : (!fir.ref<!fir.char<4,10>>) -> !fir.box<!fir.char<4,10>>
+  %2 = fircg.ext_rebox %1 : (!fir.box<!fir.char<4,10>>) -> !fir.box<!fir.ptr<!fir.char<4,10>>>
+  fir.has_value %2 : !fir.box<!fir.ptr<!fir.char<4,10>>>
+}
+// CHECK-LABEL: @pointer_char4_init
+// CHECK-SAME: { ptr @char4, i64 ptrtoint (ptr getelementptr ([10 x i32], ptr null, i32 1) to i64), i32 20180515, i8 0, i8 44, i8 1, i8 0 }
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -1894,14 +1894,22 @@
     llvm::SmallVector<mlir::Value, 2> lenParams;
     mlir::Type inputEleTy = getInputEleTy(rebox);
     if (auto charTy = inputEleTy.dyn_cast<fir::CharacterType>()) {
-      mlir::Value len = getElementSizeFromBox(
-          loc, idxTy, rebox.getBox().getType(), loweredBox, rewriter);
-      if (charTy.getFKind() != 1) {
-        mlir::Value width =
-            genConstantIndex(loc, idxTy, rewriter, charTy.getFKind());
-        len = rewriter.create<mlir::LLVM::SDivOp>(loc, idxTy, len, width);
+      if (charTy.hasConstantLen()) {
+        mlir::Value len =
+            genConstantIndex(loc, idxTy, rewriter, charTy.getLen());
+        lenParams.emplace_back(len);
+      } else {
+        mlir::Value len = getElementSizeFromBox(
+            loc, idxTy, rebox.getBox().getType(), loweredBox, rewriter);
+        if (charTy.getFKind() != 1) {
+          assert(!isInGlobalOp(rewriter) &&
+                 "character target in global op must have constant length");
+          mlir::Value width =
+              genConstantIndex(loc, idxTy, rewriter, charTy.getFKind());
+          len = rewriter.create<mlir::LLVM::SDivOp>(loc, idxTy, len, width);
+        }
+        lenParams.emplace_back(len);
       }
-      lenParams.emplace_back(len);
     } else if (auto recTy = inputEleTy.dyn_cast<fir::RecordType>()) {
       if (recTy.getNumLenParams() != 0)
         TODO(loc, "reboxing descriptor of derived type with length parameters");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154072.535743.patch
Type: text/x-patch
Size: 2682 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230629/5ba4eabb/attachment-0001.bin>


More information about the flang-commits mailing list