Skip to content

Commit 4609b6a

Browse files
authored
[libclc] Move fmin & fmax to CLC library (#134218)
This is an alternative to #128506 which doesn't attempt to change the codegen for fmin and fmax on their way to the CLC library. The amdgcn and r600 custom definitions of fmin/fmax are now converted to custom definitions of __clc_fmin and __clc_fmax. For simplicity, the CLC library doesn't provide vector/scalar versions of these builtins. The OpenCL layer wraps those up to the vector/vector versions. The only codegen change is that non-standard vector/scalar overloads of fmin/fmax have been removed. We were currently (accidentally, presumably) providing overloads with mixed elment types such as fmin(double2, float), fmax(half4, double), etc. The only vector/scalar overloads in the OpenCL spec are those with scalars of the same element type as the vector in the first argument.
1 parent f6b6fb8 commit 4609b6a

File tree

21 files changed

+329
-239
lines changed

21 files changed

+329
-239
lines changed

libclc/amdgcn/lib/SOURCES

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
cl_khr_int64_extended_atomics/minmax_helpers.ll
2-
math/fmax.cl
3-
math/fmin.cl
42
mem_fence/fence.cl
53
synchronization/barrier.cl
64
workitem/get_global_offset.cl

libclc/amdgcn/lib/math/fmax.cl

Lines changed: 0 additions & 53 deletions
This file was deleted.

libclc/amdgcn/lib/math/fmin.cl

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_MATH_CLC_FMAX_H__
10+
#define __CLC_MATH_CLC_FMAX_H__
11+
12+
#define __CLC_FUNCTION __clc_fmax
13+
#define __CLC_BODY <clc/shared/binary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_FMAX_H__
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_MATH_CLC_FMIN_H__
10+
#define __CLC_MATH_CLC_FMIN_H__
11+
12+
#define __CLC_FUNCTION __clc_fmin
13+
#define __CLC_BODY <clc/shared/binary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_FMIN_H__
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
10+
__CLC_GENTYPE y);
11+
12+
#ifndef __CLC_SCALAR
13+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
14+
__CLC_SCALAR_GENTYPE y);
15+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/utils.h>
10+
11+
#ifndef __CLC_FUNCTION
12+
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
13+
#endif
14+
15+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE a,
16+
__CLC_GENTYPE b) {
17+
return __CLC_FUNCTION(FUNCTION)(a, b);
18+
}
19+
20+
#ifndef __CLC_SCALAR
21+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE a,
22+
__CLC_SCALAR_GENTYPE b) {
23+
return __CLC_FUNCTION(FUNCTION)(a, (__CLC_GENTYPE)b);
24+
}
25+
#endif

