[clang] [HLSL] Implement the 'and' HLSL function (PR #127098)

Deric Cheung via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 14 22:15:33 PST 2025


================
@@ -2245,6 +2245,17 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
 
     break;
   }
+  case Builtin::BI__builtin_hlsl_and: {
+    if (SemaRef.checkArgCount(TheCall, 2))
+      return true;
+    if (CheckVectorElementCallArgs(&SemaRef, TheCall))
+      return true;
+    ExprResult A = TheCall->getArg(0);
----------------
Icohedron wrote:

Oh! I think I understand now. I was mistakenly thinking that `and`  (defined in `hlsl_intrinsics.h`) and `__builtin_hlsl_and` were equivalent because they "alias".

Please correct me if I get anything wrong as I try to explain in my own words:

- The `and` function is just a front for the actual implementation which is the `__builtin_hlsl_and` builtin

  - When the `and` function is used in HLSL, the compiler will, among other things, insert the instructions for implicitly casting args into the correct types (in this case, bools or bool vectors)

  - Whereas the `__builtin_hlsl_and` function has a signature of `void(...)` as defined in `Builtins.td`, so no implicit casts will be inserted if the `__builtin_hlsl_and` is used instead

  - Meanwhile, `and` has no definition, hence when `and` is used, it must be replaced by `__builtin_hlsl_and` -- as specified by the `_HLSL_BUILTIN_ALIAS(__builtin_hlsl_and)` annotation preceding each of the `and` function declarations in `hlsl_intrinsics.h`.

- You suggest to check that the vector element type is a bool in `SemaHLSL.cpp` (which defines the semantics for `__builtin_hlsl_and`) just in case someone tries to use `__builtin_hlsl_and` instead of `and` and attempts to pass non-bool (vector) arguments to it

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


More information about the cfe-commits mailing list