@@ -265,6 +265,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
265265 bool selectSpvThreadId (Register ResVReg, const SPIRVType *ResType,
266266 MachineInstr &I) const ;
267267
268+ bool selectSpvGroupThreadId (Register ResVReg, const SPIRVType *ResType,
269+ MachineInstr &I) const ;
270+
268271 bool selectWaveOpInst (Register ResVReg, const SPIRVType *ResType,
269272 MachineInstr &I, unsigned Opcode) const ;
270273
@@ -309,6 +312,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
309312 SPIRVType *widenTypeToVec4 (const SPIRVType *Type, MachineInstr &I) const ;
310313 void extractSubvector (Register &ResVReg, const SPIRVType *ResType,
311314 Register &ReadReg, MachineInstr &InsertionPoint) const ;
315+ bool loadVec3BuiltinInputID (SPIRV::BuiltIn::BuiltIn BuiltInValue,
316+ Register ResVReg, const SPIRVType *ResType,
317+ MachineInstr &I) const ;
312318};
313319
314320} // end anonymous namespace
@@ -2852,6 +2858,8 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
28522858 break ;
28532859 case Intrinsic::spv_thread_id:
28542860 return selectSpvThreadId (ResVReg, ResType, I);
2861+ case Intrinsic::spv_thread_id_in_group:
2862+ return selectSpvGroupThreadId (ResVReg, ResType, I);
28552863 case Intrinsic::spv_fdot:
28562864 return selectFloatDot (ResVReg, ResType, I);
28572865 case Intrinsic::spv_udot:
@@ -3551,30 +3559,29 @@ bool SPIRVInstructionSelector::selectLog10(Register ResVReg,
35513559 .constrainAllUses (TII, TRI, RBI);
35523560}
35533561
3554- bool SPIRVInstructionSelector::selectSpvThreadId (Register ResVReg,
3555- const SPIRVType *ResType,
3556- MachineInstr &I) const {
3557- // DX intrinsic: @llvm.dx.thread.id(i32)
3558- // ID Name Description
3559- // 93 ThreadId reads the thread ID
3560-
3562+ // Generate the instructions to load 3-element vector builtin input
3563+ // IDs/Indices.
3564+ // Like: SV_DispatchThreadID, SV_GroupThreadID, etc....
3565+ bool SPIRVInstructionSelector::loadVec3BuiltinInputID (
3566+ SPIRV::BuiltIn::BuiltIn BuiltInValue, Register ResVReg,
3567+ const SPIRVType *ResType, MachineInstr &I) const {
35613568 MachineIRBuilder MIRBuilder (I);
35623569 const SPIRVType *U32Type = GR.getOrCreateSPIRVIntegerType (32 , MIRBuilder);
35633570 const SPIRVType *Vec3Ty =
35643571 GR.getOrCreateSPIRVVectorType (U32Type, 3 , MIRBuilder);
35653572 const SPIRVType *PtrType = GR.getOrCreateSPIRVPointerType (
35663573 Vec3Ty, MIRBuilder, SPIRV::StorageClass::Input);
35673574
3568- // Create new register for GlobalInvocationID builtin variable.
3575+ // Create new register for the input ID builtin variable.
35693576 Register NewRegister =
35703577 MIRBuilder.getMRI ()->createVirtualRegister (&SPIRV::iIDRegClass);
35713578 MIRBuilder.getMRI ()->setType (NewRegister, LLT::pointer (0 , 64 ));
35723579 GR.assignSPIRVTypeToVReg (PtrType, NewRegister, MIRBuilder.getMF ());
35733580
3574- // Build GlobalInvocationID global variable with the necessary decorations.
3581+ // Build global variable with the necessary decorations for the input ID
3582+ // builtin variable.
35753583 Register Variable = GR.buildGlobalVariable (
3576- NewRegister, PtrType,
3577- getLinkStringForBuiltIn (SPIRV::BuiltIn::GlobalInvocationId), nullptr ,
3584+ NewRegister, PtrType, getLinkStringForBuiltIn (BuiltInValue), nullptr ,
35783585 SPIRV::StorageClass::Input, nullptr , true , true ,
35793586 SPIRV::LinkageType::Import, MIRBuilder, false );
35803587
@@ -3591,12 +3598,12 @@ bool SPIRVInstructionSelector::selectSpvThreadId(Register ResVReg,
35913598 .addUse (GR.getSPIRVTypeID (Vec3Ty))
35923599 .addUse (Variable);
35933600
3594- // Get Thread ID index. Expecting operand is a constant immediate value,
3601+ // Get the input ID index. Expecting operand is a constant immediate value,
35953602 // wrapped in a type assignment.
35963603 assert (I.getOperand (2 ).isReg ());
35973604 const uint32_t ThreadId = foldImm (I.getOperand (2 ), MRI);
35983605
3599- // Extract the thread ID from the loaded vector value.
3606+ // Extract the input ID from the loaded vector value.
36003607 MachineBasicBlock &BB = *I.getParent ();
36013608 auto MIB = BuildMI (BB, I, I.getDebugLoc (), TII.get (SPIRV::OpCompositeExtract))
36023609 .addDef (ResVReg)
@@ -3606,6 +3613,32 @@ bool SPIRVInstructionSelector::selectSpvThreadId(Register ResVReg,
36063613 return Result && MIB.constrainAllUses (TII, TRI, RBI);
36073614}
36083615
3616+ bool SPIRVInstructionSelector::selectSpvThreadId (Register ResVReg,
3617+ const SPIRVType *ResType,
3618+ MachineInstr &I) const {
3619+ // DX intrinsic: @llvm.dx.thread.id(i32)
3620+ // ID Name Description
3621+ // 93 ThreadId reads the thread ID
3622+ //
3623+ // In SPIR-V, llvm.dx.thread.id maps to a `GlobalInvocationId` builtin
3624+ // variable
3625+ return loadVec3BuiltinInputID (SPIRV::BuiltIn::GlobalInvocationId, ResVReg,
3626+ ResType, I);
3627+ }
3628+
3629+ bool SPIRVInstructionSelector::selectSpvGroupThreadId (Register ResVReg,
3630+ const SPIRVType *ResType,
3631+ MachineInstr &I) const {
3632+ // DX intrinsic: @llvm.dx.thread.id.in.group(i32)
3633+ // ID Name Description
3634+ // 95 GroupThreadId Reads the thread ID within the group
3635+ //
3636+ // In SPIR-V, llvm.dx.thread.id.in.group maps to a `LocalInvocationId` builtin
3637+ // variable
3638+ return loadVec3BuiltinInputID (SPIRV::BuiltIn::LocalInvocationId, ResVReg,
3639+ ResType, I);
3640+ }
3641+
36093642SPIRVType *SPIRVInstructionSelector::widenTypeToVec4 (const SPIRVType *Type,
36103643 MachineInstr &I) const {
36113644 MachineIRBuilder MIRBuilder (I);
0 commit comments