Skip to content

Commit 8460c10

Browse files
authored
[libspirv] Use clc unary relational functions (#22144)
Also cherry-picks llvm/llvm-project@9cc6c9375faa to avoid SignBitSet regression. IsNormal and SignBitSet are now vectorized. NFC to other built-ins. in function _Z16__spirv_IsNormalDv2_f: > %2 = extractelement <2 x float> %0, i64 0 > %3 = tail call noundef i1 @llvm.is.fpclass.f32(float %2, i32 264) > %4 = zext i1 %3 to i8 > %5 = insertelement <2 x i8> poison, i8 %4, i64 0 > %6 = extractelement <2 x float> %0, i64 1 > %7 = tail call noundef i1 @llvm.is.fpclass.f32(float %6, i32 264) > %8 = zext i1 %7 to i8 > %9 = insertelement <2 x i8> %5, i8 %8, i64 1 > %10 = icmp ne <2 x i8> %9, zeroinitializer > %11 = sext <2 x i1> %10 to <2 x i8> < %2 = tail call <2 x i1> @llvm.is.fpclass.v2f32(<2 x float> %0, i32 264) < %3 = sext <2 x i1> %2 to <2 x i8> in function _Z18__spirv_SignBitSetDv2_Dh: > %3 = extractelement <2 x i16> %2, i64 0 > %4 = lshr i16 %3, 15 > %5 = trunc nuw nsw i16 %4 to i8 > %6 = insertelement <2 x i8> poison, i8 %5, i64 0 > %7 = extractelement <2 x i16> %2, i64 1 > %8 = lshr i16 %7, 15 > %9 = trunc nuw nsw i16 %8 to i8 > %10 = insertelement <2 x i8> %6, i8 %9, i64 1 > %11 = icmp ne <2 x i8> %10, zeroinitializer > %12 = sext <2 x i1> %11 to <2 x i8> < %3 = ashr <2 x i16> %2, splat (i16 15) < %4 = trunc nsw <2 x i16> %3 to <2 x i8>
1 parent 3655fb6 commit 8460c10

11 files changed

Lines changed: 85 additions & 147 deletions

File tree

libclc/clc/include/clc/relational/clc_signbit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef __CLC_RELATIONAL_CLC_SIGNBIT_H__
1010
#define __CLC_RELATIONAL_CLC_SIGNBIT_H__
1111

12+
#include "clc/internal/clc.h"
13+
1214
#define __CLC_FUNCTION __clc_signbit
1315
#define __CLC_BODY "clc/relational/unary_decl.inc"
1416

libclc/clc/lib/generic/relational/clc_signbit.cl

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

9-
#include "clc/internal/clc.h"
10-
#include "clc/relational/relational.h"
9+
#include "clc/relational/clc_signbit.h"
1110

12-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC2(RET_TYPE, __CLC_FUNCTION, ARG_TYPE) \
13-
_CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { \
14-
return (RET_TYPE)((RET_TYPE){__CLC_FUNCTION(x.lo), \
15-
__CLC_FUNCTION(x.hi)} != (RET_TYPE)0); \
16-
}
17-
18-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC3(RET_TYPE, __CLC_FUNCTION, ARG_TYPE) \
19-
_CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { \
20-
return (RET_TYPE)((RET_TYPE){__CLC_FUNCTION(x.s0), __CLC_FUNCTION(x.s1), \
21-
__CLC_FUNCTION(x.s2)} != (RET_TYPE)0); \
22-
}
23-
24-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC4(RET_TYPE, __CLC_FUNCTION, ARG_TYPE) \
25-
_CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { \
26-
return (RET_TYPE)((RET_TYPE){__CLC_FUNCTION(x.s0), __CLC_FUNCTION(x.s1), \
27-
__CLC_FUNCTION(x.s2), \
28-
__CLC_FUNCTION(x.s3)} != (RET_TYPE)0); \
29-
}
30-
31-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC8(RET_TYPE, __CLC_FUNCTION, ARG_TYPE) \
32-
_CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { \
33-
return (RET_TYPE)((RET_TYPE){__CLC_FUNCTION(x.s0), __CLC_FUNCTION(x.s1), \
34-
__CLC_FUNCTION(x.s2), __CLC_FUNCTION(x.s3), \
35-
__CLC_FUNCTION(x.s4), __CLC_FUNCTION(x.s5), \
36-
__CLC_FUNCTION(x.s6), \
37-
__CLC_FUNCTION(x.s7)} != (RET_TYPE)0); \
38-
}
39-
40-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC16(RET_TYPE, __CLC_FUNCTION, ARG_TYPE) \
41-
_CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { \
42-
return (RET_TYPE)((RET_TYPE){__CLC_FUNCTION(x.s0), __CLC_FUNCTION(x.s1), \
43-
__CLC_FUNCTION(x.s2), __CLC_FUNCTION(x.s3), \
44-
__CLC_FUNCTION(x.s4), __CLC_FUNCTION(x.s5), \
45-
__CLC_FUNCTION(x.s6), __CLC_FUNCTION(x.s7), \
46-
__CLC_FUNCTION(x.s8), __CLC_FUNCTION(x.s9), \
47-
__CLC_FUNCTION(x.sa), __CLC_FUNCTION(x.sb), \
48-
__CLC_FUNCTION(x.sc), __CLC_FUNCTION(x.sd), \
49-
__CLC_FUNCTION(x.se), \
50-
__CLC_FUNCTION(x.sf)} != (RET_TYPE)0); \
51-
}
52-
53-
#define _CLC_DEFINE_RELATIONAL_UNARY_VEC_ALL(RET_TYPE, __CLC_FUNCTION, \
54-
ARG_TYPE) \
55-
_CLC_DEFINE_RELATIONAL_UNARY_VEC2(RET_TYPE##2, __CLC_FUNCTION, ARG_TYPE##2) \
56-
_CLC_DEFINE_RELATIONAL_UNARY_VEC3(RET_TYPE##3, __CLC_FUNCTION, ARG_TYPE##3) \
57-
_CLC_DEFINE_RELATIONAL_UNARY_VEC4(RET_TYPE##4, __CLC_FUNCTION, ARG_TYPE##4) \
58-
_CLC_DEFINE_RELATIONAL_UNARY_VEC8(RET_TYPE##8, __CLC_FUNCTION, ARG_TYPE##8) \
59-
_CLC_DEFINE_RELATIONAL_UNARY_VEC16(RET_TYPE##16, __CLC_FUNCTION, ARG_TYPE##16)
60-
61-
_CLC_DEF _CLC_OVERLOAD int __clc_signbit(float x) {
62-
return __builtin_signbitf(x);
63-
}
64-
65-
_CLC_DEFINE_RELATIONAL_UNARY_VEC_ALL(int, __clc_signbit, float)
66-
67-
#ifdef cl_khr_fp64
68-
69-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
70-
71-
// The scalar version of __clc_signbit(double) returns an int, but the vector
72-
// versions return long.
73-
74-
_CLC_DEF _CLC_OVERLOAD int __clc_signbit(double x) {
75-
return __builtin_signbit(x);
76-
}
77-
78-
_CLC_DEFINE_RELATIONAL_UNARY_VEC_ALL(long, __clc_signbit, double)
79-
80-
#endif
81-
#ifdef cl_khr_fp16
82-
83-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
84-
85-
// The scalar version of __clc_signbit(half) returns an int, but the vector
86-
// versions return short.
87-
88-
_CLC_DEF _CLC_OVERLOAD int __clc_signbit(half x) {
89-
return __builtin_signbit(x);
90-
}
91-
92-
_CLC_DEFINE_RELATIONAL_UNARY_VEC_ALL(short, __clc_signbit, half)
93-
94-
#endif
11+
#define __CLC_BODY "clc_signbit.inc"
12+
#include "clc/math/gentype.inc"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#if __CLC_VECSIZE_OR_1 == 1
10+
#define __CLC_RETTYPE __CLC_INTN
11+
#else
12+
#define __CLC_RETTYPE __CLC_S_GENTYPE
13+
#endif
14+
15+
_CLC_OVERLOAD _CLC_DEF __CLC_RETTYPE __clc_signbit(__CLC_GENTYPE x) {
16+
#if __CLC_VECSIZE_OR_1 == 1
17+
return (__CLC_INTN)(__CLC_AS_U_GENTYPE(x) >> (__CLC_FPSIZE - 1));
18+
#else
19+
return __CLC_AS_S_GENTYPE(x) >> (__CLC_FPSIZE - 1);
20+
#endif
21+
}
22+
23+
#undef __CLC_RETTYPE

libclc/libspirv/lib/generic/relational/genunary.inc

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

libclc/libspirv/lib/generic/relational/isfinite.cl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/relational.h>
10-
#include <libspirv/spirv.h>
9+
#include "clc/clc_convert.h"
10+
#include "clc/relational/clc_isfinite.h"
1111

12-
#define _CLC_SPIRV_BUILTIN __spirv_IsFinite
13-
#define _CLC_BUILTIN_IMPL __builtin_isfinite
14-
#include "genunary.inc"
12+
#define __CLC_FUNCTION __spirv_IsFinite
13+
#define __CLC_IMPL_FUNCTION(x) __clc_isfinite
14+
#define __CLC_BODY "relational_unary_def.inc"
15+
#include "clc/math/gentype.inc"

libclc/libspirv/lib/generic/relational/isinf.cl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/relational.h>
10-
#include <libspirv/spirv.h>
9+
#include "clc/clc_convert.h"
10+
#include "clc/relational/clc_isinf.h"
1111

12-
#define _CLC_SPIRV_BUILTIN __spirv_IsInf
13-
#define _CLC_BUILTIN_IMPL __builtin_isinf
14-
#include "genunary.inc"
12+
#define __CLC_FUNCTION __spirv_IsInf
13+
#define __CLC_IMPL_FUNCTION(x) __clc_isinf
14+
#define __CLC_BODY "relational_unary_def.inc"
15+
#include "clc/math/gentype.inc"

libclc/libspirv/lib/generic/relational/isnan.cl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/relational.h>
10-
#include <libspirv/spirv.h>
9+
#include "clc/clc_convert.h"
10+
#include "clc/relational/clc_isnan.h"
1111

12-
#define _CLC_SPIRV_BUILTIN __spirv_IsNan
13-
#define _CLC_BUILTIN_IMPL __builtin_isnan
14-
#include "genunary.inc"
12+
#define __CLC_FUNCTION __spirv_IsNan
13+
#define __CLC_IMPL_FUNCTION(x) __clc_isnan
14+
#define __CLC_BODY "relational_unary_def.inc"
15+
#include "clc/math/gentype.inc"

libclc/libspirv/lib/generic/relational/isnormal.cl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/relational.h>
10-
#include <libspirv/spirv.h>
9+
#include "clc/clc_convert.h"
10+
#include "clc/relational/clc_isnormal.h"
1111

12-
#define _CLC_SPIRV_BUILTIN __spirv_IsNormal
13-
#define _CLC_BUILTIN_IMPL __builtin_isnormal
14-
#include "genunary.inc"
12+
#define __CLC_FUNCTION __spirv_IsNormal
13+
#define __CLC_IMPL_FUNCTION(x) __clc_isnormal
14+
#define __CLC_BODY "relational_unary_def.inc"
15+
#include "clc/math/gentype.inc"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#if __CLC_VECSIZE_OR_1 == 1
12+
#define __CLC_RETTYPE bool
13+
#else
14+
#define __CLC_RETTYPE __CLC_CHARN
15+
#endif
16+
17+
_CLC_OVERLOAD _CLC_DEF __CLC_RETTYPE __CLC_FUNCTION(__CLC_GENTYPE x) {
18+
return __CLC_CONVERT_CHARN(__CLC_IMPL_FUNCTION(__CLC_FUNCTION)(x));
19+
}
20+
21+
#undef __CLC_RETTYPE

libclc/libspirv/lib/generic/relational/select.cl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/spirv.h>
10-
#include <clc/relational/clc_select.h>
11-
#include <clc/utils.h>
9+
#include "clc/relational/clc_select.h"
10+
#include "clc/utils.h"
1211

1312
#define __CLC_SELECT_FN __spirv_ocl_select
1413
#define __CLC_SELECT_DEF(x, y, z) return __clc_select(x, y, z)
1514

16-
#define __CLC_BODY <clc/relational/clc_select_impl.inc>
17-
#include <clc/math/gentype.inc>
18-
#define __CLC_BODY <clc/relational/clc_select_impl.inc>
19-
#include <clc/integer/gentype.inc>
15+
#define __CLC_BODY "clc/relational/clc_select_impl.inc"
16+
#include "clc/math/gentype.inc"
17+
#define __CLC_BODY "clc/relational/clc_select_impl.inc"
18+
#include "clc/integer/gentype.inc"

0 commit comments

Comments
 (0)