Collect static use information during parsing
We now collect metadata for variables in the symbol table. The
metadata is stored in a map using the variable unique id as a key, so
we can store the variables themselves as constexpr while still having
dynamic metadata.
For now we collect whether a variable is statically read or written.
This can be used to more accurately determine whether a variable is
statically used, but can also enable more optimizations in the future,
such as pruning variables that are never read or folding variables
that are never written after initialization. The collection is done
during parsing, so that nothing is pruned from the AST before the
static use is recorded.
Static writes are flagged in ParseContext::checkCanBeLValue, as that
function is already called for all variables that are written.
Static reads are flagged whenever there's an operation that requires
a variable to be read. This includes:
* Unary and binary math ops
* Comma ops
* Ternary ops
* Assignments
* Returning the variable
* Passing the variable as an in or inout argument to a function
* Using the variable as a constructor argument
* Using the variable as an if statement condition
* Using the variable as a loop condition or expression
* Using the variable as an index
* Using the variable as a switch statement init expression
In case there are statements that simply refer to a variable without
doing operations on it, the variable is being treated as statically
read. Examples of such statements:
my_var;
my_arr[2];
These are a bit of a corner case, but it makes sense to treat them as
static use for validation purposes.
Collecting correct static use information costs us a bit of compiler
performance, but the regression is on the order of just a few percent
in the compiler perf tests.
BUG=angleproject:2262
TEST=angle_unittests, angle_end2end_tests
Change-Id: Ib0d7add7e4a7d11bffeb2a4861eeea982c562234
Reviewed-on: https://chromium-review.googlesource.com/977964
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/tests/gl_tests/GLSLTest.cpp b/src/tests/gl_tests/GLSLTest.cpp
index 45ef2d5..d591060 100644
--- a/src/tests/gl_tests/GLSLTest.cpp
+++ b/src/tests/gl_tests/GLSLTest.cpp
@@ -4148,6 +4148,66 @@
"interface block 'S' member 'S.val1.t2'");
}
+// Test a vertex shader that doesn't declare any varyings with a fragment shader that statically
+// uses a varying, but in a statement that gets trivially optimized out by the compiler.
+TEST_P(GLSLTest_ES3, FragmentShaderStaticallyUsesVaryingMissingFromVertex)
+{
+ const std::string &vertexShader =
+ R"(#version 300 es
+
+ precision mediump float;
+
+ void main()
+ {
+ gl_Position = vec4(0, 1, 0, 1);
+ })";
+
+ const std::string &fragmentShader =
+ R"(#version 300 es
+
+ precision mediump float;
+ in float foo;
+ out vec4 my_FragColor;
+
+ void main()
+ {
+ if (false)
+ {
+ float unreferenced = foo;
+ }
+ my_FragColor = vec4(0, 1, 0, 1);
+ })";
+
+ validateComponentsInErrorMessage(vertexShader, fragmentShader, "does not match any", "foo");
+}
+
+// Test a varying that is statically used but not active in the fragment shader.
+TEST_P(GLSLTest_ES3, VaryingStaticallyUsedButNotActiveInFragmentShader)
+{
+ const std::string &vertexShader =
+ R"(#version 300 es
+ precision mediump float;
+ in vec4 iv;
+ out vec4 v;
+ void main()
+ {
+ gl_Position = iv;
+ v = iv;
+ })";
+
+ const std::string &fragmentShader =
+ R"(#version 300 es
+ precision mediump float;
+ in vec4 v;
+ out vec4 color;
+ void main()
+ {
+ color = true ? vec4(0.0) : v;
+ })";
+
+ ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
+}
+
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
ANGLE_INSTANTIATE_TEST(GLSLTest,
ES2_D3D9(),