https://github.com/shader-slang/slang
Revision ddf3d5550f06d19606e5d148edca308f852dbe03 authored by Tim Foley on 17 July 2019, 21:38:55 UTC, committed by GitHub on 17 July 2019, 21:38:55 UTC
Before this change, global and function-scope `static const` declarations were represented as instructions of type `IRGlobalConstant`, which was represented similarly to an `IRGlobalVar`: with a "body" block of instructions that compute/return the initial value.

This representation inhibited optimizations (because a reference to a global constant would not in general be replaced with a reference to its value), and also caused problems for resource type legalization because the logic for type legalization did not (and still does not) handle initializers on globals (so global *variables* that contain resource types are still unsupported).

The change here is simple at the high level: we get rid of `IRGlobalConstant` and instead handle global-scope constants as "ordinary" instructions at the global scope. E.g., if we have a declaration like:

    static const int a[] = { ... }

that will be represented in the IR as a `makeArray` instruction at the global scope, referencing other global-scope instructions that represent the values in the array.

This simple choice addresses both of the main limitations. A `static const` variable of integer/float/whatever type is now represented as just a reference to the given IR value and thus enables all the same optimizations. When a `static const` variable uses a type with resources, the existing legalization logic (which can handle most of the "ordinary" instructions already) applies.

Another secondary benefit of this approach is that the hacky `IREmitMode` enumeration is no longer needed to help us special-case source code emit for `static const` variables.

Beyond just removing `IRGlobalConstant`, and updating the lowering logic to use the initializer direclty, the main change here is to the emit logic to make it properly handle "ordinary" instructions that might appear at global scope.

One open issue with this change, that could be addressed in a follow-up change, is that "extern" global constants that need to be imported from another module (but which might not have a known value when the current module is compiled) aren't supported - we don't have a way to put a linkage decoration on them. A future change might re-introduce global constants as a distinct IR instruction type that just references the value as an operand (if it is available). We would then need to replace references to an IR constant with references to its value right after linking.
1 parent 749634a
History
Tip revision: ddf3d5550f06d19606e5d148edca308f852dbe03 authored by Tim Foley on 17 July 2019, 21:38:55 UTC
Change how global-scope constants are handled (#1001)
Tip revision: ddf3d55
File Mode Size
docs
examples
external
source
tests
tools
.editorconfig -rw-r--r-- 937 bytes
.gitattributes -rw-r--r-- 95 bytes
.gitignore -rw-r--r-- 480 bytes
.gitmodules -rw-r--r-- 406 bytes
.travis.yml -rw-r--r-- 1.7 KB
CODE_OF_CONDUCT.md -rw-r--r-- 3.1 KB
LICENSE -rw-r--r-- 1.1 KB
README.md -rw-r--r-- 6.7 KB
appveyor.yml -rw-r--r-- 3.7 KB
premake5.lua -rw-r--r-- 26.7 KB
slang-com-helper.h -rw-r--r-- 4.8 KB
slang-com-ptr.h -rw-r--r-- 4.8 KB
slang.h -rw-r--r-- 107.4 KB
slang.sln -rw-r--r-- 9.8 KB
test.bat -rw-r--r-- 1.4 KB
travis_build.sh -rw-r--r-- 304 bytes
travis_test.sh -rw-r--r-- 435 bytes

README.md

back to top