Skip to content

Commit 4ed0507

Browse files
committed
change clamp templates so they call clamp instead of __builtin_hlsl_elementwise_clamp, to ensure clamp can't be called on 16 bit types in a shader model before 6.2. Add tests to show this
1 parent 8e01d99 commit 4ed0507

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

clang/lib/Headers/hlsl/hlsl_compat_overloads.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,34 @@ template <typename T, typename R, typename U, uint N>
2525
constexpr __detail::enable_if_t<
2626
__detail::is_arithmetic<U>::Value && (N > 1 && N <= 4), vector<T, N>>
2727
clamp(vector<T, N> p0, vector<R, N> p1, U p2) {
28-
return __builtin_hlsl_elementwise_clamp(p0, (vector<T, N>)p1,
29-
(vector<T, N>)p2);
28+
return clamp(p0, (vector<T, N>)p1, (vector<T, N>)p2);
3029
}
3130
template <typename T, typename R, typename U, uint N>
3231
constexpr __detail::enable_if_t<
3332
__detail::is_arithmetic<U>::Value && (N > 1 && N <= 4), vector<T, N>>
3433
clamp(vector<T, N> p0, U p1, vector<R, N> p2) {
35-
return __builtin_hlsl_elementwise_clamp(p0, (vector<T, N>)p1,
36-
(vector<T, N>)p2);
34+
return clamp(p0, (vector<T, N>)p1, (vector<T, N>)p2);
3735
}
3836
template <typename T, typename U, typename V, uint N>
3937
constexpr __detail::enable_if_t<__detail::is_arithmetic<U>::Value &&
4038
__detail::is_arithmetic<V>::Value &&
4139
(N > 1 && N <= 4),
4240
vector<T, N>>
4341
clamp(vector<T, N> p0, U p1, V p2) {
44-
return __builtin_hlsl_elementwise_clamp(p0, (vector<T, N>)p1,
45-
(vector<T, N>)p2);
42+
return clamp(p0, (vector<T, N>)p1, (vector<T, N>)p2);
4643
}
4744
template <typename T, typename R, typename S, uint N>
4845
constexpr __detail::enable_if_t<(N > 1 && N <= 4), vector<T, N>>
4946
clamp(vector<T, N> p0, vector<R, N> p1, vector<S, N> p2) {
50-
return __builtin_hlsl_elementwise_clamp(p0, (vector<T, N>)p1,
51-
(vector<T, N>)p2);
47+
return clamp(p0, (vector<T, N>)p1, (vector<T, N>)p2);
5248
}
5349
template <typename U, typename V, typename W>
5450
constexpr __detail::enable_if_t<__detail::is_arithmetic<U>::Value &&
5551
__detail::is_arithmetic<V>::Value &&
5652
__detail::is_arithmetic<W>::Value,
5753
U>
5854
clamp(U p0, V p1, W p2) {
59-
return __builtin_hlsl_elementwise_clamp(p0, (U)p1, (U)p2);
55+
return clamp(p0, (U)p1, (U)p2);
6056
}
6157

6258
} // namespace hlsl
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 202x %s 2>&1 | FileCheck %s
2+
3+
// check we error on 16 bit type if shader model is too old
4+
// CHECK: '-enable-16bit-types' option requires target HLSL Version >= 2018 and shader model >= 6.2, but HLSL Version is 'hlsl202x' and shader model is '6.0'
5+
int16_t test_int16_t_error(int16_t p0, int p1) {
6+
return clamp(p0, p0, p1);
7+
}
8+
9+
int16_t3 test_int16_t3_error(int16_t3 p0, int3 p1) {
10+
return clamp(p0, p0, p1);
11+
}
12+
13+
half test_half_error(half p0, int p1) {
14+
return clamp(p0, p1, p1);
15+
}
16+
17+
half3 test_half3_error(half3 p0, int3 p1) {
18+
return clamp(p0, p0, p1);
19+
}
20+
21+
uint16_t test_uint16_t_error(uint16_t p0, int p1) {
22+
return clamp(p0, p0, p1);
23+
}
24+
25+
uint16_t3 test_uint16_t3_error(uint16_t3 p0, int3 p1) {
26+
return clamp(p0, p1, p1);
27+
}

0 commit comments

Comments
 (0)