This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][crashfix] Fall back to malloc when alignment is 0 for align-alloc
Needs RevisionPublic

Authored by kernhanda on May 1 2020, 12:39 AM.

Details

Reviewers
ftynse
bondhugula
Summary

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.

Diff Detail

Event Timeline

kernhanda created this revision.May 1 2020, 12:39 AM
Herald added a project: Restricted Project. · View Herald Transcript
kernhanda updated this revision to Diff 261437.May 1 2020, 12:43 AM

clang-format

bondhugula requested changes to this revision.May 1 2020, 1:11 AM
bondhugula added a subscriber: bondhugula.
bondhugula added inline comments.
mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
1524

The alignment is actually required to be a power of two for aligned_alloc. The code above was written with the assumption that the alignment would be a power of two. Unfortunately, we don't document what a valid alignment on the AllocOp is. Unlike llvm's alloca where a 0 alignment is treated the same as not specifying an alignment, we could make the alloc op's verifier fail for non power of two alignments. This will fix this case as well - since zero is not a power of two.

This fix will silently fall back to malloc for '0' alignment instead of trying to use aligned_alloc with elt size alignment (which is what happens if no alignment is set). If we wanted to handle this case, the right fix would be to check for a non-power of two alignment (in getAllocationAlignment), and use the default elt size based alignment while still using aligned_alloc.

This revision now requires changes to proceed.May 1 2020, 1:11 AM
kernhanda marked an inline comment as done.May 1 2020, 1:21 AM
kernhanda added inline comments.
mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
1524

I'm okay with either of the alternatives, if others have an opinion.

ftynse added inline comments.May 4 2020, 2:20 AM
mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
1524

I agree with @bondhugula that it should be fixed at the op verifier level. Arguably, silently falling back to the strategy that was explicitly disabled by an option (i.e. using malloc instead of aligned_alloc) is a surprising and undesirable behavior.