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.
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.