Преглед изворни кода

Fix matrix pre/post increment/decrement (#1987)

Tristan Labelle пре 6 година
родитељ
комит
c6cd8c8b55

+ 3 - 1
lib/HLSL/HLMatrixLowerPass.cpp

@@ -1030,7 +1030,9 @@ Value *HLMatrixLowerPass::lowerHLUnaryOperation(Value *MatVal, HLUnaryOpcode Opc
       ? ConstantFP::get(VecTy->getElementType(), 1)
       ? ConstantFP::get(VecTy->getElementType(), 1)
       : ConstantInt::get(VecTy->getElementType(), 1);
       : ConstantInt::get(VecTy->getElementType(), 1);
     Constant *VecOne = ConstantVector::getSplat(VecTy->getNumElements(), ScalarOne);
     Constant *VecOne = ConstantVector::getSplat(VecTy->getNumElements(), ScalarOne);
-    // BUGBUG: This implementation has incorrect semantics (GitHub #1780)
+
+    // CodeGen already emitted the load and following store, our job is only to produce
+    // the updated value.
     if (Opcode == HLUnaryOpcode::PostInc || Opcode == HLUnaryOpcode::PreInc) {
     if (Opcode == HLUnaryOpcode::PostInc || Opcode == HLUnaryOpcode::PreInc) {
       return IsFloat
       return IsFloat
         ? Builder.CreateFAdd(LoweredVal, VecOne)
         ? Builder.CreateFAdd(LoweredVal, VecOne)

+ 4 - 3
tools/clang/lib/CodeGen/CGExprScalar.cpp

@@ -218,10 +218,11 @@ public:
           }
           }
       }
       }
     } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) {
     } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) {
-      if (hlsl::IsHLSLMatType(E->getType())) {
-        llvm::Value *Oper = CGF.EmitScalarExpr(UnOp->getSubExpr());
+      // ++/-- operators are handled in EmitScalarPrePostIncDec
+      if (hlsl::IsHLSLMatType(E->getType()) && !UnOp->isIncrementDecrementOp()) {
+        llvm::Value *Operand = CGF.EmitScalarExpr(UnOp->getSubExpr());
         return CGF.CGM.getHLSLRuntime().EmitHLSLMatrixOperationCall(
         return CGF.CGM.getHLSLRuntime().EmitHLSLMatrixOperationCall(
-            CGF, E, Oper->getType(), {Oper});
+            CGF, E, Operand->getType(), { Operand });
       }
       }
     }
     }
     // HLSL Change Ends
     // HLSL Change Ends

+ 1 - 1
tools/clang/test/CodeGenHLSL/expressions/operators/matrices/increment_decrement.hlsl → tools/clang/test/CodeGenHLSL/expressions/operators/matrices/increment_decrement_locals.hlsl

@@ -1,4 +1,4 @@
-// RUN: %dxc /T vs_6_0 /E main > %s | FileCheck %s | XFail GitHub #1780
+// RUN: %dxc /T vs_6_0 /E main %s | FileCheck %s
 
 
 // Check that pre/post increment/decrement operators on
 // Check that pre/post increment/decrement operators on
 // matrices have the intended semantics for both the original
 // matrices have the intended semantics for both the original

+ 43 - 0
tools/clang/test/CodeGenHLSL/expressions/operators/matrices/increment_decrement_structbuf.hlsl

@@ -0,0 +1,43 @@
+// RUN: %dxc /T vs_6_2 /E main %s | FileCheck %s
+
+// Check that pre/post increment/decrement operators on
+// matrices have the intended semantics for both the original
+// variable and the returned value, for matrices living in resources.
+
+RWStructuredBuffer<int1x1> buf;
+AppendStructuredBuffer<int> results;
+
+void main()
+{
+  // Post-increment
+  // CHECK: rawBufferLoad
+  // CHECK: add i32 %{{.*}}, 1
+  // CHECK: rawBufferStore
+  // CHECK-NOT: rawBufferLoad
+  // CHECK: rawBufferStore
+  results.Append((buf[0]++)._11);
+  
+  // Post-decrement
+  // CHECK: rawBufferLoad
+  // CHECK: add i32 %{{.*}}, -1
+  // CHECK: rawBufferStore
+  // CHECK-NOT: rawBufferLoad
+  // CHECK: rawBufferStore
+  results.Append((buf[0]--)._11);
+  
+  // Pre-increment
+  // CHECK: rawBufferLoad
+  // CHECK: add i32 %{{.*}}, 1
+  // CHECK: rawBufferStore
+  // CHECK: rawBufferLoad
+  // CHECK: rawBufferStore
+  results.Append((++buf[0])._11);
+  
+  // Pre-decrement
+  // CHECK: rawBufferLoad
+  // CHECK: add i32 %{{.*}}, -1
+  // CHECK: rawBufferStore
+  // CHECK: rawBufferLoad
+  // CHECK: rawBufferStore
+  results.Append((--buf[0])._11);
+}

+ 12 - 0
tools/clang/test/CodeGenHLSL/expressions/operators/matrices/pre_increment_decrement_chain.hlsl

@@ -0,0 +1,12 @@
+// RUN: %dxc /T vs_6_0 /E main %s | FileCheck %s
+
+// Check that matrix pre-increment/decrement can be chained.
+
+int2 main() : OUT
+{
+  int1x1 variable = 10;
+  int1x1 result = --(++(++(++variable)));
+  // CHECK: call void @dx.op.storeOutput.i32(i32 5, i32 0, i32 0, i8 0, i32 12)
+  // CHECK: call void @dx.op.storeOutput.i32(i32 5, i32 0, i32 0, i8 1, i32 12)
+  return int2(variable._11, result._11);
+}