@@ -230,6 +230,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
230230 bool selectSpvThreadId (Register ResVReg, const SPIRVType *ResType,
231231 MachineInstr &I) const ;
232232
233+ bool selectWaveActiveCountBits (Register ResVReg, const SPIRVType *ResType,
234+ MachineInstr &I) const ;
235+
233236 bool selectWaveReadLaneAt (Register ResVReg, const SPIRVType *ResType,
234237 MachineInstr &I) const ;
235238
@@ -1762,6 +1765,38 @@ bool SPIRVInstructionSelector::selectSign(Register ResVReg,
17621765 return Result;
17631766}
17641767
1768+ bool SPIRVInstructionSelector::selectWaveActiveCountBits (Register ResVReg,
1769+ const SPIRVType *ResType,
1770+ MachineInstr &I) const {
1771+ assert (I.getNumOperands () == 3 );
1772+ assert (I.getOperand (2 ).isReg ());
1773+ MachineBasicBlock &BB = *I.getParent ();
1774+
1775+ Register BallotReg = MRI->createVirtualRegister (&SPIRV::IDRegClass);
1776+ SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType (32 , I, TII);
1777+ SPIRVType *BallotType = GR.getOrCreateSPIRVVectorType (IntTy, 4 , I, TII);
1778+
1779+ bool Result =
1780+ BuildMI (BB, I, I.getDebugLoc (),
1781+ TII.get (SPIRV::OpGroupNonUniformBallot))
1782+ .addDef (BallotReg)
1783+ .addUse (GR.getSPIRVTypeID (BallotType))
1784+ .addUse (GR.getOrCreateConstInt (SPIRV::Scope::Subgroup, I, IntTy, TII))
1785+ .addUse (I.getOperand (2 ).getReg ());
1786+
1787+ Result |=
1788+ BuildMI (BB, I, I.getDebugLoc (),
1789+ TII.get (SPIRV::OpGroupNonUniformBallotBitCount))
1790+ .addDef (ResVReg)
1791+ .addUse (GR.getSPIRVTypeID (ResType))
1792+ .addUse (GR.getOrCreateConstInt (SPIRV::Scope::Subgroup, I, IntTy, TII))
1793+ .addImm (0 )
1794+ .addUse (BallotReg)
1795+ .constrainAllUses (TII, TRI, RBI);
1796+
1797+ return Result;
1798+ }
1799+
17651800bool SPIRVInstructionSelector::selectWaveReadLaneAt (Register ResVReg,
17661801 const SPIRVType *ResType,
17671802 MachineInstr &I) const {
@@ -2559,6 +2594,8 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
25592594 } break ;
25602595 case Intrinsic::spv_saturate:
25612596 return selectSaturate (ResVReg, ResType, I);
2597+ case Intrinsic::spv_wave_active_countbits:
2598+ return selectWaveActiveCountBits (ResVReg, ResType, I);
25622599 case Intrinsic::spv_wave_is_first_lane: {
25632600 SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType (32 , I, TII);
25642601 return BuildMI (BB, I, I.getDebugLoc (),
0 commit comments