Skip to content

Commit 0c15a28

Browse files
committed
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.
1 parent 33d5174 commit 0c15a28

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

SPIRV/SpvBuilder.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ Op Builder::getMostBasicTypeClass(Id typeId) const
13121312
}
13131313
}
13141314

1315-
int Builder::getNumTypeConstituents(Id typeId) const
1315+
unsigned int Builder::getNumTypeConstituents(Id typeId) const
13161316
{
13171317
Instruction* instr = module.getInstruction(typeId);
13181318

@@ -2924,7 +2924,7 @@ Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, const std::vect
29242924
swizzle->reserveOperands(2);
29252925
swizzle->addIdOperand(target);
29262926

2927-
assert(getNumComponents(source) == (int)channels.size());
2927+
assert(getNumComponents(source) == channels.size());
29282928
assert(isVector(source));
29292929
swizzle->addIdOperand(source);
29302930

@@ -3371,7 +3371,7 @@ Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, b
33713371
Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents)
33723372
{
33733373
assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 &&
3374-
getNumTypeConstituents(typeId) == (int)constituents.size()));
3374+
getNumTypeConstituents(typeId) == constituents.size()));
33753375

33763376
if (generatingOpCodeForSpecConst) {
33773377
// 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
34643464
if (sourcesToUse + targetComponent > numTargetComponents)
34653465
sourcesToUse = numTargetComponents - targetComponent;
34663466

3467-
int col = 0;
3468-
int row = 0;
3467+
unsigned int col = 0;
3468+
unsigned int row = 0;
34693469
for (unsigned int s = 0; s < sourcesToUse; ++s) {
34703470
if (row >= getNumRows(sourceArg)) {
34713471
row = 0;
@@ -3510,8 +3510,8 @@ Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sourc
35103510
Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
35113511
{
35123512
Id componentTypeId = getScalarTypeId(resultTypeId);
3513-
int numCols = getTypeNumColumns(resultTypeId);
3514-
int numRows = getTypeNumRows(resultTypeId);
3513+
unsigned int numCols = getTypeNumColumns(resultTypeId);
3514+
unsigned int numRows = getTypeNumRows(resultTypeId);
35153515

35163516
Instruction* instr = module.getInstruction(componentTypeId);
35173517
const unsigned bitCount = instr->getImmediateOperand(0);
@@ -3526,11 +3526,11 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
35263526
Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
35273527

35283528
std::vector<unsigned> channels;
3529-
for (int row = 0; row < numRows; ++row)
3529+
for (unsigned int row = 0; row < numRows; ++row)
35303530
channels.push_back(row);
35313531

35323532
std::vector<Id> matrixColumns;
3533-
for (int col = 0; col < numCols; ++col) {
3533+
for (unsigned int col = 0; col < numCols; ++col) {
35343534
std::vector<unsigned> indexes;
35353535
indexes.push_back(col);
35363536
Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
@@ -3548,7 +3548,7 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
35483548

35493549
// Detect a matrix being constructed from a repeated vector of the correct size.
35503550
// Create the composite directly from it.
3551-
if ((int)sources.size() == numCols && isVector(sources[0]) && getNumComponents(sources[0]) == numRows &&
3551+
if (sources.size() == numCols && isVector(sources[0]) && getNumComponents(sources[0]) == numRows &&
35523552
std::equal(sources.begin() + 1, sources.end(), sources.begin())) {
35533553
return setPrecision(createCompositeConstruct(resultTypeId, sources), precision);
35543554
}
@@ -3580,12 +3580,12 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
35803580
} else if (isMatrix(sources[0])) {
35813581
// constructing from another matrix; copy over the parts that exist in both the argument and constructee
35823582
Id matrix = sources[0];
3583-
int minCols = std::min(numCols, getNumColumns(matrix));
3584-
int minRows = std::min(numRows, getNumRows(matrix));
3585-
for (int col = 0; col < minCols; ++col) {
3583+
unsigned int minCols = std::min(numCols, getNumColumns(matrix));
3584+
unsigned int minRows = std::min(numRows, getNumRows(matrix));
3585+
for (unsigned int col = 0; col < minCols; ++col) {
35863586
std::vector<unsigned> indexes;
35873587
indexes.push_back(col);
3588-
for (int row = 0; row < minRows; ++row) {
3588+
for (unsigned int row = 0; row < minRows; ++row) {
35893589
indexes.push_back(row);
35903590
ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes);
35913591
indexes.pop_back();
@@ -3594,12 +3594,12 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
35943594
}
35953595
} else {
35963596
// fill in the matrix in column-major order with whatever argument components are available
3597-
int row = 0;
3598-
int col = 0;
3597+
unsigned int row = 0;
3598+
unsigned int col = 0;
35993599

3600-
for (int arg = 0; arg < (int)sources.size() && col < numCols; ++arg) {
3600+
for (unsigned int arg = 0; arg < sources.size() && col < numCols; ++arg) {
36013601
Id argComp = sources[arg];
3602-
for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
3602+
for (unsigned int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
36033603
if (getNumComponents(sources[arg]) > 1) {
36043604
argComp = createCompositeExtract(sources[arg], componentTypeId, comp);
36053605
setPrecision(argComp, precision);
@@ -3623,9 +3623,9 @@ Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>&
36233623
// make the column vectors
36243624
Id columnTypeId = getContainedTypeId(resultTypeId);
36253625
std::vector<Id> matrixColumns;
3626-
for (int col = 0; col < numCols; ++col) {
3626+
for (unsigned int col = 0; col < numCols; ++col) {
36273627
std::vector<Id> vectorComponents;
3628-
for (int row = 0; row < numRows; ++row)
3628+
for (unsigned int row = 0; row < numRows; ++row)
36293629
vectorComponents.push_back(ids[col][row]);
36303630
Id column = createCompositeConstruct(columnTypeId, vectorComponents);
36313631
setPrecision(column, precision);
@@ -3852,7 +3852,7 @@ void Builder::accessChainStore(Id rvalue, Decoration nonUniform, spv::MemoryAcce
38523852

38533853
// If a swizzle exists and is not full and is not dynamic, then the swizzle will be broken into individual stores.
38543854
if (accessChain.swizzle.size() > 0 &&
3855-
getNumTypeComponents(getResultingAccessChainType()) != (int)accessChain.swizzle.size() &&
3855+
getNumTypeComponents(getResultingAccessChainType()) != accessChain.swizzle.size() &&
38563856
accessChain.component == NoResult) {
38573857
for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) {
38583858
accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle[i]));
@@ -4172,7 +4172,7 @@ void Builder::simplifyAccessChainSwizzle()
41724172
{
41734173
// If the swizzle has fewer components than the vector, it is subsetting, and must stay
41744174
// to preserve that fact.
4175-
if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size())
4175+
if (getNumTypeComponents(accessChain.preSwizzleBaseType) > accessChain.swizzle.size())
41764176
return;
41774177

41784178
// if components are out of order, it is a swizzle

SPIRV/SpvBuilder.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ class Builder {
264264
Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
265265
Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
266266
Op getMostBasicTypeClass(Id typeId) const;
267-
int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
268-
int getNumTypeConstituents(Id typeId) const;
269-
int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
267+
unsigned int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
268+
unsigned int getNumTypeConstituents(Id typeId) const;
269+
unsigned int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
270270
Id getScalarTypeId(Id typeId) const;
271271
Id getContainedTypeId(Id typeId) const;
272272
Id getContainedTypeId(Id typeId, int) const;
@@ -334,18 +334,18 @@ class Builder {
334334
return module.getInstruction(scalarTypeId)->getImmediateOperand(0);
335335
}
336336

337-
int getTypeNumColumns(Id typeId) const
337+
unsigned int getTypeNumColumns(Id typeId) const
338338
{
339339
assert(isMatrixType(typeId));
340340
return getNumTypeConstituents(typeId);
341341
}
342-
int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
343-
int getTypeNumRows(Id typeId) const
342+
unsigned int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
343+
unsigned int getTypeNumRows(Id typeId) const
344344
{
345345
assert(isMatrixType(typeId));
346346
return getNumTypeComponents(getContainedTypeId(typeId));
347347
}
348-
int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
348+
unsigned int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
349349

350350
Dim getTypeDimensionality(Id typeId) const
351351
{

0 commit comments

Comments
 (0)