Prechádzať zdrojové kódy

Ignore vector splat when use it as scalar. (#1239)

Xiang Li 7 rokov pred
rodič
commit
81e1d3ae8e

+ 4 - 0
lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp

@@ -3811,6 +3811,9 @@ static void ReplaceConstantWithInst(Constant *C, Value *V, IRBuilder<> &Builder)
     if (Instruction *I = dyn_cast<Instruction>(U)) {
       I->replaceUsesOfWith(C, V);
     } else {
+      // Skip unused ConstantExpr.
+      if (U->user_empty())
+        continue;
       ConstantExpr *CE = cast<ConstantExpr>(U);
       Instruction *Inst = CE->getAsInstruction();
       Builder.Insert(Inst);
@@ -3818,6 +3821,7 @@ static void ReplaceConstantWithInst(Constant *C, Value *V, IRBuilder<> &Builder)
       ReplaceConstantWithInst(CE, Inst, Builder);
     }
   }
+  C->removeDeadConstantUsers();
 }
 
 static void ReplaceUnboundedArrayUses(Value *V, Value *Src, IRBuilder<> &Builder) {

+ 10 - 1
tools/clang/lib/CodeGen/CGExpr.cpp

@@ -2901,7 +2901,16 @@ CodeGenFunction::EmitHLSLVectorElementExpr(const HLSLVectorElementExpr *E) {
     // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
     // emit the base as an lvalue.
     const Expr *base = E->getBase();
-
+    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(base)) {
+      if (ICE->getCastKind() == CastKind::CK_HLSLVectorSplat &&
+          E->getNumElements() == 1) {
+        // For pattern like:
+        //   static bool t;
+        //   t.x = bool(a);
+        // Just ignore the .x, treat it like t = bool(a);
+        return EmitLValue(ICE->getSubExpr());
+      }
+    }
     assert(hlsl::IsHLSLVecType(base->getType()));
     Base = EmitLValue(base);
   } else {

+ 12 - 0
tools/clang/test/CodeGenHLSL/quick-test/bool_cast.hlsl

@@ -0,0 +1,12 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+
+// Make sure it compiles
+// CHECK: uitofp i1
+
+static bool t;
+float main( float a:A) : SV_Target
+{
+    t.x = bool(a);
+    return t;
+}