Skip to content

Commit 8f9ee39

Browse files
authored
[HLSL] Implement rsqrt intrinsic (llvm#84820)
This change implements llvm#70074 - `hlsl_intrinsics.h` - add the `rsqrt` api - `DXIL.td` add the llvm intrinsic to DXIL op lowering map. - `Builtins.td` - add an hlsl builtin for rsqrt. - `CGBuiltin.cpp` add the ir generation for the rsqrt intrinsic. - `SemaChecking.cpp` - reuse the one arg float only checks. - `IntrinsicsDirectX.td` -add an `rsqrt` intrinsic.
1 parent aa61006 commit 8f9ee39

File tree

12 files changed

+179
-4
lines changed

12 files changed

+179
-4
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,6 +4590,12 @@ def HLSLRcp : LangBuiltin<"HLSL_LANG"> {
45904590
let Prototype = "void(...)";
45914591
}
45924592

4593+
def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
4594+
let Spellings = ["__builtin_hlsl_elementwise_rsqrt"];
4595+
let Attributes = [NoThrow, Const, CustomTypeChecking];
4596+
let Prototype = "void(...)";
4597+
}
4598+
45934599
// Builtins for XRay.
45944600
def XRayCustomEvent : Builtin {
45954601
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18089,6 +18089,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1808918089
/*ReturnType=*/Op0->getType(), Intrinsic::dx_rcp,
1809018090
ArrayRef<Value *>{Op0}, nullptr, "dx.rcp");
1809118091
}
18092+
case Builtin::BI__builtin_hlsl_elementwise_rsqrt: {
18093+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18094+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18095+
llvm_unreachable("rsqrt operand must have a float representation");
18096+
return Builder.CreateIntrinsic(
18097+
/*ReturnType=*/Op0->getType(), Intrinsic::dx_rsqrt,
18098+
ArrayRef<Value *>{Op0}, nullptr, "dx.rsqrt");
18099+
}
1809218100
}
1809318101
return nullptr;
1809418102
}

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,39 @@ double3 rcp(double3);
11531153
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
11541154
double4 rcp(double4);
11551155

