diff options
| author | Darren Wihandi <65404740+fairywreath@users.noreply.github.com> | 2025-05-14 03:01:47 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-14 07:01:47 +0000 |
| commit | eb5648b41d0718648477cbcf941fb3c6edf6dfc7 (patch) | |
| tree | 73f19cd4b4f66d0ce6fbbd61c9a1253969cc3139 /source/slang/slang-ir-validate.cpp | |
| parent | 04ba87e23435e76583c05d4530d63686f9af712f (diff) | |
Error out on invalid vector sizes (#7076)
* Error out on invalid vector sizes
* Remove unnecessary include
* Fix incorrect assert
* Add test
Diffstat (limited to 'source/slang/slang-ir-validate.cpp')
| -rw-r--r-- | source/slang/slang-ir-validate.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/source/slang/slang-ir-validate.cpp b/source/slang/slang-ir-validate.cpp index 19de64618..565ae97d8 100644 --- a/source/slang/slang-ir-validate.cpp +++ b/source/slang/slang-ir-validate.cpp @@ -511,4 +511,124 @@ void validateAtomicOperations(bool skipFuncParamValidation, DiagnosticSink* sink } } +static void validateVectorOrMatrixElementType( + DiagnosticSink* sink, + SourceLoc sourceLoc, + IRType* elementType, + uint32_t allowedWidths, + const DiagnosticInfo& disallowedElementTypeEncountered) +{ + if (!isFloatingType(elementType)) + { + if (isIntegralType(elementType)) + { + IntInfo info = getIntTypeInfo(elementType); + if (allowedWidths == 0U) + { + sink->diagnose(sourceLoc, disallowedElementTypeEncountered, elementType); + } + else + { + bool widthAllowed = false; + SLANG_ASSERT((allowedWidths & ~(0xfU << 3)) == 0U); + for (uint32_t p = 3U; p <= 6U; p++) + { + uint32_t width = 1U << p; + if (!(allowedWidths & width)) + continue; + widthAllowed = widthAllowed || (info.width == width); + } + if (!widthAllowed) + { + sink->diagnose(sourceLoc, disallowedElementTypeEncountered, elementType); + } + } + } + else if (!as<IRBoolType>(elementType)) + { + sink->diagnose(sourceLoc, disallowedElementTypeEncountered, elementType); + } + } +} + +static void validateVectorElementCount(DiagnosticSink* sink, IRVectorType* vectorType) +{ + const auto elementCount = as<IRIntLit>(vectorType->getElementCount())->getValue(); + + // 1-vectors are supported and are legalized/transformed properly when targetting unsupported + // backends. + const IRIntegerValue minCount = 1; + const IRIntegerValue maxCount = 4; + if ((elementCount < minCount) || (elementCount > maxCount)) + { + sink->diagnose( + vectorType->sourceLoc, + Diagnostics::vectorWithInvalidElementCountEncountered, + elementCount, + "1", + maxCount); + } +} + +void validateVectorsAndMatrices( + IRModule* module, + DiagnosticSink* sink, + TargetRequest* targetRequest) +{ + for (auto globalInst : module->getGlobalInsts()) + { + if (auto matrixType = as<IRMatrixType>(globalInst)) + { + // Matrices with row/col dimension 1 are only well-supported on D3D targets + if (!isD3DTarget(targetRequest)) + { + // Verify that neither row nor col count is 1 + auto colCount = as<IRIntLit>(matrixType->getColumnCount()); + auto rowCount = as<IRIntLit>(matrixType->getRowCount()); + + if ((rowCount && (rowCount->getValue() == 1)) || + (colCount && (colCount->getValue() == 1))) + { + sink->diagnose(matrixType->sourceLoc, Diagnostics::matrixColumnOrRowCountIsOne); + } + } + + // Verify that the element type is a floating point type, or an allowed integral type + auto elementType = matrixType->getElementType(); + uint32_t allowedWidths = 0U; + if (isCPUTarget(targetRequest)) + allowedWidths = 8U | 16U | 32U | 64U; + else if (isCUDATarget(targetRequest)) + allowedWidths = 32U | 64U; + else if (isD3DTarget(targetRequest)) + allowedWidths = 16U | 32U; + validateVectorOrMatrixElementType( + sink, + matrixType->sourceLoc, + elementType, + allowedWidths, + Diagnostics::matrixWithDisallowedElementTypeEncountered); + } + else if (auto vectorType = as<IRVectorType>(globalInst)) + { + // Verify that the element type is a floating point type, or an allowed integral type + auto elementType = vectorType->getElementType(); + uint32_t allowedWidths = 0U; + if (isWGPUTarget(targetRequest)) + allowedWidths = 32U; + else + allowedWidths = 8U | 16U | 32U | 64U; + + validateVectorOrMatrixElementType( + sink, + vectorType->sourceLoc, + elementType, + allowedWidths, + Diagnostics::vectorWithDisallowedElementTypeEncountered); + + validateVectorElementCount(sink, vectorType); + } + } +} + } // namespace Slang |
