Skip to content

Commit 4201517

Browse files
committed
[HLSL] Implementation of the fmod intrinsic
This change implements the frontend for #99118 Builtins.td - add the fmod builtin CGBuiltin.cpp - lower the builtin to llvm FRem instruction hlsl_intrinsics.h - add the fmod api SemaHLSL.cpp - add type checks for builtin clang/docs/LanguageExtensions.rst - add the builtin in *Elementwise Builtins* clang/docs/ReleaseNotes.rst - announce the builtin
1 parent fda0143 commit 4201517

File tree

8 files changed

+171
-1
lines changed

8 files changed

+171
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±in
700700
T __builtin_elementwise_canonicalize(T x) return the platform specific canonical encoding floating point types
701701
of a floating-point number
702702
T __builtin_elementwise_copysign(T x, T y) return the magnitude of x with the sign of y. floating point types
703+
T __builtin_elementwise_fmod(T x, T y) return The floating-point remainder of (x/y) whose sign floating point types
704+
matches the sign of x.
703705
T __builtin_elementwise_max(T x, T y) return x or y, whichever is larger integer and floating point types
704706
T __builtin_elementwise_min(T x, T y) return x or y, whichever is smaller integer and floating point types
705707
T __builtin_elementwise_add_sat(T x, T y) return the sum of x and y, clamped to the range of integer types

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ C++ Language Changes
123123

124124
- Add ``__builtin_elementwise_popcount`` builtin for integer types only.
125125

126+
- Add ``__builtin_elementwise_fmod`` builtin for floating point types only.
127+
126128
- The builtin type alias ``__builtin_common_type`` has been added to improve the
127129
performance of ``std::common_type``.
128130

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,12 @@ def HLSLStep: LangBuiltin<"HLSL_LANG"> {
47884788
let Prototype = "void(...)";
47894789
}
47904790

