Skip to content

Commit 76d1cb2

Browse files
authored
[libclc] Move rotate to CLC library; optimize (llvm#125713)
This commit moves the rotate builtin to the CLC library. It also optimizes rotate(x, n) to generate the @llvm.fshl(x, x, n) intrinsic, for both scalar and vector types. The previous implementation was too cautious in its handling of the shift amount; the OpenCL rules state that the shift amount is always treated as an unsigned value modulo the bitwidth.
1 parent 6c84d64 commit 76d1cb2

File tree

8 files changed

+46
-43
lines changed

8 files changed

+46
-43
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_INTEGER_CLC_ROTATE_H__
2+
#define __CLC_INTEGER_CLC_ROTATE_H__
3+
4+
#define __CLC_FUNCTION __clc_rotate
5+
#define __CLC_BODY <clc/shared/binary_decl.inc>
6+
7+
#include <clc/integer/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_INTEGER_CLC_ROTATE_H__

libclc/clc/lib/clspv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
../generic/integer/clc_mul_hi.cl
88
../generic/integer/clc_popcount.cl
99
../generic/integer/clc_rhadd.cl
10+
../generic/integer/clc_rotate.cl
1011
../generic/integer/clc_sub_sat.cl
1112
../generic/integer/clc_upsample.cl
1213
../generic/math/clc_ceil.cl

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ integer/clc_mul24.cl
1313
integer/clc_mul_hi.cl
1414
integer/clc_popcount.cl
1515
integer/clc_rhadd.cl
16+
integer/clc_rotate.cl
1617
integer/clc_sub_sat.cl
1718
integer/clc_upsample.cl
1819
math/clc_ceil.cl
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <clc/internal/clc.h>
2+
#include <clc/utils.h>
3+
4+
#define __CLC_BODY <clc_rotate.inc>
5+
#include <clc/integer/gentype.inc>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#define __CLC_AS_GENTYPE(x) __CLC_XCONCAT(__clc_as_, __CLC_GENTYPE)(x)
2+
#define __CLC_AS_U_GENTYPE(x) __CLC_XCONCAT(__clc_as_, __CLC_U_GENTYPE)(x)
3+
4+
// The rotate(A, B) builtin left-shifts corresponding to the usual OpenCL shift
5+
// modulo rules. These rules state that A is left-shifted by the log2(N) least
6+
// significant bits in B when viewed as an unsigned integer value. Thus we don't
7+
// have to worry about signed shift amounts, and can perform the computation in
8+
// unsigned types.
9+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_rotate(__CLC_GENTYPE x,
10+
__CLC_GENTYPE n) {
11+
__CLC_U_GENTYPE x_as_u = __CLC_AS_U_GENTYPE(x);
12+
__CLC_U_GENTYPE mask = (__CLC_U_GENTYPE)(__CLC_GENSIZE - 1);
13+
14+
__CLC_U_GENTYPE lshift_amt = __CLC_AS_U_GENTYPE(n) & mask;
15+
16+
__CLC_U_GENTYPE rshift_amt =
17+
(((__CLC_U_GENTYPE)__CLC_GENSIZE - lshift_amt) & mask);
18+
19+
__CLC_U_GENTYPE result = (x_as_u << lshift_amt) | (x_as_u >> rshift_amt);
20+
21+
return __CLC_AS_GENTYPE(result);
22+
}

libclc/clc/lib/spirv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
../generic/integer/clc_mul_hi.cl
1212
../generic/integer/clc_popcount.cl
1313
../generic/integer/clc_rhadd.cl
14+
../generic/integer/clc_rotate.cl
1415
../generic/integer/clc_sub_sat.cl
1516
../generic/integer/clc_upsample.cl
1617
../generic/math/clc_ceil.cl

libclc/generic/lib/integer/rotate.cl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <clc/clc.h>
2+
#include <clc/integer/clc_rotate.h>
3+
4+
#define FUNCTION rotate
5+
#define __CLC_BODY <clc/shared/binary_def.inc>
26

3-
#define __CLC_BODY <rotate.inc>
47
#include <clc/integer/gentype.inc>

libclc/generic/lib/integer/rotate.inc

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

0 commit comments

Comments
 (0)