Skip to content

Commit eba78dc

Browse files
frasercrmcksivan-shani
authored andcommitted
[libclc] Move step to the CLC library; add missing half variants (llvm#140936)
The half variants were missing but are trivial to implement. There were some incorrect mixed type overloads (step(float, double)) which aren't in the OpenCL specification and so have been removed. Like certain other builtins the CLC step function only deals with identical types. The OpenCL layer is responsible for casting the scalar argument to a vector. This commit also trivially vectorizes the CLC function, generating better bytecode.
1 parent 8fad80d commit eba78dc

File tree

8 files changed

+70
-67
lines changed

8 files changed

+70
-67
lines changed

libclc/clc/include/clc/clcmacro.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,39 +73,6 @@
7373
FUNCTION(x.sf, y.sf)); \
7474
}
7575

76-
#define _CLC_V_S_V_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE, \
77-
ARG2_TYPE) \
78-
DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE x, ARG2_TYPE##2 y) { \
79-
return (RET_TYPE##2)(FUNCTION(x, y.s0), FUNCTION(x, y.s1)); \
80-
} \
81-
\
82-
DECLSPEC RET_TYPE##3 FUNCTION(ARG1_TYPE x, ARG2_TYPE##3 y) { \
83-
return (RET_TYPE##3)(FUNCTION(x, y.s0), FUNCTION(x, y.s1), \
84-
FUNCTION(x, y.s2)); \
85-
} \
86-
\
87-
DECLSPEC RET_TYPE##4 FUNCTION(ARG1_TYPE x, ARG2_TYPE##4 y) { \
88-
return (RET_TYPE##4)(FUNCTION(x, y.s0), FUNCTION(x, y.s1), \
89-
FUNCTION(x, y.s2), FUNCTION(x, y.s3)); \
90-
} \
91-
\
92-
DECLSPEC RET_TYPE##8 FUNCTION(ARG1_TYPE x, ARG2_TYPE##8 y) { \
93-
return (RET_TYPE##8)(FUNCTION(x, y.s0), FUNCTION(x, y.s1), \
94-
FUNCTION(x, y.s2), FUNCTION(x, y.s3), \
95-
FUNCTION(x, y.s4), FUNCTION(x, y.s5), \
96-
FUNCTION(x, y.s6), FUNCTION(x, y.s7)); \
97-
} \
98-
\
99-
DECLSPEC RET_TYPE##16 FUNCTION(ARG1_TYPE x, ARG2_TYPE##16 y) { \
100-
return (RET_TYPE##16)( \
101-
FUNCTION(x, y.s0), FUNCTION(x, y.s1), FUNCTION(x, y.s2), \
102-
FUNCTION(x, y.s3), FUNCTION(x, y.s4), FUNCTION(x, y.s5), \
103-
FUNCTION(x, y.s6), FUNCTION(x, y.s7), FUNCTION(x, y.s8), \
104-
FUNCTION(x, y.s9), FUNCTION(x, y.sa), FUNCTION(x, y.sb), \
105-
FUNCTION(x, y.sc), FUNCTION(x, y.sd), FUNCTION(x, y.se), \
106-
FUNCTION(x, y.sf)); \
107-
}
108-
10976
#define _CLC_TERNARY_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE, \
11077
ARG2_TYPE, ARG3_TYPE) \
11178
DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x, ARG2_TYPE##2 y, \
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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_COMMON_CLC_STEP_H__
10+
#define __CLC_COMMON_CLC_STEP_H__
11+
12+
#define __CLC_FUNCTION __clc_step
13+
#define __CLC_BODY <clc/shared/binary_decl.inc>
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_FUNCTION
18+
19+
#endif // __CLC_COMMON_CLC_STEP_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ common/clc_degrees.cl
22
common/clc_radians.cl
33
common/clc_sign.cl
44
common/clc_smoothstep.cl
5+
common/clc_step.cl
56
geometric/clc_cross.cl
67
geometric/clc_distance.cl
78
geometric/clc_dot.cl
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
11+
#define __CLC_BODY <clc_step.inc>
12+
#include <clc/math/gentype.inc>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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_DEF __CLC_GENTYPE __clc_step(__CLC_GENTYPE edge,
10+
__CLC_GENTYPE x) {
11+
return x < edge ? __CLC_FP_LIT(0.0) : __CLC_FP_LIT(1.0);
12+
}

libclc/opencl/include/clc/opencl/common/step.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(__CLC_GENTYPE edge, __CLC_GENTYPE x);
10-
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(float edge, __CLC_GENTYPE x);
1110

12-
#ifdef cl_khr_fp64
13-
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(double edge, __CLC_GENTYPE x);
11+
#ifndef __CLC_SCALAR
12+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(__CLC_SCALAR_GENTYPE edge,
13+
__CLC_GENTYPE x);
1414
#endif

libclc/opencl/lib/generic/common/step.cl

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <clc/clcmacro.h>
9+
#include <clc/common/clc_step.h>
1010
#include <clc/opencl/clc.h>
1111

12-
_CLC_OVERLOAD _CLC_DEF float step(float edge, float x) {
13-
return x < edge ? 0.0f : 1.0f;
14-
}
15-
16-
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, float, float);
17-
18-
_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, float, float);
19-
20-
#ifdef cl_khr_fp64
21-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
22-
23-
#define STEP_DEF(edge_type, x_type) \
24-
_CLC_OVERLOAD _CLC_DEF x_type step(edge_type edge, x_type x) { \
25-
return x < edge ? 0.0 : 1.0; \
26-
}
27-
28-
STEP_DEF(double, double);
29-
30-
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
31-
_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
32-
33-
#if !defined(CLC_SPIRV)
34-
STEP_DEF(float, double);
35-
STEP_DEF(double, float);
36-
37-
_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, float, double);
38-
_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, double, float);
39-
#endif
40-
41-
#endif
12+
#define __CLC_BODY <step.inc>
13+
#include <clc/math/gentype.inc>
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+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE step(__CLC_GENTYPE edge, __CLC_GENTYPE x) {
10+
return __clc_step(edge, x);
11+
}
12+
13+
#if !defined(__CLC_SCALAR)
14+
15+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE step(__CLC_SCALAR_GENTYPE edge,
16+
__CLC_GENTYPE x) {
17+
return __clc_step((__CLC_GENTYPE)edge, x);
18+
}
19+
20+
#endif

0 commit comments

Comments
 (0)