Skip to content

Commit 25af075

Browse files
authored
[libspirv] Reuse clc function in several generic geometric built-ins (#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

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

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

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

11-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_length(float p) {
12-
return __spirv_ocl_fabs(p);
13-
}
14-
15-
#define V_FLENGTH(p) \
16-
float l2 = __spirv_Dot(p, p); \
17-
\
18-
if (l2 < FLT_MIN) { \
19-
p *= 0x1.0p+86F; \
20-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p-86F; \
21-
} else if (l2 == INFINITY) { \
22-
p *= 0x1.0p-65F; \
23-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p+65F; \
24-
} \
25-
\
26-
return __spirv_ocl_sqrt(l2);
27-
28-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_length(float2 p) { V_FLENGTH(p); }
29-
30-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_length(float3 p) { V_FLENGTH(p); }
31-
32-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_length(float4 p) { V_FLENGTH(p); }
33-
34-
#ifdef cl_khr_fp64
35-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
36-
37-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_length(double p) {
38-
return __spirv_ocl_fabs(p);
39-
}
40-
41-
#define V_DLENGTH(p) \
42-
double l2 = __spirv_Dot(p, p); \
43-
\
44-
if (l2 < DBL_MIN) { \
45-
p *= 0x1.0p+563; \
46-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p-563; \
47-
} else if (l2 == INFINITY) { \
48-
p *= 0x1.0p-513; \
49-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p+513; \
50-
} \
51-
\
52-
return __spirv_ocl_sqrt(l2);
53-
54-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_length(double2 p) { V_DLENGTH(p); }
55-
56-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_length(double3 p) { V_DLENGTH(p); }
57-
58-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_length(double4 p) { V_DLENGTH(p); }
59-
60-
#endif
61-
62-
#ifdef cl_khr_fp16
63-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
64-
65-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_length(half p) {
66-
return __spirv_ocl_fabs(p);
67-
}
68-
69-
// Only available in CLC1.2
70-
#ifndef HALF_MIN
71-
#define HALF_MIN 0x1.0p-14h
72-
#endif
73-
74-
#define V_HLENGTH(p) \
75-
half l2 = __spirv_Dot(p, p); \
76-
\
77-
if (l2 < HALF_MIN) { \
78-
p *= 0x1.0p+12h; \
79-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p-12h; \
80-
} else if (l2 == INFINITY) { \
81-
p *= 0x1.0p-7h; \
82-
return __spirv_ocl_sqrt(__spirv_Dot(p, p)) * 0x1.0p+7h; \
83-
} \
84-
\
85-
return __spirv_ocl_sqrt(l2);
86-
87-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_length(half2 p) { V_HLENGTH(p); }
88-
89-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_length(half3 p) { V_HLENGTH(p); }
90-
91-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_length(half4 p) { V_HLENGTH(p); }
92-
93-
#endif
12+
#define __CLC_BODY <length.inc>
13+
#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_length(__CLC_GENTYPE p) {
14+
return __clc_length(p);
15+
}
16+
17+
#endif

0 commit comments

Comments
 (0)