<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/231171>231171</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [SPIR-V] Crash in SPIRVModuleAnalysis on PHI operands of fcmp/atomic store since #229736
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          maleadt
      </td>
    </tr>
</table>

<pre>
    Since #229736, `SPIRVModuleAnalysis` crashes when an instruction whose requirements depend on an operand's type takes a PHI as that operand. This happens for any SPIR-V target at any optimization level.

```llvm
; llc -O0 -mtriple=spirv64-unknown-unknown phi.ll -o /dev/null
define spir_func i1 @f(float %x, float %y, i1 %c) {
entry:
  br i1 %c, label %then, label %exit

then:
  br label %exit

exit:
  %v = phi float [ %x, %entry ], [ %y, %then ]
  %r = fcmp olt float %v, %y
  ret i1 %r
}
```

```
llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp:25: llvm::SPIRVTypeInst::SPIRVTypeInst(const llvm::MachineInstr*): Assertion `!MI || definesATypeRegister(*MI)' failed.
...
#11 addInstrRequirements(llvm::MachineInstr const&, llvm::SPIRV::ModuleAnalysisInfo&, llvm::SPIRVSubtarget const&)
#12 collectReqs(...)
```

An atomic store of a PHI value (`store atomic i32 %v, ptr addrspace(1) %p seq_cst, align 4`) fails the same way. Both cases worked before #229736.

`addInstrRequirements` finds an operand's type through the type operand of the operand's definition, e.g. `MRI.getVRegDef(MI.getOperand(2).getReg())->getOperand(1)` for the relational opcodes, and `InstrPtr->getOperand(1)` for `OpAtomicStore`. `patchPhis` used to turn `PHI` into `OpPhi` with a result-type operand before this ran. #229736 now leaves PHIs alone until `SPIRVMCInstLower`, so operand 1 of the definition is the first incoming value, not a type. The dot-product input check in the same function uses the same pattern.

cc @aobolensk

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org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