Skip to content

Commit 128f381

Browse files
authored
[SPIR-V] Add OpConstantCompositeContinuedINTEL instruction (#129086)
Specification: https://github.khronos.org/SPIRV-Registry/extensions/INTEL/SPV_INTEL_long_composites.html
1 parent 74d4fc0 commit 128f381

File tree

6 files changed

+97
-23
lines changed

6 files changed

+97
-23
lines changed

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,24 +2940,30 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
29402940
case Intrinsic::spv_const_composite: {
29412941
// If no values are attached, the composite is null constant.
29422942
bool IsNull = I.getNumExplicitDefs() + 1 == I.getNumExplicitOperands();
2943-
// Select a proper instruction.
2944-
unsigned Opcode = SPIRV::OpConstantNull;
29452943
SmallVector<Register> CompositeArgs;
2946-
if (!IsNull) {
2947-
Opcode = SPIRV::OpConstantComposite;
2948-
if (!wrapIntoSpecConstantOp(I, CompositeArgs))
2949-
return false;
2950-
}
29512944
MRI->setRegClass(ResVReg, GR.getRegClass(ResType));
2952-
auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(Opcode))
2953-
.addDef(ResVReg)
2954-
.addUse(GR.getSPIRVTypeID(ResType));
2945+
29552946
// skip type MD node we already used when generated assign.type for this
29562947
if (!IsNull) {
2957-
for (Register OpReg : CompositeArgs)
2958-
MIB.addUse(OpReg);
2948+
if (!wrapIntoSpecConstantOp(I, CompositeArgs))
2949+
return false;
2950+
MachineIRBuilder MIR(I);
2951+
SmallVector<MachineInstr *, 4> Instructions = createContinuedInstructions(
2952+
MIR, SPIRV::OpConstantComposite, 3,
2953+
SPIRV::OpConstantCompositeContinuedINTEL, CompositeArgs, ResVReg,
2954+
GR.getSPIRVTypeID(ResType));
2955+
for (auto *Instr : Instructions) {
2956+
Instr->setDebugLoc(I.getDebugLoc());
2957+
if (!constrainSelectedInstRegOperands(*Instr, TII, TRI, RBI))
2958+
return false;
2959+
}
2960+
return true;
2961+
} else {
2962+
auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantNull))
2963+
.addDef(ResVReg)
2964+
.addUse(GR.getSPIRVTypeID(ResType));
2965+
return MIB.constrainAllUses(TII, TRI, RBI);
29592966
}
2960-
return MIB.constrainAllUses(TII, TRI, RBI);
29612967
}
29622968
case Intrinsic::spv_assign_name: {
29632969
auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpName));

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,16 @@ void SPIRVModuleAnalysis::visitDecl(
361361
} else if (Opcode == SPIRV::OpFunction ||
362362
Opcode == SPIRV::OpFunctionParameter) {
363363
GReg = handleFunctionOrParameter(MF, MI, GlobalToGReg, IsFunDef);
364-
} else if (Opcode == SPIRV::OpTypeStruct) {
364+
} else if (Opcode == SPIRV::OpTypeStruct ||
365+
Opcode == SPIRV::OpConstantComposite) {
365366
GReg = handleTypeDeclOrConstant(MI, SignatureToGReg);
366367
const MachineInstr *NextInstr = MI.getNextNode();
367368
while (NextInstr &&
368-
NextInstr->getOpcode() == SPIRV::OpTypeStructContinuedINTEL) {
369+
((Opcode == SPIRV::OpTypeStruct &&
370+
NextInstr->getOpcode() == SPIRV::OpTypeStructContinuedINTEL) ||
371+
(Opcode == SPIRV::OpConstantComposite &&
372+
NextInstr->getOpcode() ==
373+
SPIRV::OpConstantCompositeContinuedINTEL))) {
369374
MCRegister Tmp = handleTypeDeclOrConstant(*NextInstr, SignatureToGReg);
370375
MAI.setRegisterAlias(MF, NextInstr->getOperand(0).getReg(), Tmp);
371376
MAI.setSkipEmission(NextInstr);

llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,13 @@ bool isSpvIntrinsic(const Value *Arg) {
813813

814814
// Function to create continued instructions for SPV_INTEL_long_composites
815815
// extension
816-
void createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode,
817-
unsigned MinWC, unsigned ContinuedOpcode,
818-
ArrayRef<Register> Args,
819-
Register ReturnRegister, Register TypeID) {
816+
SmallVector<MachineInstr *, 4>
817+
createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode,
818+
unsigned MinWC, unsigned ContinuedOpcode,
819+
ArrayRef<Register> Args, Register ReturnRegister,
820+
Register TypeID) {
821+
822+
SmallVector<MachineInstr *, 4> Instructions;
820823
constexpr unsigned MaxWordCount = UINT16_MAX;
821824
const size_t NumElements = Args.size();
822825
size_t MaxNumElements = MaxWordCount - MinWC;
@@ -835,12 +838,16 @@ void createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode,
835838
for (size_t I = 0; I < SPIRVStructNumElements; ++I)
836839
MIB.addUse(Args[I]);
837840

841+
Instructions.push_back(MIB.getInstr());
842+
838843
for (size_t I = SPIRVStructNumElements; I < NumElements;
839844
I += MaxNumElements) {
840845
auto MIB = MIRBuilder.buildInstr(ContinuedOpcode);
841846
for (size_t J = I; J < std::min(I + MaxNumElements, NumElements); ++J)
842847
MIB.addUse(Args[J]);
848+
Instructions.push_back(MIB.getInstr());
843849
}
850+
return Instructions;
844851
}
845852

846853
} // namespace llvm

llvm/lib/Target/SPIRV/SPIRVUtils.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,11 @@ inline FPDecorationId demangledPostfixToDecorationId(const std::string &S) {
454454
return It == Mapping.end() ? FPDecorationId::NONE : It->second;
455455
}
456456

457-
void createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode,
458-
unsigned MinWC, unsigned ContinuedOpcode,
459-
ArrayRef<Register> Args,
460-
Register ReturnRegister, Register TypeID);
457+
SmallVector<MachineInstr *, 4>
458+
createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode,
459+
unsigned MinWC, unsigned ContinuedOpcode,
460+
ArrayRef<Register> Args, Register ReturnRegister,
461+
Register TypeID);
461462

462463
} // namespace llvm
463464
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H

llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_long_composites/long-constant-array.ll

Lines changed: 28 additions & 0 deletions
Large diffs are not rendered by default.

llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_long_composites/long-constant-composite.ll

Lines changed: 27 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)