Browse Source

Don't change bitcast (gep a, 0, 0) into bicast a for validation. (#164)

Xiang Li 8 years ago
parent
commit
9afb4cfcb8

+ 5 - 0
lib/Transforms/InstCombine/InstCombineCasts.cpp

@@ -1447,6 +1447,11 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
     // If casting the result of a getelementptr instruction with no offset, turn
     // If casting the result of a getelementptr instruction with no offset, turn
     // this into a cast of the original pointer!
     // this into a cast of the original pointer!
     if (GEP->hasAllZeroIndices() &&
     if (GEP->hasAllZeroIndices() &&
+        // HLSL Change Begin.
+        // Ignore this optimization for pointer bitcast to pass validation.
+        // TODO: enable this optimization when validation is updated.
+        0 &&
+        // HLSL Change End.
         // If CI is an addrspacecast and GEP changes the poiner type, merging
         // If CI is an addrspacecast and GEP changes the poiner type, merging
         // GEP into CI would undo canonicalizing addrspacecast with different
         // GEP into CI would undo canonicalizing addrspacecast with different
         // pointer types, causing infinite loops.
         // pointer types, causing infinite loops.

+ 15 - 0
tools/clang/test/CodeGenHLSL/gep_zero_idx.hlsl

@@ -0,0 +1,15 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// CHECK: bitcast
+
+uint uintConstant;
+uint i;
+float4 main() : SV_Target
+{
+    float a[2];
+
+    a[0] = asfloat(uintConstant);
+    a[1] = 0;
+
+    return a[i];
+}

+ 5 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -362,6 +362,7 @@ public:
   TEST_METHOD(CodeGenGatherCmp)
   TEST_METHOD(CodeGenGatherCmp)
   TEST_METHOD(CodeGenGatherCubeOffset)
   TEST_METHOD(CodeGenGatherCubeOffset)
   TEST_METHOD(CodeGenGatherOffset)
   TEST_METHOD(CodeGenGatherOffset)
+  TEST_METHOD(CodeGenGepZeroIdx)
   TEST_METHOD(CodeGenGloballyCoherent)
   TEST_METHOD(CodeGenGloballyCoherent)
   TEST_METHOD(CodeGenIcb1)
   TEST_METHOD(CodeGenIcb1)
   TEST_METHOD(CodeGenIf1)
   TEST_METHOD(CodeGenIf1)
@@ -2229,6 +2230,10 @@ TEST_F(CompilerTest, CodeGenGatherOffset) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\gatherOffset.hlsl");
   CodeGenTestCheck(L"..\\CodeGenHLSL\\gatherOffset.hlsl");
 }
 }
 
 
+TEST_F(CompilerTest, CodeGenGepZeroIdx) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\gep_zero_idx.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenGloballyCoherent) {
 TEST_F(CompilerTest, CodeGenGloballyCoherent) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\globallycoherent.hlsl");
   CodeGenTestCheck(L"..\\CodeGenHLSL\\globallycoherent.hlsl");
 }
 }