[flang-commits] [flang] [flang] Preserve the alignment attribute across stack/heap conversions (PR #234898)

Caroline Newcombe via flang-commits flang-commits at lists.llvm.org
Fri Oct 23 13:56:52 PDT 2026


================
@@ -215,9 +215,15 @@ void AllocationPlacementPass::runOnOperation() {
             ? placementHook(info, basePolicy, stackBytesUsed)
             : fir::decideAllocationPlacement(info, basePolicy, stackBytesUsed);
 
-    // Account for the decision in the running stack budget.
-    if (endsUpOnStack(placement, info.isCurrentlyOnStack) && info.byteSize)
+    // Account for the decision in the running stack budget, including the
+    // worst-case padding a requested alignment may need.
+    if (endsUpOnStack(placement, info.isCurrentlyOnStack) && info.byteSize) {
       stackBytesUsed += static_cast<std::size_t>(*info.byteSize);
+      std::optional<std::int64_t> alignment =
+          alloca ? alloca.getAlignment() : allocmem.getAlignment();
+      if (alignment && *alignment > 1)
+        stackBytesUsed += static_cast<std::size_t>(*alignment - 1);
----------------
cenewcombe wrote:

Should this padding also be considered in the placement decision above? As written, it is charged after deciding to use the stack, so it affects subsequent allocations but not whether this allocation fits. Or is that acceptable for this heuristic budget?

https://github.com/llvm/llvm-project/pull/234898


More information about the flang-commits mailing list