Skip to content

[SPIR-V] Rework duplicate tracker and tracking of IR entities and types to improve compile-time performance #130605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6d83ad0
remove spv_track_constant() intrnal intrinsics
VyacheslavLevytskyy Mar 10, 2025
b729dcb
fixes
VyacheslavLevytskyy Mar 10, 2025
c304139
remove duplicate tracker
VyacheslavLevytskyy Mar 11, 2025
ae58dee
add a new duplicate tracker
VyacheslavLevytskyy Mar 11, 2025
7b48e12
a new duplicate tracker
VyacheslavLevytskyy Mar 11, 2025
336f1d1
a new duplicate tracker
VyacheslavLevytskyy Mar 11, 2025
c6afdb2
a new duplicate tracker
VyacheslavLevytskyy Mar 11, 2025
4a02bef
rework assign_type, type/const instruction selection
VyacheslavLevytskyy Mar 14, 2025
abc6895
experiment with cashes
VyacheslavLevytskyy Mar 14, 2025
4582603
instruction selection for constants to account for duplicate tracking
VyacheslavLevytskyy Mar 17, 2025
c5783d2
fix test cases
VyacheslavLevytskyy Mar 17, 2025
a934aee
fix test cases
VyacheslavLevytskyy Mar 17, 2025
a638682
SPV_INTEL_long_composites is not completed: mark test cases as XFAIL
VyacheslavLevytskyy Mar 17, 2025
dbfb282
fix generation of OpTypeBool
VyacheslavLevytskyy Mar 18, 2025
b81263e
fix test cases
VyacheslavLevytskyy Mar 18, 2025
9064ce0
fix instruction selection breaking of consistent register types
VyacheslavLevytskyy Mar 18, 2025
2506f5a
fix instruction selection breaking of consistent register types
VyacheslavLevytskyy Mar 18, 2025
aa2297e
harden implementation of the duplicate tracker; fix generation of glo…
VyacheslavLevytskyy Mar 18, 2025
922e981
remove comments
VyacheslavLevytskyy Mar 18, 2025
daeac7d
tweak
VyacheslavLevytskyy Mar 18, 2025
bf93269
wrap null to a constant tracker intrinsic
VyacheslavLevytskyy Mar 19, 2025
ed1ec93
add a reproducer
VyacheslavLevytskyy Mar 19, 2025
7667337
improve null type inference
VyacheslavLevytskyy Mar 19, 2025
18af7fb
remove bak file
VyacheslavLevytskyy Mar 20, 2025
be709fd
fix the test case
VyacheslavLevytskyy Mar 21, 2025
d0f1878
we don't need these pseudo instructions anymore
VyacheslavLevytskyy Mar 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/IntrinsicsSPIRV.td
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let TargetPrefix = "spv" in {
def int_spv_assign_type : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty]>;
def int_spv_assign_ptr_type : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty, llvm_i32_ty], [ImmArg<ArgIndex<2>>]>;
def int_spv_assign_name : Intrinsic<[], [llvm_any_ty, llvm_vararg_ty]>;
def int_spv_assign_name : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty]>;
def int_spv_assign_decoration : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty]>;
def int_spv_value_md : Intrinsic<[], [llvm_metadata_ty]>;

