[Mlir-commits] [mlir] [MLIR] Fix: `alloca` promotion for `AllocationOpInterface` (PR #97672)

Nikhil Kalra llvmlistbot at llvm.org
Wed Jul 3 20:49:18 PDT 2024


https://github.com/nikalra created https://github.com/llvm/llvm-project/pull/97672

The std::optional returned by buildPromotedAlloc was directly dereferenced and assumed to be non-null, even though the documentation for AllocationOpInterface indicates that std::nullopt is a legal value if buffer stack promotion is not supported (and is the default value supplied by the TableGen interface file). This patch removes the direct dereference so that the optional can be null-checked prior to use.

>From 0730358f8a88bc4fdd20ff59e7af156f898022b8 Mon Sep 17 00:00:00 2001
From: Nikhil Kalra <nkalra at apple.com>
Date: Wed, 3 Jul 2024 20:45:00 -0700
Subject: [PATCH] [MLIR] Fix: `alloca` promotion for `AllocationOpInterface`

The std::optional returned by buildPromotedAlloc was directly dereferenced and assumed to be non-null, even though the documentation for AllocationOpInterface indicates that std::nullopt is a legal value if buffer stack promotion is not supported (and is the default value supplied by the TableGen interface file). This patch removes the direct dereference so that the optional can be null-checked prior to use.
---
 .../Bufferization/Transforms/BufferOptimizations.cpp        | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Dialect/Bufferization/Transforms/BufferOptimizations.cpp b/mlir/lib/Dialect/Bufferization/Transforms/BufferOptimizations.cpp
index d7056f35cbc8d..93c1f9a4f2b55 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/BufferOptimizations.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/BufferOptimizations.cpp
@@ -397,12 +397,12 @@ class BufferPlacementPromotion : BufferPlacementTransformationBase {
       OpBuilder builder(startOperation);
       Operation *allocOp = alloc.getDefiningOp();
       if (auto allocInterface = dyn_cast<AllocationOpInterface>(allocOp)) {
-        Operation *alloca =
-            allocInterface.buildPromotedAlloc(builder, alloc).value();
+        std::optional<Operation *> alloca =
+            allocInterface.buildPromotedAlloc(builder, alloc);
         if (!alloca)
           continue;
         // Replace the original alloc by a newly created alloca.
-        allocOp->replaceAllUsesWith(alloca);
+        allocOp->replaceAllUsesWith(alloca.value());
         allocOp->erase();
       }
     }



More information about the Mlir-commits mailing list