From e77c831fbbe83b4c0d27eb0e318b66426b6118a6 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 29 Sep 2023 16:08:04 +0200 Subject: [PATCH] avoid having constants pointing to other constants --- src/front/glsl/parser_tests.rs | 17 +-- src/proc/constant_evaluator.rs | 5 + tests/in/const-exprs.wgsl | 5 + tests/out/glsl/const-exprs.main.Compute.glsl | 3 + tests/out/hlsl/const-exprs.hlsl | 3 + tests/out/msl/const-exprs.msl | 3 + tests/out/spv/const-exprs.spvasm | 124 +++++++++---------- tests/out/wgsl/const-exprs.wgsl | 3 + 8 files changed, 89 insertions(+), 74 deletions(-) diff --git a/src/front/glsl/parser_tests.rs b/src/front/glsl/parser_tests.rs index dd5880068c..1813a4ce49 100644 --- a/src/front/glsl/parser_tests.rs +++ b/src/front/glsl/parser_tests.rs @@ -543,33 +543,26 @@ fn constants() { } ); - let (init_a_handle, init_a) = const_expressions.next().unwrap(); - assert_eq!(init_a, &Expression::Literal(crate::Literal::F32(1.0))); + let (init_handle, init) = const_expressions.next().unwrap(); + assert_eq!(init, &Expression::Literal(crate::Literal::F32(1.0))); - let (constant_a_handle, constant_a) = constants.next().unwrap(); assert_eq!( - constant_a, + constants.next().unwrap().1, &Constant { name: Some("a".to_owned()), r#override: crate::Override::None, ty: ty_handle, - init: init_a_handle + init: init_handle } ); - // skip const expr that was inserted for `global` var - const_expressions.next().unwrap(); - - let (init_b_handle, init_b) = const_expressions.next().unwrap(); - assert_eq!(init_b, &Expression::Constant(constant_a_handle)); - assert_eq!( constants.next().unwrap().1, &Constant { name: Some("b".to_owned()), r#override: crate::Override::None, ty: ty_handle, - init: init_b_handle + init: init_handle } ); diff --git a/src/proc/constant_evaluator.rs b/src/proc/constant_evaluator.rs index e22b1275eb..a95684e78b 100644 --- a/src/proc/constant_evaluator.rs +++ b/src/proc/constant_evaluator.rs @@ -210,6 +210,11 @@ impl<'a> ConstantEvaluator<'a> { ) -> Result, ConstantEvaluatorError> { log::trace!("try_eval_and_append: {:?}", expr); match *expr { + Expression::Constant(c) if self.function_local_data.is_none() => { + // "See through" the constant and use its initializer. + // This is mainly done to avoid having constants pointing to other constants. + Ok(self.constants[c].init) + } Expression::Literal(_) | Expression::ZeroValue(_) | Expression::Constant(_) => { Ok(self.register_evaluated_expr(expr.clone(), span)) } diff --git a/tests/in/const-exprs.wgsl b/tests/in/const-exprs.wgsl index 73d5b4af2f..89d8976f5b 100644 --- a/tests/in/const-exprs.wgsl +++ b/tests/in/const-exprs.wgsl @@ -53,6 +53,11 @@ fn non_constant_initializers() { // their values. const FOUR: i32 = 4; +const FOUR_ALIAS: i32 = FOUR; + +const TEST_CONSTANT_ADDITION: i32 = FOUR + FOUR; +const TEST_CONSTANT_ALIAS_ADDITION: i32 = FOUR_ALIAS + FOUR_ALIAS; + fn splat_of_constant() { out = -vec4(FOUR); } diff --git a/tests/out/glsl/const-exprs.main.Compute.glsl b/tests/out/glsl/const-exprs.main.Compute.glsl index 5b65b0d260..4c24570040 100644 --- a/tests/out/glsl/const-exprs.main.Compute.glsl +++ b/tests/out/glsl/const-exprs.main.Compute.glsl @@ -6,6 +6,9 @@ precision highp int; layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; const int FOUR = 4; +const int FOUR_ALIAS = 4; +const int TEST_CONSTANT_ADDITION = 8; +const int TEST_CONSTANT_ALIAS_ADDITION = 8; layout(std430) buffer type_block_0Compute { ivec4 _group_0_binding_0_cs; }; diff --git a/tests/out/hlsl/const-exprs.hlsl b/tests/out/hlsl/const-exprs.hlsl index 51b6cf1c11..a4b4044f7a 100644 --- a/tests/out/hlsl/const-exprs.hlsl +++ b/tests/out/hlsl/const-exprs.hlsl @@ -1,4 +1,7 @@ static const int FOUR = 4; +static const int FOUR_ALIAS = 4; +static const int TEST_CONSTANT_ADDITION = 8; +static const int TEST_CONSTANT_ALIAS_ADDITION = 8; RWByteAddressBuffer out_ : register(u0); RWByteAddressBuffer out2_ : register(u1); diff --git a/tests/out/msl/const-exprs.msl b/tests/out/msl/const-exprs.msl index a3d86bd5ea..cb448595b0 100644 --- a/tests/out/msl/const-exprs.msl +++ b/tests/out/msl/const-exprs.msl @@ -5,6 +5,9 @@ using metal::uint; constant int FOUR = 4; +constant int FOUR_ALIAS = 4; +constant int TEST_CONSTANT_ADDITION = 8; +constant int TEST_CONSTANT_ALIAS_ADDITION = 8; void swizzle_of_compose( device metal::int4& out diff --git a/tests/out/spv/const-exprs.spvasm b/tests/out/spv/const-exprs.spvasm index bbbc164c61..d29340068f 100644 --- a/tests/out/spv/const-exprs.spvasm +++ b/tests/out/spv/const-exprs.spvasm @@ -8,42 +8,42 @@ OpExtension "SPV_KHR_storage_buffer_storage_class" OpMemoryModel Logical GLSL450 OpEntryPoint GLCompute %80 "main" OpExecutionMode %80 LocalSize 1 1 1 -OpDecorate %8 DescriptorSet 0 -OpDecorate %8 Binding 0 -OpDecorate %9 Block -OpMemberDecorate %9 0 Offset 0 -OpDecorate %11 DescriptorSet 0 -OpDecorate %11 Binding 1 -OpDecorate %12 Block -OpMemberDecorate %12 0 Offset 0 +OpDecorate %9 DescriptorSet 0 +OpDecorate %9 Binding 0 +OpDecorate %10 Block +OpMemberDecorate %10 0 Offset 0 +OpDecorate %12 DescriptorSet 0 +OpDecorate %12 Binding 1 +OpDecorate %13 Block +OpMemberDecorate %13 0 Offset 0 %2 = OpTypeVoid %4 = OpTypeInt 32 1 %3 = OpTypeVector %4 4 %5 = OpTypeVector %4 2 %6 = OpTypeVector %4 3 %7 = OpConstant %4 4 -%9 = OpTypeStruct %3 -%10 = OpTypePointer StorageBuffer %9 -%8 = OpVariable %10 StorageBuffer -%12 = OpTypeStruct %4 -%13 = OpTypePointer StorageBuffer %12 -%11 = OpVariable %13 StorageBuffer -%16 = OpTypeFunction %2 -%17 = OpTypePointer StorageBuffer %3 -%19 = OpTypeInt 32 0 -%18 = OpConstant %19 0 -%21 = OpConstant %4 1 -%22 = OpConstant %4 2 -%23 = OpConstantComposite %5 %21 %22 -%24 = OpConstant %4 3 -%25 = OpConstantComposite %5 %24 %7 -%26 = OpConstantComposite %3 %7 %24 %22 %21 -%30 = OpTypePointer StorageBuffer %4 -%38 = OpConstant %4 6 -%39 = OpConstant %4 7 -%40 = OpConstantComposite %5 %38 %39 -%41 = OpConstant %4 8 -%42 = OpConstantComposite %6 %38 %39 %41 +%8 = OpConstant %4 8 +%10 = OpTypeStruct %3 +%11 = OpTypePointer StorageBuffer %10 +%9 = OpVariable %11 StorageBuffer +%13 = OpTypeStruct %4 +%14 = OpTypePointer StorageBuffer %13 +%12 = OpVariable %14 StorageBuffer +%17 = OpTypeFunction %2 +%18 = OpTypePointer StorageBuffer %3 +%20 = OpTypeInt 32 0 +%19 = OpConstant %20 0 +%22 = OpConstant %4 1 +%23 = OpConstant %4 2 +%24 = OpConstantComposite %5 %22 %23 +%25 = OpConstant %4 3 +%26 = OpConstantComposite %5 %25 %7 +%27 = OpConstantComposite %3 %7 %25 %23 %22 +%31 = OpTypePointer StorageBuffer %4 +%39 = OpConstant %4 6 +%40 = OpConstant %4 7 +%41 = OpConstantComposite %5 %39 %40 +%42 = OpConstantComposite %6 %39 %40 %8 %43 = OpConstant %4 9 %50 = OpConstant %4 30 %51 = OpConstant %4 70 @@ -52,41 +52,41 @@ OpMemberDecorate %12 0 Offset 0 %57 = OpConstantNull %4 %72 = OpConstant %4 -4 %73 = OpConstantComposite %3 %72 %72 %72 %72 -%15 = OpFunction %2 None %16 -%14 = OpLabel -%20 = OpAccessChain %17 %8 %18 -OpBranch %27 -%27 = OpLabel -OpStore %20 %26 +%16 = OpFunction %2 None %17 +%15 = OpLabel +%21 = OpAccessChain %18 %9 %19 +OpBranch %28 +%28 = OpLabel +OpStore %21 %27 OpReturn OpFunctionEnd -%29 = OpFunction %2 None %16 -%28 = OpLabel -%31 = OpAccessChain %30 %11 %18 -OpBranch %32 -%32 = OpLabel -%33 = OpLoad %4 %31 -%34 = OpIAdd %4 %33 %22 -OpStore %31 %34 +%30 = OpFunction %2 None %17 +%29 = OpLabel +%32 = OpAccessChain %31 %12 %19 +OpBranch %33 +%33 = OpLabel +%34 = OpLoad %4 %32 +%35 = OpIAdd %4 %34 %23 +OpStore %32 %35 OpReturn OpFunctionEnd -%36 = OpFunction %2 None %16 -%35 = OpLabel -%37 = OpAccessChain %30 %11 %18 +%37 = OpFunction %2 None %17 +%36 = OpLabel +%38 = OpAccessChain %31 %12 %19 OpBranch %44 %44 = OpLabel -%45 = OpLoad %4 %37 -%46 = OpIAdd %4 %45 %38 -OpStore %37 %46 +%45 = OpLoad %4 %38 +%46 = OpIAdd %4 %45 %39 +OpStore %38 %46 OpReturn OpFunctionEnd -%48 = OpFunction %2 None %16 +%48 = OpFunction %2 None %17 %47 = OpLabel %54 = OpVariable %53 Function %55 %58 = OpVariable %53 Function %51 %52 = OpVariable %53 Function %50 %56 = OpVariable %53 Function %57 -%49 = OpAccessChain %17 %8 %18 +%49 = OpAccessChain %18 %9 %19 OpBranch %59 %59 = OpLabel %60 = OpLoad %4 %52 @@ -103,31 +103,31 @@ OpStore %56 %61 OpStore %49 %68 OpReturn OpFunctionEnd -%70 = OpFunction %2 None %16 +%70 = OpFunction %2 None %17 %69 = OpLabel -%71 = OpAccessChain %17 %8 %18 +%71 = OpAccessChain %18 %9 %19 OpBranch %74 %74 = OpLabel OpStore %71 %73 OpReturn OpFunctionEnd -%76 = OpFunction %2 None %16 +%76 = OpFunction %2 None %17 %75 = OpLabel -%77 = OpAccessChain %17 %8 %18 +%77 = OpAccessChain %18 %9 %19 OpBranch %78 %78 = OpLabel OpStore %77 %73 OpReturn OpFunctionEnd -%80 = OpFunction %2 None %16 +%80 = OpFunction %2 None %17 %79 = OpLabel -%81 = OpAccessChain %17 %8 %18 -%82 = OpAccessChain %30 %11 %18 +%81 = OpAccessChain %18 %9 %19 +%82 = OpAccessChain %31 %12 %19 OpBranch %83 %83 = OpLabel -%84 = OpFunctionCall %2 %15 -%85 = OpFunctionCall %2 %29 -%86 = OpFunctionCall %2 %36 +%84 = OpFunctionCall %2 %16 +%85 = OpFunctionCall %2 %30 +%86 = OpFunctionCall %2 %37 %87 = OpFunctionCall %2 %48 %88 = OpFunctionCall %2 %70 %89 = OpFunctionCall %2 %76 diff --git a/tests/out/wgsl/const-exprs.wgsl b/tests/out/wgsl/const-exprs.wgsl index 9fb648fd32..aa0b7e8e78 100644 --- a/tests/out/wgsl/const-exprs.wgsl +++ b/tests/out/wgsl/const-exprs.wgsl @@ -1,4 +1,7 @@ const FOUR: i32 = 4; +const FOUR_ALIAS: i32 = 4; +const TEST_CONSTANT_ADDITION: i32 = 8; +const TEST_CONSTANT_ALIAS_ADDITION: i32 = 8; @group(0) @binding(0) var out: vec4;