Skip to content

Commit 52077bb

Browse files
committed
[HLSL] Implement rsqrt intrinsic
This change implements #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 2a3f27c commit 52077bb

File tree

10 files changed

+211
-0
lines changed

10 files changed

+211
-0
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
@@ -18077,6 +18077,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1807718077
/*ReturnType=*/Op0->getType(), Intrinsic::dx_rcp,
1807818078
ArrayRef<Value *>{Op0}, nullptr, "dx.rcp");
1807918079
}
18080+
case Builtin::BI__builtin_hlsl_elementwise_rsqrt: {
18081+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18082+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18083+
llvm_unreachable("rsqrt operand must have a float representation");
18084+
return Builder.CreateIntrinsic(
18085+
/*ReturnType=*/Op0->getType(), Intrinsic::dx_rsqrt,
18086+
ArrayRef<Value *>{Op0}, nullptr, "dx.rsqrt");
18087+
}
1808018088
}
1808118089
return nullptr;
1808218090
}

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,38 @@ 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 RReturns the reciprocal of the square root of the specified value \a x.
1162+
/// \param x The specified input value.
1163+
///
1164+
/// This function uses the following formula: 1 / sqrt(x).
1165+
1166+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1167+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1168+
half rsqrt(half);
1169+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1170+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1171+
half2 rsqrt(half2);
1172+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1173+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1174+
half3 rsqrt(half3);
1175+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1176+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1177+
half4 rsqrt(half4);
1178+
1179+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1180+
float rsqrt(float);
1181+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1182+
float2 rsqrt(float2);
1183+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1184+
float3 rsqrt(float3);
1185+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rsqrt)
1186+
float4 rsqrt(float4);
1187+
11561188
//===----------------------------------------------------------------------===//
11571189
// round builtins
11581190
//===----------------------------------------------------------------------===//

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_rsqrt:
52885289
case Builtin::BI__builtin_hlsl_elementwise_rcp:
52895290
case Builtin::BI__builtin_hlsl_elementwise_frac: {
52905291
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
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); }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; ModuleID = 'D:\projects\llvm-project\clang\test\SemaHLSL\BuiltIns\dot-warning.hlsl'
2+
source_filename = "D:\\projects\\llvm-project\\clang\\test\\SemaHLSL\\BuiltIns\\dot-warning.hlsl"
3+
target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
4+
target triple = "dxil-pc-shadermodel6.3-library"
5+
6+
; Function Attrs: noinline nounwind optnone
7+
define noundef float @"?test_dot_builtin_vector_elem_size_reduction@@YAMT?$__vector@J$01@__clang@@M@Z"(<2 x i64> noundef %p0, float noundef %p1) #0 {
8+
entry:
9+
%p1.addr = alloca float, align 4
10+
%p0.addr = alloca <2 x i64>, align 16
11+
store float %p1, ptr %p1.addr, align 4
12+
store <2 x i64> %p0, ptr %p0.addr, align 16
13+
%0 = load <2 x i64>, ptr %p0.addr, align 16
14+
%conv = sitofp <2 x i64> %0 to <2 x float>
15+
%1 = load float, ptr %p1.addr, align 4
16+
%splat.splatinsert = insertelement <2 x float> poison, float %1, i64 0
17+
%splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
18+
%dx.dot = call float @llvm.dx.dot.v2f32(<2 x float> %conv, <2 x float> %splat.splat)
19+
ret float %dx.dot
20+
}
21+
22+
; Function Attrs: nounwind willreturn memory(none)
23+
declare float @llvm.dx.dot.v2f32(<2 x float>, <2 x float>) #1
24+
25+
; Function Attrs: noinline nounwind optnone
26+
define noundef float @"?test_dot_builtin_int_vector_elem_size_reduction@@YAMT?$__vector@H$01@__clang@@M@Z"(<2 x i32> noundef %p0, float noundef %p1) #0 {
27+
entry:
28+
%p1.addr = alloca float, align 4
29+
%p0.addr = alloca <2 x i32>, align 8
30+
store float %p1, ptr %p1.addr, align 4
31+
store <2 x i32> %p0, ptr %p0.addr, align 8
32+
%0 = load <2 x i32>, ptr %p0.addr, align 8
33+
%conv = sitofp <2 x i32> %0 to <2 x float>
34+
%1 = load float, ptr %p1.addr, align 4
35+
%splat.splatinsert = insertelement <2 x float> poison, float %1, i64 0
36+
%splat.splat = shufflevector <2 x float> %splat.splatinsert, <2 x float> poison, <2 x i32> zeroinitializer
37+
%dx.dot = call float @llvm.dx.dot.v2f32(<2 x float> %conv, <2 x float> %splat.splat)
38+
ret float %dx.dot
39+
}
40+
41+
attributes #0 = { noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
42+
attributes #1 = { nounwind willreturn memory(none) }
43+
44+
!llvm.module.flags = !{!0, !1}
45+
!llvm.ident = !{!2}
46+
47+
!0 = !{i32 1, !"wchar_size", i32 4}
48+
!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
49+
!2 = !{!"clang version 19.0.0git (https://github.com/farzonl/llvm-project.git f40562c7b4224e00da2ff2e13d175abfaac68532)"}
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 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def Exp2 : DXILOpMapping<21, unary, int_exp2,
224224
def Frac : DXILOpMapping<22, unary, int_dx_frac,
225225
"Returns a fraction from 0 to 1 that represents the "
226226
"decimal part of the input.">;
227+
def RSqrt : DXILOpMapping<25, unary, int_dx_rsqrt,
228+
"Returns the reciprocal of the square root of the specified value."
229+
"rsqrt(x) = 1 / sqrt(x).">;
227230
def Round : DXILOpMapping<26, unary, int_round,
228231
"Returns the input rounded to the nearest integer"
229232
"within a floating-point type.">;

llvm/test/CodeGen/DirectX/rsqrt.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
; CHECK:call float @dx.op.unary.f32(i32 25, float %{{.*}})
5+
; CHECK:call half @dx.op.unary.f16(i32 25, half %{{.*}})
6+
7+
target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
8+
target triple = "dxil-pc-shadermodel6.7-library"
9+
10+
; Function Attrs: noinline nounwind optnone
11+
define noundef float @rsqrt_float(float noundef %a) #0 {
12+
entry:
13+
%a.addr = alloca float, align 4
14+
store float %a, ptr %a.addr, align 4
15+
%0 = load float, ptr %a.addr, align 4
16+
%dx.rsqrt = call float @llvm.dx.rsqrt.f32(float %0)
17+
ret float %dx.rsqrt
18+
}
19+
20+
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
21+
declare float @llvm.dx.rsqrt.f32(float) #1
22+
23+
; Function Attrs: noinline nounwind optnone
24+
define noundef half @rsqrt_half(half noundef %a) #0 {
25+
entry:
26+
%a.addr = alloca half, align 2
27+
store half %a, ptr %a.addr, align 2
28+
%0 = load half, ptr %a.addr, align 2
29+
%dx.rsqrt = call half @llvm.dx.rsqrt.f16(half %0)
30+
ret half %dx.rsqrt
31+
}

0 commit comments

Comments
 (0)