[clang] [HLSL] Update Sema Checking Diagnostics for builtins (PR #138429)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Thu May 15 12:10:34 PDT 2025


================
@@ -2446,37 +2376,41 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
   case Builtin::BI__builtin_hlsl_any: {
     if (SemaRef.checkArgCount(TheCall, 1))
       return true;
+    if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
+      return true;
     break;
   }
   case Builtin::BI__builtin_hlsl_asdouble: {
     if (SemaRef.checkArgCount(TheCall, 2))
       return true;
-    if (CheckUnsignedIntRepresentation(&SemaRef, TheCall))
+    if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
+                            0)) // only check for uint
+      return true;
+    if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
+                            1)) // only check for uint
+      return true;
+    if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
       return true;
 
     SetElementTypeAsReturnType(&SemaRef, TheCall, getASTContext().DoubleTy);
     break;
   }
   case Builtin::BI__builtin_hlsl_elementwise_clamp: {
-    if (SemaRef.checkArgCount(TheCall, 3))
-      return true;
-    if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0) ||
-        CheckAllArgsHaveSameType(&SemaRef, TheCall))
-      return true;
     if (SemaRef.BuiltinElementwiseTernaryMath(
             TheCall, /*ArgTyRestr=*/
-            TheCall->getArg(0)->getType()->hasFloatingRepresentation()
-                ? Sema::EltwiseBuiltinArgTyRestriction::FloatTy
-                : Sema::EltwiseBuiltinArgTyRestriction::None))
+            Sema::EltwiseBuiltinArgTyRestriction::None))
       return true;
     break;
   }
   case Builtin::BI__builtin_hlsl_cross: {
     if (SemaRef.checkArgCount(TheCall, 2))
       return true;
-    if (CheckVectorElementCallArgs(&SemaRef, TheCall))
+
+    // ensure args are a half3 or float3
----------------
farzonl wrote:

This comment doesn not seem to match what `CheckAllArgTypesAreCorrect` does since there is no specific vec3 checking.  Now that I am seeing this. I don't think cross product should be handled in sema since we know it is only valid for vec3s we should probably just create a typed builtin like so

```
def HLSLCrossHalf: Builtin { 
  let Spellings = ["__builtin_hlsl_crossf16"];
  let Attributes = [NoThrow, Const];
   let Prototype = "_ExtVector<3, _Float16>(_ExtVector<3, _Float16>, _ExtVector<3, _Float16>)";
}
}

def HLSLCrossFloat: Builtin { 
  let Spellings = ["__builtin_hlsl_crossf];
  let Attributes = [NoThrow, Const];
  let Prototype = "_ExtVector<3, float>(_ExtVector<3, float>, _ExtVector<3, float>)";
}
```

or 
```
class HLSLFPMathTemplate : Template<["_Float16", "float"],  ["f16",    "f"]>;

def HLSLCross : Builtin, HLSLFPMathTemplate {
  let Spellings = ["__builtin_hlsl_cross"];
  let Attributes = [FunctionWithBuiltinPrefix, NoThrow,Const];
  let Prototype = "_ExtVector<3, T>(_ExtVector<3, T>, _ExtVector<3, T>)";
}
}
```

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


More information about the cfe-commits mailing list