Browse Source

Merged PR 41: Prevent GVN from upsizing loads and bitcasting i32* to i64* breaking DXIL

Prevent GVN from upsizing loads and bitcasting i32* to i64* breaking DXIL
Tex Riddell 7 years ago
parent
commit
64dada19d7

+ 12 - 0
lib/Transforms/Scalar/GVN.cpp

@@ -955,6 +955,8 @@ static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
                                           Value *WritePtr,
                                           uint64_t WriteSizeInBits,
                                           const DataLayout &DL) {
+#if 0   // HLSL Change: Don't support bitcasting to different sizes.
+
   // If the loaded or stored value is a first class array or struct, don't try
   // to transform them.  We need to be able to bitcast to integer.
   if (LoadTy->isStructTy() || LoadTy->isArrayTy())
@@ -1022,12 +1024,15 @@ static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
   // Okay, we can do this transformation.  Return the number of bytes into the
   // store that the load is.
   return LoadOffset-StoreOffset;
+#endif  // HLSL Change: Don't support bitcasting to different sizes.
+  return -1;
 }
 
 /// This function is called when we have a
 /// memdep query of a load that ends up being a clobbering store.
 static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
                                           StoreInst *DepSI) {
+#if 0   // HLSL Change: Don't support bitcasting to different sizes.
   // Cannot handle reading from store of first-class aggregate yet.
   if (DepSI->getValueOperand()->getType()->isStructTy() ||
       DepSI->getValueOperand()->getType()->isArrayTy())
@@ -1038,6 +1043,8 @@ static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
   uint64_t StoreSize =DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
   return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
                                         StorePtr, StoreSize, DL);
+#endif  // HLSL Change: Don't support bitcasting to different sizes.
+  return -1;
 }
 
 /// This function is called when we have a
@@ -1045,6 +1052,7 @@ static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
 /// the other load can feed into the second load.
 static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
                                          LoadInst *DepLI, const DataLayout &DL){
+#if 0   // HLSL Change: Don't support bitcasting to different sizes.
   // Cannot handle reading from store of first-class aggregate yet.
   if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
     return -1;
@@ -1066,6 +1074,8 @@ static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
   if (Size == 0) return -1;
 
   return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size*8, DL);
+#endif
+  return -1;
 }
 
 
@@ -1073,6 +1083,7 @@ static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
 static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
                                             MemIntrinsic *MI,
                                             const DataLayout &DL) {
+#if 0   // HLSL Change: Don't support bitcasting to different sizes.
   // If the mem operation is a non-constant size, we can't handle it.
   ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
   if (!SizeCst) return -1;
@@ -1113,6 +1124,7 @@ static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
   Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
   if (ConstantFoldLoadFromConstPtr(Src, DL))
     return Offset;
+#endif
   return -1;
 }
 

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

@@ -0,0 +1,27 @@
+// RUN: %dxc -T lib_6_3 -Zpr %s | FileCheck %s
+
+// Make sure GVN does not do illegal bitcast for DXIL
+// CHECK-NOT: bitcast i32* {{.*}} to i64*
+
+Make sure compile was successful (function props for main)
+// void ()* @main, i32 5
+
+struct FOO
+{
+  int val0;
+  int val1;
+};
+
+void externFunc(inout FOO);
+void append(int);
+
+[numthreads(1,1,1)]
+void main()
+{
+  FOO foo;
+  foo.val0 = 0;
+  foo.val0 = 1;
+  externFunc(foo);
+  append(foo.val0);
+  append(foo.val1);
+}