Skip to content

Commit a12d65f

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*
1 parent fda0143 commit a12d65f

File tree

7 files changed

+172
-1
lines changed

7 files changed

+172
-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/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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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: --check-prefixes=CHECK \
9+
// RUN: -DFNATTRS=noundef -DTYPE=half
10+
11+
//
12+
// ---------- No Native Half support test -----------
13+
//
14+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
15+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
16+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK \
17+
// RUN: -DFNATTRS=noundef -DTYPE=float
18+
19+
20+
// Spirv target:
21+
//
22+
// ---------- Native Half support test -----------
23+
//
24+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
25+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
26+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
27+
// RUN: --check-prefixes=CHECK \
28+
// RUN: -DFNATTRS="spir_func noundef" -DTYPE=half
29+
30+
//
31+
// ---------- No Native Half support test -----------
32+
//
33+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
34+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
35+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK \
36+
// RUN: -DFNATTRS="spir_func noundef" -DTYPE=float
37+
38+
39+
40+
// CHECK: define [[FNATTRS]] [[TYPE]] @
41+
// CHECK: %fmod = frem [[TYPE]]
42+
// CHECK: ret [[TYPE]] %fmod
43+
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
44+
45+
// CHECK: define [[FNATTRS]] <2 x [[TYPE]]> @
46+
// CHECK: %fmod = frem <2 x [[TYPE]]>
47+
// CHECK: ret <2 x [[TYPE]]> %fmod
48+
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
49+
50+
// CHECK: define [[FNATTRS]] <3 x [[TYPE]]> @
51+
// CHECK: %fmod = frem <3 x [[TYPE]]>
52+
// CHECK: ret <3 x [[TYPE]]> %fmod
53+
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
54+
55+
// CHECK: define [[FNATTRS]] <4 x [[TYPE]]> @
56+
// CHECK: %fmod = frem <4 x [[TYPE]]>
57+
// CHECK: ret <4 x [[TYPE]]> %fmod
58+
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
59+
60+
// CHECK: define [[FNATTRS]] float @
61+
// CHECK: %fmod = frem float
62+
// CHECK: ret float %fmod
63+
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
64+
65+
// CHECK: define [[FNATTRS]] <2 x float> @
66+
// CHECK: %fmod = frem <2 x float>
67+
// CHECK: ret <2 x float> %fmod
68+
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
69+
70+
// CHECK: define [[FNATTRS]] <3 x float> @
71+
// CHECK: %fmod = frem <3 x float>
72+
// CHECK: ret <3 x float> %fmod
73+
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
74+
75+
// CHECK: define [[FNATTRS]] <4 x float> @
76+
// CHECK: %fmod = frem <4 x float>
77+
// CHECK: ret <4 x float> %fmod
78+
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
79+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
// builtins are variadic functions and so are subject to DefaultVariadicArgumentPromotion
30+
half builtin_fmod_half_scalar (half p0, half p1) {
31+
return __builtin_elementwise_fmod(p0, p1);
32+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
33+
}
34+
35+
float builtin_fmod_float_scalar (float p0, float p1) {
36+
return __builtin_elementwise_fmod (p0, p1);
37+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
38+
}

0 commit comments

Comments
 (0)