@@ -256,6 +256,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
256256 bool selectSpvThreadId (Register ResVReg, const SPIRVType *ResType,
257257 MachineInstr &I) const ;
258258
259+ bool selectWaveActiveCountBits (Register ResVReg, const SPIRVType *ResType,
260+ MachineInstr &I) const ;
261+
259262 bool selectWaveReadLaneAt (Register ResVReg, const SPIRVType *ResType,
260263 MachineInstr &I) const ;
261264
@@ -1917,6 +1920,38 @@ bool SPIRVInstructionSelector::selectSign(Register ResVReg,
19171920 return Result;
19181921}
19191922
1923+ bool SPIRVInstructionSelector::selectWaveActiveCountBits (Register ResVReg,
1924+ const SPIRVType *ResType,
1925+ MachineInstr &I) const {
1926+ assert (I.getNumOperands () == 3 );
1927+ assert (I.getOperand (2 ).isReg ());
1928+ MachineBasicBlock &BB = *I.getParent ();
1929+
1930+ Register BallotReg = MRI->createVirtualRegister (&SPIRV::IDRegClass);
1931+ SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType (32 , I, TII);
1932+ SPIRVType *BallotType = GR.getOrCreateSPIRVVectorType (IntTy, 4 , I, TII);
1933+
1934+ bool Result =
1935+ BuildMI (BB, I, I.getDebugLoc (),
1936+ TII.get (SPIRV::OpGroupNonUniformBallot))
1937+ .addDef (BallotReg)
1938+ .addUse (GR.getSPIRVTypeID (BallotType))
1939+ .addUse (GR.getOrCreateConstInt (SPIRV::Scope::Subgroup, I, IntTy, TII))
1940+ .addUse (I.getOperand (2 ).getReg ());
1941+
1942+ Result |=
1943+ BuildMI (BB, I, I.getDebugLoc (),
1944+ TII.get (SPIRV::OpGroupNonUniformBallotBitCount))
1945+ .addDef (ResVReg)
1946+ .addUse (GR.getSPIRVTypeID (ResType))
1947+ .addUse (GR.getOrCreateConstInt (SPIRV::Scope::Subgroup, I, IntTy, TII))
1948+ .addImm (0 )
1949+ .addUse (BallotReg)
1950+ .constrainAllUses (TII, TRI, RBI);
1951+
1952+ return Result;
1953+ }
1954+
19201955bool SPIRVInstructionSelector::selectWaveReadLaneAt (Register ResVReg,
19211956 const SPIRVType *ResType,
19221957 MachineInstr &I) const {
@@ -2739,6 +2774,8 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
27392774 } break ;
27402775 case Intrinsic::spv_saturate:
27412776 return selectSaturate (ResVReg, ResType, I);
2777+ case Intrinsic::spv_wave_active_countbits:
2778+ return selectWaveActiveCountBits (ResVReg, ResType, I);
27422779 case Intrinsic::spv_wave_is_first_lane: {
27432780 SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType (32 , I, TII);
27442781 return BuildMI (BB, I, I.getDebugLoc (),
0 commit comments