Skip to content

[GISel] applyExtendThroughPhis is unsound for ops with multiple destination operands #77762

Closed
@widlarizer

Description

@widlarizer

Discovered in a proprietary fork, so no reproducer.

before:

%91:_(s32), %92:_(s32) = G_UNMERGE_VALUES %8:_(s64) ; g_105.f0, g_105.f1,pad
...
%75:_(s32) = G_PHI %92:_(s32), %bb.4, %81:_(s32), %bb.3, %95:_(s32), %bb.2 ; g_105.f1,pad

combiner says:

Try combining %75:_(s32) = G_PHI %92:_(s32), %bb.4, %81:_(s32), %bb.3, %95:_(s32), %bb.2
Applying rule 'extend_through_phis'
Creating: G_ANYEXT

CSEInfo::Recording new MI G_ANYEXT
Creating: G_ANYEXT

CSEInfo::Recording new MI G_ANYEXT
Creating: G_ANYEXT

CSEInfo::Recording new MI G_ANYEXT
Creating: %103:_(s64) = G_PHI %120:_(s64), %bb.4, %121:_(s64), %bb.3, %122:_(s64), %bb.2

Erasing: %103:_(s64) = G_ANYEXT %75:_(s32)

Created: %120:_(s64) = G_ANYEXT %91:_(s32)
Created: %121:_(s64) = G_ANYEXT %81:_(s32)
Created: %122:_(s64) = G_ANYEXT %95:_(s32)
Created: %103:_(s64) = G_PHI %120:_(s64), %bb.4, %121:_(s64), %bb.3, %122:_(s64), %bb.2

what I guess it looks like after this one combiner rule:

%120:_(s64) = G_ANYEXT %91:_(s32)
%103:_(s64) = G_PHI %120:_(s64), %bb.4, %121:_(s64), %bb.3, %122:_(s64), %bb.2

after all of post legalizer combiner:

%8:_(s64) = G_LOAD %6:_(p0) :: (load (s64) from %ir.1, align 4)
%103:_(s64) = G_PHI %8:_(s64), %bb.4, %121:_(s64), %bb.3, %122:_(s64), %bb.2   ; g_105.f0,f1,pad

You can see that a use of %92 was incorrectly replaced with an anyext use of %91. You can turn this into a crash, "bug scanner"-style, like this (diff line nos likely wrong):

--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -4024,6 +4024,7 @@ void CombinerHelper::applyExtendThroughPhis(MachineInstr &MI,
     Builder.setDebugLoc(MI.getDebugLoc());
     auto NewExt = Builder.buildExtOrTrunc(ExtMI->getOpcode(), ExtTy,
                                           SrcMI->getOperand(0).getReg());
+    assert(MI.getOperand(SrcIdx).getReg() == SrcMI->getOperand(0).getReg());
     OldToNewSrcMap[SrcMI] = NewExt;
   }

PR incoming.

Tagging @aemerson as author of combiner rule extend_through_phis, @andcarminati @agostonrobert as collaborators

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions