refactor: Make type component/constituent counts unsigned

A type can't have a negative number of constituents, components, rows,
or columns. Therefore, the utility functions to query said information
on types and values should return unsigned int rather than int.
This commit is contained in:
Arcady Goldmints-Orlov 2024-06-21 17:31:00 -04:00 committed by arcady-lunarg
parent 33d517470e
commit 0c15a28c94
2 changed files with 29 additions and 29 deletions

View File

@ -1312,7 +1312,7 @@ Op Builder::getMostBasicTypeClass(Id typeId) const
} }
} }
int Builder::getNumTypeConstituents(Id typeId) const unsigned int Builder::getNumTypeConstituents(Id typeId) const
{ {
Instruction* instr = module.getInstruction(typeId); Instruction* instr = module.getInstruction(typeId);
@ -2924,7 +2924,7 @@ Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, const std::vect
swizzle->reserveOperands(2); swizzle->reserveOperands(2);
swizzle->addIdOperand(target); swizzle->addIdOperand(target);
assert(getNumComponents(source) == (int)channels.size()); assert(getNumComponents(source) == channels.size());
assert(isVector(source)); assert(isVector(source));
swizzle->addIdOperand(source); swizzle->addIdOperand(source);
@ -3371,7 +3371,7 @@ Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, b
Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents) Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents)
{ {
assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 && assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 &&
getNumTypeConstituents(typeId) == (int)constituents.size())); getNumTypeConstituents(typeId) == constituents.size()));
if (generatingOpCodeForSpecConst) { if (generatingOpCodeForSpecConst) {
// Sometime, even in spec-constant-op mode, the constant composite to be // Sometime, even in spec-constant-op mode, the constant composite to be
@ -3464,8 +3464,8 @@ Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sourc
if (sourcesToUse + targetComponent > numTargetComponents) if (sourcesToUse + targetComponent > numTargetComponents)
sourcesToUse = numTargetComponents - targetComponent; sourcesToUse = numTargetComponents - targetComponent;
int col = 0; unsigned int col = 0;
int row = 0; unsigned int row = 0;
for (unsigned int s = 0; s < sourcesToUse; ++s) { for (unsigned int s = 0; s < sourcesToUse; ++s) {
if (row >= getNumRows(sourceArg)) { if (row >= getNumRows(sourceArg)) {
row = 0; row = 0;
@ -3510,8 +3510,8 @@ Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sourc
Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId) Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
{ {
Id componentTypeId = getScalarTypeId(resultTypeId); Id componentTypeId = getScalarTypeId(resultTypeId);
int numCols = getTypeNumColumns(resultTypeId); unsigned int numCols = getTypeNumColumns(resultTypeId);
int numRows = getTypeNumRows(resultTypeId); unsigned int numRows = getTypeNumRows(resultTypeId);
Instruction* instr = module.getInstruction(componentTypeId); Instruction* instr = module.getInstruction(componentTypeId);
const unsigned bitCount = instr->getImmediateOperand(0); const unsigned bitCount = instr->getImmediateOperand(0);
@ -3526,11 +3526,11 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix)); Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
std::vector<unsigned> channels; std::vector<unsigned> channels;
for (int row = 0; row < numRows; ++row) for (unsigned int row = 0; row < numRows; ++row)
channels.push_back(row); channels.push_back(row);
std::vector<Id> matrixColumns; std::vector<Id> matrixColumns;
for (int col = 0; col < numCols; ++col) { for (unsigned int col = 0; col < numCols; ++col) {
std::vector<unsigned> indexes; std::vector<unsigned> indexes;
indexes.push_back(col); indexes.push_back(col);
Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes); Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
@ -3548,7 +3548,7 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
// Detect a matrix being constructed from a repeated vector of the correct size. // Detect a matrix being constructed from a repeated vector of the correct size.
// Create the composite directly from it. // Create the composite directly from it.
if ((int)sources.size() == numCols && isVector(sources[0]) && getNumComponents(sources[0]) == numRows && if (sources.size() == numCols && isVector(sources[0]) && getNumComponents(sources[0]) == numRows &&
std::equal(sources.begin() + 1, sources.end(), sources.begin())) { std::equal(sources.begin() + 1, sources.end(), sources.begin())) {
return setPrecision(createCompositeConstruct(resultTypeId, sources), precision); return setPrecision(createCompositeConstruct(resultTypeId, sources), precision);
} }
@ -3580,12 +3580,12 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
} else if (isMatrix(sources[0])) { } else if (isMatrix(sources[0])) {
// constructing from another matrix; copy over the parts that exist in both the argument and constructee // constructing from another matrix; copy over the parts that exist in both the argument and constructee
Id matrix = sources[0]; Id matrix = sources[0];
int minCols = std::min(numCols, getNumColumns(matrix)); unsigned int minCols = std::min(numCols, getNumColumns(matrix));
int minRows = std::min(numRows, getNumRows(matrix)); unsigned int minRows = std::min(numRows, getNumRows(matrix));
for (int col = 0; col < minCols; ++col) { for (unsigned int col = 0; col < minCols; ++col) {
std::vector<unsigned> indexes; std::vector<unsigned> indexes;
indexes.push_back(col); indexes.push_back(col);
for (int row = 0; row < minRows; ++row) { for (unsigned int row = 0; row < minRows; ++row) {
indexes.push_back(row); indexes.push_back(row);
ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes); ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes);
indexes.pop_back(); indexes.pop_back();
@ -3594,12 +3594,12 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
} }
} else { } else {
// fill in the matrix in column-major order with whatever argument components are available // fill in the matrix in column-major order with whatever argument components are available
int row = 0; unsigned int row = 0;
int col = 0; unsigned int col = 0;
for (int arg = 0; arg < (int)sources.size() && col < numCols; ++arg) { for (unsigned int arg = 0; arg < sources.size() && col < numCols; ++arg) {
Id argComp = sources[arg]; Id argComp = sources[arg];
for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) { for (unsigned int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
if (getNumComponents(sources[arg]) > 1) { if (getNumComponents(sources[arg]) > 1) {
argComp = createCompositeExtract(sources[arg], componentTypeId, comp); argComp = createCompositeExtract(sources[arg], componentTypeId, comp);
setPrecision(argComp, precision); setPrecision(argComp, precision);
@ -3623,9 +3623,9 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
// make the column vectors // make the column vectors
Id columnTypeId = getContainedTypeId(resultTypeId); Id columnTypeId = getContainedTypeId(resultTypeId);
std::vector<Id> matrixColumns; std::vector<Id> matrixColumns;
for (int col = 0; col < numCols; ++col) { for (unsigned int col = 0; col < numCols; ++col) {
std::vector<Id> vectorComponents; std::vector<Id> vectorComponents;
for (int row = 0; row < numRows; ++row) for (unsigned int row = 0; row < numRows; ++row)
vectorComponents.push_back(ids[col][row]); vectorComponents.push_back(ids[col][row]);
Id column = createCompositeConstruct(columnTypeId, vectorComponents); Id column = createCompositeConstruct(columnTypeId, vectorComponents);
setPrecision(column, precision); setPrecision(column, precision);
@ -3852,7 +3852,7 @@ void Builder::accessChainStore(Id rvalue, Decoration nonUniform, spv::MemoryAcce
// If a swizzle exists and is not full and is not dynamic, then the swizzle will be broken into individual stores. // If a swizzle exists and is not full and is not dynamic, then the swizzle will be broken into individual stores.
if (accessChain.swizzle.size() > 0 && if (accessChain.swizzle.size() > 0 &&
getNumTypeComponents(getResultingAccessChainType()) != (int)accessChain.swizzle.size() && getNumTypeComponents(getResultingAccessChainType()) != accessChain.swizzle.size() &&
accessChain.component == NoResult) { accessChain.component == NoResult) {
for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) { for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) {
accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle[i])); accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle[i]));
@ -4172,7 +4172,7 @@ void Builder::simplifyAccessChainSwizzle()
{ {
// If the swizzle has fewer components than the vector, it is subsetting, and must stay // If the swizzle has fewer components than the vector, it is subsetting, and must stay
// to preserve that fact. // to preserve that fact.
if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size()) if (getNumTypeComponents(accessChain.preSwizzleBaseType) > accessChain.swizzle.size())
return; return;
// if components are out of order, it is a swizzle // if components are out of order, it is a swizzle

View File

@ -264,9 +264,9 @@ public:
Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); } Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
Op getTypeClass(Id typeId) const { return getOpCode(typeId); } Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
Op getMostBasicTypeClass(Id typeId) const; Op getMostBasicTypeClass(Id typeId) const;
int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); } unsigned int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
int getNumTypeConstituents(Id typeId) const; unsigned int getNumTypeConstituents(Id typeId) const;
int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); } unsigned int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
Id getScalarTypeId(Id typeId) const; Id getScalarTypeId(Id typeId) const;
Id getContainedTypeId(Id typeId) const; Id getContainedTypeId(Id typeId) const;
Id getContainedTypeId(Id typeId, int) const; Id getContainedTypeId(Id typeId, int) const;
@ -334,18 +334,18 @@ public:
return module.getInstruction(scalarTypeId)->getImmediateOperand(0); return module.getInstruction(scalarTypeId)->getImmediateOperand(0);
} }
int getTypeNumColumns(Id typeId) const unsigned int getTypeNumColumns(Id typeId) const
{ {
assert(isMatrixType(typeId)); assert(isMatrixType(typeId));
return getNumTypeConstituents(typeId); return getNumTypeConstituents(typeId);
} }
int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); } unsigned int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
int getTypeNumRows(Id typeId) const unsigned int getTypeNumRows(Id typeId) const
{ {
assert(isMatrixType(typeId)); assert(isMatrixType(typeId));
return getNumTypeComponents(getContainedTypeId(typeId)); return getNumTypeComponents(getContainedTypeId(typeId));
} }
int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); } unsigned int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
Dim getTypeDimensionality(Id typeId) const Dim getTypeDimensionality(Id typeId) const
{ {