This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyLibcalls] Fold malloc + memset to calloc even for llvm.memset
AbandonedPublic

Authored by xbolva00 on Apr 5 2018, 5:41 PM.

Details

Diff Detail

Event Timeline

xbolva00 created this revision.Apr 5 2018, 5:41 PM
xbolva00 updated this revision to Diff 141256.Apr 5 2018, 6:48 PM
xbolva00 updated this revision to Diff 141259.Apr 5 2018, 7:07 PM

@xbolva00 please always always upload all the patches with the full context (-U99999)

test/Transforms/InstCombine/memset-1.ll
30

There is no https://bugs.llvm.org/show_bug.cgi?id=45344
You mean D45344?
Then it may be better not to use such a specific name.

xbolva00 updated this revision to Diff 141288.Apr 6 2018, 12:56 AM

Better test

(The diff still seem to be lacking context)

xbolva00 updated this revision to Diff 141289.Apr 6 2018, 1:11 AM
xbolva00 marked an inline comment as done.

Fixed previous test, now it passes

xbolva00 updated this revision to Diff 141290.Apr 6 2018, 1:12 AM

Added context

xbolva00 added a comment.EditedApr 6 2018, 1:13 AM

(The diff still seem to be lacking context)

Yes, fixed now :D I am sorry for too many updated revisions, it is my first contribution to llvm :D

xbolva00 retitled this revision from Fold malloc + memset to calloc even for llvm.memset to [InstCombine] Fold malloc + memset to calloc even for llvm.memset.Apr 6 2018, 1:15 AM
spatel added a comment.Apr 6 2018, 9:21 AM

IIUC, these are 2 independent changes, so they should be split into separate patches:

  1. Handle the memset intrinsic identically to the way we handle the memset libcall. What about memset_chk()?
  2. Remove the hasOneUse() limitation on the malloc. The reason that's there is discussed in D16337 - the transform must be made safe from intermediate stores. This patch as-is will miscompile that pattern.

I've added tests at rL329412 and regenerated the check lines. Please fix/limit this patch and rebase.

xbolva00 updated this revision to Diff 141382.Apr 6 2018, 11:12 AM

IIUC, these are 2 independent changes, so they should be split into separate patches:

  1. Handle the memset intrinsic identically to the way we handle the memset libcall. What about memset_chk()?
  2. Remove the hasOneUse() limitation on the malloc. The reason that's there is discussed in D16337 - the transform must be made safe from intermediate stores. This patch as-is will miscompile that pattern.

I've added tests at rL329412 and regenerated the check lines. Please fix/limit this patch and rebase.

  1. memset_chk change was removed from this commit.
  2. https://reviews.llvm.org/D45381
xbolva00 updated this revision to Diff 141388.Apr 6 2018, 11:25 AM

Tests?

Without https://reviews.llvm.org/D45381 is llvm.memset not folded to calloc.

Tests?

Your patch https://reviews.llvm.org/rL329412 (@malloc_and_memset_intrinsic) needs to be updated after these two patches are applied.

xbolva00 updated this revision to Diff 141424.Apr 6 2018, 2:16 PM

Added test

spatel added a comment.Apr 6 2018, 2:49 PM

I'm confused. This patch is supposed to show a calloc transform.

@malloc_and_memset_intrinsic has multiple uses of the malloc result, so the transform doesn't work there. Is there some other way to show the transform? If not, then sorry that I misunderstood, but we can't split the patches.

But D45381 just removes the one-use check and miscompiles @buffer_is_modified_then_memset. Why is that ok?

xbolva00 retitled this revision from [InstCombine] Fold malloc + memset to calloc even for llvm.memset to [SimplifyLibcalls] Fold malloc + memset to calloc even for llvm.memset.Apr 6 2018, 3:13 PM
xbolva00 updated this revision to Diff 141486.Apr 7 2018, 4:57 AM

I'm confused. This patch is supposed to show a calloc transform.

@malloc_and_memset_intrinsic has multiple uses of the malloc result, so the transform doesn't work there. Is there some other way to show the transform? If not, then sorry that I misunderstood, but we can't split the patches.

But D45381 just removes the one-use check and miscompiles @buffer_is_modified_then_memset. Why is that ok?

I closed D45381 & reupdated this patch, added test case. Test @buffer_is_modified_then_memset is ok now too.

spatel requested changes to this revision.Apr 7 2018, 8:01 AM

If I apply this patch, the test case fails.

Do you understand the problem with the intervening store? You can't solve that by simply removing the one-use check.

This revision now requires changes to proceed.Apr 7 2018, 8:01 AM

If I apply this patch, the test case fails.

Do you understand the problem with the intervening store? You can't solve that by simply removing the one-use check.

Oh, yes :( Any idea how to work around it?

nlopes added a subscriber: nlopes.Apr 7 2018, 10:35 AM
xbolva00 updated this revision to Diff 141495.Apr 7 2018, 10:55 AM
xbolva00 added a comment.EditedApr 7 2018, 10:57 AM

I added solution.

Simply check any Instruction between malloc and memset if use value from malloc, if so, do not fold.

xbolva00 updated this revision to Diff 141497.Apr 7 2018, 11:09 AM

Removed old test and added new one. Works for me.

xbolva00 updated this revision to Diff 141499.Apr 7 2018, 11:20 AM
xbolva00 updated this revision to Diff 141510.Apr 7 2018, 1:47 PM

Anything to be done? Or is this ok? @efriedma , @spatel...

xbolva00 abandoned this revision.Apr 13 2018, 2:54 AM
xbolva00 updated this revision to Diff 144399.Apr 27 2018, 2:22 PM

Patch reworked.

  • Malloc + llvm.memset folding
  • Store instructions eliminated between malloc and memset calls.

Can you look at it? @spatel @efriedma

This transform really belongs in DeadStoreElimination, not here; it has the relevant logic and some similar transforms.

This transform really belongs in DeadStoreElimination, not here; it has the relevant logic and some similar transforms.

But folding part for intrinsic memset can be here, no?

I mean the whole foldMallocMemset transform should be in DSE. I mean, arguably the transform isn't actually eliminating a dead store, but the code you need is very similar to eliminateNoopStore.

xbolva00 abandoned this revision.Apr 27 2018, 3:28 PM

I mean the whole foldMallocMemset transform should be in DSE. I mean, arguably the transform isn't actually eliminating a dead store, but the code you need is very similar to eliminateNoopStore.

Right, so I will close this for now..