This is an archive of the discontinued LLVM Phabricator instance.

[mlir] [llvm] Add memset support for mem2reg/sroa.
ClosedPublic

Authored by Moxinilian on Jun 7 2023, 5:40 AM.

Details

Summary

This revision introduces support for memset intrinsics in SROA and
mem2reg for the LLVM dialect. This is achieved for SROA by breaking
memsets of aggregates into multiple memsets of scalars, and for mem2reg
by promoting memsets of single integer slots into the value the memset
operation would yield.

The SROA logic supports breaking memsets of static size operating at the
start of a memory slot. The intended most common case is for memsets
covering the entirety of a struct, most often as a way to initialize it
to 0.

The mem2reg logic supports dynamic values and static sizes as input to
promotable memsets. This is achieved by lowering memsets into
ceil(log_2(n)) LeftShift operations, ceil(log_2(n)) Or operations
and up to one ZExt operation (for n the byte width of the integer),
computing in registers the integer value the memset would create. Only
byte-aligned integers are supported, more types could easily be added
afterwards.

Diff Detail

Event Timeline

Moxinilian created this revision.Jun 7 2023, 5:40 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Moxinilian requested review of this revision.Jun 7 2023, 5:40 AM
Moxinilian updated this revision to Diff 529268.Jun 7 2023, 5:43 AM
Moxinilian edited the summary of this revision. (Show Details)

Remove accidental header includes.

gysit added inline comments.Jun 7 2023, 7:15 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
424

nit: Can you add a doc comment for the static helper methods?

486

nit: we could probably simplify the logic by extending the while loop by one more iteration. The shl operation does not produce a poison value by default if the shift overflows.

582

I think we need to take struct padding into account?

Moxinilian updated this revision to Diff 529580.Jun 8 2023, 6:53 AM
Moxinilian marked 3 inline comments as done.

Address comments.

Address comments.

gysit added a comment.Jun 9 2023, 12:55 AM

Thanks, things are already looking very good!

I have some more comments/questions regarding alias analysis metadata and packed structs.

mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
26

ultra nit: can you double check all the headers are needed. I would assume at least MLIRContext is included transitively.

496

ultra nit: Would it make sense to put

// TODO: Support non-integer types.

Here and in the type switch above.

555

Should we take the packed flag into account here? The difficulty may be that we may work on an array or a struct here?

572–575

Cloning here also copies possible alias analysis and access group metadata. Do you know if LLVM handles alias analysis metadata when splitting a memset? It seems like the newly created memset may alias with less other memory access and tbaa metadata probably does not make sense anymore. I doubt we can accurately compute the new alias set though. In case an alias analysis handling is needed dropping the alias information, for example, by creating a new memset operation using its builder, may make more sense.

mlir/test/Dialect/LLVMIR/sroa-intrinsics.mlir
21

Should we add a // CHECK: return %[[ALLOCA]]. I assume there is no gep needed after sroa?

39

nit: 6 bytes?

Moxinilian updated this revision to Diff 529919.Jun 9 2023, 5:08 AM
Moxinilian marked 6 inline comments as done.

Address comments.

mlir/test/Dialect/LLVMIR/sroa-intrinsics.mlir
21

I think this is not necessary because this is already tested elsewhere. I am just putting those return values to make sure the elements are actually used, but I am only testing how memset reacts to that.

Moxinilian updated this revision to Diff 529923.Jun 9 2023, 5:22 AM

Actually test packing.

gysit accepted this revision.Jun 9 2023, 5:30 AM

Thanks!

LGTM

This revision is now accepted and ready to land.Jun 9 2023, 5:30 AM

Rebase to latest main.

This revision was automatically updated to reflect the committed changes.