4791+
def HLSLFmod : LangBuiltin<"HLSL_LANG"> {
4792+
let Spellings = ["__builtin_elementwise_fmod"];
4793+
let Attributes = [NoThrow, Const];
4794+
let Prototype = "void(...)";
4795+
}
4796+
47914797
// Builtins for XRay.
47924798
def XRayCustomEvent : Builtin {
47934799
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
28442844
case Builtin::BI__builtin_fmodf:
28452845
case Builtin::BI__builtin_fmodf16:
28462846
case Builtin::BI__builtin_fmodl:
2847-
case Builtin::BI__builtin_fmodf128: {
2847+
case Builtin::BI__builtin_fmodf128:
2848+
case Builtin::BI__builtin_elementwise_fmod: {
28482849
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
28492850
Value *Arg1 = EmitScalarExpr(E->getArg(0));
28502851
Value *Arg2 = EmitScalarExpr(E->getArg(1));

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,39 @@ float3 floor(float3);
913913
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
914914
float4 floor(float4);
915915

916+
//===----------------------------------------------------------------------===//
917+
// fmod builtins
918+
//===----------------------------------------------------------------------===//
919+
920+
/// \fn T fmod(T x, T y)
921+
/// \brief Returns the linear interpolation of x to y.
922+
/// \param x [in] The dividend.
923+
/// \param y [in] The divisor.
924+
///
925+
/// Return the floating-point remainder of the x parameter divided by the y parameter.
926+
927+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
928+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
929+
half fmod(half, half);
930+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
931+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
932+
half2 fmod(half2, half2);
933+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
934+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
935+
half3 fmod(half3, half3);
936+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
937+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
938+
half4 fmod(half4, half4);
939+
940+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
941+
float fmod(float, float);
942+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
943+
float2 fmod(float2, float2);
944+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
945+
float3 fmod(float3, float3);
946+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_fmod)
947+
float4 fmod(float4, float4);
948+
916949
//===----------------------------------------------------------------------===//
917950
// frac builtins
918951
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,18 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
16451645
return true;
16461646
break;
16471647
}
1648+
case Builtin::BI__builtin_elementwise_fmod: {
1649+
if (SemaRef.checkArgCount(TheCall, 2))
1650+
return true;
1651+
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
1652+
return true;
1653+
1654+
ExprResult A = TheCall->getArg(0);
1655+
QualType ArgTyA = A.get()->getType();
1656+
// return type is the same as the input type
1657+
TheCall->setType(ArgTyA);
1658+
break;
1659+
}
16481660
case Builtin::BI__builtin_hlsl_select: {
16491661
if (SemaRef.checkArgCount(TheCall, 3))
16501662
return true;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// DirectX target:
2+
//
3+
// ---------- Native Half support test -----------
4+
//
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
7+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
8+
// RUN: -DFNATTRS=noundef -DTYPE=half
9+
10+
//
11+
// ---------- No Native Half support test -----------
12+
//
13+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
14+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
15+
// RUN: -o - | FileCheck %s \
16+
// RUN: -DFNATTRS=noundef -DTYPE=float
17+
18+
19+
// Spirv target:
20+
//
21+
// ---------- Native Half support test -----------
22+
//
23+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
24+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
25+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
26+
// RUN: -DFNATTRS="spir_func noundef" -DTYPE=half
27+
28+
//
29+
// ---------- No Native Half support test -----------
30+
//
31+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
32+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
33+
// RUN: -o - | FileCheck %s \
34+
// RUN: -DFNATTRS="spir_func noundef" -DTYPE=float
35+
36+
37+
38+
// CHECK: define [[FNATTRS]] [[TYPE]] @
39+
// CHECK: %fmod = frem [[TYPE]]
40+
// CHECK: ret [[TYPE]] %fmod
41+
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
42+
43+
// CHECK: define [[FNATTRS]] <2 x [[TYPE]]> @
44+
// CHECK: %fmod = frem <2 x [[TYPE]]>
45+
// CHECK: ret <2 x [[TYPE]]> %fmod
46+
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
47+
48+
// CHECK: define [[FNATTRS]] <3 x [[TYPE]]> @
49+
// CHECK: %fmod = frem <3 x [[TYPE]]>
50+
// CHECK: ret <3 x [[TYPE]]> %fmod
51+
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
52+
53+
// CHECK: define [[FNATTRS]] <4 x [[TYPE]]> @
54+
// CHECK: %fmod = frem <4 x [[TYPE]]>
55+
// CHECK: ret <4 x [[TYPE]]> %fmod
56+
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
57+
58+
// CHECK: define [[FNATTRS]] float @
59+
// CHECK: %fmod = frem float
60+
// CHECK: ret float %fmod
61+
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
62+
63+
// CHECK: define [[FNATTRS]] <2 x float> @
64+
// CHECK: %fmod = frem <2 x float>
65+
// CHECK: ret <2 x float> %fmod
66+
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
67+
68+
// CHECK: define [[FNATTRS]] <3 x float> @
69+
// CHECK: %fmod = frem <3 x float>
70+
// CHECK: ret <3 x float> %fmod
71+
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
72+
73+
// CHECK: define [[FNATTRS]] <4 x float> @
74+
// CHECK: %fmod = frem <4 x float>
75+
// CHECK: ret <4 x float> %fmod
76+
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
77+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
3+
4+
float test_too_few_arg() {
5+
return __builtin_elementwise_fmod();
6+
// expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
7+
}
8+
9+
float2 test_too_many_arg(float2 p0, float2 p1, float2 p3) {
10+
return __builtin_elementwise_fmod(p0, p1, p3);
11+
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
12+
}
13+
14+
float builtin_bool_to_float_type_promotion(bool p1, bool p2) {
15+
return __builtin_elementwise_fmod(p1, p2);
16+
// expected-error@-1 {{passing 'bool' to parameter of incompatible type 'float'}}
17+
}
18+
19+
float builtin_fmod_int_to_float_promotion(int p1, int p2) {
20+
return __builtin_elementwise_fmod(p1, p2);
21+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
22+
}
23+
24+
float2 builtin_fmod_int2_to_float2_promotion(int2 p1, int2 p2) {
25+
return __builtin_elementwise_fmod(p1, p2);
26+
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
27+
}
28+
29+
half builtin_fmod_double_type (double p0, double p1) {
30+
return __builtin_elementwise_fmod(p0, p1);
31+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
32+
}
33+
34+
half builtin_fmod_double2_type (double2 p0, double2 p1) {
35+
return __builtin_elementwise_fmod(p0, p1);
36+
// expected-error@-1 {{passing 'double2' (aka 'vector<double, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
37+
}

0 commit comments

Comments
 (0)