[flang-commits] [flang] [flang] allow assumed-rank box in fir.alloca (PR #95947)

via flang-commits flang-commits at lists.llvm.org
Tue Jun 18 09:01:50 PDT 2024


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/95947

The alloca can be maximized with the maximum number or ranks, which is reasonable (15 currently as per the standard). Introducing a rank based dynamic allocation would complexify alloca hoisting and stack size analysis (this can be revisited if the standard changes to allow more ranks).

No change is needed since this is already reflected in how the fir.box type is translated to LLVM.

>From 008a56d0e6b3b4cc14475b83ab8de52271f053c8 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Tue, 18 Jun 2024 08:56:35 -0700
Subject: [PATCH] [flang] allow assumed-rank box in fir.alloca

---
 flang/lib/Optimizer/Dialect/FIROps.cpp |  2 --
 flang/test/Fir/convert-to-llvm.fir     | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index ea8a9752eeeee..6cf0dd23fe456 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -272,8 +272,6 @@ mlir::LogicalResult fir::AllocaOp::verify() {
   mlir::Type outType = getType();
   if (!mlir::isa<fir::ReferenceType>(outType))
     return emitOpError("must be a !fir.ref type");
-  if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType)))
-    return emitOpError("cannot allocate !fir.box of unknown rank or type");
   return mlir::success();
 }
 
diff --git a/flang/test/Fir/convert-to-llvm.fir b/flang/test/Fir/convert-to-llvm.fir
index d7059671d3a88..857d5b2ff303c 100644
--- a/flang/test/Fir/convert-to-llvm.fir
+++ b/flang/test/Fir/convert-to-llvm.fir
@@ -1277,6 +1277,20 @@ func.func @alloca_array_with_holes(%0 : index, %1 : index) -> !fir.ref<!fir.arra
 
 // -----
 
+// Test alloca of assumed-rank box
+
+func.func @alloca_assumed_rank_box() -> !fir.ref<!fir.box<!fir.array<*:f32>>> {
+  %a = fir.alloca !fir.box<!fir.array<*:f32>>
+  return %a : !fir.ref<!fir.box<!fir.array<*:f32>>>
+}
+// CHECK-LABEL: llvm.func @alloca_assumed_rank_box
+// CHECK: %[[ONE:.*]] = llvm.mlir.constant(1 : i64) : i64
+// GENERIC: llvm.alloca %[[ONE]] x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8, array<15 x array<3 x i64>>)> : (i64) -> !llvm.ptr
+// AMDGPU: %[[AA:.*]] = llvm.alloca %[[ONE]] x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8, array<15 x array<3 x i64>>)> : (i64) -> !llvm.ptr<5>
+// AMDGPU: llvm.addrspacecast %[[AA]] : !llvm.ptr<5> to !llvm.ptr
+
+// -----
+
 // Test `fir.select_case` operation conversion with INTEGER.
 
 func.func @select_case_integer(%arg0: !fir.ref<i32>) -> i32 {



More information about the flang-commits mailing list