Skip to content

Commit b542501

Browse files
authored
[HLSL][DXIL] Implementation of round intrinsic (#83570)
hlsl_intrinsics.h - add the round api DXIL.td add the llvm intrinsic to DXIL lowering mapping This change reuses llvm's existing intrinsic `__builtin_elementwise_round`\ `int_round` This change implements: #70077
1 parent 53f89a0 commit b542501

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

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

834+
//===----------------------------------------------------------------------===//
835+
// round builtins
836+
//===----------------------------------------------------------------------===//
837+
838+
/// \fn T round(T x)
839+
/// \brief Rounds the specified value \a x to the nearest integer.
840+
/// \param x The specified input value.
841+
///
842+
/// The return value is the \a x parameter, rounded to the nearest integer
843+
/// within a floating-point type. Halfway cases are
844+
/// rounded to the nearest even value.
845+
846+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
847+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
848+
half round(half);
849+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
850+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
851+
half2 round(half2);
852+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
853+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
854+
half3 round(half3);
855+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
856+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
857+
half4 round(half4);
858+
859+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
860+
float round(float);
861+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
862+
float2 round(float2);
863+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
864+
float3 round(float3);
865+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
866+
float4 round(float4);
867+
834868
//===----------------------------------------------------------------------===//
835869
// sin builtins
836870
//===----------------------------------------------------------------------===//
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: %elt.round = call half @llvm.round.f16(
11+
// NATIVE_HALF: ret half %elt.round
12+
// NO_HALF: define noundef float @"?test_round_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %elt.round = call float @llvm.round.f32(
14+
// NO_HALF: ret float %elt.round
15+
half test_round_half(half p0) { return round(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %elt.round = call <2 x half> @llvm.round.v2f16
18+
// NATIVE_HALF: ret <2 x half> %elt.round
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %elt.round = call <2 x float> @llvm.round.v2f32(
21+
// NO_HALF: ret <2 x float> %elt.round
22+
half2 test_round_half2(half2 p0) { return round(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %elt.round = call <3 x half> @llvm.round.v3f16
25+
// NATIVE_HALF: ret <3 x half> %elt.round
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %elt.round = call <3 x float> @llvm.round.v3f32(
28+
// NO_HALF: ret <3 x float> %elt.round
29+
half3 test_round_half3(half3 p0) { return round(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %elt.round = call <4 x half> @llvm.round.v4f16
32+
// NATIVE_HALF: ret <4 x half> %elt.round
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %elt.round = call <4 x float> @llvm.round.v4f32(
35+
// NO_HALF: ret <4 x float> %elt.round
36+
half4 test_round_half4(half4 p0) { return round(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %elt.round = call float @llvm.round.f32(
40+
// CHECK: ret float %elt.round
41+
float test_round_float(float p0) { return round(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %elt.round = call <2 x float> @llvm.round.v2f32
44+
// CHECK: ret <2 x float> %elt.round
45+
float2 test_round_float2(float2 p0) { return round(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %elt.round = call <3 x float> @llvm.round.v3f32
48+
// CHECK: ret <3 x float> %elt.round
49+
float3 test_round_float3(float3 p0) { return round(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %elt.round = call <4 x float> @llvm.round.v4f32
52+
// CHECK: ret <4 x float> %elt.round
53+
float4 test_round_float4(float4 p0) { return round(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_elementwise_round();
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_elementwise_round(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_elementwise_round(p1);
16+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
17+
}
18+
19+
float builtin_round_int_to_float_promotion(int p1) {
20+
return __builtin_elementwise_round(p1);
21+
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
22+
}
23+
24+
float2 builtin_round_int2_to_float2_promotion(int2 p1) {
25+
return __builtin_elementwise_round(p1);
26+
// expected-error@-1 {{1st argument must be a floating point type (was 'int2' (aka 'vector<int, 2>'))}}
27+
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ class DXILOpMapping<int opCode, DXILOpClass opClass, Intrinsic intrinsic, string
218218
// Concrete definition of DXIL Operation mapping to corresponding LLVM intrinsic
219219
def Sin : DXILOpMapping<13, unary, int_sin,
220220
"Returns sine(theta) for theta in radians.">;
221+
def Round : DXILOpMapping<26, unary, int_round,
222+
"Returns the input rounded to the nearest integer"
223+
"within a floating-point type.">;
221224
def UMax : DXILOpMapping<39, binary, int_umax,
222225
"Unsigned integer maximum. UMax(a,b) = a > b ? a : b">;
223226
def ThreadId : DXILOpMapping<93, threadId, int_dx_thread_id,

llvm/test/CodeGen/DirectX/round.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 round are generated for float and half.
4+
; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
5+
; CHECK:call half @dx.op.unary.f16(i32 26, 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 @round_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+
%elt.round = call float @llvm.round.f32(float %0)
17+
ret float %elt.round
18+
}
19+
20+
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
21+
declare float @llvm.round.f32(float) #1
22+
23+
; Function Attrs: noinline nounwind optnone
24+
define noundef half @round_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+
%elt.round = call half @llvm.round.f16(half %0)
30+
ret half %elt.round
31+
}

0 commit comments

Comments
 (0)