libclc/clc/lib/amdgcn/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
math/clc_fmax.cl
2+
math/clc_fmin.cl
13
math/clc_ldexp_override.cl
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clcmacro.h>
10+
#include <clc/internal/clc.h>
11+
#include <clc/relational/clc_isnan.h>
12+
13+
_CLC_DEF _CLC_OVERLOAD float __clc_fmax(float x, float y) {
14+
// fcanonicalize removes sNaNs and flushes denormals if not enabled. Otherwise
15+
// fmax instruction flushes the values for comparison, but outputs original
16+
// denormal
17+
x = __builtin_canonicalizef(x);
18+
y = __builtin_canonicalizef(y);
19+
return __builtin_fmaxf(x, y);
20+
}
21+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmax, float, float)
22+
23+
#ifdef cl_khr_fp64
24+
25+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
26+
27+
_CLC_DEF _CLC_OVERLOAD double __clc_fmax(double x, double y) {
28+
x = __builtin_canonicalize(x);
29+
y = __builtin_canonicalize(y);
30+
return __builtin_fmax(x, y);
31+
}
32+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmax, double,
33+
double)
34+
35+
#endif
36+
#ifdef cl_khr_fp16
37+
38+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
39+
40+
_CLC_DEF _CLC_OVERLOAD half __clc_fmax(half x, half y) {
41+
if (__clc_isnan(x))
42+
return y;
43+
if (__clc_isnan(y))
44+
return x;
45+
return (y < x) ? x : y;
46+
}
47+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmax, half, half)
48+
49+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clcmacro.h>
10+
#include <clc/internal/clc.h>
11+
#include <clc/relational/clc_isnan.h>
12+
13+
_CLC_DEF _CLC_OVERLOAD float __clc_fmin(float x, float y) {
14+
// fcanonicalize removes sNaNs and flushes denormals if not enabled. Otherwise
15+
// fmin instruction flushes the values for comparison, but outputs original
16+
// denormal
17+
x = __builtin_canonicalizef(x);
18+
y = __builtin_canonicalizef(y);
19+
return __builtin_fminf(x, y);
20+
}
21+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmin, float, float)
22+
23+
#ifdef cl_khr_fp64
24+
25+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
26+
27+
_CLC_DEF _CLC_OVERLOAD double __clc_fmin(double x, double y) {
28+
x = __builtin_canonicalize(x);
29+
y = __builtin_canonicalize(y);
30+
return __builtin_fmin(x, y);
31+
}
32+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmin, double,
33+
double)
34+
35+
#endif
36+
37+
#ifdef cl_khr_fp16
38+
39+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
40+
41+
_CLC_DEF _CLC_OVERLOAD half __clc_fmin(half x, half y) {
42+
if (__clc_isnan(x))
43+
return y;
44+
if (__clc_isnan(y))
45+
return x;
46+
return (y < x) ? y : x;
47+
}
48+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmin, half, half)
49+
50+
#endif

libclc/clc/lib/generic/SOURCES

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ math/clc_expm1.cl
4242
math/clc_exp_helper.cl
4343
math/clc_fabs.cl
4444
math/clc_fma.cl
45-
math/clc_fmod.cl
45+
math/clc_fmax.cl
46+
math/clc_fmin.cl
4647
math/clc_floor.cl
48+
math/clc_fmod.cl
4749
math/clc_frexp.cl
4850
math/clc_hypot.cl
4951
math/clc_ldexp.cl
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clcmacro.h>
10+
#include <clc/internal/clc.h>
11+
#include <clc/relational/clc_isnan.h>
12+
13+
_CLC_DEFINE_BINARY_BUILTIN(float, __clc_fmax, __builtin_fmaxf, float, float);
14+
15+
#ifdef cl_khr_fp64
16+
17+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
18+
19+
_CLC_DEFINE_BINARY_BUILTIN(double, __clc_fmax, __builtin_fmax, double, double);
20+
21+
#endif
22+
23+
#ifdef cl_khr_fp16
24+
25+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
26+
27+
_CLC_DEF _CLC_OVERLOAD half __clc_fmax(half x, half y) {
28+
if (__clc_isnan(x))
29+
return y;
30+
if (__clc_isnan(y))
31+
return x;
32+
return (x < y) ? y : x;
33+
}
34+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmax, half, half)
35+
36+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clcmacro.h>
10+
#include <clc/internal/clc.h>
11+
#include <clc/relational/clc_isnan.h>
12+
13+
_CLC_DEFINE_BINARY_BUILTIN(float, __clc_fmin, __builtin_fminf, float, float);
14+
15+
#ifdef cl_khr_fp64
16+
17+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
18+
19+
_CLC_DEFINE_BINARY_BUILTIN(double, __clc_fmin, __builtin_fmin, double, double);
20+
21+
#endif
22+
23+
#ifdef cl_khr_fp16
24+
25+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
26+
27+
_CLC_DEF _CLC_OVERLOAD half __clc_fmin(half x, half y) {
28+
if (__clc_isnan(x))
29+
return y;
30+
if (__clc_isnan(y))
31+
return x;
32+
return (y < x) ? y : x;
33+
}
34+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmin, half, half)
35+
36+
#endif

libclc/clc/lib/r600/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
math/clc_fmax.cl
2+
math/clc_fmin.cl
13
math/clc_native_rsqrt.cl
24
math/clc_rsqrt_override.cl

0 commit comments

Comments
 (0)