[PATCH] D79230: [MLIR][crashfix] Fall back to malloc when alignment is 0 for align-alloc

Kern Handa via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 00:40:00 PDT 2020


kernhanda created this revision.
Herald added subscribers: llvm-commits, Kayjukh, frgossen, grosul1, Joonsoo, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, jpienaar, rriddle, mehdi_amini.
Herald added a reviewer: ftynse.
Herald added a project: LLVM.

Currently, the following crashes when running the StandardToLLVM conversion
pass with the use-aligned-alloc option.

  module {
    func @f() {
      %0 = alloc() {alignment = 0} : memref<64xf32>
      return
    }
  }

To fix this, I chose the option falling back to malloc instead of emitting
an error. This is because aligned_alloc is not supposed to be called with 0
as a given alignment, so this allows for an escape hatch in the conversion,
while also fixing the crash.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79230

Files:
  mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
  mlir/test/Conversion/StandardToLLVM/convert-dynamic-memref-ops.mlir


Index: mlir/test/Conversion/StandardToLLVM/convert-dynamic-memref-ops.mlir
===================================================================
--- mlir/test/Conversion/StandardToLLVM/convert-dynamic-memref-ops.mlir
+++ mlir/test/Conversion/StandardToLLVM/convert-dynamic-memref-ops.mlir
@@ -182,6 +182,13 @@
   // ALIGNED-ALLOC:  %[[c128:.*]] = llvm.mlir.constant(128 : i64) : !llvm.i64
   // ALIGNED-ALLOC:  llvm.call @aligned_alloc(%[[c128]]
   %6 = alloc(%N) : memref<?xvector<18xf32>>
+  // When alignment is 0, revert to malloc behavior
+  // ALIGNED-ALLOC:  %[[c64:.*]] = llvm.mlir.constant(64 : index) : !llvm.i64
+  // ALIGNED-ALLOC:  llvm.mul %[[c64]]
+  // ALIGNED-ALLOC:  llvm.add
+  // ALIGNED-ALLOC-NEXT:  %[[ZERO_ALIGNED:.*]] = llvm.sub
+  // ALIGNED-ALLOC-NEXT:  llvm.call @malloc(%[[ZERO_ALIGNED]])
+  %7 = alloc() {alignment = 0} : memref<64xf32>
   return %0 : memref<32x18xf32>
 }
 
Index: mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
===================================================================
--- mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
+++ mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
@@ -1521,7 +1521,8 @@
 
     Optional<int64_t> allocationAlignment = getAllocationAlignment(allocOp);
     // Whether to use std lib function aligned_alloc that supports alignment.
-    bool useAlignedAlloc = allocationAlignment.hasValue();
+    bool useAlignedAlloc = allocationAlignment &&
+                           *allocationAlignment != 0;
 
     // Insert the malloc/aligned_alloc declaration if it is not already present.
     auto allocFuncName = useAlignedAlloc ? "aligned_alloc" : "malloc";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79230.261436.patch
Type: text/x-patch
Size: 1650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200501/d8e271d3/attachment.bin>


More information about the llvm-commits mailing list