[libclc] Add generic implementation of bitfield_insert/extract,bit_reverse (#149070)

The implementation is based on reference implementation in
OpenCL-CTS/test_integer_ops. The generic implementations pass
OpenCL-CTS/test_integer_ops tests on Intel GPU.
This commit is contained in:
Wenju He 2025-07-18 08:06:29 +08:00 committed by GitHub
parent 8813fc07f2
commit c0294f497d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 426 additions and 0 deletions

View File

@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_INTEGER_CLC_BIT_REVERSE_H__
#define __CLC_INTEGER_CLC_BIT_REVERSE_H__
#define FUNCTION __clc_bit_reverse
#define __CLC_BODY <clc/shared/unary_decl.inc>
#include <clc/integer/gentype.inc>
#undef FUNCTION
#endif // __CLC_INTEGER_CLC_BIT_REVERSE_H__

View File

@ -0,0 +1,10 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __RETTYPE FUNCTION(__CLC_GENTYPE base, uint offset,
uint count);

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__
#define __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__
#include <clc/internal/clc.h>
#define FUNCTION __clc_bitfield_extract_signed
#define __RETTYPE __CLC_S_GENTYPE
#define __CLC_BODY <clc/integer/clc_bitfield_extract_decl.inc>
#include <clc/integer/gentype.inc>
#undef __RETTYPE
#undef FUNCTION
#endif // __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__
#define __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__
#include <clc/internal/clc.h>
#define FUNCTION __clc_bitfield_extract_unsigned
#define __RETTYPE __CLC_U_GENTYPE
#define __CLC_BODY <clc/integer/clc_bitfield_extract_decl.inc>
#include <clc/integer/gentype.inc>
#undef __RETTYPE
#undef FUNCTION
#endif // __CLC_INTEGER_CLC_BITFIELD_EXTRACT_SIGNED_H__

View File

@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_INTEGER_CLC_BITFIELD_INSERT_H__
#define __CLC_INTEGER_CLC_BITFIELD_INSERT_H__
#include <clc/internal/clc.h>
#define FUNCTION __clc_bitfield_insert
#define __CLC_BODY <clc/integer/clc_bitfield_insert.inc>
#include <clc/integer/gentype.inc>
#endif // __CLC_INTEGER_CLC_BITFIELD_INSERT_H__

View File

@ -0,0 +1,11 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE FUNCTION(__CLC_GENTYPE base,
__CLC_GENTYPE insert,
uint offset, uint count);

View File

@ -15,6 +15,10 @@ geometric/clc_normalize.cl
integer/clc_abs.cl
integer/clc_abs_diff.cl
integer/clc_add_sat.cl
integer/clc_bitfield_extract_signed.cl
integer/clc_bitfield_extract_unsigned.cl
integer/clc_bitfield_insert.cl
integer/clc_bit_reverse.cl
integer/clc_clz.cl
integer/clc_ctz.cl
integer/clc_hadd.cl

View File

@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/integer/clc_bit_reverse.h>
#define FUNCTION __clc_bit_reverse
#define __IMPL_FUNCTION(x) __builtin_elementwise_bitreverse
#define __CLC_BODY <clc/shared/unary_def.inc>
#include <clc/integer/gentype.inc>

View File

@ -0,0 +1,12 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/integer/clc_bitfield_extract_signed.h>
#define __CLC_BODY <clc_bitfield_extract_signed.inc>
#include <clc/integer/gentype.inc>

View File

@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_S_GENTYPE
__clc_bitfield_extract_signed(__CLC_GENTYPE base, uint offset, uint count) {
if (count == 0)
return 0;
__CLC_U_GENTYPE x = __CLC_AS_U_GENTYPE(base)
<< (__CLC_GENSIZE - offset - count);
// Implement an arithmetic shift right.
__CLC_U_GENTYPE s = -(x >> (__CLC_GENSIZE - 1));
__CLC_U_GENTYPE result = ((s ^ x) >> (__CLC_GENSIZE - count)) ^ s;
return __CLC_AS_S_GENTYPE(result);
}

View File

@ -0,0 +1,12 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/integer/clc_bitfield_extract_unsigned.h>
#define __CLC_BODY <clc_bitfield_extract_unsigned.inc>
#include <clc/integer/gentype.inc>

View File

@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_U_GENTYPE
__clc_bitfield_extract_unsigned(__CLC_GENTYPE base, uint offset, uint count) {
if (count == 0)
return 0;
__CLC_U_GENTYPE result = __CLC_AS_U_GENTYPE(base)
<< (__CLC_GENSIZE - offset - count);
return result >> (__CLC_GENSIZE - count);
}

View File

@ -0,0 +1,12 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/integer/clc_bitfield_insert.h>
#define __CLC_BODY <clc_bitfield_insert.inc>
#include <clc/integer/gentype.inc>

View File

@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_bitfield_insert(__CLC_GENTYPE base,
__CLC_GENTYPE insert,
uint offset,
uint count) {
__CLC_U_GENTYPE u_base = __CLC_AS_U_GENTYPE(base);
__CLC_U_GENTYPE u_insert = __CLC_AS_U_GENTYPE(insert);
__CLC_U_GENTYPE mask = (((__CLC_U_GENTYPE)1 << count) - (__CLC_U_GENTYPE)1)
<< offset;
mask = count < __CLC_GENSIZE ? mask : ~(__CLC_U_GENTYPE)0;
__CLC_U_GENTYPE result = ((u_insert << offset) & mask) | (u_base & ~mask);
return __CLC_AS_GENTYPE(result);
}

View File

@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_OPENCL_INTEGER_BIT_REVERSE_H__
#define __CLC_OPENCL_INTEGER_BIT_REVERSE_H__
#ifdef cl_khr_extended_bit_ops
#include <clc/opencl/opencl-base.h>
#define FUNCTION bit_reverse
#define __CLC_BODY <clc/shared/unary_decl.inc>
#include <clc/integer/gentype.inc>
#undef FUNCTION
#endif // cl_khr_extended_bit_ops
#endif // __CLC_OPENCL_INTEGER_BIT_REVERSE_H__

View File

@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_SIGNED_H__
#define __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_SIGNED_H__
#ifdef cl_khr_extended_bit_ops
#include <clc/opencl/opencl-base.h>
#define FUNCTION bitfield_extract_signed
#define __RETTYPE __CLC_S_GENTYPE
#define __CLC_BODY <clc/integer/clc_bitfield_extract_decl.inc>
#include <clc/integer/gentype.inc>
#undef __RETTYPE
#undef FUNCTION
#endif // cl_khr_extended_bit_ops
#endif // __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_SIGNED_H__

View File

@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_UNSIGNED_H__
#define __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_UNSIGNED_H__
#ifdef cl_khr_extended_bit_ops
#include <clc/opencl/opencl-base.h>
#define FUNCTION bitfield_extract_unsigned
#define __RETTYPE __CLC_U_GENTYPE
#define __CLC_BODY <clc/integer/clc_bitfield_extract_decl.inc>
#include <clc/integer/gentype.inc>
#undef __RETTYPE
#undef FUNCTION
#endif // cl_khr_extended_bit_ops
#endif // __CLC_OPENCL_INTEGER_BITFIELD_EXTRACT_UNSIGNED_H__

View File

@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_OPENCL_INTEGER_BITFIELD_INSERT_H__
#define __CLC_OPENCL_INTEGER_BITFIELD_INSERT_H__
#ifdef cl_khr_extended_bit_ops
#include <clc/opencl/opencl-base.h>
#define __CLC_BODY <clc/integer/clc_bitfield_insert.inc>
#include <clc/integer/gentype.inc>
#undef FUNCTION
#endif // cl_khr_extended_bit_ops
#endif // __CLC_OPENCL_INTEGER_BITFIELD_INSERT_H__

View File

@ -43,6 +43,10 @@ geometric/normalize.cl
integer/abs.cl
integer/abs_diff.cl
integer/add_sat.cl
integer/bitfield_extract_signed.cl
integer/bitfield_extract_unsigned.cl
integer/bitfield_insert.cl
integer/bit_reverse.cl
integer/clz.cl
integer/ctz.cl
integer/hadd.cl

View File

@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifdef cl_khr_extended_bit_ops
#include <clc/integer/clc_bit_reverse.h>
#include <clc/opencl/integer/bit_reverse.h>
#define FUNCTION bit_reverse
#define __CLC_BODY <clc/shared/unary_def.inc>
#include <clc/integer/gentype.inc>
#endif // cl_khr_extended_bit_ops

View File

@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __IMPL_FUNCTION
#define __IMPL_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif
_CLC_OVERLOAD _CLC_DEF __RETTYPE FUNCTION(__CLC_GENTYPE base, uint offset,
uint count) {
return __IMPL_FUNCTION(FUNCTION)(base, offset, count);
}

View File

@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifdef cl_khr_extended_bit_ops
#include <clc/integer/clc_bitfield_extract_signed.h>
#include <clc/opencl/integer/bitfield_extract_signed.h>
#define FUNCTION bitfield_extract_signed
#define __RETTYPE __CLC_S_GENTYPE
#define __CLC_BODY <bitfield_extract_def.inc>
#include <clc/integer/gentype.inc>
#endif // cl_khr_extended_bit_ops

View File

@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifdef cl_khr_extended_bit_ops
#include <clc/integer/clc_bitfield_extract_unsigned.h>
#include <clc/opencl/integer/bitfield_extract_unsigned.h>
#define FUNCTION bitfield_extract_unsigned
#define __RETTYPE __CLC_U_GENTYPE
#define __CLC_BODY <bitfield_extract_def.inc>
#include <clc/integer/gentype.inc>
#endif // cl_khr_extended_bit_ops

View File

@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifdef cl_khr_extended_bit_ops
#include <clc/integer/clc_bitfield_insert.h>
#include <clc/opencl/integer/bitfield_insert.h>
#define FUNCTION bitfield_insert
#define __CLC_BODY <clc/integer/clc_bitfield_insert.inc>
#include <clc/integer/gentype.inc>
#endif // cl_khr_extended_bit_ops

View File

@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE bitfield_insert(__CLC_GENTYPE base,
__CLC_GENTYPE insert,
uint offset, uint count) {
return __clc_bitfield_insert(base, insert, offset, count);
}