From 5a174dfab4ae0852cb96df5f48bae474949cc017 Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:34:51 -0700 Subject: Fix the issue in emitFloatCast (#4559) * Fix the issue in emitFloatCast In emitFloatCast function, we only considered the input type is float scalar or float vector, so if the input type is a float matrix type, it will crash. We should also handle the float matrix type. Also, we add some diagnose info to point out the source location where there is error happened, so in the future it's easier to tell us what happens. * Add a unit test * Disable the test for metal Metal doesn't support 'double'. " metal 32023.35: /tmp/unknown-YgHAsJ.metal(15): error : 'double' is not supported in Metal matrix b_0 = matrix (a_0); " --- source/slang/slang-emit-spirv.cpp | 35 +++++++++++++++++++++++++++++++---- source/slang/slang-ir-util.cpp | 7 +++++++ source/slang/slang-ir-util.h | 3 +++ 3 files changed, 41 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index 1a4d80ae1..f7d807190 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -2403,7 +2403,7 @@ struct SPIRVEmitContext // For now we aren't handling function declarations; // we expect to deal only with fully linked modules. // - SLANG_UNUSED(irFunc); + m_sink->diagnose(irFunc, Diagnostics::internalCompilerError); SLANG_UNEXPECTED("function declaration in SPIR-V emit"); UNREACHABLE_RETURN(nullptr); } @@ -5194,9 +5194,36 @@ struct SPIRVEmitContext { const auto fromTypeV = inst->getOperand(0)->getDataType(); const auto toTypeV = inst->getDataType(); - SLANG_ASSERT(!as(fromTypeV) == !as(toTypeV)); - const auto fromType = getVectorElementType(fromTypeV); - const auto toType = getVectorElementType(toTypeV); + + IRType* fromType = nullptr; + IRType* toType = nullptr; + + if (as(fromTypeV) || as(toTypeV)) + { + fromType = getVectorElementType(fromTypeV); + toType = getVectorElementType(toTypeV); + } + else if (as(fromTypeV) || as(toTypeV)) + { + fromType = getMatrixElementType(fromTypeV); + toType = getMatrixElementType(toTypeV); + } + else + { + fromType = fromTypeV; + toType = toTypeV; + } + + // We'd better give some diagnostics to at least point out which line in the shader is wrong, so + // it can help the user or developers to locate the issue easier. + if (!isFloatingType(fromType)) { + m_sink->diagnose(inst, Diagnostics::internalCompilerError); + } + + if (!isFloatingType(toType)) { + m_sink->diagnose(inst, Diagnostics::internalCompilerError); + } + SLANG_ASSERT(isFloatingType(fromType)); SLANG_ASSERT(isFloatingType(toType)); SLANG_ASSERT(!isTypeEqual(fromType, toType)); diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 10c7bfea6..8294cd533 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -23,6 +23,13 @@ IRType* getVectorElementType(IRType* type) return type; } +IRType* getMatrixElementType(IRType* type) +{ + if (auto matrixType = as(type)) + return matrixType->getElementType(); + return type; +} + Dictionary buildInterfaceRequirementDict(IRInterfaceType* interfaceType) { Dictionary result; diff --git a/source/slang/slang-ir-util.h b/source/slang/slang-ir-util.h index 855046c04..c7d6a1544 100644 --- a/source/slang/slang-ir-util.h +++ b/source/slang/slang-ir-util.h @@ -77,6 +77,9 @@ bool isComInterfaceType(IRType* type); // If `type` is a vector, returns its element type. Otherwise, return `type`. IRType* getVectorElementType(IRType* type); +// If `type` is a matrix, returns its element type. Otherwise, return `type`. +IRType* getMatrixElementType(IRType* type); + // True if type is a resource backing memory bool isResourceType(IRType* type); -- cgit v1.2.3