Fix deferring global array initialization

The initial implementation of DeferGlobalInitializers did not take
HLSL corner cases into account. In particular, in case there was a
const-qualified array variable with an initializer that contained
elements that weren't constant folded, initialization would not be
deferred and the global scope of HLSL output would contain a call to
angle_construct_into_*().

On the other hand, deferring global initializers was also done in
cases where it wasn't necessary. Initializers of non-const qualified
array variables that could be written as HLSL literals by HLSL output
were unnecessarily deferred.

This patch fixes both of these issues: Now all global initializers are
potential candidates for deferral instead of just those where the
symbol has the EvqGlobal qualifier, and initializers that are
constructors taking only constant unions as parameters are not
unnecessarily deferred.

BUG=angleproject:1205
BUG=541551
TEST=angle_end2end_tests

Change-Id: I4027059e0e5f39c8a5a48b5c97a3fceaac6b6f8a
Reviewed-on: https://chromium-review.googlesource.com/339201
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/DeferGlobalInitializers.cpp b/src/compiler/translator/DeferGlobalInitializers.cpp
index 12c7a9f..6904f94 100644
--- a/src/compiler/translator/DeferGlobalInitializers.cpp
+++ b/src/compiler/translator/DeferGlobalInitializers.cpp
@@ -27,17 +27,20 @@
     functionNode->setNameObj(nameObj);
 }
 
-TIntermAggregate *CreateFunctionPrototypeNode(const char *name)
+TIntermAggregate *CreateFunctionPrototypeNode(const char *name, const int functionId)
 {
     TIntermAggregate *functionNode = new TIntermAggregate(EOpPrototype);
 
     SetInternalFunctionName(functionNode, name);
     TType returnType(EbtVoid);
     functionNode->setType(returnType);
+    functionNode->setFunctionId(functionId);
     return functionNode;
 }
 
-TIntermAggregate *CreateFunctionDefinitionNode(const char *name, TIntermAggregate *functionBody)
+TIntermAggregate *CreateFunctionDefinitionNode(const char *name,
+                                               TIntermAggregate *functionBody,
+                                               const int functionId)
 {
     TIntermAggregate *functionNode = new TIntermAggregate(EOpFunction);
     TIntermAggregate *paramsNode = new TIntermAggregate(EOpParameters);
@@ -47,16 +50,19 @@
     SetInternalFunctionName(functionNode, name);
     TType returnType(EbtVoid);
     functionNode->setType(returnType);
+    functionNode->setFunctionId(functionId);
     return functionNode;
 }
 
-TIntermAggregate *CreateFunctionCallNode(const char *name)
+TIntermAggregate *CreateFunctionCallNode(const char *name, const int functionId)
 {
     TIntermAggregate *functionNode = new TIntermAggregate(EOpFunctionCall);
 
+    functionNode->setUserDefined();
     SetInternalFunctionName(functionNode, name);
     TType returnType(EbtVoid);
     functionNode->setType(returnType);
+    functionNode->setFunctionId(functionId);
     return functionNode;
 }
 
@@ -86,8 +92,9 @@
         ASSERT(symbolNode);
         TIntermTyped *expression = node->getRight();
 
-        if (symbolNode->getQualifier() == EvqGlobal &&
-            (expression->getQualifier() != EvqConst || expression->getAsConstantUnion() == nullptr))
+        if (mInGlobalScope && (expression->getQualifier() != EvqConst ||
+                               (expression->getAsConstantUnion() == nullptr &&
+                                !expression->isConstructorWithOnlyConstantUnionParameters())))
         {
             // For variables which are not constant, defer their real initialization until
             // after we initialize uniforms.
@@ -95,13 +102,35 @@
             // since otherwise there's a chance that HLSL output will generate extra statements
             // from the initializer expression.
             TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
-            deferredInit->setLeft(node->getLeft()->deepCopy());
+            deferredInit->setLeft(symbolNode->deepCopy());
             deferredInit->setRight(node->getRight());
             deferredInit->setType(node->getType());
             mDeferredInitializers.push_back(deferredInit);
 
+            // Change const global to a regular global if its initialization is deferred.
+            // This can happen if ANGLE has not been able to fold the constant expression used
+            // as an initializer.
+            ASSERT(symbolNode->getQualifier() == EvqConst ||
+                   symbolNode->getQualifier() == EvqGlobal);
+            if (symbolNode->getQualifier() == EvqConst)
+            {
+                // All of the siblings in the same declaration need to have consistent qualifiers.
+                auto *siblings = getParentNode()->getAsAggregate()->getSequence();
+                for (TIntermNode *siblingNode : *siblings)
+                {
+                    TIntermBinary *siblingBinary = siblingNode->getAsBinaryNode();
+                    if (siblingBinary)
+                    {
+                        ASSERT(siblingBinary->getOp() == EOpInitialize);
+                        siblingBinary->getLeft()->getTypePointer()->setQualifier(EvqGlobal);
+                    }
+                    siblingNode->getAsTyped()->getTypePointer()->setQualifier(EvqGlobal);
+                }
+                // This node is one of the siblings.
+                ASSERT(symbolNode->getQualifier() == EvqGlobal);
+            }
             // Remove the initializer from the global scope and just declare the global instead.
-            mReplacements.push_back(NodeUpdateEntry(getParentNode(), node, node->getLeft(), false));
+            mReplacements.push_back(NodeUpdateEntry(getParentNode(), node, symbolNode, false));
         }
     }
     return false;
@@ -113,13 +142,15 @@
     {
         return;
     }
+    const int initFunctionId  = TSymbolTable::nextUniqueId();
     TIntermAggregate *rootAgg = root->getAsAggregate();
     ASSERT(rootAgg != nullptr && rootAgg->getOp() == EOpSequence);
 
     const char *functionName = "initializeDeferredGlobals";
 
     // Add function prototype to the beginning of the shader
-    TIntermAggregate *functionPrototypeNode = CreateFunctionPrototypeNode(functionName);
+    TIntermAggregate *functionPrototypeNode =
+        CreateFunctionPrototypeNode(functionName, initFunctionId);
     rootAgg->getSequence()->insert(rootAgg->getSequence()->begin(), functionPrototypeNode);
 
     // Add function definition to the end of the shader
@@ -130,7 +161,7 @@
         functionBody->push_back(deferredInit);
     }
     TIntermAggregate *functionDefinition =
-        CreateFunctionDefinitionNode(functionName, functionBodyNode);
+        CreateFunctionDefinitionNode(functionName, functionBodyNode, initFunctionId);
     rootAgg->getSequence()->push_back(functionDefinition);
 
     // Insert call into main function
@@ -140,7 +171,8 @@
         if (nodeAgg != nullptr && nodeAgg->getOp() == EOpFunction &&
             TFunction::unmangleName(nodeAgg->getName()) == "main")
         {
-            TIntermAggregate *functionCallNode = CreateFunctionCallNode(functionName);
+            TIntermAggregate *functionCallNode =
+                CreateFunctionCallNode(functionName, initFunctionId);
 
             TIntermNode *mainBody         = nodeAgg->getSequence()->back();
             TIntermAggregate *mainBodyAgg = mainBody->getAsAggregate();