Browse Source

Prevent FoldCmpLoadFromIndexedGlobal from introducing i64 use (#2787)

In future, we need to be more intentional for whether i64 use is desired, and produce i64 pattern if so.
Tex Riddell 5 years ago
parent
commit
bd88e666ad

+ 22 - 2
lib/Transforms/InstCombine/InstCombineCompares.cpp

@@ -482,8 +482,14 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
     // - Default to i32
     if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
       Ty = Idx->getType();
-    else
-      Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
+    // HLSL Change Begins: Don't introduce use of i64 here.
+    //               TODO: Find a way to do this safely.
+    //else
+    //  Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
+    // Use i32 if index type was i16 and too small, for instance
+    else if (ArrayElementCount <= 32)
+      Ty = Builder->getInt32Ty();
+    // HLSL Change Ends
 
     if (Ty) {
       Value *V = Builder->CreateIntCast(Idx, Ty, false);
@@ -491,6 +497,20 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
       V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
       return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
     }
+    // HLSL Change Begins: Generate 32-bit pattern for 64-bit case for now.
+    else if (ArrayElementCount <= 64) {
+      Ty = Builder->getInt32Ty();
+      Value *V = Builder->CreateIntCast(Idx, Ty, false);
+      Value *Cmp = Builder->CreateICmpULT(V, ConstantInt::get(Ty, 32));
+      Value *Sel = Builder->CreateSelect(Cmp,
+        ConstantInt::get(Ty, MagicBitvector & 0xFFFFFFFF),
+        ConstantInt::get(Ty, (MagicBitvector >> 32) & 0xFFFFFFFF));
+      Value *Shift = Builder->CreateAnd(V, ConstantInt::get(Ty, 0x1F));
+      V = Builder->CreateShl(ConstantInt::get(Ty, 0x1), Shift);
+      V = Builder->CreateAnd(Sel, V);
+      return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
+    }
+    // HLSL Change Ends
   }
 
   return nullptr;

+ 57 - 0
tools/clang/test/HLSLFileCheck/passes/llvm/instcombine/check_inst_combine_no_i64.hlsl

@@ -0,0 +1,57 @@
+// RUN: %dxc -E main -T ps_6_0 %s  | FileCheck %s
+
+// Make sure instcombine doesn't introduce i64 from FoldCmpLoadFromIndexedGlobal.
+
+// CHECK: define void @main()
+// CHECK-NOT: i64
+
+static const float A[33] = {
+  -1.0,
+  -1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+   1.0,
+  -1.0,
+  -1.0,
+  -1.0,
+};
+
+
+[RootSignature("DescriptorTable(SRV(t0),SRV(t1, numDescriptors=9))")]
+uint main(uint i : A) : SV_Target
+{
+  float f = A[i];
+  if (f < 0.0)
+  {
+    return 17;
+  }
+  else
+  {
+    return 23;
+  }
+}