Skip to content

Commit

Permalink
[RISCV][Clang] Add C API header for bitmanip CORE-V intrinsics.
Browse files Browse the repository at this point in the history
This commit adds the C API header to serve as an interface for
the intrinsic functions provided with the CORE-V xcvbitmanip
extension.

The commit includes the addition of the header itself, the update of
cmake to support this header file and more to follow from RISC-V
and a clang CodeGen test that checks the integrity of the call to the
wrapper functions designed in the C API CORE-V bitmanip header.
  • Loading branch information
PaoloS02 committed Dec 28, 2023
1 parent 5c8a7fe commit 6007f6d
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
13 changes: 12 additions & 1 deletion clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ set(ppc_htm_files
htmxlintrin.h
)

set(riscv_files
riscv_corev_bitmanip.h
)

set(systemz_files
s390intrin.h
vecintrin.h
Expand Down Expand Up @@ -232,6 +236,7 @@ set(files
${opencl_files}
${ppc_files}
${ppc_htm_files}
${riscv_files}
${systemz_files}
${ve_files}
${x86_files}
Expand Down Expand Up @@ -415,7 +420,7 @@ add_header_target("hip-resource-headers" "${hip_files}")
add_header_target("mips-resource-headers" "${mips_msa_files}")
add_header_target("ppc-resource-headers" "${ppc_files};${ppc_wrapper_files}")
add_header_target("ppc-htm-resource-headers" "${ppc_htm_files}")
add_header_target("riscv-resource-headers" "${riscv_generated_files}")
add_header_target("riscv-resource-headers" "${riscv_generated_files};${riscv_files}")
add_header_target("systemz-resource-headers" "${systemz_files}")
add_header_target("ve-resource-headers" "${ve_files}")
add_header_target("webassembly-resource-headers" "${webassembly_files}")
Expand Down Expand Up @@ -532,6 +537,12 @@ install(
EXCLUDE_FROM_ALL
COMPONENT riscv-resource-headers)

install(
FILES ${riscv_files}
DESTINATION ${header_install_dir}
EXCLUDE_FROM_ALL
COMPONENT riscv-resource-headers)

install(
FILES ${systemz_files}
DESTINATION ${header_install_dir}
Expand Down
74 changes: 74 additions & 0 deletions clang/lib/Headers/riscv_corev_bitmanip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*===---- riscv_corev_bitmanip.h - CORE-V bit manipulation intrinsics ------===
*
* 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 __RISCV_COREV_BITMANIP_H
#define __RISCV_COREV_BITMANIP_H

#include <stdint.h>

#if defined(__cplusplus)
extern "C" {
#endif

#if defined(__riscv_xcvbitmanip)

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

static __inline__ long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_extract(long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_extract(a, range);
}

static __inline__ unsigned long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_extractu(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_extractu(a, range);
}

static __inline__ unsigned long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_insert(unsigned long a, uint16_t range,
unsigned long k) {
return __builtin_riscv_cv_bitmanip_insert(a, range, k);
}

static __inline__ unsigned long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_bclr(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_bclr(a, range);
}

static __inline__ unsigned long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_bset(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_bset(a, range);
}

static __inline__ uint8_t __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_ff1(unsigned long a) {
return __builtin_riscv_cv_bitmanip_ff1(a);
}

static __inline__ uint8_t __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_fl1(unsigned long a) {
return __builtin_riscv_cv_bitmanip_fl1(a);
}

static __inline__ uint8_t __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_clb(unsigned long a) {
return __builtin_riscv_cv_bitmanip_clb(a);
}

static __inline__ uint8_t __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_cnt(unsigned long a) {
return __builtin_riscv_cv_bitmanip_cnt(a);
}

static __inline__ unsigned long __DEFAULT_FN_ATTRS __riscv_cv_bitmanip_ror(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_bitmanip_ror(a, b);
}

#define __riscv_cv_bitmanip_bitrev(rs1, PTS, RADIX) \
(unsigned long) __builtin_riscv_cv_bitmanip_bitrev((unsigned long) (rs1), \
(const uint8_t) (PTS), (const uint8_t) (RADIX))

#endif // defined(__riscv_xcvbitmanip)

#if defined(__cplusplus)
}
#endif

#endif // define __RISCV_COREV_BITMANIP_H
102 changes: 102 additions & 0 deletions clang/test/CodeGen/RISCV/corev-intrinsics/bitmanip-c-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
// RUN: %clang_cc1 -triple riscv32 -target-feature +xcvbitmanip -emit-llvm %s -o - \
// RUN: | FileCheck %s

#include <stdint.h>
#include <riscv_corev_bitmanip.h>

// CHECK-LABEL: @test_extract
// CHECK: @llvm.riscv.cv.bitmanip.extract
uint32_t test_extract(uint32_t a) {
return __riscv_cv_bitmanip_extract(a, 0);
}

// CHECK-LABEL: @test_extractr
// CHECK: @llvm.riscv.cv.bitmanip.extract
uint32_t test_extractr(uint32_t a, uint16_t b) {
return __riscv_cv_bitmanip_extract(a, b);
}

// CHECK-LABEL: @test_extractu
// CHECK: @llvm.riscv.cv.bitmanip.extractu
uint32_t test_extractu(uint32_t a) {
return __riscv_cv_bitmanip_extractu(a, 0);
}

// CHECK-LABEL: @test_extractur
// CHECK: @llvm.riscv.cv.bitmanip.extractu
uint32_t test_extractur(uint32_t a, uint16_t b) {
return __riscv_cv_bitmanip_extractu(a, b);
}

// CHECK-LABEL: @test_insert
// CHECK: @llvm.riscv.cv.bitmanip.insert
uint32_t test_insert(uint32_t a, uint32_t c) {
return __riscv_cv_bitmanip_insert(a, 0, c);
}

// CHECK-LABEL: @test_insertr
// CHECK: @llvm.riscv.cv.bitmanip.insert
uint32_t test_insertr(uint32_t a, uint16_t b, uint32_t c) {
return __riscv_cv_bitmanip_insert(a, b, c);
}

// CHECK-LABEL: @test_bclr
// CHECK: @llvm.riscv.cv.bitmanip.bclr
uint32_t test_bclr(uint32_t a) {
return __riscv_cv_bitmanip_bclr(a, 0);
}

// CHECK-LABEL: @test_bclrr
// CHECK: @llvm.riscv.cv.bitmanip.bclr
uint32_t test_bclrr(uint32_t a, uint16_t b) {
return __riscv_cv_bitmanip_bclr(a, b);
}

// CHECK-LABEL: @test_bset
// CHECK: @llvm.riscv.cv.bitmanip.bset
uint32_t test_bset(uint32_t a) {
return __riscv_cv_bitmanip_bset(a, 0);
}

// CHECK-LABEL: @test_bsetr
// CHECK: @llvm.riscv.cv.bitmanip.bclr
uint32_t test_bsetr(uint32_t a, uint16_t b) {
return __riscv_cv_bitmanip_bclr(a, b);
}

// CHECK-LABEL: @test_ff1
// CHECK: @llvm.riscv.cv.bitmanip.ff1
uint32_t test_ff1(uint32_t a) {
return __riscv_cv_bitmanip_ff1(a);
}

// CHECK-LABEL: @test_fl1
// CHECK: @llvm.riscv.cv.bitmanip.fl1
uint32_t test_fl1(uint32_t a) {
return __riscv_cv_bitmanip_fl1(a);
}

// CHECK-LABEL: @test_clb
// CHECK: @llvm.riscv.cv.bitmanip.clb
uint32_t test_clb(uint32_t a) {
return __riscv_cv_bitmanip_clb(a);
}

// CHECK-LABEL: @test_cnt
// CHECK: @llvm.riscv.cv.bitmanip.cnt
uint32_t test_cnt(uint32_t a) {
return __riscv_cv_bitmanip_cnt(a);
}

// CHECK-LABEL: @test_ror
// CHECK: @llvm.riscv.cv.bitmanip.ror
uint32_t test_ror(uint32_t a, uint32_t b) {
return __riscv_cv_bitmanip_ror(a, b);
}

// CHECK-LABEL: @test_bitrev
// CHECK: @llvm.riscv.cv.bitmanip.bitrev
uint32_t test_bitrev(uint32_t a) {
return __riscv_cv_bitmanip_bitrev(a, 1, 2);
}

0 comments on commit 6007f6d

Please sign in to comment.