瀏覽代碼

Make sure no short circuiting. (#336)

Xiang Li 8 年之前
父節點
當前提交
1d0da57987

+ 16 - 2
tools/clang/lib/CodeGen/CGExprScalar.cpp

@@ -3378,8 +3378,15 @@ Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
     }
 
     // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
-    if (!CGF.ContainsLabel(E->getRHS()))
+    if (!CGF.ContainsLabel(E->getRHS())) {
+      // HLSL Change Begins.
+      if (CGF.getLangOpts().HLSL) {
+        // HLSL does not short circuit.
+        Visit(E->getRHS());
+      }
+      // HLSL Change Ends.
       return llvm::Constant::getNullValue(ResTy);
+    }
   }
 
   // HLSL Change Begins.
@@ -3476,8 +3483,15 @@ Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
     }
 
     // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
-    if (!CGF.ContainsLabel(E->getRHS()))
+    if (!CGF.ContainsLabel(E->getRHS())) {
+      // HLSL Change Begins.
+      if (CGF.getLangOpts().HLSL) {
+        // HLSL does not short circuit.
+        Visit(E->getRHS());
+      }
+      // HLSL Change Ends.
       return llvm::ConstantInt::get(ResTy, 1);
+    }
   }
 
   // HLSL Change Begins.

+ 6 - 0
tools/clang/lib/CodeGen/CGStmt.cpp

@@ -568,6 +568,12 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S,
     if (!ContainsLabel(Skipped)) {
       if (CondConstant)
         incrementProfileCounter(&S);
+      // HLSL Change Begin.
+      if (getLangOpts().HLSL) {
+        // Emit Cond to make sure not short circuiting.
+        EmitScalarExpr(S.getCond());
+      }
+      // HLSL Change End.
       if (Executed) {
         RunCleanupsScope ExecutedScope(*this);
         EmitStmt(Executed);

+ 14 - 0
tools/clang/test/CodeGenHLSL/short_circuiting0.hlsl

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// inc(color) will add 1 to color.
+// Make sure result is add 5.2.
+// CHECK: 0x4014CCCCC0000000
+
+bool inc(inout float4 v) { v++; return true; }
+
+float4 main(int val: A, float4 color: COLOR) : SV_TARGET {
+  if (false && inc(color)) return color;
+
+  return color + 4.2;
+}
+

+ 14 - 0
tools/clang/test/CodeGenHLSL/short_circuiting1.hlsl

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// inc(color) will add 1 to color.
+// Make sure result is add 1.
+//CHECK: 1.0
+
+bool inc(inout float4 v) { v++; return true; }
+
+float4 main(int val: A, float4 color: COLOR) : SV_TARGET {
+  if (true && inc(color)) return color;
+
+  return color + 4.2;
+}
+

+ 14 - 0
tools/clang/test/CodeGenHLSL/short_circuiting2.hlsl

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// inc(color) will add 1 to color.
+// Make sure result is add 5.2.
+// CHECK: 0x4014CCCCC0000000
+
+bool inc(inout float4 v) { v++; return false; }
+
+float4 main(int val: A, float4 color: COLOR) : SV_TARGET {
+  if (false || inc(color)) return color;
+
+  return color + 4.2;
+}
+

+ 14 - 0
tools/clang/test/CodeGenHLSL/short_circuiting3.hlsl

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// inc(color) will add 1 to color.
+// Make sure result is add 1.
+//CHECK: 1.0
+
+bool inc(inout float4 v) { v++; return true; }
+
+float4 main(int val: A, float4 color: COLOR) : SV_TARGET {
+  if (true || inc(color)) return color;
+
+  return color + 4.2;
+}
+

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

@@ -606,6 +606,10 @@ public:
   TEST_METHOD(CodeGenShare_Mem2)
   TEST_METHOD(CodeGenShare_Mem2Dim)
   TEST_METHOD(CodeGenShift)
+  TEST_METHOD(CodeGenShortCircuiting0)
+  TEST_METHOD(CodeGenShortCircuiting1)
+  TEST_METHOD(CodeGenShortCircuiting2)
+  TEST_METHOD(CodeGenShortCircuiting3)
   TEST_METHOD(CodeGenSimpleDS1)
   TEST_METHOD(CodeGenSimpleGS1)
   TEST_METHOD(CodeGenSimpleGS2)
@@ -3186,6 +3190,22 @@ TEST_F(CompilerTest, CodeGenShift) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\shift.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenShortCircuiting0) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\short_circuiting0.hlsl");
+}
+
+TEST_F(CompilerTest, CodeGenShortCircuiting1) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\short_circuiting1.hlsl");
+}
+
+TEST_F(CompilerTest, CodeGenShortCircuiting2) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\short_circuiting2.hlsl");
+}
+
+TEST_F(CompilerTest, CodeGenShortCircuiting3) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\short_circuiting3.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenSimpleDS1) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\SimpleDS1.hlsl");
 }