From b06e94b87a115c5979c728ce13ddf34b80285a9f Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Tue, 3 Jun 2025 12:03:11 +0300 Subject: Fix specialization constants getting incorrectly folded (#7299) --- source/slang/slang-check-expr.cpp | 41 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'source') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 9472138c3..1e052a553 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1976,26 +1976,31 @@ IntVal* SemanticsVisitor::tryConstantFoldDeclRef( // The values of specialization constants aren't known at compile time even // if they're marked `const`. - if ((decl->hasModifier() || - decl->hasModifier()) && - kind == ConstantFoldingKind::SpecializationConstant) - { - // Float-to-inst casts cannot be`OpSpecConstOp` operations in SPIR-V, - // which means they need to be local instructions can cannot be hoisted to the - // global scope. Deduplication logic is run for `IntVal`s however and without hoisting - // instructions using this `IntVal` will trigger error. Hence we emit error here - // to not allow such cases. - // - // Note that float-to-inst casts for non-`IntVal`s are allowed. - if (!isScalarIntegerType(decl->getType())) + if (decl->hasModifier() || + decl->hasModifier()) + { + if (kind == ConstantFoldingKind::SpecializationConstant) { - getSink()->diagnose(declRef, Diagnostics::intValFromNonIntSpecConstEncountered); - return nullptr; - } + // Float-to-inst casts cannot be`OpSpecConstOp` operations in SPIR-V, + // which means they need to be local instructions can cannot be hoisted to the + // global scope. Deduplication logic is run for `IntVal`s however and without hoisting + // instructions using this `IntVal` will trigger error. Hence we emit error here + // to not allow such cases. + // + // Note that float-to-inst casts for non-`IntVal`s are allowed. + if (!isScalarIntegerType(decl->getType())) + { + getSink()->diagnose(declRef, Diagnostics::intValFromNonIntSpecConstEncountered); + return nullptr; + } - return m_astBuilder->getOrCreate( - declRef.substitute(m_astBuilder, decl->getType()), - declRef); + return m_astBuilder->getOrCreate( + declRef.substitute(m_astBuilder, decl->getType()), + declRef); + } + // Don't fold on other folding passes, we don't actually know the + // values. + return nullptr; } if (decl->hasModifier()) -- cgit v1.2.3