[clang] [llvm] [HLSL] Implementation of dot intrinsic (PR #81190)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 12 18:17:30 PST 2024


================
@@ -4518,6 +4518,12 @@ def HLSLCreateHandle : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void*(unsigned char)";
 }
 
+def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_dot"];
+  let Attributes = [NoThrow, Const, CustomTypeChecking];
----------------
farzonl wrote:

I have added SemaChecking 
However I have 4 cases I can't check via `SemaChecking.cpp` Because
`BuildRecoveryCallExpr` is usurping calls to `BuildResolvedCallExpr`. 
This seems to be caused by how we are defining apis via `hlsl_intrinsics.h` and it doesn't seem to matter if I added Types to the table gen or not like so
```python
class HLSLMathTemplate : Template<["__fp16", "float", "double", "short", "int", "int64_t", "unsigned short", "uint32_t", "uint64_t" ],
                                  ["f16",    "",      "f64",    "i16",   "i32", "i64",     "u16",            "u32",      "u64"]>;

def HLSLDotProduct : LangBuiltin<"HLSL_LANG">, HLSLMathTemplate {
  let Spellings = ["__builtin_hlsl_dot"];
  let Attributes = [NoThrow, Const, CustomTypeChecking];
  let Prototype = "T(_Complex T , _Complex T )";
}
```
Example test cases that hit `BuildRecoveryCallExpr`:
```hlsl
float test_dot_scalar_mismatch ( float p0, int p1 ) {
  return dot ( p0, p1 );
}

float test_dot_vector_size_mismatch ( float3 p0, float2 p1 ) {
  return dot ( p0, p1 );
}

float test__no_second_arg ( float2 p0) {
  return dot ( p0 );
}

float test_dot_element_type_mismatch ( int2 p0, float2 p1 ) {
  return dot ( p0, p1 );
}
```

For example If we take `sin`:

```hlsl
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
// RUN:   dxil-pc-shadermodel6.3-library %s -fnative-half-type \
// RUN:   -emit-llvm -disable-llvm-passes  -o -

float4 test( int4 p0) {
  return sin ( p0 );
}
```
I get the same errors:

```
clang\test\CodeGenHLSL\builtins\dot2.hlsl:6:10: error: no matching function for call to 'sin'
    6 |   return sin ( p0 );
      |          ^~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:611:6: note: candidate function not viable: no known conversion from 'int4' (aka 'vector<int, 4>') to 'half' for 1st argument
  611 | half sin(half);
      |      ^   ~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:613:7: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<half, 2>' for 1st argument
  613 | half2 sin(half2);
      |       ^   ~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:615:7: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<half, 3>' for 1st argument
  615 | half3 sin(half3);
      |       ^   ~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:617:7: note: candidate function not viable: no known conversion from 'vector<int, [...]>' to 'vector<half, [...]>' for 1st argument
  617 | half4 sin(half4);
      |       ^   ~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:621:7: note: candidate function not viable: no known conversion from 'int4' (aka 'vector<int, 4>') to 'float' for 1st argument
  621 | float sin(float);
      |       ^   ~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:623:8: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<float, 2>' for 1st argument
  623 | float2 sin(float2);
      |        ^   ~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:625:8: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<float, 3>' for 1st argument
  625 | float3 sin(float3);
      |        ^   ~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:627:8: note: candidate function not viable: no known conversion from 'vector<int, [...]>' to 'vector<float, [...]>' for 1st argument
  627 | float4 sin(float4);
      |        ^   ~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:630:8: note: candidate function not viable: no known conversion from 'int4' (aka 'vector<int, 4>') to 'double' for 1st argument
  630 | double sin(double);
      |        ^   ~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:632:9: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<double, 2>' for 1st argument
  632 | double2 sin(double2);
      |         ^   ~~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:634:9: note: candidate function not viable: no known conversion from 'vector<int, 4>' to 'vector<double, 3>' for 1st argument
  634 | double3 sin(double3);
      |         ^   ~~~~~~~
D:\projects\llvm_win_clang-cl_debug_build\lib\clang\19\include\hlsl/hlsl_intrinsics.h:636:9: note: candidate function not viable: no known conversion from 'vector<int, [...]>' to 'vector<double, [...]>' for 1st argument
  636 | double4 sin(double4);
      |         ^   ~~~~~~~
1 error generated.
```

The problem with this is that we can't promote `int` to a `float` like DXC currently does: https://godbolt.org/z/e7673or4r

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


More information about the cfe-commits mailing list