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

Use lt instead of le for clip. (#103)

Xiang Li пре 8 година
родитељ
комит
4cf8d75cee

+ 3 - 3
lib/HLSL/HLOperationLower.cpp

@@ -992,14 +992,14 @@ Value *TranslateClip(CallInst *CI, IntrinsicOp IOP, OP::OpCode opcode,
   Value *arg = CI->getArgOperand(HLOperandIndex::kUnaryOpSrc0Idx);
   if (VectorType *VT = dyn_cast<VectorType>(arg->getType())) {
     Value *elt = Builder.CreateExtractElement(arg, (uint64_t)0);
-    cond = Builder.CreateFCmpOLE(elt, hlslOP->GetFloatConst(0));
+    cond = Builder.CreateFCmpOLT(elt, hlslOP->GetFloatConst(0));
     for (unsigned i = 1; i < VT->getNumElements(); i++) {
       Value *elt = Builder.CreateExtractElement(arg, i);
-      Value *eltCond = Builder.CreateFCmpOLE(elt, hlslOP->GetFloatConst(0));
+      Value *eltCond = Builder.CreateFCmpOLT(elt, hlslOP->GetFloatConst(0));
       cond = Builder.CreateOr(cond, eltCond);
     }
   } else
-    cond = Builder.CreateFCmpOLE(arg, hlslOP->GetFloatConst(0));
+    cond = Builder.CreateFCmpOLT(arg, hlslOP->GetFloatConst(0));
 
   Constant *opArg = hlslOP->GetU32Const((unsigned)OP::OpCode::Discard);
   Builder.CreateCall(discard, {opArg, cond});

+ 2 - 2
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -4501,8 +4501,8 @@ Value *CGMSHLSLRuntime::EmitHLSLLiteralCast(CodeGenFunction &CGF, Value *Src,
           Value *Sel = Builder.CreateSelect(Cond, T, F, "cond");
           return Sel;
         } else if (DstTy->isFloatingPointTy()) {
-          T = ConstantFP::get(DstTy, lhs.getLimitedValue());
-          F = ConstantFP::get(DstTy, rhs.getLimitedValue());
+          T = ConstantFP::get(DstTy, int64_t(lhs.getLimitedValue()));
+          F = ConstantFP::get(DstTy, int64_t(rhs.getLimitedValue()));
           Value *Sel = Builder.CreateSelect(Cond, T, F, "cond");
           return Sel;
         }

+ 5 - 0
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -4345,6 +4345,11 @@ static ArBasicKind LiteralToConcrete(Expr *litExpr) {
   } else if (ParenExpr *PE = dyn_cast<ParenExpr>(litExpr)) {
     ArBasicKind kind = LiteralToConcrete(PE->getSubExpr());
     return kind;
+  } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(litExpr)) {
+    ArBasicKind kind = LiteralToConcrete(CO->getLHS());
+    ArBasicKind kind1 = LiteralToConcrete(CO->getRHS());
+    CombineBasicTypes(kind, kind1, &kind);
+    return kind;
   } else {
     // Could only be function call.
     CallExpr *CE = cast<CallExpr>(litExpr);

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

@@ -0,0 +1,14 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// CHECK: slt
+// CHECK: discard
+// CHECK: olt
+// CHECK: discard
+
+float main(float a : A, int b : B, float r : R) : SV_Target
+{
+  clip(b < 0? -1 : 1);
+
+  clip(a);
+  return r;
+}

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

@@ -327,6 +327,7 @@ public:
   TEST_METHOD(CodeGenCbufferAlloc)
   TEST_METHOD(CodeGenCbufferAllocLegacy)
   TEST_METHOD(CodeGenCbufferInLoop)
+  TEST_METHOD(CodeGenClip)
   TEST_METHOD(CodeGenClipPlanes)
   TEST_METHOD(CodeGenConstoperand1)
   TEST_METHOD(CodeGenDiscard)
@@ -1982,6 +1983,10 @@ TEST_F(CompilerTest, CodeGenCbufferInLoop) {
   CodeGenTest(L"..\\CodeGenHLSL\\cbufferInLoop.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenClip) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\clip.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenClipPlanes) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\clip_planes.hlsl");
 }