Skip to content

Commit 565ec72

Browse files
committed
[HLSL] Implementation of the fmod intrinsic
This change implements the frontend for #99118 Builtins.td - add the fmod builtin CGBuiltin.cpp - add the builtin to DirectX intrinsic mapping hlsl_intrinsics.h - add the fmod api SemaHLSL.cpp - add type checks for builtin IntrinsicsDirectX.td - add the fmod DirectX intrinsic
1 parent ff1de24 commit 565ec72

File tree

7 files changed

+231
-0
lines changed

7 files changed

+231
-0
lines changed

clang/include/clang/Basic/Builtins.td

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

4785+
def HLSLFmod : LangBuiltin<"HLSL_LANG"> {
4786+
let Spellings = ["__builtin_hlsl_elementwise_fmod"];
4787+
let Attributes = [NoThrow, Const];
4788+
let Prototype = "void(...)";
4789+
}
4790+
47854791
// Builtins for XRay.
47864792
def XRayCustomEvent : Builtin {
47874793
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18709,6 +18709,25 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1870918709
CGM.getHLSLRuntime().getNormalizeIntrinsic(), ArrayRef<Value *>{X},
1871018710
nullptr, "hlsl.normalize");
1871118711
}
18712+
case Builtin::BI__builtin_hlsl_elementwise_fmod: {
18713+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18714+
Value *Op1 = EmitScalarExpr(E->getArg(1));
18715+
if (!E->getArg(0)->getType()->hasFloatingRepresentation() ||
18716+
(E->getArg(0)->getType() != E->getArg(1)->getType()))
18717+
llvm_unreachable("fmod operands must have the same float representation");
18718+
18719+
llvm::Triple::ArchType Arch = CGM.getTarget().getTriple().getArch();
18720+
assert(((Arch == llvm::Triple::dxil) || (Arch == llvm::Triple::spirv)) &&
18721+
"Unknown target architecture");
18722+
18723+
if (CGM.getTarget().getTriple().getArch() == llvm::Triple::dxil)
18724+
return Builder.CreateIntrinsic(
18725+
/*ReturnType=*/Op0->getType(), Intrinsic::dx_fmod,
18726+
ArrayRef<Value *>{Op0, Op1}, nullptr, "dx.fmod");
18727+
18728+
// HLSL's Fmod builtin is equivalent to SPIRV's OpFRem instruction
18729+
return Builder.CreateFRem(Op0, Op1, "fmod");
18730+
}
1871218731
case Builtin::BI__builtin_hlsl_elementwise_frac: {
1871318732
Value *Op0 = EmitScalarExpr(E->getArg(0));
1871418733
if (!E->getArg(0)->getType()->hasFloatingRepresentation())

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,39 @@ float3 floor(float3);
807807
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
808808
float4 floor(float4);
809809

810+
//===----------------------------------------------------------------------===//
811+
// fmod builtins
812+
//===----------------------------------------------------------------------===//
813+
814+
/// \fn T fmod(T x, T y)
815+
/// \brief Returns the linear interpolation of x to y.
816+
/// \param x [in] The dividend.
817+
/// \param y [in] The divisor.
818+
///
819+
/// Return the floating-point remainder of the x parameter divided by the y parameter.
820+
821+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
822+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
823+
half fmod(half, half);
824+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
825+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
826+
half2 fmod(half2, half2);
827+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
828+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
829+
half3 fmod(half3, half3);
830+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
831+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
832+
half4 fmod(half4, half4);
833+
834+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
835+
float fmod(float, float);
836+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
837+
float2 fmod(float2, float2);
838+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
839+
float3 fmod(float3, float3);
840+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
841+
float4 fmod(float4, float4);
842+
810843
//===----------------------------------------------------------------------===//
811844
// frac builtins
812845
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,18 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
16491649
return true;
16501650
break;
16511651
}
1652+
case Builtin::BI__builtin_hlsl_elementwise_fmod: {
1653+
if (SemaRef.checkArgCount(TheCall, 2))
1654+
return true;
1655+
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
1656+
return true;
1657+
1658+
ExprResult A = TheCall->getArg(0);
1659+
QualType ArgTyA = A.get()->getType();
1660+
// return type is the same as the input type
1661+
TheCall->setType(ArgTyA);
1662+
break;
1663+
}
16521664
case Builtin::BI__builtin_hlsl_select: {
16531665
if (SemaRef.checkArgCount(TheCall, 3))
16541666
return true;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// DirectX target:
2+
//
3+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
4+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
5+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
6+
// RUN: --check-prefixes=DX-CHECK,DX-NATIVE_HALF
7+
8+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
9+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
10+
// RUN: -o - | FileCheck %s --check-prefixes=DX-CHECK,DX-NO_HALF
11+
12+
13+
14+
// Spirv target:
15+
//
16+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
17+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
18+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
19+
// RUN: --check-prefixes=SPV-CHECK,SPV-NATIVE_HALF
20+
21+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
22+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
23+
// RUN: -o - | FileCheck %s --check-prefixes=SPV-CHECK,SPV-NO_HALF
24+
25+
26+
27+
// DX-NATIVE_HALF: define noundef half @
28+
// DX-NATIVE_HALF: %dx.fmod = call half @llvm.dx.fmod.f16(
29+
// DX-NATIVE_HALF: ret half %dx.fmod
30+
// DX-NO_HALF: define noundef float @
31+
// DX-NO_HALF: %dx.fmod = call float @llvm.dx.fmod.f32(
32+
// DX-NO_HALF: ret float %dx.fmod
33+
//
34+
// SPV-NATIVE_HALF: define spir_func noundef half @
35+
// SPV-NATIVE_HALF: %fmod = frem half
36+
// SPV-NATIVE_HALF: ret half %fmod
37+
// SPV-NO_HALF: define spir_func noundef float @
38+
// SPV-NO_HALF: %fmod = frem float
39+
// SPV-NO_HALF: ret float %fmod
40+
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
41+
42+
// DX-NATIVE_HALF: define noundef <2 x half> @
43+
// DX-NATIVE_HALF: %dx.fmod = call <2 x half> @llvm.dx.fmod.v2f16
44+
// DX-NATIVE_HALF: ret <2 x half> %dx.fmod
45+
// DX-NO_HALF: define noundef <2 x float> @
46+
// DX-NO_HALF: %dx.fmod = call <2 x float> @llvm.dx.fmod.v2f32(
47+
// DX-NO_HALF: ret <2 x float> %dx.fmod
48+
//
49+
// SPV-NATIVE_HALF: define spir_func noundef <2 x half> @
50+
// SPV-NATIVE_HALF: %fmod = frem <2 x half>
51+
// SPV-NATIVE_HALF: ret <2 x half> %fmod
52+
// SPV-NO_HALF: define spir_func noundef <2 x float> @
53+
// SPV-NO_HALF: %fmod = frem <2 x float>
54+
// SPV-NO_HALF: ret <2 x float> %fmod
55+
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
56+
57+
// DX-NATIVE_HALF: define noundef <3 x half> @
58+
// DX-NATIVE_HALF: %dx.fmod = call <3 x half> @llvm.dx.fmod.v3f16
59+
// DX-NATIVE_HALF: ret <3 x half> %dx.fmod
60+
// DX-NO_HALF: define noundef <3 x float> @
61+
// DX-NO_HALF: %dx.fmod = call <3 x float> @llvm.dx.fmod.v3f32(
62+
// DX-NO_HALF: ret <3 x float> %dx.fmod
63+
//
64+
// SPV-NATIVE_HALF: define spir_func noundef <3 x half> @
65+
// SPV-NATIVE_HALF: %fmod = frem <3 x half>
66+
// SPV-NATIVE_HALF: ret <3 x half> %fmod
67+
// SPV-NO_HALF: define spir_func noundef <3 x float> @
68+
// SPV-NO_HALF: %fmod = frem <3 x float>
69+
// SPV-NO_HALF: ret <3 x float> %fmod
70+
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
71+
72+
// DX-NATIVE_HALF: define noundef <4 x half> @
73+
// DX-NATIVE_HALF: %dx.fmod = call <4 x half> @llvm.dx.fmod.v4f16
74+
// DX-NATIVE_HALF: ret <4 x half> %dx.fmod
75+
// DX-NO_HALF: define noundef <4 x float> @
76+
// DX-NO_HALF: %dx.fmod = call <4 x float> @llvm.dx.fmod.v4f32(
77+
// DX-NO_HALF: ret <4 x float> %dx.fmod
78+
//
79+
// SPV-NATIVE_HALF: define spir_func noundef <4 x half> @
80+
// SPV-NATIVE_HALF: %fmod = frem <4 x half>
81+
// SPV-NATIVE_HALF: ret <4 x half> %fmod
82+
// SPV-NO_HALF: define spir_func noundef <4 x float> @
83+
// SPV-NO_HALF: %fmod = frem <4 x float>
84+
// SPV-NO_HALF: ret <4 x float> %fmod
85+
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
86+
87+
// DX-CHECK: define noundef float @
88+
// DX-CHECK: %dx.fmod = call float @llvm.dx.fmod.f32(
89+
// DX-CHECK: ret float %dx.fmod
90+
//
91+
// SPV-CHECK: define spir_func noundef float @
92+
// SPV-CHECK: %fmod = frem float
93+
// SPV-CHECK: ret float %fmod
94+
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
95+
96+
// DX-CHECK: define noundef <2 x float> @
97+
// DX-CHECK: %dx.fmod = call <2 x float> @llvm.dx.fmod.v2f32
98+
// DX-CHECK: ret <2 x float> %dx.fmod
99+
//
100+
// SPV-CHECK: define spir_func noundef <2 x float> @
101+
// SPV-CHECK: %fmod = frem <2 x float>
102+
// SPV-CHECK: ret <2 x float> %fmod
103+
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
104+
105+
// DX-CHECK: define noundef <3 x float> @
106+
// DX-CHECK: %dx.fmod = call <3 x float> @llvm.dx.fmod.v3f32
107+
// DX-CHECK: ret <3 x float> %dx.fmod
108+
//
109+
// SPV-CHECK: define spir_func noundef <3 x float> @
110+
// SPV-CHECK: %fmod = frem <3 x float>
111+
// SPV-CHECK: ret <3 x float> %fmod
112+
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
113+
114+
// DX-CHECK: define noundef <4 x float> @
115+
// DX-CHECK: %dx.fmod = call <4 x float> @llvm.dx.fmod.v4f32
116+
// DX-CHECK: ret <4 x float> %dx.fmod
117+
//
118+
// SPV-CHECK: define spir_func noundef <4 x float> @
119+
// SPV-CHECK: %fmod = frem <4 x float>
120+
// SPV-CHECK: ret <4 x float> %fmod
121+
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
122+
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_hlsl_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_hlsl_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_hlsl_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_hlsl_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_hlsl_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_hlsl_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_hlsl_elementwise_fmod (p0, p1);
37+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
38+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
9090
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
9191
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty]>;
9292
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>]>;
93+
def int_dx_fmod : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>]>;
9394
}

0 commit comments

Comments
 (0)