Skip to content

Conversation

@ergawy
Copy link
Member

@ergawy ergawy commented Jun 17, 2025

Extends fir::FirOpBuilder::getAllocaBlock() to support fir.local. This allows us to retrieve an allocation block when needed for fir.local.

@ergawy ergawy requested review from clementval and jeanPerier June 17, 2025 12:30
@ergawy
Copy link
Member Author

ergawy commented Jun 17, 2025

gfortran passes locally with this change.

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

return doConcurentOp.getBody();

if (auto firLocalOp =
getRegion().getParentOfType<fir::LocalitySpecifierOp>()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no braces.

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Same comment as Jean for the braces

ergawy added 2 commits June 18, 2025 02:33
Extends `fir::FirOpBuilder::getAllocaBlock()` to support `fir.local`.
This allows us to retrieve an allocation block when needed for
`fir.local`.
@ergawy ergawy force-pushed the users/ergawy/fir_local_alloca_block branch from 99384ad to abb6d26 Compare June 18, 2025 07:34
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2025

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

Author: Kareem Ergawy (ergawy)

Changes

Extends fir::FirOpBuilder::getAllocaBlock() to support fir.local. This allows us to retrieve an allocation block when needed for fir.local.


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

2 Files Affected:

  • (modified) flang/lib/Optimizer/Builder/FIRBuilder.cpp (+3)
  • (added) flang/test/HLFIR/fir-local-alloca-block.fir (+34)
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 584f3c8ee310e..6ac87067f6511 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -283,6 +283,9 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
   if (auto doConcurentOp = getRegion().getParentOfType<fir::DoConcurrentOp>())
     return doConcurentOp.getBody();
 
+  if (auto firLocalOp = getRegion().getParentOfType<fir::LocalitySpecifierOp>())
+    return &getRegion().front();
+
   return getEntryBlock();
 }
 
diff --git a/flang/test/HLFIR/fir-local-alloca-block.fir b/flang/test/HLFIR/fir-local-alloca-block.fir
new file mode 100644
index 0000000000000..9d76e86fec3d9
--- /dev/null
+++ b/flang/test/HLFIR/fir-local-alloca-block.fir
@@ -0,0 +1,34 @@
+// Tests that `fir.local` ops are able to provide an alloca block when required.
+
+// RUN: fir-opt %s -convert-hlfir-to-fir | FileCheck %s
+
+fir.local {type = local_init} @localizer : !fir.box<!fir.array<1xi32>> copy {
+^bb0(%arg0: !fir.ref<!fir.box<!fir.array<1xi32>>>, %arg1: !fir.ref<!fir.box<!fir.array<1xi32>>>):
+  %0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.array<1xi32>>>
+  hlfir.assign %0 to %arg1 : !fir.box<!fir.array<1xi32>>, !fir.ref<!fir.box<!fir.array<1xi32>>>
+  fir.yield(%arg1 : !fir.ref<!fir.box<!fir.array<1xi32>>>)
+}
+
+func.func @foo() {
+  %c1 = arith.constant 1 : index
+  %0 = fir.alloca !fir.box<!fir.array<1xi32>>
+  fir.do_concurrent {
+    fir.do_concurrent.loop (%arg0) = (%c1) to (%c1) step (%c1) local(@localizer %0 -> %arg1 : !fir.ref<!fir.box<!fir.array<1xi32>>>) {
+    }
+  }
+  return
+}
+
+// CHECK:  fir.local {type = local_init} @localizer : ![[TYPE:fir.box<!fir.array<1xi32>>]] copy {
+// CHECK:    ^bb0(%[[VAL_0:.*]]: !fir.ref<![[TYPE]]>, %[[VAL_1:.*]]: !fir.ref<![[TYPE]]>):
+// CHECK:      %[[VAL_2:.*]] = fir.alloca ![[TYPE]]
+// CHECK:      %[[VAL_3:.*]] = fir.load %[[VAL_0]] : !fir.ref<![[TYPE]]>
+// CHECK:      %[[VAL_4:.*]] = arith.constant 0 : index
+// CHECK:      %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_4]] : (![[TYPE]], index) -> (index, index, index)
+// CHECK:      %[[VAL_6:.*]] = fir.load %[[VAL_1]] : !fir.ref<![[TYPE]]>
+// CHECK:      fir.store %[[VAL_6]] to %[[VAL_2]] : !fir.ref<![[TYPE]]>
+// CHECK:      %[[VAL_10:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<![[TYPE]]>) -> !fir.ref<!fir.box<none>>
+// CHECK:      %[[VAL_11:.*]] = fir.convert %[[VAL_3]] : (![[TYPE]]) -> !fir.box<none>
+// CHECK:      fir.call @_FortranAAssign(%[[VAL_10]], %[[VAL_11]], %{{.*}}, %{{.*}})
+// CHECK:      fir.yield(%[[VAL_1]] : !fir.ref<![[TYPE]]>)
+// CHECK:  }

@ergawy ergawy merged commit 59d6fbb into main Jun 18, 2025
9 checks passed
@ergawy ergawy deleted the users/ergawy/fir_local_alloca_block branch June 18, 2025 08:24
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.

5 participants