Skip to content

Commit 9ed71b8

Browse files
committed
[HLSL] Implementation of the elementwise fmod builtin
This change add the elementwise fmod builtin to support HLSL function 'fmod' in clang for #99118 Builtins.td - add the fmod builtin CGBuiltin.cpp - lower the builtin to llvm FRem instruction hlsl_intrinsics.h - add the fmod api SemaChecking.cpp - add type checks for builtin SemaHLSL.cpp - add HLSL type checks for builtin clang/docs/LanguageExtensions.rst - add the builtin in *Elementwise Builtins* clang/docs/ReleaseNotes.rst - announce the builtin
1 parent f5b95db commit 9ed71b8

File tree

13 files changed

+211
-2
lines changed

13 files changed

+211
-2
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
@@ -1328,6 +1328,12 @@ def ElementwisePopcount : Builtin {
13281328
let Prototype = "void(...)";
13291329
}
13301330

1331+
def ElementwiseFmod : Builtin {
1332+
let Spellings = ["__builtin_elementwise_fmod"];
1333+
let Attributes = [NoThrow, Const, CustomTypeChecking];
1334+
let Prototype = "void(...)";
1335+
}
1336+
13311337
def ElementwisePow : Builtin {
13321338
let Spellings = ["__builtin_elementwise_pow"];
13331339
let Attributes = [NoThrow, Const, CustomTypeChecking];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
28782878
case Builtin::BI__builtin_fmodf:
28792879
case Builtin::BI__builtin_fmodf16:
28802880
case Builtin::BI__builtin_fmodl:
2881-
case Builtin::BI__builtin_fmodf128: {
2881+
case Builtin::BI__builtin_fmodf128:
2882+
case Builtin::BI__builtin_elementwise_fmod: {
28822883
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
28832884
Value *Arg1 = EmitScalarExpr(E->getArg(0));
28842885
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/SemaChecking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
27552755

27562756
// These builtins restrict the element type to floating point
27572757
// types only, and take in two arguments.
2758+
case Builtin::BI__builtin_elementwise_fmod:
27582759
case Builtin::BI__builtin_elementwise_pow: {
27592760
if (BuiltinElementwiseMath(TheCall))
27602761
return ExprError();

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
17711771
case Builtin::BI__builtin_elementwise_exp:
17721772
case Builtin::BI__builtin_elementwise_exp2:
17731773
case Builtin::BI__builtin_elementwise_floor:
1774+
case Builtin::BI__builtin_elementwise_fmod:
17741775
case Builtin::BI__builtin_elementwise_log:
17751776
case Builtin::BI__builtin_elementwise_log2:
17761777
case Builtin::BI__builtin_elementwise_log10:

clang/test/CodeGen/builtins-elementwise-math.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,26 @@ void test_builtin_elementwise_popcount(si8 vi1, si8 vi2,
607607
si = __builtin_elementwise_popcount(si);
608608
}
609609

610+
void test_builtin_elementwise_fmod(float f1, float f2, double d1, double d2,
611+
float4 vf1, float4 vf2) {
612+
613+
// CHECK-LABEL: define void @test_builtin_elementwise_fmod(
614+
// CHECK: [[F1:%.+]] = load float, ptr %f1.addr, align 4
615+
// CHECK: [[F2:%.+]] = load float, ptr %f2.addr, align 4
616+
// CHECK-NEXT: frem float [[F1]], [[F2]]
617+
f2 = __builtin_elementwise_fmod(f1, f2);
618+
619+
// CHECK: [[D1:%.+]] = load double, ptr %d1.addr, align 8
620+
// CHECK: [[D2:%.+]] = load double, ptr %d2.addr, align 8
621+
// CHECK-NEXT: frem double [[D1]], [[D2]]
622+
d2 = __builtin_elementwise_fmod(d1, d2);
623+
624+
// CHECK: [[VF1:%.+]] = load <4 x float>, ptr %vf1.addr, align 16
625+
// CHECK: [[VF2:%.+]] = load <4 x float>, ptr %vf2.addr, align 16
626+
// CHECK-NEXT: frem <4 x float> [[VF1]], [[VF2]]
627+
vf2 = __builtin_elementwise_fmod(vf1, vf2);
628+
}
629+
610630
void test_builtin_elementwise_pow(float f1, float f2, double d1, double d2,
611631
float4 vf1, float4 vf2) {
612632

clang/test/CodeGen/strictfp-elementwise-bulitins.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,14 @@ float4 strict_elementwise_fma(float4 a, float4 b, float4 c) {
306306
float4 strict_elementwise_pow(float4 a, float4 b) {
307307
return __builtin_elementwise_pow(a, b);
308308
}
309+
310+
// CHECK-LABEL: define dso_local noundef <4 x float> @_Z23strict_elementwise_fmodDv4_fS_
311+
// CHECK-SAME: (<4 x float> noundef [[A:%.*]], <4 x float> noundef [[B:%.*]]) local_unnamed_addr #[[ATTR0]] {
312+
// CHECK-NEXT: entry:
313+
// CHECK-NEXT: [[TMP0:%.*]] = tail call <4 x float> @llvm.experimental.constrained.frem.v4f32(<4 x float> [[A]], <4 x float> [[B]],
314+
// CHECK-SAME: metadata !"round.dynamic", metadata !"fpexcept.strict") #[[ATTR4]]
315+
// CHECK-NEXT: ret <4 x float> [[TMP0]]
316+
//
317+
float4 strict_elementwise_fmod(float4 a, float4 b) {
318+
return __builtin_elementwise_fmod(a, b);
319+
}
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+

clang/test/Sema/builtins-elementwise-math.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,32 @@ void test_builtin_elementwise_popcount(int i, float f, double d, float4 v, int3
538538
// expected-error@-1 {{assigning to 'int3' (vector of 3 'int' values) from incompatible type 'unsigned3' (vector of 3 'unsigned int' values)}}
539539
}
540540

541+
void test_builtin_elementwise_fmod(int i, short s, double d, float4 v, int3 iv, unsigned3 uv, int *p) {
542+
i = __builtin_elementwise_fmod(p, d);
543+
// expected-error@-1 {{arguments are of different types ('int *' vs 'double')}}
544+
545+
struct Foo foo = __builtin_elementwise_fmod(i, i);
546+
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
547+
548+
i = __builtin_elementwise_fmod(i);
549+
// expected-error@-1 {{too few arguments to function call, expected 2, have 1}}
550+
551+
i = __builtin_elementwise_fmod();
552+
// expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
553+
554+
i = __builtin_elementwise_fmod(i, i, i);
555+
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
556+
557+
i = __builtin_elementwise_fmod(v, iv);
558+
// expected-error@-1 {{arguments are of different types ('float4' (vector of 4 'float' values) vs 'int3' (vector of 3 'int' values))}}
559+
560+
i = __builtin_elementwise_fmod(uv, iv);
561+
// expected-error@-1 {{arguments are of different types ('unsigned3' (vector of 3 'unsigned int' values) vs 'int3' (vector of 3 'int' values))}}
562+
563+
i = __builtin_elementwise_fmod(d, v);
564+
// expected-error@-1 {{arguments are of different types ('double' vs 'float4' (vector of 4 'float' values))}}
565+
}
566+
541567
void test_builtin_elementwise_pow(int i, short s, double d, float4 v, int3 iv, unsigned3 uv, int *p) {
542568
i = __builtin_elementwise_pow(p, d);
543569
// expected-error@-1 {{arguments are of different types ('int *' vs 'double')}}
@@ -562,7 +588,6 @@ void test_builtin_elementwise_pow(int i, short s, double d, float4 v, int3 iv, u
562588

563589
}
564590

565-
566591
void test_builtin_elementwise_roundeven(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
567592

568593
struct Foo s = __builtin_elementwise_roundeven(f);

clang/test/SemaCXX/builtins-elementwise-math.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ void test_builtin_elementwise_fma() {
255255
static_assert(!is_const<decltype(__builtin_elementwise_fma(c, c, c))>::value);
256256
}
257257

258+
void test_builtin_elementwise_fmod() {
259+
const double a = 2;
260+
double b = 1;
261+
static_assert(!is_const<decltype(__builtin_elementwise_fmod(a, b))>::value);
262+
static_assert(!is_const<decltype(__builtin_elementwise_fmod(b, a))>::value);
263+
static_assert(!is_const<decltype(__builtin_elementwise_fmod(a, a))>::value);
264+
}
265+
258266
void test_builtin_elementwise_pow() {
259267
const double a = 2;
260268
double b = 1;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 builtin_bool_to_float_type_promotion(bool p1, bool p2) {
5+
return __builtin_elementwise_fmod(p1, p2);
6+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
7+
}
8+
9+
float2 builtin_fmod_int2_to_float2_promotion(int2 p1, int2 p2) {
10+
return __builtin_elementwise_fmod(p1, p2);
11+
// expected-error@-1 {{1st argument must be a floating point type (was 'int2' (aka 'vector<int, 2>'))}}
12+
}
13+
14+
half builtin_fmod_double_type (double p0, double p1) {
15+
return __builtin_elementwise_fmod(p0, p1);
16+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
17+
}
18+
19+
half builtin_fmod_double2_type (double2 p0, double2 p1) {
20+
return __builtin_elementwise_fmod(p0, p1);
21+
// 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)}}
22+
}

0 commit comments

Comments
 (0)