[flang-commits] [flang] 51a3468 - [flang] Support CHARACTER(4) pointer targets

Jean Perier via flang-commits flang-commits at lists.llvm.org
Thu Jun 29 09:37:19 PDT 2023


Author: Jean Perier
Date: 2023-06-29T18:36:44+02:00
New Revision: 51a346815027df5d03f64c3554a11f633c0720e0

URL: https://github.com/llvm/llvm-project/commit/51a346815027df5d03f64c3554a11f633c0720e0
DIFF: https://github.com/llvm/llvm-project/commit/51a346815027df5d03f64c3554a11f633c0720e0.diff

LOG: [flang] Support CHARACTER(4) pointer targets

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.

Reviewed By: PeteSteinfeld, vzakhari

Differential Revision: https://reviews.llvm.org/D154072

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 0061d18af65fc1..9934746e770cfb 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -1894,14 +1894,22 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> {
     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");

diff  --git a/flang/test/Fir/rebox-global.fir b/flang/test/Fir/rebox-global.fir
index d63e2b0e0ab081..cf39e71f30da3c 100644
--- a/flang/test/Fir/rebox-global.fir
+++ b/flang/test/Fir/rebox-global.fir
@@ -10,3 +10,14 @@ fir.global @p : !fir.box<!fir.ptr<i32>> {
   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 }


        


More information about the flang-commits mailing list