Skip to content

Commit 5a52662

Browse files
authored
[HLSL] implement the rcp intrinsic (#83857)
This PR implements the frontend for #70100 This PR is part 1 of 2. Part 2 requires an intrinsic to instructions lowering. - `Builtins.td` - add an `rcp` builtin - `CGBuiltin.cpp` - add the builtin to intrinsic lowering - `hlsl_intrinsics.h` - add the `rcp` api - `SemaChecking.cpp` - reuse frac's sema checks - `IntrinsicsDirectX.td` - add the llvm intrinsic
1 parent a5095b9 commit 5a52662

File tree

7 files changed

+144
-7
lines changed

7 files changed

+144
-7
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4584,6 +4584,12 @@ def HLSLMad : LangBuiltin<"HLSL_LANG"> {
45844584
let Prototype = "void(...)";
45854585
}
45864586

4587+
def HLSLRcp : LangBuiltin<"HLSL_LANG"> {
4588+
let Spellings = ["__builtin_hlsl_elementwise_rcp"];
4589+
let Attributes = [NoThrow, Const];
4590+
let Prototype = "void(...)";
4591+
}
4592+
45874593
// Builtins for XRay.
45884594
def XRayCustomEvent : Builtin {
45894595
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17966,7 +17966,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1796617966
case Builtin::BI__builtin_hlsl_elementwise_any: {
1796717967
Value *Op0 = EmitScalarExpr(E->getArg(0));
1796817968
return Builder.CreateIntrinsic(
17969-
/*ReturnType*/ llvm::Type::getInt1Ty(getLLVMContext()),
17969+
/*ReturnType=*/llvm::Type::getInt1Ty(getLLVMContext()),
1797017970
Intrinsic::dx_any, ArrayRef<Value *>{Op0}, nullptr, "dx.any");
1797117971
}
1797217972
case Builtin::BI__builtin_hlsl_dot: {
@@ -18002,7 +18002,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1800218002
"Dot product requires vectors to be of the same size.");
1800318003

1800418004
return Builder.CreateIntrinsic(
18005-
/*ReturnType*/ T0->getScalarType(), Intrinsic::dx_dot,
18005+
/*ReturnType=*/T0->getScalarType(), Intrinsic::dx_dot,
1800618006
ArrayRef<Value *>{Op0, Op1}, nullptr, "dx.dot");
1800718007
} break;
1800818008
case Builtin::BI__builtin_hlsl_lerp: {
@@ -18039,15 +18039,15 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1803918039
XVecTy->getElementType() == SVecTy->getElementType() &&
1804018040
"Lerp requires float vectors to be of the same type.");
1804118041
return Builder.CreateIntrinsic(
18042-
/*ReturnType*/ Xty, Intrinsic::dx_lerp, ArrayRef<Value *>{X, Y, S},
18042+
/*ReturnType=*/Xty, Intrinsic::dx_lerp, ArrayRef<Value *>{X, Y, S},
1804318043
nullptr, "dx.lerp");
1804418044
}
1804518045
case Builtin::BI__builtin_hlsl_elementwise_frac: {
1804618046
Value *Op0 = EmitScalarExpr(E->getArg(0));
1804718047
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
1804818048
llvm_unreachable("frac operand must have a float representation");
1804918049
return Builder.CreateIntrinsic(
18050-
/*ReturnType*/ Op0->getType(), Intrinsic::dx_frac,
18050+
/*ReturnType=*/Op0->getType(), Intrinsic::dx_frac,
1805118051
ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
1805218052
}
1805318053
case Builtin::BI__builtin_hlsl_mad: {
@@ -18066,9 +18066,17 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1806618066
}
1806718067
assert(E->getArg(0)->getType()->hasUnsignedIntegerRepresentation());
1806818068
return Builder.CreateIntrinsic(
18069-
/*ReturnType*/ M->getType(), Intrinsic::dx_umad,
18069+
/*ReturnType=*/M->getType(), Intrinsic::dx_umad,
1807018070
ArrayRef<Value *>{M, A, B}, nullptr, "dx.umad");
1807118071
}
18072+
case Builtin::BI__builtin_hlsl_elementwise_rcp: {
18073+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18074+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18075+
llvm_unreachable("rcp operand must have a float representation");
18076+
return Builder.CreateIntrinsic(
18077+
/*ReturnType=*/Op0->getType(), Intrinsic::dx_rcp,
18078+
ArrayRef<Value *>{Op0}, nullptr, "dx.rcp");
18079+
}
1807218080
}
1807318081
return nullptr;
1807418082
}

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,47 @@ uint64_t3 reversebits(uint64_t3);
11121112
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse)
11131113
uint64_t4 reversebits(uint64_t4);
11141114

