Fixes #1404. Don't DCE workgroup size
authorAlan Baker <alanbaker@google.com>
Tue, 13 Mar 2018 17:48:48 +0000 (13:48 -0400)
committerLei Zhang <antiagainst@google.com>
Tue, 13 Mar 2018 23:38:31 +0000 (19:38 -0400)
Added test.

source/opt/aggressive_dead_code_elim_pass.cpp
source/opt/aggressive_dead_code_elim_pass.h
test/opt/aggressive_dead_code_elim_test.cpp

index 9f27977..cc075cc 100644 (file)
@@ -478,12 +478,23 @@ void AggressiveDCEPass::Initialize(ir::IRContext* c) {
 }
 
 void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
+  // Keep all execution modes.
   for (auto& exec : get_module()->execution_modes()) {
     AddToWorklist(&exec);
   }
+  // Keep all entry points.
   for (auto& entry : get_module()->entry_points()) {
     AddToWorklist(&entry);
   }
+  // Keep workgroup size.
+  for (auto& anno : get_module()->annotations()) {
+    if (anno.opcode() == SpvOpDecorate) {
+      if (anno.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn &&
+          anno.GetSingleWordInOperand(2u) == SpvBuiltInWorkgroupSize) {
+        AddToWorklist(&anno);
+      }
+    }
+  }
 }
 
 Pass::Status AggressiveDCEPass::ProcessImpl() {
index 86b784a..9c1749b 100644 (file)
@@ -66,8 +66,8 @@ class AggressiveDCEPass : public MemPass {
   // Returns true if |inst| is dead.
   bool IsDead(ir::Instruction* inst);
 
-  // Adds entry points and execution modes to the worklist for processing with
-  // the first function.
+  // Adds entry points, execution modes and workgroup size decorations to the
+  // worklist for processing with the first function.
   void InitializeModuleScopeLiveInstructions();
 
   // Add |inst| to worklist_ and live_insts_.
index 61f0593..efed7ae 100644 (file)
@@ -5176,6 +5176,31 @@ OpFunctionEnd
 
   SinglePassRunAndMatch<opt::AggressiveDCEPass>(text, true);
 }
+
+// Test for #1404
+TEST_F(AggressiveDCETest, DontRemoveWorkgroupSize) {
+  const std::string text = R"(
+; CHECK: OpDecorate [[wgs:%\w+]] BuiltIn WorkgroupSize
+; CHECK: [[wgs]] = OpSpecConstantComposite
+OpCapability Shader
+OpMemoryModel Logical GLSL450
+OpEntryPoint GLCompute %func "func"
+OpExecutionMode %func LocalSize 1 1 1
+OpDecorate %1 BuiltIn WorkgroupSize
+%void = OpTypeVoid
+%int = OpTypeInt 32 0
+%functy = OpTypeFunction %void
+%v3int = OpTypeVector %int 3
+%2 = OpSpecConstant %int 1
+%1 = OpSpecConstantComposite %v3int %2 %2 %2
+%func = OpFunction %void None %functy
+%3 = OpLabel
+OpReturn
+OpFunctionEnd
+)";
+
+  SinglePassRunAndMatch<opt::AggressiveDCEPass>(text, true);
+}
 #endif  // SPIRV_EFFCEE
 
 // Test for #1214