Browse Source

Disabled FoldCondBranchOnPHI in simplify cfg (#3180)

Adam Yang 4 years ago
parent
commit
e0d1f5d123

+ 6 - 0
lib/Transforms/Utils/SimplifyCFG.cpp

@@ -1672,6 +1672,7 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
   return true;
   return true;
 }
 }
 
 
+#if 0 // HLSL Change - Unused function
 /// \returns True if this block contains a CallInst with the NoDuplicate
 /// \returns True if this block contains a CallInst with the NoDuplicate
 /// attribute.
 /// attribute.
 static bool HasNoDuplicateCall(const BasicBlock *BB) {
 static bool HasNoDuplicateCall(const BasicBlock *BB) {
@@ -1684,6 +1685,7 @@ static bool HasNoDuplicateCall(const BasicBlock *BB) {
   }
   }
   return false;
   return false;
 }
 }
+#endif
 
 
 /// Return true if we can thread a branch across this block.
 /// Return true if we can thread a branch across this block.
 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
@@ -1709,6 +1711,7 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
   return true;
   return true;
 }
 }
 
 
+#if 0 // HLSL Change - Unused function
 /// If we have a conditional branch on a PHI node value that is defined in the
 /// If we have a conditional branch on a PHI node value that is defined in the
 /// same block as the branch and if any PHI entries are constants, thread edges
 /// same block as the branch and if any PHI entries are constants, thread edges
 /// corresponding to that entry to be branches to their ultimate destination.
 /// corresponding to that entry to be branches to their ultimate destination.
@@ -1807,6 +1810,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL) {
 
 
   return false;
   return false;
 }
 }
+#endif
 
 
 /// Given a BB that starts with the specified two-entry PHI node,
 /// Given a BB that starts with the specified two-entry PHI node,
 /// see if we can eliminate it.
 /// see if we can eliminate it.
@@ -4573,12 +4577,14 @@ bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
         return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
         return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
   }
   }
 
 
+#if 0 // HLSL Change - this transformation creates unstructured flow
   // If this is a branch on a phi node in the current block, thread control
   // If this is a branch on a phi node in the current block, thread control
   // through this block if any PHI node entries are constants.
   // through this block if any PHI node entries are constants.
   if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
   if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
     if (PN->getParent() == BI->getParent())
     if (PN->getParent() == BI->getParent())
       if (FoldCondBranchOnPHI(BI, DL))
       if (FoldCondBranchOnPHI(BI, DL))
         return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
         return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
+#endif // HLSL Change
 
 
   // Scan predecessor blocks for conditional branches.
   // Scan predecessor blocks for conditional branches.
   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)

+ 55 - 0
tools/clang/test/HLSLFileCheck/passes/llvm/simplifycfg/fold-cond-branch-on-phi.hlsl

@@ -0,0 +1,55 @@
+// RUN: %dxc /Zi -E main -T ps_6_0 %s  | FileCheck %s
+
+// the FoldCondBranchOnPhi transformation in simplify cfg
+// often creates unstructured flow. This test makes sure
+// that transformation doesn't happen.
+
+// CHECK: %[[cond:.+]] = phi i1
+// CHECK-SAME: [ false
+// CHECK: br i1 %[[cond]]
+
+cbuffer cb : register(b0) {
+  uint a,b,c,d,e,f,g,h,i,j,k,l,m,n;
+  float nums[10];
+};
+
+bool foo() {
+  [branch]
+  if (a) {
+    return false;
+  }
+
+  [branch]
+  if (b & c) {
+
+    [branch]
+    if (g && h) {
+      return true;
+    }
+
+    [branch]
+    if (e && f) {
+      return true;
+    }
+    return false;
+  }
+
+  return true;
+}
+
+[RootSignature("CBV(b0)")]
+float main(uint ia : IA) : SV_Target {
+  float ret = 0;
+  if (foo()) {
+    int i = 0;
+    [loop]
+    do {
+      ret += sin(nums[i]);
+      i++;
+    }
+    while(i < ia);
+  }
+  return ret;
+}
+
+