-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SPIR-V] Rework usage of virtual registers' types and classes #104104
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
VyacheslavLevytskyy
merged 14 commits into
llvm:main
from
VyacheslavLevytskyy:expensive_checks_2
Aug 22, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
741349d
Rework usage of virtual registers' types and classes
VyacheslavLevytskyy fc161ee
apply clang-format; simplify inline asm operands usage
VyacheslavLevytskyy 51f2ead
address ptr bitcasts and OpExtInst
VyacheslavLevytskyy d868570
improve virtual registers types and classes
VyacheslavLevytskyy a2597ca
fix builtin vreg type & class
VyacheslavLevytskyy a962e08
improve tablegen, builtins, virtual registers types and classes
VyacheslavLevytskyy 9e85ffd
uniform creation of vregs; address call lowering & builtins
VyacheslavLevytskyy dd41fdf
builtins & call lowering
VyacheslavLevytskyy 56cd0a6
correct virtual registers creation for builtins
VyacheslavLevytskyy 7b40fbe
account for arbitrary integer sizes, update legalizer
VyacheslavLevytskyy 1023742
update tests with '-verify-machineinstrs'
VyacheslavLevytskyy 3b0540c
update tests with '-verify-machineinstrs'
VyacheslavLevytskyy 74992ab
update tests with '-verify-machineinstrs'
VyacheslavLevytskyy 14d7a72
fix instruction selection for pointers; clang-format
VyacheslavLevytskyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,17 +72,14 @@ void SPIRVGlobalRegistry::assignSPIRVTypeToVReg(SPIRVType *SpirvType, | |
VRegToTypeMap[&MF][VReg] = SpirvType; | ||
} | ||
|
||
static Register createTypeVReg(MachineIRBuilder &MIRBuilder) { | ||
auto &MRI = MIRBuilder.getMF().getRegInfo(); | ||
auto Res = MRI.createGenericVirtualRegister(LLT::scalar(32)); | ||
static Register createTypeVReg(MachineRegisterInfo &MRI) { | ||
auto Res = MRI.createGenericVirtualRegister(LLT::scalar(64)); | ||
MRI.setRegClass(Res, &SPIRV::TYPERegClass); | ||
return Res; | ||
} | ||
|
||
static Register createTypeVReg(MachineRegisterInfo &MRI) { | ||
auto Res = MRI.createGenericVirtualRegister(LLT::scalar(32)); | ||
MRI.setRegClass(Res, &SPIRV::TYPERegClass); | ||
return Res; | ||
inline Register createTypeVReg(MachineIRBuilder &MIRBuilder) { | ||
return createTypeVReg(MIRBuilder.getMF().getRegInfo()); | ||
} | ||
|
||
SPIRVType *SPIRVGlobalRegistry::getOpTypeBool(MachineIRBuilder &MIRBuilder) { | ||
|
@@ -157,26 +154,24 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeVector(uint32_t NumElems, | |
return MIB; | ||
} | ||
|
||
std::tuple<Register, ConstantInt *, bool> | ||
std::tuple<Register, ConstantInt *, bool, unsigned> | ||
SPIRVGlobalRegistry::getOrCreateConstIntReg(uint64_t Val, SPIRVType *SpvType, | ||
MachineIRBuilder *MIRBuilder, | ||
MachineInstr *I, | ||
const SPIRVInstrInfo *TII) { | ||
const IntegerType *LLVMIntTy; | ||
if (SpvType) | ||
LLVMIntTy = cast<IntegerType>(getTypeForSPIRVType(SpvType)); | ||
else | ||
LLVMIntTy = IntegerType::getInt32Ty(CurMF->getFunction().getContext()); | ||
assert(SpvType); | ||
const IntegerType *LLVMIntTy = | ||
cast<IntegerType>(getTypeForSPIRVType(SpvType)); | ||
unsigned BitWidth = getScalarOrVectorBitWidth(SpvType); | ||
bool NewInstr = false; | ||
// Find a constant in DT or build a new one. | ||
ConstantInt *CI = ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val); | ||
Register Res = DT.find(CI, CurMF); | ||
if (!Res.isValid()) { | ||
unsigned BitWidth = SpvType ? getScalarOrVectorBitWidth(SpvType) : 32; | ||
// TODO: handle cases where the type is not 32bit wide | ||
// TODO: https://github.com/llvm/llvm-project/issues/88129 | ||
LLT LLTy = LLT::scalar(32); | ||
Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy); | ||
Res = | ||
CurMF->getRegInfo().createGenericVirtualRegister(LLT::scalar(BitWidth)); | ||
CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass); | ||
if (MIRBuilder) | ||
assignTypeToVReg(LLVMIntTy, Res, *MIRBuilder); | ||
|
@@ -185,35 +180,27 @@ SPIRVGlobalRegistry::getOrCreateConstIntReg(uint64_t Val, SPIRVType *SpvType, | |
DT.add(CI, CurMF, Res); | ||
NewInstr = true; | ||
} | ||
return std::make_tuple(Res, CI, NewInstr); | ||
return std::make_tuple(Res, CI, NewInstr, BitWidth); | ||
} | ||
|
||
std::tuple<Register, ConstantFP *, bool, unsigned> | ||
SPIRVGlobalRegistry::getOrCreateConstFloatReg(APFloat Val, SPIRVType *SpvType, | ||
MachineIRBuilder *MIRBuilder, | ||
MachineInstr *I, | ||
const SPIRVInstrInfo *TII) { | ||
const Type *LLVMFloatTy; | ||
assert(SpvType); | ||
LLVMContext &Ctx = CurMF->getFunction().getContext(); | ||
unsigned BitWidth = 32; | ||
if (SpvType) | ||
LLVMFloatTy = getTypeForSPIRVType(SpvType); | ||
else { | ||
LLVMFloatTy = Type::getFloatTy(Ctx); | ||
if (MIRBuilder) | ||
SpvType = getOrCreateSPIRVType(LLVMFloatTy, *MIRBuilder); | ||
} | ||
const Type *LLVMFloatTy = getTypeForSPIRVType(SpvType); | ||
unsigned BitWidth = getScalarOrVectorBitWidth(SpvType); | ||
bool NewInstr = false; | ||
// Find a constant in DT or build a new one. | ||
auto *const CI = ConstantFP::get(Ctx, Val); | ||
Register Res = DT.find(CI, CurMF); | ||
if (!Res.isValid()) { | ||
if (SpvType) | ||
BitWidth = getScalarOrVectorBitWidth(SpvType); | ||
// TODO: handle cases where the type is not 32bit wide | ||
// TODO: https://github.com/llvm/llvm-project/issues/88129 | ||
LLT LLTy = LLT::scalar(32); | ||
Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy); | ||
Res = | ||
CurMF->getRegInfo().createGenericVirtualRegister(LLT::scalar(BitWidth)); | ||
CurMF->getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass); | ||
if (MIRBuilder) | ||
assignTypeToVReg(LLVMFloatTy, Res, *MIRBuilder); | ||
|
@@ -269,7 +256,8 @@ Register SPIRVGlobalRegistry::getOrCreateConstInt(uint64_t Val, MachineInstr &I, | |
ConstantInt *CI; | ||
Register Res; | ||
bool New; | ||
std::tie(Res, CI, New) = | ||
unsigned BitWidth; | ||
std::tie(Res, CI, New, BitWidth) = | ||
getOrCreateConstIntReg(Val, SpvType, nullptr, &I, &TII); | ||
// If we have found Res register which is defined by the passed G_CONSTANT | ||
// machine instruction, a new constant instruction should be created. | ||
|
@@ -281,7 +269,7 @@ Register SPIRVGlobalRegistry::getOrCreateConstInt(uint64_t Val, MachineInstr &I, | |
MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantI)) | ||
.addDef(Res) | ||
.addUse(getSPIRVTypeID(SpvType)); | ||
addNumImm(APInt(getScalarOrVectorBitWidth(SpvType), Val), MIB); | ||
addNumImm(APInt(BitWidth, Val), MIB); | ||
} else { | ||
MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantNull)) | ||
.addDef(Res) | ||
|
@@ -297,19 +285,17 @@ Register SPIRVGlobalRegistry::buildConstantInt(uint64_t Val, | |
MachineIRBuilder &MIRBuilder, | ||
SPIRVType *SpvType, | ||
bool EmitIR) { | ||
assert(SpvType); | ||
auto &MF = MIRBuilder.getMF(); | ||
const IntegerType *LLVMIntTy; | ||
if (SpvType) | ||
LLVMIntTy = cast<IntegerType>(getTypeForSPIRVType(SpvType)); | ||
else | ||
LLVMIntTy = IntegerType::getInt32Ty(MF.getFunction().getContext()); | ||
const IntegerType *LLVMIntTy = | ||
cast<IntegerType>(getTypeForSPIRVType(SpvType)); | ||
// Find a constant in DT or build a new one. | ||
const auto ConstInt = | ||
ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val); | ||
Register Res = DT.find(ConstInt, &MF); | ||
if (!Res.isValid()) { | ||
unsigned BitWidth = SpvType ? getScalarOrVectorBitWidth(SpvType) : 32; | ||
LLT LLTy = LLT::scalar(EmitIR ? BitWidth : 32); | ||
unsigned BitWidth = getScalarOrVectorBitWidth(SpvType); | ||
LLT LLTy = LLT::scalar(BitWidth); | ||
Res = MF.getRegInfo().createGenericVirtualRegister(LLTy); | ||
MF.getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass); | ||
assignTypeToVReg(LLVMIntTy, Res, MIRBuilder, | ||
|
@@ -318,18 +304,17 @@ Register SPIRVGlobalRegistry::buildConstantInt(uint64_t Val, | |
if (EmitIR) { | ||
MIRBuilder.buildConstant(Res, *ConstInt); | ||
} else { | ||
if (!SpvType) | ||
SpvType = getOrCreateSPIRVIntegerType(BitWidth, MIRBuilder); | ||
Register SpvTypeReg = getSPIRVTypeID(SpvType); | ||
MachineInstrBuilder MIB; | ||
if (Val) { | ||
MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI) | ||
.addDef(Res) | ||
.addUse(getSPIRVTypeID(SpvType)); | ||
.addUse(SpvTypeReg); | ||
addNumImm(APInt(BitWidth, Val), MIB); | ||
} else { | ||
MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull) | ||
.addDef(Res) | ||
.addUse(getSPIRVTypeID(SpvType)); | ||
.addUse(SpvTypeReg); | ||
} | ||
const auto &Subtarget = CurMF->getSubtarget(); | ||
constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(), | ||
|
@@ -353,7 +338,8 @@ Register SPIRVGlobalRegistry::buildConstantFP(APFloat Val, | |
const auto ConstFP = ConstantFP::get(Ctx, Val); | ||
Register Res = DT.find(ConstFP, &MF); | ||
if (!Res.isValid()) { | ||
Res = MF.getRegInfo().createGenericVirtualRegister(LLT::scalar(32)); | ||
Res = MF.getRegInfo().createGenericVirtualRegister( | ||
LLT::scalar(getScalarOrVectorBitWidth(SpvType))); | ||
MF.getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass); | ||
assignSPIRVTypeToVReg(SpvType, Res, MF); | ||
DT.add(ConstFP, &MF, Res); | ||
|
@@ -407,7 +393,7 @@ Register SPIRVGlobalRegistry::getOrCreateCompositeOrNull( | |
|
||
// TODO: handle cases where the type is not 32bit wide | ||
// TODO: https://github.com/llvm/llvm-project/issues/88129 | ||
LLT LLTy = LLT::scalar(32); | ||
LLT LLTy = LLT::scalar(64); | ||
Register SpvVecConst = | ||
CurMF->getRegInfo().createGenericVirtualRegister(LLTy); | ||
CurMF->getRegInfo().setRegClass(SpvVecConst, &SPIRV::iIDRegClass); | ||
|
@@ -509,7 +495,7 @@ Register SPIRVGlobalRegistry::getOrCreateIntCompositeOrNull( | |
getOrCreateSPIRVIntegerType(BitWidth, MIRBuilder); | ||
SpvScalConst = buildConstantInt(Val, MIRBuilder, SpvBaseType, EmitIR); | ||
} | ||
LLT LLTy = EmitIR ? LLT::fixed_vector(ElemCnt, BitWidth) : LLT::scalar(32); | ||
LLT LLTy = EmitIR ? LLT::fixed_vector(ElemCnt, BitWidth) : LLT::scalar(64); | ||
Register SpvVecConst = | ||
CurMF->getRegInfo().createGenericVirtualRegister(LLTy); | ||
CurMF->getRegInfo().setRegClass(SpvVecConst, &SPIRV::iIDRegClass); | ||
|
@@ -650,7 +636,6 @@ Register SPIRVGlobalRegistry::buildGlobalVariable( | |
|
||
// Set to Reg the same type as ResVReg has. | ||
auto MRI = MIRBuilder.getMRI(); | ||
assert(MRI->getType(ResVReg).isPointer() && "Pointer type is expected"); | ||
if (Reg != ResVReg) { | ||
LLT RegLLTy = | ||
LLT::pointer(MRI->getType(ResVReg).getAddressSpace(), getPointerSize()); | ||
|
@@ -706,8 +691,9 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems, | |
bool EmitIR) { | ||
assert((ElemType->getOpcode() != SPIRV::OpTypeVoid) && | ||
"Invalid array element type"); | ||
SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder); | ||
Register NumElementsVReg = | ||
buildConstantInt(NumElems, MIRBuilder, nullptr, EmitIR); | ||
buildConstantInt(NumElems, MIRBuilder, SpvTypeInt32, EmitIR); | ||
auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeArray) | ||
.addDef(createTypeVReg(MIRBuilder)) | ||
.addUse(getSPIRVTypeID(ElemType)) | ||
|
@@ -1188,14 +1174,15 @@ SPIRVType *SPIRVGlobalRegistry::getOrCreateOpTypeCoopMatr( | |
if (ResVReg.isValid()) | ||
return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(ResVReg); | ||
ResVReg = createTypeVReg(MIRBuilder); | ||
SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder); | ||
SPIRVType *SpirvTy = | ||
MIRBuilder.buildInstr(SPIRV::OpTypeCooperativeMatrixKHR) | ||
.addDef(ResVReg) | ||
.addUse(getSPIRVTypeID(ElemType)) | ||
.addUse(buildConstantInt(Scope, MIRBuilder, nullptr, true)) | ||
.addUse(buildConstantInt(Rows, MIRBuilder, nullptr, true)) | ||
.addUse(buildConstantInt(Columns, MIRBuilder, nullptr, true)) | ||
.addUse(buildConstantInt(Use, MIRBuilder, nullptr, true)); | ||
.addUse(buildConstantInt(Scope, MIRBuilder, SpvTypeInt32, true)) | ||
.addUse(buildConstantInt(Rows, MIRBuilder, SpvTypeInt32, true)) | ||
.addUse(buildConstantInt(Columns, MIRBuilder, SpvTypeInt32, true)) | ||
.addUse(buildConstantInt(Use, MIRBuilder, SpvTypeInt32, true)); | ||
DT.add(ExtensionType, &MIRBuilder.getMF(), ResVReg); | ||
return SpirvTy; | ||
} | ||
|
@@ -1386,8 +1373,8 @@ SPIRVType *SPIRVGlobalRegistry::getOrCreateSPIRVArrayType( | |
if (Reg.isValid()) | ||
return getSPIRVTypeForVReg(Reg); | ||
MachineBasicBlock &BB = *I.getParent(); | ||
SPIRVType *SpirvType = getOrCreateSPIRVIntegerType(32, I, TII); | ||
Register Len = getOrCreateConstInt(NumElements, I, SpirvType, TII); | ||
SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, I, TII); | ||
Register Len = getOrCreateConstInt(NumElements, I, SpvTypeInt32, TII); | ||
auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeArray)) | ||
.addDef(createTypeVReg(CurMF->getRegInfo())) | ||
.addUse(getSPIRVTypeID(BaseType)) | ||
|
@@ -1436,7 +1423,7 @@ Register SPIRVGlobalRegistry::getOrCreateUndef(MachineInstr &I, | |
Register Res = DT.find(UV, CurMF); | ||
if (Res.isValid()) | ||
return Res; | ||
LLT LLTy = LLT::scalar(32); | ||
LLT LLTy = LLT::scalar(64); | ||
Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy); | ||
CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass); | ||
assignSPIRVTypeToVReg(SpvType, Res, *CurMF); | ||
|
@@ -1451,3 +1438,61 @@ Register SPIRVGlobalRegistry::getOrCreateUndef(MachineInstr &I, | |
*ST.getRegisterInfo(), *ST.getRegBankInfo()); | ||
return Res; | ||
} | ||
|
||
const TargetRegisterClass * | ||
SPIRVGlobalRegistry::getRegClass(SPIRVType *SpvType) const { | ||
unsigned Opcode = SpvType->getOpcode(); | ||
switch (Opcode) { | ||
case SPIRV::OpTypeFloat: | ||
return &SPIRV::fIDRegClass; | ||
case SPIRV::OpTypePointer: | ||
return &SPIRV::pIDRegClass; | ||
case SPIRV::OpTypeVector: { | ||
SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg()); | ||
unsigned ElemOpcode = ElemType ? ElemType->getOpcode() : 0; | ||
if (ElemOpcode == SPIRV::OpTypeFloat) | ||
return &SPIRV::vfIDRegClass; | ||
if (ElemOpcode == SPIRV::OpTypePointer) | ||
return &SPIRV::vpIDRegClass; | ||
return &SPIRV::vIDRegClass; | ||
} | ||
} | ||
return &SPIRV::iIDRegClass; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add an assert for |
||
} | ||
|
||
inline unsigned getAS(SPIRVType *SpvType) { | ||
return storageClassToAddressSpace( | ||
static_cast<SPIRV::StorageClass::StorageClass>( | ||
SpvType->getOperand(1).getImm())); | ||
} | ||
|
||
LLT SPIRVGlobalRegistry::getRegType(SPIRVType *SpvType) const { | ||
unsigned Opcode = SpvType ? SpvType->getOpcode() : 0; | ||
switch (Opcode) { | ||
case SPIRV::OpTypeInt: | ||
case SPIRV::OpTypeFloat: | ||
case SPIRV::OpTypeBool: | ||
return LLT::scalar(getScalarOrVectorBitWidth(SpvType)); | ||
case SPIRV::OpTypePointer: | ||
return LLT::pointer(getAS(SpvType), getPointerSize()); | ||
case SPIRV::OpTypeVector: { | ||
SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg()); | ||
LLT ET; | ||
switch (ElemType ? ElemType->getOpcode() : 0) { | ||
case SPIRV::OpTypePointer: | ||
ET = LLT::pointer(getAS(ElemType), getPointerSize()); | ||
break; | ||
case SPIRV::OpTypeInt: | ||
case SPIRV::OpTypeFloat: | ||
case SPIRV::OpTypeBool: | ||
ET = LLT::scalar(getScalarOrVectorBitWidth(ElemType)); | ||
break; | ||
default: | ||
ET = LLT::scalar(64); | ||
} | ||
return LLT::fixed_vector( | ||
static_cast<unsigned>(SpvType->getOperand(2).getImm()), ET); | ||
} | ||
} | ||
return LLT::scalar(64); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo doesn't seem to make sense anymore.