This is an archive of the discontinued LLVM Phabricator instance.

[mlir][mem2reg] Add support for mem2reg in MemRef.
ClosedPublic

Authored by Moxinilian on Apr 28 2023, 6:07 AM.

Details

Summary

This patch implements the mem2reg interfaces for MemRef types. This only supports scalar memrefs of a small list of types. It would be beneficial to create more interfaces for default values before expanding support to more types. Additionally, I am working on an upcoming revision to bring SROA to MLIR that should help with non-scalar memrefs.

Diff Detail

Event Timeline

Moxinilian created this revision.Apr 28 2023, 6:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2023, 6:07 AM
Moxinilian requested review of this revision.Apr 28 2023, 6:07 AM
kuhar added inline comments.Apr 28 2023, 7:52 AM
mlir/lib/Dialect/MemRef/IR/MemRefMem2Reg.cpp
102

nit: return {}; or return nullptr; for consistency with other code, e.g., LoadOp::getStored ?

mlir/test/Transforms/mem2reg-memref.mlir
136 ↗(On Diff #517890)

Could we match the function argument here? This way it'd be easier to tell why the load below remained.

kuhar added inline comments.Apr 28 2023, 7:58 AM
mlir/lib/Dialect/MemRef/IR/MemRefMem2Reg.cpp
85–86

Do any of the testcases exercise this logic? It'd difficult for me to tell based just on the function names (used in tests)

85–86

By exercise I mean having tests where this condition both succeeds and fails

Is there any limitation on the sizes of buffers that can be promoted? Obviously you don't want memref<16x16xf64> to get promoted.

mlir/test/Transforms/mem2reg-memref.mlir
1 ↗(On Diff #517890)

I believe the policy is that this test should go with the rest of the MemRef dialect tests, not in the Transforms tests

Matt added a subscriber: Matt.May 1 2023, 1:58 PM
Moxinilian updated this revision to Diff 518659.May 2 2023, 2:33 AM
Moxinilian marked 3 inline comments as done.

Address comments.

Thanks for the reviews!

mlir/lib/Dialect/MemRef/IR/MemRefMem2Reg.cpp
39

@Mogball This implementation only allows mem2reg on scalar memrefs, this is the check enforcing that.

85–86

getMemRef() == slot.ptr for stores is exercised by deny_store_of_alloca.

For the rest, this logic is hard to trigger from mem2reg because it would typically mean that the interface implementation for allocators is buggy. So it could make sense to make this an assert it in theory, but the interface specification is more flexible than that, expecting no crash. Notably, nothing prevents a third-party user of the interface to ask for the use of a slot that has nothing to do with this load. Considering how the testing infrastructure works, I feel like the burden of testing falls on the user here, because I don't really know how to test it in practice. I would like to hear your opinion about this.

I still added a test (promotable_nonpromotable_intertwined) that would be likely to trigger this in case of bug.

mlir/test/Transforms/mem2reg-memref.mlir
1 ↗(On Diff #517890)

Okay! I also moved the LLVM tests which were put in the same place.

gysit accepted this revision.May 3 2023, 12:05 AM

LGTM modulo nit comment.

mlir/lib/Dialect/MemRef/IR/MemRefMem2Reg.cpp
48
50–51

Is it possible to move MemRefType into the lambda?

i.e.:

.Case([&](MemRefType type) {
  return builder.create<memref::AllocaOp>(getLoc(), type);
})
This revision is now accepted and ready to land.May 3 2023, 12:05 AM
Moxinilian marked 2 inline comments as done.

Address comments.

Mark as done.

Mogball accepted this revision.May 4 2023, 2:39 AM
Mogball added inline comments.
mlir/lib/Dialect/MemRef/IR/MemRefMem2Reg.cpp
39

swag

This revision was automatically updated to reflect the committed changes.