瀏覽代碼

Use LHS type as result type for compound assignment. (#746)

Xiang Li 8 年之前
父節點
當前提交
b304e729a2
共有 2 個文件被更改,包括 25 次插入6 次删除
  1. 11 6
      tools/clang/lib/Sema/SemaHLSL.cpp
  2. 14 0
      tools/clang/test/CodeGenHLSL/quick-test/assignCast.hlsl

+ 11 - 6
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -7697,6 +7697,7 @@ void HLSLExternalSource::CheckBinOpForHLSL(
   case BO_ShlAssign:
   case BO_ShrAssign:
   case BO_SubAssign:
+  case BO_OrAssign:
   case BO_XorAssign: {
     extern bool CheckForModifiableLvalue(Expr * E, SourceLocation Loc,
                                          Sema & S);
@@ -7834,12 +7835,16 @@ void HLSLExternalSource::CheckBinOpForHLSL(
     // element kind may be taken from one side and the dimensions from the
     // other.
 
-    // Legal dimension combinations are identical, splat, and truncation.
-    // ResultTy will be set to whichever type can be converted to, if legal,
-    // with preference for leftType if both are possible.
-    if (FAILED(CombineDimensions(leftType, rightType, &ResultTy))) {
-      m_sema->Diag(OpLoc, diag::err_hlsl_type_mismatch);
-      return;
+    if (!isCompoundAssignment) {
+      // Legal dimension combinations are identical, splat, and truncation.
+      // ResultTy will be set to whichever type can be converted to, if legal,
+      // with preference for leftType if both are possible.
+      if (FAILED(CombineDimensions(leftType, rightType, &ResultTy))) {
+        m_sema->Diag(OpLoc, diag::err_hlsl_type_mismatch);
+        return;
+      }
+    } else {
+      ResultTy = LHS.get()->getType();
     }
 
     // Here, element kind is combined with dimensions for computation type.

+ 14 - 0
tools/clang/test/CodeGenHLSL/quick-test/assignCast.hlsl

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// Make sure implict cast on compound assign works.
+// CHECK: fptoui float {{.*}} to i32
+// CHECK: uitofp i32 {{.*}} to float
+// CHECK: fadd
+// CHECK: fptoui float {{.*}} to i32
+// CHECK: uitofp i32 {{.*}} to float
+
+float main(float4 a : A, float b : B) : SV_Target {
+  uint c = b;
+  c += a;
+  return c;
+}