This is an archive of the discontinued LLVM Phabricator instance.

[MemRefToLLVM] Fix the lowering of memref.assume_alignment
ClosedPublic

Authored by qcolombet on Apr 21 2023, 8:40 AM.

Details

Summary

memref.assume_alignment annotates the alignment of the source buffer
not the base pointer.
Put differently, prior to this patch memref.assume_alignment would lower
to llvm.assume %buffer.base.isAligned(X) whereas what we want is
llvm.assume (%buffer.base + %buffer.offset).isAligned(X).
In other words, we were missing to include the offset in the expression
checked by the llvm.assume.

Diff Detail

Event Timeline

qcolombet created this revision.Apr 21 2023, 8:40 AM
Herald added a project: Restricted Project. · View Herald Transcript
qcolombet requested review of this revision.Apr 21 2023, 8:40 AM
qcolombet added inline comments.
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
372

I was talking with @mravishankar offline and he pointed out that if we are aligned at (alignPtr + offset) then alignPtr must be aligned at least with the same alignment.
I.e., we could issue two assumes here:

  • one for the aligned base pointer.
  • one for the aligned base pointer + offset.

It sounded like useful information to lower.

What do you guys think?

mravishankar added inline comments.Apr 21 2023, 11:48 AM
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
372

To clarify this a bit. If you need an basePtr + offset to be aligned to a value v, you need both basePtr and offset to be aligned to v. Without that you cannot guarantee that basePtr + offset is always aligned to v for all values of basePtr.
This is the reverse logic though. Here it is saying basePtr + offset is aligned, but I am not sure if can always assume that basePtr is aligned. Maybe we skip it for now... In most common case though, basePtr is also aligned. We can let the user (in this case IREE) generate assume for both.

mravishankar accepted this revision.Apr 21 2023, 1:29 PM

Looks good to me. It is strange that `memref.assume_alignment %0, 16 : memref<4x4xf16, strided<[?, ?], offset: ?>>` refers to the pointer after offset, and not the base. But I understand the reasoning here.

This revision is now accepted and ready to land.Apr 21 2023, 1:29 PM
qcolombet marked an inline comment as done.Apr 25 2023, 9:00 PM
qcolombet added inline comments.
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
372

Thinking about it a little more, I think it is better to let the producers of memref.assume_alignment to separately generate one for the base pointer if they want.
I can think of counter examples where this wouldn't be correct to issue that assume.

This revision was automatically updated to reflect the committed changes.
qcolombet marked an inline comment as done.