1156+
//===----------------------------------------------------------------------===//
1157+
// rsqrt builtins
1158+
//===----------------------------------------------------------------------===//
1159+
1160+
/// \fn T rsqrt(T x)
1161+
/// \brief Returns the reciprocal of the square root of the specified value.
1162+
/// ie 1 / sqrt( \a x).
1163+
/// \param x The specified input value.
1164+
///
1165+
/// This function uses the following formula: 1 / sqrt(x).
1166+
1167+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1168+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1169+
half rsqrt(half);
1170+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1171+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1172+
half2 rsqrt(half2);
1173+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1174+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1175+
half3 rsqrt(half3);
1176+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1177+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1178+
half4 rsqrt(half4);
1179+
1180+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1181+
float rsqrt(float);
1182+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1183+
float2 rsqrt(float2);
1184+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1185+
float3 rsqrt(float3);
1186+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1187+
float4 rsqrt(float4);
1188+
11561189
//===----------------------------------------------------------------------===//
11571190
// round builtins
11581191
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,12 +5286,13 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
52865286
return true;
52875287
break;
52885288
}
5289+
case Builtin::BI__builtin_hlsl_elementwise_rsqrt:
52895290
case Builtin::BI__builtin_hlsl_elementwise_rcp:
52905291
case Builtin::BI__builtin_hlsl_elementwise_frac: {
5291-
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
5292-
return true;
52935292
if (CheckAllArgsHaveFloatRepresentation(this, TheCall))
52945293
return true;
5294+
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
5295+
return true;
52955296
break;
52965297
}
52975298
case Builtin::BI__builtin_hlsl_lerp: {
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.rsqrt = call half @llvm.dx.rsqrt.f16(
11+
// NATIVE_HALF: ret half %dx.rsqrt
12+
// NO_HALF: define noundef float @"?test_rsqrt_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %dx.rsqrt = call float @llvm.dx.rsqrt.f32(
14+
// NO_HALF: ret float %dx.rsqrt
15+
half test_rsqrt_half(half p0) { return rsqrt(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %dx.rsqrt = call <2 x half> @llvm.dx.rsqrt.v2f16
18+
// NATIVE_HALF: ret <2 x half> %dx.rsqrt
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %dx.rsqrt = call <2 x float> @llvm.dx.rsqrt.v2f32(
21+
// NO_HALF: ret <2 x float> %dx.rsqrt
22+
half2 test_rsqrt_half2(half2 p0) { return rsqrt(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %dx.rsqrt = call <3 x half> @llvm.dx.rsqrt.v3f16
25+
// NATIVE_HALF: ret <3 x half> %dx.rsqrt
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %dx.rsqrt = call <3 x float> @llvm.dx.rsqrt.v3f32(
28+
// NO_HALF: ret <3 x float> %dx.rsqrt
29+
half3 test_rsqrt_half3(half3 p0) { return rsqrt(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %dx.rsqrt = call <4 x half> @llvm.dx.rsqrt.v4f16
32+
// NATIVE_HALF: ret <4 x half> %dx.rsqrt
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %dx.rsqrt = call <4 x float> @llvm.dx.rsqrt.v4f32(
35+
// NO_HALF: ret <4 x float> %dx.rsqrt
36+
half4 test_rsqrt_half4(half4 p0) { return rsqrt(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %dx.rsqrt = call float @llvm.dx.rsqrt.f32(
40+
// CHECK: ret float %dx.rsqrt
41+
float test_rsqrt_float(float p0) { return rsqrt(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %dx.rsqrt = call <2 x float> @llvm.dx.rsqrt.v2f32
44+
// CHECK: ret <2 x float> %dx.rsqrt
45+
float2 test_rsqrt_float2(float2 p0) { return rsqrt(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %dx.rsqrt = call <3 x float> @llvm.dx.rsqrt.v3f32
48+
// CHECK: ret <3 x float> %dx.rsqrt
49+
float3 test_rsqrt_float3(float3 p0) { return rsqrt(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %dx.rsqrt = call <4 x float> @llvm.dx.rsqrt.v4f32
52+
// CHECK: ret <4 x float> %dx.rsqrt
53+
float4 test_rsqrt_float4(float4 p0) { return rsqrt(p0); }

clang/test/SemaHLSL/BuiltIns/frac-errors.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ float2 test_too_many_arg(float2 p0) {
1313

1414
float builtin_bool_to_float_type_promotion(bool p1) {
1515
return __builtin_hlsl_elementwise_frac(p1);
16-
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
16+
// expected-error@-1 {{passing 'bool' to parameter of incompatible type 'float'}}
1717
}
1818

1919
float builtin_frac_int_to_float_promotion(int p1) {

clang/test/SemaHLSL/BuiltIns/rcp-errors.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ float2 test_too_many_arg(float2 p0) {
1313

1414
float builtin_bool_to_float_type_promotion(bool p1) {
1515
return __builtin_hlsl_elementwise_rcp(p1);
16-
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
16+
// expected-error@-1 {passing 'bool' to parameter of incompatible type 'float'}}
1717
}
1818

1919
float builtin_rcp_int_to_float_promotion(int p1) {
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_rsqrt();
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_rsqrt(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_rsqrt(p1);
16+
// expected-error@-1 {{passing 'bool' to parameter of incompatible type 'float'}}
17+
}
18+
19+
float builtin_rsqrt_int_to_float_promotion(int p1) {
20+
return __builtin_hlsl_elementwise_rsqrt(p1);
21+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
22+
}
23+
24+
float2 builtin_rsqrt_int2_to_float2_promotion(int2 p1) {
25+
return __builtin_hlsl_elementwise_rsqrt(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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ def int_dx_lerp :
3737
def int_dx_imad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
3838
def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
3939
def int_dx_rcp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
40+
def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
4041
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ def Frac : DXILOpMapping<22, unary, int_dx_frac,
266266
"Returns a fraction from 0 to 1 that represents the "
267267
"decimal part of the input.",
268268
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
269+
def RSqrt : DXILOpMapping<25, unary, int_dx_rsqrt,
270+
"Returns the reciprocal of the square root of the specified value."
271+
"rsqrt(x) = 1 / sqrt(x).",
272+
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
269273
def Round : DXILOpMapping<26, unary, int_round,
270274
"Returns the input rounded to the nearest integer"
271275
"within a floating-point type.",

llvm/test/CodeGen/DirectX/rsqrt.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
2+
3+
; Make sure dxil operation function calls for rsqrt are generated for float and half.
4+
5+
; CHECK-LABEL: rsqrt_float
6+
; CHECK: call float @dx.op.unary.f32(i32 25, float %{{.*}})
7+
define noundef float @rsqrt_float(float noundef %a) {
8+
entry:
9+
%a.addr = alloca float, align 4
10+
store float %a, ptr %a.addr, align 4
11+
%0 = load float, ptr %a.addr, align 4
12+
%dx.rsqrt = call float @llvm.dx.rsqrt.f32(float %0)
13+
ret float %dx.rsqrt
14+
}
15+
16+
; CHECK-LABEL: rsqrt_half
17+
; CHECK: call half @dx.op.unary.f16(i32 25, half %{{.*}})
18+
define noundef half @rsqrt_half(half noundef %a) {
19+
entry:
20+
%a.addr = alloca half, align 2
21+
store half %a, ptr %a.addr, align 2
22+
%0 = load half, ptr %a.addr, align 2
23+
%dx.rsqrt = call half @llvm.dx.rsqrt.f16(half %0)
24+
ret half %dx.rsqrt
25+
}
26+
27+
declare half @llvm.dx.rsqrt.f16(half)
28+
declare float @llvm.dx.rsqrt.f32(float)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: not opt -S -dxil-op-lower %s 2>&1 | FileCheck %s
2+
3+
; DXIL operation rsqrt does not support double overload type
4+
; CHECK: LLVM ERROR: Invalid Overload Type
5+
6+
; Function Attrs: noinline nounwind optnone
7+
define noundef double @rsqrt_double(double noundef %a) #0 {
8+
entry:
9+
%a.addr = alloca double, align 8
10+
store double %a, ptr %a.addr, align 8
11+
%0 = load double, ptr %a.addr, align 8
12+
%dx.rsqrt = call double @llvm.dx.rsqrt.f64(double %0)
13+
ret double %dx.rsqrt
14+
}

0 commit comments

Comments
 (0)