llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LICM][LoopSimplifyCFG] Instruction does not dominate all uses!

Open dbakunevich opened this issue 4 years ago • 13 comments

Godbolt link: https://godbolt.org/z/nxcc7nzTa

Run opt -passes='loop-mssa(licm,loop-simplifycfg)' on the test bug.ll.txt

Failure looks like:

Instruction does not dominate all uses!
  %tmp34 = call token (i64, i32, i8 addrspace(1)* (i64, i32, i32, i32)*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_p1i8i64i32i32i32f(i64 2882400000, i32 0, i8 addrspace(1)* (i64, i32, i32, i32)* nonnull elementtype(i8 addrspace(1)* (i64, i32, i32, i32)) @barney.4, i32 4, i32 0, i64 undef, i32 5, i32 5, i32 undef, i32 0, i32 0) [ "deopt"(), "gc-live"(i8 addrspace(1)* undef) ]
  %tmp35.le = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %tmp34, i32 0, i32 0) ; (undef, undef)
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-mssa(licm,loop-simplifycfg) <source>
 #0 0x000055bed996d55f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055bed996af8c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fdf224013c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x143c0)
 #3 0x00007fdf21ece03b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4303b)
 #4 0x00007fdf21ead859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x000055bed98afbed llvm::report_fatal_error(llvm::Twine const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x34afbed)
 #6 0x000055bed98afd68 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x34afd68)
 #7 0x000055bed911b00a llvm::VerifierPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2d1b00a)
 #8 0x000055bed70a37a1 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xca37a1)
 #9 0x000055bed90e7444 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2ce7444)
#10 0x000055bed70ade8a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::StringRef>, llvm::ArrayRef<llvm::PassPlugin>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xcade8a)
#11 0x000055bed6feb473 main (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xbeb473)
#12 0x00007fdf21eaf0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b3)
#13 0x000055bed70a171a _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xca171a)
Compiler returned: 139

dbakunevich avatar Jun 27 '22 10:06 dbakunevich

Root cause evaluation (tl;dr: here be dragons).

The bug happens on interference of 2 transforms.

Act 1: Before transform, we had the following code:

entry:
  br loop

loop:
  %x = call token foo()
  %y = call i32 bar(%x)
  br true, backedge, exit

backedge:
  br loop

exit:
  %y.lcssa = phi(%y)
  ret %y.lcssa

LICM decides that it can sink %y to the loop exit block. Normally, it would require to create LCSSA phis for all arguments of %y that are defined in the loop. However, the only such argument is %x which has the type of token. It is impossible to create a Phi node of type token. For some reason I don't really understand, instead of prohibiting the transform, LICM decides to still sink without LCSSA phi. Result is like

entry:
  br loop

loop:
  %x = call token foo()
  br true, backedge, exit

backedge:
  br loop

exit:
  %y.le = call i32 bar(%x)
  ret %y.lcssa

Verifier says "this is fine" and does not complain about broken LCSSA form, because it's a token.

Act 2: optimization that relies on LCSSA form

It is not really important what exactly the transform does. This might have happened to any transform that expects that loop passes preserve LCSSA for all values. Here is what happens. LoopSimplifyCFG tries to remove br true, backedge, exit, because the exit branch is always dead. However, simple removal of such branch would break one of invariants of loop pipeline, specifically, it would make the exit block unreachable from entry, which is not allowed (some reasons related to DomTree AFAIK). So it tries to make this block statically reachable. Here is what it does:

entry:
  switch 0, preheader [1, exit]

preheader:
  br loop

loop:
  %x = call token foo()
  br true, backedge, exit

backedge:
  br loop

exit:
  %y.le = call i32 bar(%x)
  ret %y.lcssa

This trick makes exit still reachable even after the edge loop -> exit will be deleted.

LoopSimplifyCFG expects that the loop is in LCSSA form. When it introduces a new edge to the exit block, it should go and add a new undef input to all LCSSA Phis in exit block, and therefore the SSA form will be sustained. However, the token value has already broken it!

So we cannot fix an existing LCSSA phi or create a new one for %y. LoopSimplifyCFG does not expect this, and it doesn't check or fix anything other than LCSSA phis. As result, we end up with code where %y.le is no longer dominated by its argument.

Here is the story, in short.

The definition of LCSSA I was able to find says: https://llvm.org/docs/LoopTerminology.html#:~:text=the%20loop%20header.-,Loop%20Closed%20SSA%20(LCSSA),but%20not%20necessarily%20in%20LCSSA.

A program is in Loop Closed SSA Form if it is in SSA form and all values
that are defined in a loop are used only inside this loop.

It makes no exception for tokens. According to the definition, a loop that is not in LCSSA form should fail the verification, but it doesn't happen because tokens are explicitly allowed through it.

Digging through code, it seems that we have intentionally made some holes in LCSSA checkers to make this happen. As result, any pass that now expects that LCSSA form is sustained is potentially broken, because of these tokens. So three options here (or maybe all at the same time):

  1. Bug in LICM. It should not have sunk it. In this case, it's still not clear what to do if the initial code would be like this.
  2. Bug in LoopSimplifyCFG. It should not rely on fact that LCSSA form is sustained for loops. In this case, every loop pass that uses knowledge about this is also potentially broken.
  3. Bug in langref, it should allow phis for tokens.

