[clang] [llvm] [HLSL][SPIRV] Added clamp intrinsic (PR #113394)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 23 16:58:29 PDT 2024


================
@@ -18661,14 +18661,30 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
     Value *OpMax = EmitScalarExpr(E->getArg(2));
 
     QualType Ty = E->getArg(0)->getType();
-    bool IsUnsigned = false;
     if (auto *VecTy = Ty->getAs<VectorType>())
       Ty = VecTy->getElementType();
-    IsUnsigned = Ty->isUnsignedIntegerType();
-    return Builder.CreateIntrinsic(
-        /*ReturnType=*/OpX->getType(),
-        IsUnsigned ? Intrinsic::dx_uclamp : Intrinsic::dx_clamp,
-        ArrayRef<Value *>{OpX, OpMin, OpMax}, nullptr, "dx.clamp");
+    bool IsUnsigned = Ty->isUnsignedIntegerType();
+    switch (CGM.getTarget().getTriple().getArch()) {
+    case llvm::Triple::dxil: {
+      return Builder.CreateIntrinsic(
+          /*ReturnType=*/OpX->getType(),
+          IsUnsigned ? Intrinsic::dx_uclamp : Intrinsic::dx_clamp,
+          ArrayRef<Value *>{OpX, OpMin, OpMax}, nullptr, "dx.clamp");
+    } break;
+    case llvm::Triple::spirv: {
+      Intrinsic::ID Intr = Intrinsic::spv_sclamp;
+      if (Ty->isFloatingType()) {
+        Intr = Intrinsic::spv_fclamp;
----------------
farzonl wrote:

I don't beleive fclamp is correct for HLSL. I beleive HLSL uses NClamp since max and min are NMin and NMax.  
We had to fix a bug like this in the past.
https://github.com/llvm/llvm-project/issues/87072

https://github.com/llvm/llvm-project/pull/113394


More information about the cfe-commits mailing list