llvm / llvm/llvm-project

[HLSL][LongVec] Add support for the fmod intrinsic

Open
#220,649 0 comments 0 reactions 0 assignees View on GitHub
HLSL
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

We need to add `let VaryingLongVector = 1;` to `def hlsl_fmod : HLSLBuiltin<"fmod"> {` in clang/include/clang/Basic/HLSLIntrinsics.td. fmod is only `__builtin_elementwise_fmod` for DirectX and so uses a helper `fmod_impl` in clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h to cover the non DirectX cases.

While we are here we should fix up this unfortunate structure. one get rid of the scalar vs vector differences. Second looking at the ticket at added these preprocessor changes we should only be doing this codegen for SPIR-V. It is probably better if as many targets post DirectX uses the clang. builtin as possible.

```cpp
template constexpr T fmod_impl(T X, T Y) {
#if !defined(__DIRECTX__)
return __builtin_elementwise_fmod(X, Y);
#else
T div = X / Y;
bool ge = div >= 0;
T frc = frac(abs(div));
return select(ge, frc, -frc) * Y;
#endif
}

template
constexpr vector fmod_vec_impl(vector X, vector Y) {
#if !defined(__DIRECTX__)
return __builtin_elementwise_fmod(X, Y);
#else
vector div = X / Y;
vector ge = div >= 0;
vector frc = frac(abs(div));
return select(ge, frc, -frc) * Y;
#endif
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the hlsl_fmod definition in clang/include/clang/Basic/HLSLIntrinsics.td and the fmod_impl and fmod_vec_impl helpers in clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h. Trace how long vectors and non-DirectX targets currently select the implementation. Done means fmod supports long vectors and the scalar/vector helper structure and target-specific code generation match the issue’s requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.