Expand Down
36 changes: 17 additions & 19 deletions llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,9 @@ static MachineInstr *getBlockStructInstr(Register ParamReg,
// TODO: maybe unify with prelegalizer pass.
static unsigned getConstFromIntrinsic(Register Reg, MachineRegisterInfo *MRI) {
MachineInstr *DefMI = MRI->getUniqueVRegDef(Reg);
assert(isSpvIntrinsic(*DefMI, Intrinsic::spv_track_constant) &&
DefMI->getOperand(2).isReg());
MachineInstr *DefMI2 = MRI->getUniqueVRegDef(DefMI->getOperand(2).getReg());
assert(DefMI2->getOpcode() == TargetOpcode::G_CONSTANT &&
DefMI2->getOperand(1).isCImm());
return DefMI2->getOperand(1).getCImm()->getValue().getZExtValue();
assert(DefMI->getOpcode() == TargetOpcode::G_CONSTANT &&
DefMI->getOperand(1).isCImm());
return DefMI->getOperand(1).getCImm()->getValue().getZExtValue();
}

// Return type of the instruction result from spv_assign_type intrinsic.
Expand Down Expand Up @@ -538,10 +535,9 @@ static Register buildBuiltinVariableLoad(
/// assign SPIRVType to both registers. If SpirvTy is provided, use it as
/// SPIRVType in ASSIGN_TYPE, otherwise create it from \p Ty. Defined in
/// SPIRVPreLegalizer.cpp.
extern Register insertAssignInstr(Register Reg, Type *Ty, SPIRVType *SpirvTy,
SPIRVGlobalRegistry *GR,
MachineIRBuilder &MIB,
MachineRegisterInfo &MRI);
extern void insertAssignInstr(Register Reg, Type *Ty, SPIRVType *SpirvTy,
SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB,
MachineRegisterInfo &MRI);

// TODO: Move to TableGen.
static SPIRV::MemorySemantics::MemorySemantics
Expand Down Expand Up @@ -1818,20 +1814,23 @@ static bool generateImageSizeQueryInst(const SPIRV::IncomingCall *Call,
// Query result may either be a vector or a scalar. If return type is not a
// vector, expect only a single size component. Otherwise get the number of
// expected components.
SPIRVType *RetTy = Call->ReturnType;
unsigned NumExpectedRetComponents = RetTy->getOpcode() == SPIRV::OpTypeVector
? RetTy->getOperand(2).getImm()
: 1;
unsigned NumExpectedRetComponents =
Call->ReturnType->getOpcode() == SPIRV::OpTypeVector
? Call->ReturnType->getOperand(2).getImm()
: 1;
// Get the actual number of query result/size components.
SPIRVType *ImgType = GR->getSPIRVTypeForVReg(Call->Arguments[0]);
unsigned NumActualRetComponents = getNumSizeComponents(ImgType);
Register QueryResult = Call->ReturnRegister;
SPIRVType *QueryResultType = Call->ReturnType;
if (NumExpectedRetComponents != NumActualRetComponents) {
unsigned Bitwidth = Call->ReturnType->getOpcode() == SPIRV::OpTypeInt
? Call->ReturnType->getOperand(1).getImm()
: 32;
QueryResult = MIRBuilder.getMRI()->createGenericVirtualRegister(
LLT::fixed_vector(NumActualRetComponents, 32));
LLT::fixed_vector(NumActualRetComponents, Bitwidth));
MIRBuilder.getMRI()->setRegClass(QueryResult, &SPIRV::vIDRegClass);
SPIRVType *IntTy = GR->getOrCreateSPIRVIntegerType(32, MIRBuilder);
SPIRVType *IntTy = GR->getOrCreateSPIRVIntegerType(Bitwidth, MIRBuilder);
QueryResultType = GR->getOrCreateSPIRVVectorType(
IntTy, NumActualRetComponents, MIRBuilder, true);
GR->assignSPIRVTypeToVReg(QueryResultType, QueryResult, MIRBuilder.getMF());
Expand Down Expand Up @@ -1971,8 +1970,7 @@ static bool generateReadImageInst(const StringRef DemangledCall,
Sampler = GR->buildConstantSampler(
Register(), getSamplerAddressingModeFromBitmask(SamplerMask),
getSamplerParamFromBitmask(SamplerMask),
getSamplerFilterModeFromBitmask(SamplerMask), MIRBuilder,
GR->getSPIRVTypeForVReg(Sampler));
getSamplerFilterModeFromBitmask(SamplerMask), MIRBuilder);
}
SPIRVType *ImageType = GR->getSPIRVTypeForVReg(Image);
SPIRVType *SampledImageType =
Expand Down Expand Up @@ -2059,7 +2057,7 @@ static bool generateSampleImageInst(const StringRef DemangledCall,
Register Sampler = GR->buildConstantSampler(
Call->ReturnRegister, getSamplerAddressingModeFromBitmask(Bitmask),
getSamplerParamFromBitmask(Bitmask),
getSamplerFilterModeFromBitmask(Bitmask), MIRBuilder, Call->ReturnType);
getSamplerFilterModeFromBitmask(Bitmask), MIRBuilder);
return Sampler.isValid();
} else if (Call->Builtin->Name.contains_insensitive("__spirv_SampledImage")) {
// Create OpSampledImage.
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
auto MRI = MIRBuilder.getMRI();
Register FuncVReg = MRI->createGenericVirtualRegister(LLT::scalar(64));
MRI->setRegClass(FuncVReg, &SPIRV::iIDRegClass);
if (F.isDeclaration())
GR->add(&F, &MIRBuilder.getMF(), FuncVReg);
FunctionType *FTy = getOriginalFunctionType(F);
Type *FRetTy = FTy->getReturnType();
if (isUntypedPointerTy(FRetTy)) {
Expand All @@ -425,6 +423,8 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
.addUse(GR->getSPIRVTypeID(FuncTy));
GR->recordFunctionDefinition(&F, &MB.getInstr()->getOperand(0));
GR->addGlobalObject(&F, &MIRBuilder.getMF(), FuncVReg);
if (F.isDeclaration())
GR->add(&F, MB);

// Add OpFunctionParameter instructions
int i = 0;
Expand All @@ -433,11 +433,11 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
Register ArgReg = VRegs[i][0];
MRI->setRegClass(ArgReg, GR->getRegClass(ArgTypeVRegs[i]));
MRI->setType(ArgReg, GR->getRegType(ArgTypeVRegs[i]));
MIRBuilder.buildInstr(SPIRV::OpFunctionParameter)
.addDef(ArgReg)
.addUse(GR->getSPIRVTypeID(ArgTypeVRegs[i]));
auto MIB = MIRBuilder.buildInstr(SPIRV::OpFunctionParameter)
.addDef(ArgReg)
.addUse(GR->getSPIRVTypeID(ArgTypeVRegs[i]));
if (F.isDeclaration())
GR->add(&Arg, &MIRBuilder.getMF(), ArgReg);
GR->add(&Arg, MIB);
GR->addGlobalObject(&Arg, &MIRBuilder.getMF(), ArgReg);
i++;
}
Expand Down
Loading