Skip to content

Commit 710cef1

Browse files
committed
[HLSL] implement the rcp intrinsic
This PR implements the frontend for #70100 This PR is part 1 of 2. Part 2 depends on dixl-lerp-intrinsic-lowering PR which will have an intrinsic to instruction expansion pass. THat pass is what we need to complete the DXIL lowering portion of this PR: llvm/[email protected]:llvm-project:dixl-lerp-intrinsic-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 semachecks IntrinsicsDirectX.td add the llvm intrinsic
1 parent 1e828f8 commit 710cef1

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4572,6 +4572,12 @@ def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
45724572
let Prototype = "void(...)";
45734573
}
45744574

4575+
def HLSLRcp : LangBuiltin<"HLSL_LANG"> {
4576+
let Spellings = ["__builtin_hlsl_elementwise_rcp"];
4577+
let Attributes = [NoThrow, Const];
4578+
let Prototype = "void(...)";
4579+
}
4580+
45754581
// Builtins for XRay.
45764582
def XRayCustomEvent : Builtin {
45774583
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18044,6 +18044,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1804418044
/*ReturnType*/ Op0->getType(), Intrinsic::dx_frac,
1804518045
ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
1804618046
}
18047+
case Builtin::BI__builtin_hlsl_elementwise_rcp: {
18048+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18049+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18050+
llvm_unreachable("rcp operand must have a float representation");
18051+
return Builder.CreateIntrinsic(
18052+
/*ReturnType*/ Op0->getType(), Intrinsic::dx_rcp,
18053+
ArrayRef<Value *>{Op0}, nullptr, "dx.rcp");
18054+
}
1804718055
}
1804818056
return nullptr;
1804918057
}

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,47 @@ uint64_t3 reversebits(uint64_t3);
831831
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse)
832832
uint64_t4 reversebits(uint64_t4);
833833

834+
//===----------------------------------------------------------------------===//
835+
// rcp builtins
836+
//===----------------------------------------------------------------------===//
837+
838+
/// \fn T rcp(T x)
839+
/// \brief Calculates a fast, approximate, per-component reciprocal ie 1 / \a x.
840+
/// \param x The specified input value.
841+
///
842+
/// The return value is the reciprocal of the \a x parameter.
843+
844+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
845+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
846+
half rcp(half);
847+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
848+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
849+
half2 rcp(half2);
850+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
851+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
852+
half3 rcp(half3);
853+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
854+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
855+
half4 rcp(half4);
856+
857+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
858+
float rcp(float);
859+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
860+
float2 rcp(float2);
861+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
862+
float3 rcp(float3);
863+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
864+
float4 rcp(float4);
865+
866+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
867+
double rcp(double);
868+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
869+
double2 rcp(double2);
870+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
871+
double3 rcp(double3);
872+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_rcp)
873+
double4 rcp(double4);
874+
834875
//===----------------------------------------------------------------------===//
835876
// round builtins
836877
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5280,6 +5280,7 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
52805280
return true;
52815281
break;
52825282
}
5283+
case Builtin::BI__builtin_hlsl_elementwise_rcp:
52835284
case Builtin::BI__builtin_hlsl_elementwise_frac: {
52845285
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
52855286
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ def int_dx_lerp :
3131
Intrinsic<[LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
3232
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>,LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
3333
[IntrNoMem, IntrWillReturn] >;
34+
35+
def int_dx_rcp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
3436
}

0 commit comments

Comments
 (0)