1115+
//===----------------------------------------------------------------------===//
1116+
// rcp builtins
1117+
//===----------------------------------------------------------------------===//
1118+
1119+
/// \fn T rcp(T x)
1120+
/// \brief Calculates a fast, approximate, per-component reciprocal ie 1 / \a x.
1121+
/// \param x The specified input value.
1122+
///
1123+
/// The return value is the reciprocal of the \a x parameter.
1124+
1125+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1126+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1127+
half rcp(half);
1128+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1129+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1130+
half2 rcp(half2);
1131+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1132+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1133+
half3 rcp(half3);
1134+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1135+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1136+
half4 rcp(half4);
1137+
1138+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1139+
float rcp(float);
1140+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1141+
float2 rcp(float2);
1142+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1143+
float3 rcp(float3);
1144+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1145+
float4 rcp(float4);
1146+
1147+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1148+
double rcp(double);
1149+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1150+
double2 rcp(double2);
1151+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1152+
double3 rcp(double3);
1153+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
1154+
double4 rcp(double4);
1155+
11151156
//===----------------------------------------------------------------------===//
11161157
// round builtins
11171158
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5285,6 +5285,7 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
52855285
return true;
52865286
break;
52875287
}
5288+
case Builtin::BI__builtin_hlsl_elementwise_rcp:
52885289
case Builtin::BI__builtin_hlsl_elementwise_frac: {
52895290
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
52905291
return true;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,NATIVE_HALF
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %dx.rcp = call half @llvm.dx.rcp.f16(
11+
// NATIVE_HALF: ret half %dx.rcp
12+
// NO_HALF: define noundef float @"?test_rcp_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %dx.rcp = call float @llvm.dx.rcp.f32(
14+
// NO_HALF: ret float %dx.rcp
15+
half test_rcp_half(half p0) { return rcp(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %dx.rcp = call <2 x half> @llvm.dx.rcp.v2f16
18+
// NATIVE_HALF: ret <2 x half> %dx.rcp
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %dx.rcp = call <2 x float> @llvm.dx.rcp.v2f32(
21+
// NO_HALF: ret <2 x float> %dx.rcp
22+
half2 test_rcp_half2(half2 p0) { return rcp(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %dx.rcp = call <3 x half> @llvm.dx.rcp.v3f16
25+
// NATIVE_HALF: ret <3 x half> %dx.rcp
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %dx.rcp = call <3 x float> @llvm.dx.rcp.v3f32(
28+
// NO_HALF: ret <3 x float> %dx.rcp
29+
half3 test_rcp_half3(half3 p0) { return rcp(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %dx.rcp = call <4 x half> @llvm.dx.rcp.v4f16
32+
// NATIVE_HALF: ret <4 x half> %dx.rcp
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %dx.rcp = call <4 x float> @llvm.dx.rcp.v4f32(
35+
// NO_HALF: ret <4 x float> %dx.rcp
36+
half4 test_rcp_half4(half4 p0) { return rcp(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %dx.rcp = call float @llvm.dx.rcp.f32(
40+
// CHECK: ret float %dx.rcp
41+
float test_rcp_float(float p0) { return rcp(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %dx.rcp = call <2 x float> @llvm.dx.rcp.v2f32
44+
// CHECK: ret <2 x float> %dx.rcp
45+
float2 test_rcp_float2(float2 p0) { return rcp(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %dx.rcp = call <3 x float> @llvm.dx.rcp.v3f32
48+
// CHECK: ret <3 x float> %dx.rcp
49+
float3 test_rcp_float3(float3 p0) { return rcp(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %dx.rcp = call <4 x float> @llvm.dx.rcp.v4f32
52+
// CHECK: ret <4 x float> %dx.rcp
53+
float4 test_rcp_float4(float4 p0) { return rcp(p0); }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected
3+
4+
float test_too_few_arg() {
5+
return __builtin_hlsl_elementwise_rcp();
6+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
7+
}
8+
9+
float2 test_too_many_arg(float2 p0) {
10+
return __builtin_hlsl_elementwise_rcp(p0, p0);
11+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
12+
}
13+
14+
float builtin_bool_to_float_type_promotion(bool p1) {
15+
return __builtin_hlsl_elementwise_rcp(p1);
16+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
17+
}
18+
19+
float builtin_rcp_int_to_float_promotion(int p1) {
20+
return __builtin_hlsl_elementwise_rcp(p1);
21+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
22+
}
23+
24+
float2 builtin_rcp_int2_to_float2_promotion(int2 p1) {
25+
return __builtin_hlsl_elementwise_rcp(p1);
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+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def int_dx_lerp :
3434
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>,LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
3535
[IntrNoMem, IntrWillReturn] >;
3636

37-
def int_dx_imad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
38-
def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
37+
def int_dx_imad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
38+
def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
39+
def int_dx_rcp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
3940
}

0 commit comments

Comments
 (0)