Skip to content

[flang] Allow no type parameters for fir.pack_array. #131662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025

Conversation

vzakhari
Copy link
Contributor

Arrays with assumed-length types are represented with a box
without explicit length parameters. This patch fixes the verification
to allow it for fir.pack_array.

Arrays with assumed-length types are represented with a box
without explicit length parameters. This patch fixes the verification
to allow it for `fir.pack_array`.
@vzakhari vzakhari requested review from tblah and jeanPerier March 17, 2025 19:36
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Slava Zakharin (vzakhari)

Changes

Arrays with assumed-length types are represented with a box
without explicit length parameters. This patch fixes the verification
to allow it for fir.pack_array.


Full diff: https://github.com/llvm/llvm-project/pull/131662.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Dialect/FIROps.cpp (+6)
  • (modified) flang/test/Fir/fir-ops.fir (+11)
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index 90202f3cee588..052cc9c717bcb 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -388,6 +388,12 @@ static bool validTypeParams(mlir::Type dynTy, mlir::ValueRange typeParams,
     if (!allowParamsForBox)
       return typeParams.size() == 0;
 
+    // A boxed value may have no length parameters, when the lengths
+    // are assumed. If dynamic lengths are used, then proceed
+    // to the verification below.
+    if (typeParams.size() == 0)
+      return true;
+
     dynTy = fir::getFortranElementType(dynTy);
   }
   // Derived type must have all type parameters satisfied.
diff --git a/flang/test/Fir/fir-ops.fir b/flang/test/Fir/fir-ops.fir
index 9cd02dce626ab..9c444d2f4e0bc 100644
--- a/flang/test/Fir/fir-ops.fir
+++ b/flang/test/Fir/fir-ops.fir
@@ -968,6 +968,17 @@ func.func @test_pack_unpack_array(%arg0: !fir.ref<!fir.box<none>>, %arg1: !fir.b
   return
 }
 
+// Test assumed-length character array packing.
+// CHECK-LABEL:   func.func @test_pack_array_character(
+// CHECK-SAME:                                         %[[VAL_0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: !fir.box<!fir.array<?x!fir.char<1,?>>>) {
+func.func @test_pack_array_character(%arg0: !fir.box<!fir.array<?x!fir.char<1,?>>>) {
+// CHECK:           %[[VAL_1:.*]] = fir.pack_array %[[VAL_0]] heap whole : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
+  %0 = fir.pack_array %arg0 heap whole : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
+// CHECK:           fir.unpack_array %[[VAL_1]] to %[[VAL_0]] heap no_copy : !fir.box<!fir.array<?x!fir.char<1,?>>>
+  fir.unpack_array %0 to %arg0 heap no_copy : !fir.box<!fir.array<?x!fir.char<1,?>>>
+  return
+}
+
 // CHECK-LABEL:   func.func @test_is_contiguous_box(
 func.func @test_is_contiguous_box(%arg0: !fir.class<!fir.type<sometype{i:i32}>>, %arg1: !fir.box<!fir.array<?x?xf32>>, %arg2: !fir.box<i32>) -> i1 {
 // CHECK:           fir.is_contiguous_box %{{.*}} innermost : (!fir.class<!fir.type<sometype{i:i32}>>) -> i1

@vzakhari vzakhari merged commit e0bcf3a into llvm:main Mar 18, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants