Skip to content

Commit 25af075

Browse files
authored
[libspirv] Reuse clc function in several generic geometric built-ins (intel#19337)
llvm-diff shows fmul+fadd are replaced with llvm.fmuladd in libspirv-nvptx64--nvidiacl.bc and libspirv-amdgcn--amdhsa.bc.
1 parent e79e1b7 commit 25af075

File tree

12 files changed

+107
-186
lines changed

12 files changed

+107
-186
lines changed

libclc/libspirv/lib/generic/geometric/cross.cl

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

9+
#include <clc/geometric/clc_cross.h>
910
#include <libspirv/spirv.h>
1011

11-
_CLC_OVERLOAD _CLC_DEF float3 __spirv_ocl_cross(float3 p0, float3 p1) {
12-
return (float3)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
13-
p0.x * p1.y - p0.y * p1.x);
14-
}
15-
16-
_CLC_OVERLOAD _CLC_DEF float4 __spirv_ocl_cross(float4 p0, float4 p1) {
17-
return (float4)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
18-
p0.x * p1.y - p0.y * p1.x, 0.f);
19-
}
20-
21-
#ifdef cl_khr_fp64
22-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
23-
24-
_CLC_OVERLOAD _CLC_DEF double3 __spirv_ocl_cross(double3 p0, double3 p1) {
25-
return (double3)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
26-
p0.x * p1.y - p0.y * p1.x);
27-
}
28-
29-
_CLC_OVERLOAD _CLC_DEF double4 __spirv_ocl_cross(double4 p0, double4 p1) {
30-
return (double4)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
31-
p0.x * p1.y - p0.y * p1.x, 0.f);
32-
}
33-
#endif
34-
35-
#ifdef cl_khr_fp16
36-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
37-
38-
_CLC_OVERLOAD _CLC_DEF half3 __spirv_ocl_cross(half3 p0, half3 p1) {
39-
return (half3)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
40-
p0.x * p1.y - p0.y * p1.x);
41-
}
42-
43-
_CLC_OVERLOAD _CLC_DEF half4 __spirv_ocl_cross(half4 p0, half4 p1) {
44-
return (half4)(p0.y * p1.z - p0.z * p1.y, p0.z * p1.x - p0.x * p1.z,
45-
p0.x * p1.y - p0.y * p1.x, 0.f);
46-
}
47-
#endif
12+
#define __CLC_BODY <cross.inc>
13+
#include <clc/math/gentype.inc>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 == 3 || __CLC_VECSIZE_OR_1 == 4
10+
11+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_cross(__CLC_GENTYPE p0,
12+
__CLC_GENTYPE p1) {
13+
return __clc_cross(p0, p1);
14+
}
15+
16+
#endif

libclc/libspirv/lib/generic/geometric/distance.cl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/geometric/clc_distance.h>
910
#include <libspirv/spirv.h>
1011

1112
#define __CLC_BODY <distance.inc>
12-
#include <libspirv/generic/math/floatn.inc>
13+
#include <clc/math/gentype.inc>

libclc/libspirv/lib/generic/geometric/distance.inc

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

9-
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT __spirv_ocl_distance(__CLC_FLOATN p0,
10-
__CLC_FLOATN p1) {
11-
return __spirv_ocl_length(p0 - p1);
9+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
10+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
13+
__spirv_ocl_distance(__CLC_GENTYPE p0, __CLC_GENTYPE p1) {
14+
return __clc_distance(p0, p1);
1215
}
16+
17+
#endif

libclc/libspirv/lib/generic/geometric/dot.cl

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

9-
#include <clc/clcmacro.h>
9+
#include <clc/geometric/clc_dot.h>
1010
#include <libspirv/spirv.h>
1111

12-
#define _CLC_GEN_DOT(DECLSPEC, TYPE) \
13-
DECLSPEC static TYPE __spirv_Dot(TYPE x, TYPE y) { return x * y; } \
14-
DECLSPEC TYPE __spirv_Dot(TYPE##2 x, TYPE##2 y) { \
15-
return __spirv_Dot(x.x, y.x) + __spirv_Dot(x.y, y.y); \
16-
} \
17-
\
18-
DECLSPEC TYPE __spirv_Dot(TYPE##3 x, TYPE##3 y) { \
19-
return __spirv_Dot(x.x, y.x) + __spirv_Dot(x.y, y.y) + \
20-
__spirv_Dot(x.z, y.z); \
21-
} \
22-
\
23-
DECLSPEC TYPE __spirv_Dot(TYPE##4 x, TYPE##4 y) { \
24-
return __spirv_Dot(x.lo, y.lo) + __spirv_Dot(x.hi, y.hi); \
25-
} \
26-
\
27-
DECLSPEC TYPE __spirv_Dot(TYPE##8 x, TYPE##8 y) { \
28-
return __spirv_Dot(x.lo, y.lo) + __spirv_Dot(x.hi, y.hi); \
29-
} \
30-
\
31-
DECLSPEC TYPE __spirv_Dot(TYPE##16 x, TYPE##16 y) { \
32-
return __spirv_Dot(x.lo, y.lo) + __spirv_Dot(x.hi, y.hi); \
33-
}
34-
35-
_CLC_GEN_DOT(_CLC_OVERLOAD _CLC_DEF, float)
36-
37-
#ifdef cl_khr_fp64
38-
39-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
40-
41-
_CLC_GEN_DOT(_CLC_OVERLOAD _CLC_DEF, double)
42-
43-
#endif
44-
45-
#ifdef cl_khr_fp16
46-
47-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
48-
49-
_CLC_GEN_DOT(_CLC_OVERLOAD _CLC_DEF, half)
50-
51-
#endif
12+
#define __CLC_BODY <dot.inc>
13+
#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 >= 8
10+
11+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __spirv_Dot(__CLC_GENTYPE p0,
12+
__CLC_GENTYPE p1) {
13+
return __spirv_Dot(p0.lo, p1.lo) + __spirv_Dot(p0.hi, p1.hi);
14+
}
15+
16+
#elif __CLC_VECSIZE_OR_1 >= 2
17+
18+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __spirv_Dot(__CLC_GENTYPE p0,
19+
__CLC_GENTYPE p1) {
20+
return __clc_dot(p0, p1);
21+
}
22+
23+
#endif

libclc/libspirv/lib/generic/geometric/fast_distance.cl

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

9+
#include <clc/geometric/clc_fast_distance.h>
910
#include <libspirv/spirv.h>
1011

11-
#define __CLC_BODY <fast_distance.inc>
1212
#define __FLOAT_ONLY
13-
#include <libspirv/generic/math/floatn.inc>
14-
#undef __FLOAT_ONLY
13+
#define __CLC_BODY <fast_distance.inc>
14+
#include <clc/math/gentype.inc>

libclc/libspirv/lib/generic/geometric/fast_distance.inc

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

9-
_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT __spirv_ocl_fast_distance(__CLC_FLOATN p0,
10-
__CLC_FLOATN p1) {
11-
return __spirv_ocl_fast_length(p0 - p1);
9+
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
10+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
13+
__spirv_ocl_fast_distance(__CLC_GENTYPE p0, __CLC_GENTYPE p1) {
14+
return __clc_fast_distance(p0, p1);
1215
}
16+
17+
#endif

libclc/libspirv/lib/generic/geometric/fast_length.cl

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

9+
#include <clc/geometric/clc_fast_length.h>
910
#include <libspirv/spirv.h>
1011

11-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_fast_length(float p) {
12-
return __spirv_ocl_fabs(p);
13-
}
14-
15-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_fast_length(float2 p) {
16-
return __spirv_ocl_half_sqrt(__spirv_Dot(p, p));
17-
}
18-
19-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_fast_length(float3 p) {
20-
return __spirv_ocl_half_sqrt(__spirv_Dot(p, p));
21-
}
22-
23-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_fast_length(float4 p) {
24-
return __spirv_ocl_half_sqrt(__spirv_Dot(p, p));
25-
}
12+
#define __FLOAT_ONLY
13+
#define __CLC_BODY <fast_length.inc>
14+
#include <clc/math/gentype.inc>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 || __CLC_VECSIZE_OR_1 == 2 || \
10+
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
13+
__spirv_ocl_fast_length(__CLC_GENTYPE p) {
14+
return __clc_fast_length(p);
15+
}
16+
17+
#endif

0 commit comments

Comments
 (0)