This is an archive of the discontinued LLVM Phabricator instance.

[mlir][memref] Fix num elements in lowering of memref.alloca op to LLVM
ClosedPublic

Authored by fmorac on May 16 2023, 12:12 PM.

Details

Summary

Fixes a mistake in the lowering of memref.alloca to llvm.alloca, as llvm.alloca uses the number of elements to allocate in the stack and not the size in bytes.

Reference:
LLVM IR: https://llvm.org/docs/LangRef.html#alloca-instruction
LLVM MLIR: https://mlir.llvm.org/docs/Dialects/LLVM/#llvmalloca-mlirllvmallocaop

Diff Detail

Event Timeline

fmorac created this revision.May 16 2023, 12:12 PM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
fmorac updated this revision to Diff 523036.May 17 2023, 6:54 AM

Fixed the tests.

fmorac edited the summary of this revision. (Show Details)May 17 2023, 6:57 AM
fmorac added a reviewer: mehdi_amini.
fmorac published this revision for review.May 17 2023, 7:13 AM
fmorac added inline comments.
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
108

The bug happens here, it shouldn't be sizeBytes, instead it should be sizeBytes / sizeof(elementType).

ftynse requested changes to this revision.May 17 2023, 7:20 AM

We should be already computing the number of elements to get the size in bytes. I don't think we want to first multiply that with the byte size only to divide it back. Consider refactoring allocateBuffer so it accepts the number of elements instead.

This revision now requires changes to proceed.May 17 2023, 7:20 AM

We should be already computing the number of elements to get the size in bytes. I don't think we want to first multiply that with the byte size only to divide it back. Consider refactoring allocateBuffer so it accepts the number of elements instead.

I agree with you, this was the quick fix. A proper solution requires modifying more parts.

We first need a function to compute the numElements in general:

As getMemRefDescriptorSizes always return the size in bytes, and getNumElements doesn't automatically handle static shapes.
https://github.com/llvm/llvm-project/blob/main/mlir/lib/Conversion/LLVMCommon/Pattern.cpp#L121-L137

I would introduce a new getNumElements with a similar signature as getMemRefDescriptorSizes to compute the number of elements in general.

Since AllocaOpLowering inherits from AllocLikeOpLLVMLowering, either we modify AllocLikeOpLLVMLowering or stop inheriting from it. I prefer stop inheriting from it, as it's alloca is not exactly alloc like.
And the quick fix for modifying AllocLikeOpLLVMLowering is introducing a conditional for testing AllocaOp, which I'm not to thrilled.
https://github.com/llvm/llvm-project/blob/main/mlir/lib/Conversion/MemRefToLLVM/AllocLikeConversion.cpp#L159-L187

My proposal is then: introducing a new getNumElements, stop inheriting from AllocLikeOpLLVMLowering.

fmorac updated this revision to Diff 524318.EditedMay 22 2023, 8:00 AM

Refactors allocateBuffer to use numElements. For achieving the refactor the function ConvertToLLVMPattern::getMemRefDescriptorSizes was modified to accept an optional parameter specifying to return numElements instead of sizeInBytes.

ftynse added inline comments.May 22 2023, 8:17 AM
mlir/lib/Conversion/LLVMCommon/Pattern.cpp
171

Nit: please use braces symmetrically.

204–216

This will no longer handle 0D memrefs correctly, which was special-cased before.

206

Nit: please expand auto unless the type is obvious from context (e.g., there is a cast on the RHS).

mlir/lib/Conversion/MemRefToLLVM/AllocLikeConversion.cpp
177

Nit: leftover commented-out code.

fmorac updated this revision to Diff 524329.May 22 2023, 8:38 AM

Fixed comments.

ftynse accepted this revision.May 22 2023, 9:12 AM
This revision is now accepted and ready to land.May 22 2023, 9:12 AM
This revision was automatically updated to reflect the committed changes.
fmorac marked an inline comment as done.