From 13c6b69cc2601ff4764f095fb45ad9573d34ff2f Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 11 Oct 2018 11:17:40 -0700 Subject: Fix error when one constant is defined equal to another (#670) * Fix error when one constant is defined equal to another Fixes #666 When a user declares one constant (usually a `static const` variable) to be exactly equal to another by name: ```hlsl static const a = 999; static const b = a; ``` Then the IR-level representation of `b` is an `IRGlobalConstant` whose value expression is just a pointer to the definition of `a`. The logic in `emitIRGlobalConstantInitializer()` was trying to always call `emitIRInstExpr` to emit the value of the constant as an expression, but that function only handles complex/compound expressions and not the case of simple named values (e.g., constants like `a`). The intention is for code to call `emitIROperand()` instead, and let it decide whether to emit an expression or a named reference using its own decision-making. The `IRGlobalConstant` case really just wants to pass in the "mode" flag it uses to influence that decision-making, but shouldn't be working around it. This change just replaces the `emitIRInstExp()` call with `emitIROpernad()` and adds a test case to confirm that this fixes the reported problem. * Fixups for bugs in previous change The first problem was that certain instruction ops were being special-cased to opt out of "folding" into expressions *before* we make the universal check to always fold when inside an initializer for a global constant. The second problem is that the `emitIROperand()` logic was always putting expressions around sub-expressions, which breaks parsing when the sub-expression is an initializer list (`{...}`). This fixup is pretty much a hack, but will be something we can remove once we don't emit unncessary parentheses overall, which is a better fix. --- tests/bugs/gh-666.slang | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/bugs/gh-666.slang (limited to 'tests') diff --git a/tests/bugs/gh-666.slang b/tests/bugs/gh-666.slang new file mode 100644 index 000000000..fa46d1de7 --- /dev/null +++ b/tests/bugs/gh-666.slang @@ -0,0 +1,11 @@ +// gh-666.slang + +//TEST:COMPARE_HLSL:-no-mangle -profile ps_5_0 -entry main + +static const uint foo = 1; +static const uint bar = foo; + +float4 main() : SV_TARGET +{ + return bar; +} -- cgit v1.2.3