Explorar el Código

Do not generate phi of pointers. (#745)

* Disable optimizations which may generate phi of pointers.
Xiang Li hace 7 años
padre
commit
8c790a8b0e

+ 3 - 0
lib/HLSL/ComputeViewIdState.cpp

@@ -627,6 +627,9 @@ void DxilViewIdState::CollectReachingDeclsRec(Value *pValue, ValueSetType &Reach
     for (Value *pPtrValue : phi->operands()) {
       CollectReachingDeclsRec(pPtrValue, ReachingDecls, Visited);
     }
+  } else if (SelectInst *SelI = dyn_cast<SelectInst>(pValue)) {
+    CollectReachingDeclsRec(SelI->getTrueValue(), ReachingDecls, Visited);
+    CollectReachingDeclsRec(SelI->getFalseValue(), ReachingDecls, Visited);
   } else if (Argument *pArg = dyn_cast<Argument>(pValue)) {
     ReachingDecls.emplace(pValue);
   } else {

+ 3 - 5
lib/Transforms/InstCombine/InstCombinePHI.cpp

@@ -283,6 +283,9 @@ static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
 }
 
 Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) {
+  // HLSL Change Begin - Do not create phi on pointer.
+  return nullptr;
+  // HLSL Change End.
   LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0));
 
   // FIXME: This is overconservative; this transform is allowed in some cases
@@ -298,11 +301,6 @@ Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) {
   bool isVolatile = FirstLI->isVolatile();
   unsigned LoadAlignment = FirstLI->getAlignment();
   unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace();
-  // HLSL Change Begin.
-  // Do not create phi on non-default address space.
-  if (LoadAddrSpace != 0)
-    return nullptr;
-  // HLSL Change End.
 
   // We can't sink the load if the loaded value could be modified between the
   // load and the PHI.

+ 3 - 4
lib/Transforms/Scalar/GVN.cpp

@@ -2486,11 +2486,10 @@ bool GVN::performScalarPRE(Instruction *CurInst) {
   if (isa<CmpInst>(CurInst))
     return false;
 
-  // HLSL Change Begin
-  // Don't do PRE on share memory. The PHI may merge two shader memory variable.
+  // HLSL Change Begin - Don't do PRE on pointer which may generate phi of
+  // pointers.
   if (PointerType *PT = dyn_cast<PointerType>(CurInst->getType())) {
-    if (PT->getAddressSpace() == hlsl::DXIL::kTGSMAddrSpace)
-      return false;
+    return false;
   }
   // HLSL Change End
 

+ 27 - 0
tools/clang/test/CodeGenHLSL/quick-test/gep_phi.hlsl

@@ -0,0 +1,27 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// Make sure no phi of pointers
+// CHECK-NOT: phi float*
+
+static float a[4] = {1,2,3,4};
+static float b[4] = {5,6,7,8};
+
+float main(float xx : X, uint y : Y) : SV_Target {
+  float c[4] = {xx, xx, xx, xx};
+  float x = 0;
+  if (xx > 2.5)
+    x = a[y];
+  else if (xx > 1.0)
+    x = b[y];
+  else
+    x = c[y];
+
+  float x2 = 0;
+  if (xx < 1)
+    x2 = b[y+1];
+  else
+    x2 = a[y-1];
+
+  return x + y;
+
+}