Fix unreachable-block removal.
authorDejan Mircevski <deki@google.com>
Mon, 11 Jan 2016 00:37:00 +0000 (19:37 -0500)
committerDejan Mircevski <deki@google.com>
Mon, 11 Jan 2016 00:37:00 +0000 (19:37 -0500)
Add a test for loop without a condition.

Change-Id: Idd7fc462218a84b1e745207e2975a3f2897d30a0

SPIRV/GlslangToSpv.cpp
SPIRV/SpvBuilder.cpp
SPIRV/spvIR.h
Test/baseResults/spv.for-notest.vert.out [new file with mode: 0644]
Test/spv.for-notest.vert [new file with mode: 0644]

index 769cf50..06ab094 100755 (executable)
@@ -1356,7 +1356,7 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
 
         builder.setBuildPoint(&blocks.body);
         if (node->getBody())
-            node->getBody()->traverse(this);  // continue->cont, break->exit
+            node->getBody()->traverse(this);
         builder.createBranch(&blocks.continue_target);
 
         builder.setBuildPoint(&blocks.continue_target);
@@ -1368,7 +1368,7 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
 
         builder.setBuildPoint(&blocks.body);
         if (node->getBody())
-            node->getBody()->traverse(this);  // continue->cont, break->exit
+            node->getBody()->traverse(this);
         builder.createBranch(&blocks.continue_target);
 
         builder.setBuildPoint(&blocks.continue_target);
index 57e27e0..8e023ea 100755 (executable)
@@ -852,7 +852,7 @@ void Builder::leaveFunction()
         if (unreachable) {
             // Given that this block is at the end of a function, it must be right after an
             // explicit return, just remove it.
-            function.popBlock(block);
+            function.removeBlock(block);
         } else {
             // We'll add a return instruction at the end of the current block,
             // which for a non-void function is really error recovery (?), as the source
index 6736a13..70f27f7 100755 (executable)
@@ -52,6 +52,7 @@
 
 #include "spirv.hpp"
 
+#include <algorithm>
 #include <vector>
 #include <iostream>
 #include <assert.h>
@@ -235,7 +236,13 @@ public:
     Id getParamId(int p) { return parameterInstructions[p]->getResultId(); }
 
     void addBlock(Block* block) { blocks.push_back(block); }
-    void popBlock(Block*) { blocks.pop_back(); }
+    void removeBlock(Block* block)
+    {
+        auto found = find(blocks.begin(), blocks.end(), block);
+        assert(found != blocks.end());
+        blocks.erase(found);
+        delete block;
+    }
 
     Module& getParent() const { return parent; }
     Block* getEntryBlock() const { return blocks.front(); }
diff --git a/Test/baseResults/spv.for-notest.vert.out b/Test/baseResults/spv.for-notest.vert.out
new file mode 100644 (file)
index 0000000..e55ef9b
--- /dev/null
@@ -0,0 +1,50 @@
+spv.for-notest.vert\r
+Warning, version 450 is not yet complete; most version-specific features are present, but some are missing.\r
+\r
+\r
+Linked vertex stage:\r
+\r
+\r
+// Module Version 10000\r
+// Generated by (magic number): 80001\r
+// Id's are bound by 22\r
+\r
+                              Capability Shader\r
+               1:             ExtInstImport  "GLSL.std.450"\r
+                              MemoryModel Logical GLSL450\r
+                              EntryPoint Vertex 4  "main" 14 20 21\r
+                              Source GLSL 450\r
+                              Name 4  "main"\r
+                              Name 8  "i"\r
+                              Name 14  "r"\r
+                              Name 20  "gl_VertexID"\r
+                              Name 21  "gl_InstanceID"\r
+                              Decorate 14(r) Location 0\r
+                              Decorate 20(gl_VertexID) BuiltIn VertexId\r
+                              Decorate 21(gl_InstanceID) BuiltIn InstanceId\r
+               2:             TypeVoid\r
+               3:             TypeFunction 2\r
+               6:             TypeInt 32 1\r
+               7:             TypePointer Function 6(int)\r
+               9:      6(int) Constant 0\r
+              13:             TypePointer Output 6(int)\r
+           14(r):     13(ptr) Variable Output\r
+              17:      6(int) Constant 1\r
+              19:             TypePointer Input 6(int)\r
+ 20(gl_VertexID):     19(ptr) Variable Input\r
+21(gl_InstanceID):     19(ptr) Variable Input\r
+         4(main):           2 Function None 3\r
+               5:             Label\r
+            8(i):      7(ptr) Variable Function\r
+                              Store 8(i) 9\r
+                              Branch 10\r
+              10:             Label\r
+              15:      6(int) Load 8(i)\r
+                              Store 14(r) 15\r
+                              Branch 12\r
+              12:             Label\r
+              16:      6(int) Load 8(i)\r
+              18:      6(int) IAdd 16 17\r
+                              Store 8(i) 18\r
+                              Branch 10\r
+                              FunctionEnd\r
diff --git a/Test/spv.for-notest.vert b/Test/spv.for-notest.vert
new file mode 100644 (file)
index 0000000..f40e666
--- /dev/null
@@ -0,0 +1,6 @@
+#version 450
+layout(location=0) out highp int r;
+void main() {
+  int i;
+  for (i=0; ; i++) { r = i; }
+}