Skip to content

Commit e74332a

Browse files
authored
[HLSL][DXIL] HLSL's round should follow roundeven behavior (#87078)
fixes #86999
1 parent fdef5c4 commit e74332a

File tree

7 files changed

+50
-57
lines changed

7 files changed

+50
-57
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,25 +1248,25 @@ float4 rsqrt(float4);
12481248
/// rounded to the nearest even value.
12491249

12501250
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1251-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1251+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12521252
half round(half);
12531253
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1254-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1254+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12551255
half2 round(half2);
12561256
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1257-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1257+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12581258
half3 round(half3);
12591259
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1260-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1260+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12611261
half4 round(half4);
12621262

1263-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1263+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12641264
float round(float);
1265-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1265+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12661266
float2 round(float2);
1267-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1267+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12681268
float3 round(float3);
1269-
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
1269+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
12701270
float4 round(float4);
12711271

12721272
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,6 +5650,7 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
56505650
case Builtin::BI__builtin_elementwise_log2:
56515651
case Builtin::BI__builtin_elementwise_log10:
56525652
case Builtin::BI__builtin_elementwise_pow:
5653+
case Builtin::BI__builtin_elementwise_roundeven:
56535654
case Builtin::BI__builtin_elementwise_sin:
56545655
case Builtin::BI__builtin_elementwise_sqrt:
56555656
case Builtin::BI__builtin_elementwise_trunc: {

clang/test/CodeGenHLSL/builtins/round.hlsl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@
77
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
88

99
// NATIVE_HALF: define noundef half @
10-
// NATIVE_HALF: %elt.round = call half @llvm.round.f16(
11-
// NATIVE_HALF: ret half %elt.round
10+
// NATIVE_HALF: %elt.roundeven = call half @llvm.roundeven.f16(
11+
// NATIVE_HALF: ret half %elt.roundeven
1212
// 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
13+
// NO_HALF: %elt.roundeven = call float @llvm.roundeven.f32(
14+
// NO_HALF: ret float %elt.roundeven
1515
half test_round_half(half p0) { return round(p0); }
1616
// 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
17+
// NATIVE_HALF: %elt.roundeven = call <2 x half> @llvm.roundeven.v2f16
18+
// NATIVE_HALF: ret <2 x half> %elt.roundeven
1919
// 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
20+
// NO_HALF: %elt.roundeven = call <2 x float> @llvm.roundeven.v2f32(
21+
// NO_HALF: ret <2 x float> %elt.roundeven
2222
half2 test_round_half2(half2 p0) { return round(p0); }
2323
// 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
24+
// NATIVE_HALF: %elt.roundeven = call <3 x half> @llvm.roundeven.v3f16
25+
// NATIVE_HALF: ret <3 x half> %elt.roundeven
2626
// 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
27+
// NO_HALF: %elt.roundeven = call <3 x float> @llvm.roundeven.v3f32(
28+
// NO_HALF: ret <3 x float> %elt.roundeven
2929
half3 test_round_half3(half3 p0) { return round(p0); }
3030
// 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
31+
// NATIVE_HALF: %elt.roundeven = call <4 x half> @llvm.roundeven.v4f16
32+
// NATIVE_HALF: ret <4 x half> %elt.roundeven
3333
// 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
34+
// NO_HALF: %elt.roundeven = call <4 x float> @llvm.roundeven.v4f32(
35+
// NO_HALF: ret <4 x float> %elt.roundeven
3636
half4 test_round_half4(half4 p0) { return round(p0); }
3737

3838
// CHECK: define noundef float @
39-
// CHECK: %elt.round = call float @llvm.round.f32(
40-
// CHECK: ret float %elt.round
39+
// CHECK: %elt.roundeven = call float @llvm.roundeven.f32(
40+
// CHECK: ret float %elt.roundeven
4141
float test_round_float(float p0) { return round(p0); }
4242
// 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
43+
// CHECK: %elt.roundeven = call <2 x float> @llvm.roundeven.v2f32
44+
// CHECK: ret <2 x float> %elt.roundeven
4545
float2 test_round_float2(float2 p0) { return round(p0); }
4646
// 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
47+
// CHECK: %elt.roundeven = call <3 x float> @llvm.roundeven.v3f32
48+
// CHECK: ret <3 x float> %elt.roundeven
4949
float3 test_round_float3(float3 p0) { return round(p0); }
5050
// 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
51+
// CHECK: %elt.roundeven = call <4 x float> @llvm.roundeven.v4f32
52+
// CHECK: ret <4 x float> %elt.roundeven
5353
float4 test_round_float4(float4 p0) { return round(p0); }

clang/test/SemaHLSL/BuiltIns/half-float-only-errors.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_log10
99
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_sin
1010
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_sqrt
11+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_roundeven
1112
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_trunc
1213

1314
double2 test_double_builtin(double2 p0) {

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def RSqrt : DXILOpMapping<25, unary, int_dx_rsqrt,
285285
"Returns the reciprocal of the square root of the specified value."
286286
"rsqrt(x) = 1 / sqrt(x).",
287287
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
288-
def Round : DXILOpMapping<26, unary, int_round,
288+
def Round : DXILOpMapping<26, unary, int_roundeven,
289289
"Returns the input rounded to the nearest integer"
290290
"within a floating-point type.",
291291
[llvm_halforfloat_ty, LLVMMatchType<0>]>;

llvm/test/CodeGen/DirectX/round.ll

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
22

33
; 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 %{{.*}})
64

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 {
5+
; CHECK-LABEL: round_half
6+
define noundef half @round_half(half noundef %a) {
127
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
8+
; CHECK: call half @dx.op.unary.f16(i32 26, half %{{.*}})
9+
%elt.roundeven = call half @llvm.roundeven.f16(half %a)
10+
ret half %elt.roundeven
1811
}
1912

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 {
13+
; CHECK-LABEL: round_float
14+
define noundef float @round_float(float noundef %a) {
2515
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
16+
; CHECK: call float @dx.op.unary.f32(i32 26, float %{{.*}})
17+
%elt.roundeven = call float @llvm.roundeven.f32(float %a)
18+
ret float %elt.roundeven
3119
}
20+
21+
declare half @llvm.roundeven.f16(half)
22+
declare float @llvm.roundeven.f32(float)

llvm/test/CodeGen/DirectX/round_error.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ entry:
88
%a.addr = alloca double, align 8
99
store double %a, ptr %a.addr, align 8
1010
%0 = load double, ptr %a.addr, align 8
11-
%elt.round = call double @llvm.round.f64(double %0)
12-
ret double %elt.round
11+
%elt.roundeven = call double @llvm.roundeven.f64(double %0)
12+
ret double %elt.roundeven
1313
}

0 commit comments

Comments
 (0)