Looks like it's a general problem not only specific to these two passes.

One of patches that introduced the problem is this one: https://reviews.llvm.org/D98728

@preames @fhahn I'd like to know your opinion on this. Do we really want LCSSA to be penetrated by tokens?

xortator avatar Jun 28 '22 11:06 xortator

I'd like to know your opinion on this. Do we really want LCSSA to be penetrated by tokens?

Interesting question! I think for correctness of certain look transformations we need to make sure that LCSSA form is also preserved for tokens. I am not too familiar with the uses for tokens, but I think with respect to LCSSA we have to treat them like any other value.

Preventing movement of instructions defining tokens seems like a reasonable first step to me.

fhahn avatar Jun 29 '22 10:06 fhahn

Yeah, the problem is that LangRef says that https://llvm.org/docs/LangRef.html#token-type

it is not appropriate to have a phi or select of type token.

We can fix this particular one by preventing moving, but what if the test had a token defined in loop and use outside the loop from the very beginning? Verifier says this is fine, but LCSSA form construction here is impossible.

xortator avatar Jun 29 '22 11:06 xortator

Proposed fix (not sure if it's the right way, but still): https://reviews.llvm.org/D128815

xortator avatar Jun 29 '22 13:06 xortator

I think it makes sense to treat this as a bug in LoopSimplifyCFG. The alternative implies that it's possible to have a loop that can't be transformed into LCSSA form, which seems a lot more invasive to change. And I think most transforms that depend on "LCSSA" don't actually care about token values.

efriedma-quic avatar Jun 29 '22 20:06 efriedma-quic

I think it makes sense to treat this as a bug in LoopSimplifyCFG. The alternative implies that it's possible to have a loop that can't be transformed into LCSSA form, which seems a lot more invasive to change. And I think most transforms that depend on "LCSSA" don't actually care about token values.

The problem with this approach is that

  • It is impossible to ensure that no other transform relies on this very fact in this way
  • It is impossible to track when it changes

We do have passes that create extra branches to loop exit blocks, for example loop unroll and unswitch. Where are the guarantees they aren't broken for same reason? It is not prohibited to create edges to exit blocks, I don't see why it is a bug.

We don't even have an utility function that ensures that a loop is in LCSSA form (existing utils contain a hole for tokens). And there is no theoretical justification for that in definition of LCSSA form.

xortator avatar Jun 30 '22 12:06 xortator

If we assume we aren't going to mess with the definition of "token", then most loop passes need some sort of handling for tokens. The only question is what helper functions we implement for loop passes.

We can split the LCSSA checker function into two functions: one checking for LCSSA-ignoring-tokens, and one checking for LCSSA-including-tokens. (We could change which one "Loop::isLCSSAForm" refers to.) But I'm not sure where we'd actually use the LCSSA-including-tokens.

Unroll/unswitch already have explicit checks for tokens.

efriedma-quic avatar Jun 30 '22 17:06 efriedma-quic

Unroll/unswitch already have explicit checks for tokens.

Do you think it's a good practice? It looks like that we've introduced 2 contradictory statements (LCSSA must be sustained for all values and LCSSA Phi cannot be created for token) and then need to hack every loop pass to maintain this.

xortator avatar Jul 01 '22 07:07 xortator

If you treat the very existence of both the LCSSA transform and the "token" type as a contradiction, then the only solution is to get rid of one of them. Getting rid of one of them doesn't seem like a path forward, though; they're both useful aspects of LLVM IR, so we need to reconcile them somehow.

Changing unroll/unswitch/etc. to use a global "LCSSA-including-tokens" predicate, as opposed to sticking the check into the middle of the cost modeling, might be easier to understand. But we'd have to introduce some mechanism to cache that information, and caches introduce their own complexity.

We could introduce some equivalent of "LCSSA PHI" specifically for tokens, I guess. But we'd have to add support for it to a bunch of non-loop passes.

Basically, I don't see a way forward that's really better than the status quo of requiring each loop pass to handle tokens as necessary.

efriedma-quic avatar Jul 01 '22 18:07 efriedma-quic

API to detect this situation: https://reviews.llvm.org/D129983

xortator avatar Jul 18 '22 07:07 xortator

Alternative fix proposed by Eli: https://reviews.llvm.org/D129984

xortator avatar Jul 18 '22 07:07 xortator

commit 82309831c3ad922874221cfa47b7deda5607448e (HEAD -> main, origin/main, origin/HEAD)
Author: Max Kazantsev <[email protected]>
Date:   Tue Jul 19 15:22:44 2022 +0700

    [LoopSimplifyCFG] Prevent use-def dominance breach by handling dead exits. PR56243

    One of the transforms in LoopSimplifyCFG demands that the LCSSA form is
    truly maintained for all values, tokens included, otherwise it may end up creating
    a use that is not dominated by def (and Phi creation for tokens is impossible).
    Detect this situation and prevent transform for it early.

    Differential Revision: https://reviews.llvm.org/D129984
    Reviewed By: efriedma

xortator avatar Jul 19 '22 09:07 xortator

@max-quazan just FYI, if you include something like Fixes #56243 the Github issue will auto-close after the commit is pushed to main.

fhahn avatar Jul 19 '22 11:07 fhahn