浏览代码

Fixed lifetime marker causing unroll to fail (#4830)

Fixed a bug where enabling lifetime markers causes unroll to fail. Running instcombine before loop unroll may cause unroll to fail, because instcombine may rewrite expressions in a way that makes ScalarEvolutions unable to evaluate. This change replaces the instcombine with simplify-inst, which serves the same purpose as the instcombine, but leaves the expression easy for ScalarEvolutions to evaluate.

Also moved the original test for instcombine to a new folder with the new test, since instcombine pass is no longer being tested for that test.

The problematic expression rewrite was as follows:
```
limit = (non_const_int_value % 2) ? 1 : 2;
```
Instcombine rewrites it to
```
limit = 2 - (non_const_int_value & 1);
```
Which stumped ScalarEvolutions.
Adam Yang 2 年之前
父节点
当前提交
5b2201db00

+ 1 - 1
lib/Transforms/IPO/PassManagerBuilder.cpp

@@ -270,7 +270,7 @@ static void addHLSLPasses(bool HLSLHighLevel, unsigned OptLevel, bool OnlyWarnOn
     // without interfering with HLSL-specific lowering.
     if (EnableLifetimeMarkers) {
       MPM.add(createSROAPass());
-      MPM.add(createInstructionCombiningPass());
+      MPM.add(createSimplifyInstPass());
       MPM.add(createJumpThreadingPass());
     }
   }

+ 0 - 0
tools/clang/test/HLSLFileCheck/passes/llvm/instcombine/del_phi_self_reference.hlsl → tools/clang/test/HLSLFileCheck/passes/llvm/simplifyinst/del_phi_self_reference.hlsl


+ 35 - 0
tools/clang/test/HLSLFileCheck/passes/llvm/simplifyinst/unroll_simplify.hlsl

@@ -0,0 +1,35 @@
+// RUN: %dxc -T ps_6_6 %s     -enable-lifetime-markers /HV 2021 | FileCheck %s
+// RUN: %dxc -T ps_6_6 %s /Od -enable-lifetime-markers /HV 2021 | FileCheck %s
+
+// CHECK: @main
+
+//
+// Regression check for loop unrolling failure. Running instcombine when
+// lifetime markers are enabled rewrote the IR in a way that ScalarEvolution
+// could no longer figure out an upper bound. The expression:
+//
+//   limit = (l % 2) ? 1 : 2;
+//
+// Got rewritten to
+//
+//   limit = 2 - (l & 1);
+//
+// This fix for this was switching from running instcombine to running
+// simplifyinst, which is less aggressive in its rewriting.
+//
+
+cbuffer cb : register(b0) {
+  float foo[2];
+};
+
+[RootSignature("CBV(b0)")]
+float main(uint l : L) : SV_Target {
+  uint limit = (l % 2) ? 1 : 2;
+  float ret = 0;
+  [unroll]
+  for (uint i = 0; i < limit; i++) {
+    ret += foo[i];
+  }
+  return ret;
+}
+