This is an archive of the discontinued LLVM Phabricator instance.

[Coroutines] Do not add alloca to the frame if the size is 0
ClosedPublic

Authored by lxfind on May 4 2021, 8:48 AM.

Details

Summary

This patch is to address https://bugs.llvm.org/show_bug.cgi?id=49916.
When the size of an alloca is 0, it will trigger an assertion in OptimizedStructLayout when being added to the frame.
Fix it by not adding it at all. We return index 0 (beginning of the frame) for all 0-sized allocas.

Diff Detail

Event Timeline

lxfind created this revision.May 4 2021, 8:48 AM
lxfind requested review of this revision.May 4 2021, 8:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2021, 8:48 AM

Can you test this with some code that doesn't bitcast the zero-sized alloca before its use? Just slightly worried that we might assert because of the type difference.

Assuming that that isn't a problem, LGTM.

lxfind updated this revision to Diff 342797.May 4 2021, 10:32 AM

Add allocas without cast in test case

rjmccall accepted this revision.May 4 2021, 11:02 AM

Great, thanks.

This revision is now accepted and ready to land.May 4 2021, 11:02 AM
This revision was landed with ongoing or failed builds.May 4 2021, 12:55 PM
This revision was automatically updated to reflect the committed changes.
lanza added a subscriber: lanza.May 4 2021, 3:56 PM
ChuanqiXu added inline comments.May 6 2021, 12:28 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

The checks for foo.resume fails in my environment. My output was:

define internal fastcc void @foo.resume(%foo.Frame* noalias nonnull align 8 dereferenceable(24) %FramePtr) {
entry.resume:
  %vFrame = bitcast %foo.Frame* %FramePtr to i8*
  %a1.reload.addr = getelementptr inbounds %foo.Frame, %foo.Frame* %FramePtr, i64 0, i32 2
  %a4.reload.addr = getelementptr inbounds %foo.Frame, %foo.Frame* %FramePtr, i64 0, i32 3
  %a0.reload.addr = bitcast %foo.Frame* %FramePtr to [0 x i8]*
  %a4.cast = bitcast i16* %a4.reload.addr to i8*
  %a1.cast = bitcast i32* %a1.reload.addr to i8*
  tail call void @usePointer(i8* nonnull %vFrame)
  tail call void @usePointer(i8* nonnull %a1.cast)
  tail call void @usePointer(i8* nonnull %vFrame)
  tail call void @usePointer(i8* nonnull %vFrame)
  tail call void @usePointer(i8* nonnull %a4.cast)
  tail call void @usePointer2([0 x i8]* nonnull %a0.reload.addr)
  tail call void @free(i8* nonnull %vFrame)
  ret void
}

It also looks good without failing to match.

lxfind added inline comments.May 6 2021, 8:33 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

That's strange. Seems the frame placement isn't deterministic?

rjmccall added inline comments.May 6 2021, 12:38 PM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

a3.cast is a redundant bitcast, which some passes will eliminate. Maybe the test is accidentally testing more than just the result of the split pass?

ChuanqiXu added inline comments.May 7 2021, 2:56 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

My bad. It looks like I pollute my code locally. After I cloned a new version, everything looks good.

djtodoro added inline comments.
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

I think that this test still fails: https://lab.llvm.org/buildbot/#/builders/123/builds/3751

On my RHEL7, it also fails.

djtodoro added inline comments.May 31 2021, 2:58 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

ping

ChuanqiXu added inline comments.May 31 2021, 3:02 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

It is really interesting. I fix this problem by cloning a new clean LLVM source. Did you try that?

djtodoro added inline comments.May 31 2021, 3:09 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

It is still failing on my machine.

ChuanqiXu added inline comments.May 31 2021, 4:23 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

Do you test this with upstream or downstream? It is OK in trunk in my machine.

djtodoro added inline comments.May 31 2021, 4:55 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

upstream

lxfind added inline comments.May 31 2021, 9:05 AM
llvm/test/Transforms/Coroutines/coro-zero-alloca.ll
64–81

Let me simplify the test so it doesn't run the entire O2