From 48a7218b1551a97d1c710ed47c7e01c755a7c253 Mon Sep 17 00:00:00 2001 From: Anders Leino Date: Thu, 9 Jan 2025 11:58:36 +0200 Subject: [PATCH] Emit errors when seeing a matrix with row/col count of 1 Such matrices aren't well supported except by D3D targets. Therefore, generate an error rather than outputting invalid code for non-D3D targets. This closes #5987. --- source/slang/slang-diagnostic-defs.h | 6 ++++++ source/slang/slang-emit.cpp | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index d86cd8be2a..eacc345a73 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -1658,6 +1658,12 @@ DIAGNOSTIC( overloadedParameterToHigherOrderFunction, "passing overloaded functions to higher order functions is not supported") +DIAGNOSTIC( + 39999, + Error, + matrixColumnOrRowCountIsOne, + "matrices with 1 column or row are not supported by the current code generation target") + // 38xxx DIAGNOSTIC( diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index ee25822670..b17bc03f30 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -555,6 +555,24 @@ static void unexportNonEmbeddableIR(CodeGenTarget target, IRModule* irModule) } } +static void validateMatrixDimensions(DiagnosticSink* sink, IRModule* module) +{ + for (auto globalInst : module->getGlobalInsts()) + { + if (auto matrixType = as(globalInst)) + { + auto colCount = as(matrixType->getColumnCount()); + auto rowCount = as(matrixType->getRowCount()); + + if ((rowCount && (rowCount->getValue() == 1)) || + (colCount && (colCount->getValue() == 1))) + { + sink->diagnose(matrixType->sourceLoc, Diagnostics::matrixColumnOrRowCountIsOne); + } + } + } +} + Result linkAndOptimizeIR( CodeGenContext* codeGenContext, LinkingAndOptimizationOptions const& options, @@ -1504,6 +1522,10 @@ Result linkAndOptimizeIR( #endif validateIRModuleIfEnabled(codeGenContext, irModule); + // Make sure there are no matrices with 1 row/column, except for D3D targets where it's allowed. + if (!isD3DTarget(targetRequest)) + validateMatrixDimensions(sink, irModule); + // The resource-based specialization pass above // may create specialized versions of functions, but // it does not try to completely eliminate the original