Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant-fold for the type-casting in switch-case labels #5436

14 changes: 13 additions & 1 deletion source/slang/slang-check-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,19 @@ IntVal* SemanticsVisitor::tryConstantFoldExpr(
return nullptr;
if (!isValidCompileTimeConstantType(substType))
return nullptr;
auto val = tryConstantFoldExpr(typeCastOperand, kind, circularityInfo);

IntVal* val = tryConstantFoldExpr(typeCastOperand, kind, circularityInfo);
if (!val)
{
if (auto floatLitExpr = typeCastOperand.as<FloatingPointLiteralExpr>())
{
// When explicitly casting from float type to integer type, let's fold it as
// an integer value.
const IntegerLiteralValue value = IntegerLiteralValue(floatLitExpr.getExpr()->value);
val = m_astBuilder->getIntVal(substType, value);
}
}

if (val)
{
if (!expr.getExpr()->type)
Expand Down
45 changes: 45 additions & 0 deletions tests/bugs/gh-5372.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//TEST:SIMPLE(filecheck=SPV): -allow-glsl -target spirv-asm -entry vertexMain -stage vertex

// This test is to make sure the constant-folding works for the switch-case label.
// The shader code is from VK-CTS but modified,
// dEQP-VK.glsl.switch.const_expr_in_label_dynamic_fragment

layout(location = 0) in highp vec4 a_position;
layout(location = 1) in highp vec4 a_coords;

layout(location = 0) out mediump vec4 v_color;
layout (std140, set=0, binding=0) uniform buffer0 { highp int ui_two; };

void vertexMain(void)
{
gl_Position = a_position;
highp vec4 coords = a_coords;
mediump vec3 res = vec3(0.0);

const int t = 2;
switch (ui_two)
{
//SPV-NOT:([[# @LINE+1]]): error
case int(0.0):
res = coords.xyz;
break;

//SPV-NOT:([[# @LINE+1]]): error
case 2-1:
res = coords.wzy;
break;

//SPV-NOT:([[# @LINE+1]]): error
case 3&(1<<1):
res = coords.yzw;
break;

//SPV-NOT:([[# @LINE+1]]): error
case t+1:
res = coords.zyx;
break;
}

v_color = vec4(res, 1.0);
}

6 changes: 4 additions & 2 deletions tests/diagnostics/attribute-error.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Tests reflection of user defined attributes.

//DIAGNOSTIC_TEST:REFLECTION:-stage compute -entry main -target hlsl
//TEST:SIMPLE(filecheck=REFLECTION):-stage compute -entry main -target hlsl

[__AttributeUsage(_AttributeTargets.Struct)]
struct MyStructAttribute
Expand All @@ -16,9 +16,11 @@ struct DefaultValueAttribute
int iParam;
};

//REFLECTION:([[# @LINE+1]]): error 30019: expected an expression of type 'float', got 'String'
[MyStruct(0, "stringVal")] // attribute arg type mismatch
struct A
{
//REFLECTION:([[# @LINE+1]]): error 31002: attribute 'MyStruct' is not valid here
[MyStruct(0, 10.0)] // attribute does not apply to this construct
float x;
[DefaultValue(2.0)] // attribute arg type mismatch
Expand All @@ -31,4 +33,4 @@ ParameterBlock<A> param;
void main(
uint3 dispatchThreadID : SV_DispatchThreadID)
{
}
}
Loading