From bedcb920045dd68f522e29d90fc3804f84bc08d0 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 25 Mar 2019 14:39:13 -0700 Subject: Fix handling of enum tags that aren't literals (#926) An `enum` case currently lowers to the IR as the value of its tag expression, so if we have: ```hlsl enum E { X = 99, } ``` then `X` will lower as IR for the expression `99` which is just a literal. If instead we have: ```hlsl enum E { X = 99u, } ``` then after type checking the expression is `int(99u)`, which will get emitted to the global (module) scope as a cast/conversion instruction, that then gets referenced at the use sites of `E.X`. The emit logic wasn't set up to handle the case of referencing something directly from the global scope, so this change makes it so that side-effect-free global instructions are treated just like literal constants. This simple handling is fine for now since it will only apply to `enum` tag values (true global constants declared with `static const` have different handling). --- source/slang/emit.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source/slang/emit.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 84585f248..f17b13762 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -2393,6 +2393,7 @@ struct EmitVisitor case kIROp_GlobalConstant: case kIROp_GlobalParam: case kIROp_Param: + case kIROp_Func: return false; // Always fold these in, because they are trivial @@ -2513,6 +2514,15 @@ struct EmitVisitor } } + // If the instruction is at global scope, then it might represent + // a constant (e.g., the value of an enum case). + // + if(as(inst->getParent())) + { + if(!inst->mightHaveSideEffects()) + return true; + } + // Having dealt with all of the cases where we *must* fold things // above, we can now deal with the more general cases where we // *should not* fold things. -- cgit v1.2.3