From ebc7d4fcd6822c9053078ae3a1473ed8452f39b7 Mon Sep 17 00:00:00 2001 From: Paolo Savini Date: Fri, 15 Dec 2023 16:33:18 +0000 Subject: [PATCH] [RISCV][Clang] Add C API header for mac CORE-V intrinsics. This commit adds the C API header to serve as an interface for the intrinsic functions provided with the CORE-V xcvmac extension. The commit includes the addition of the header itself and a clang CodeGen test that checks the integrity of the call to the wrapper functions designed in the C API CORE-V mac header. --- clang/lib/Headers/CMakeLists.txt | 1 + clang/lib/Headers/riscv_corev_mac.h | 109 +++++++++++++++++ .../RISCV/corev-intrinsics/mac-c-api.c | 114 ++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 clang/lib/Headers/riscv_corev_mac.h create mode 100644 clang/test/CodeGen/RISCV/corev-intrinsics/mac-c-api.c diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt index 126a4fd4fe7d..0fcd3cd8e4f2 100644 --- a/clang/lib/Headers/CMakeLists.txt +++ b/clang/lib/Headers/CMakeLists.txt @@ -96,6 +96,7 @@ set(riscv_files riscv_corev_alu.h riscv_corev_bitmanip.h riscv_corev_elw.h + riscv_corev_mac.h ) set(systemz_files diff --git a/clang/lib/Headers/riscv_corev_mac.h b/clang/lib/Headers/riscv_corev_mac.h new file mode 100644 index 000000000000..986316264760 --- /dev/null +++ b/clang/lib/Headers/riscv_corev_mac.h @@ -0,0 +1,109 @@ +/*===---- riscv_corev_mac.h - CORE-V multiply accumulate 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_MAC_H +#define __RISCV_COREV_MAC_H + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +#if defined(__riscv_xcvmac) + +#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__)) + +static __inline__ long __DEFAULT_FN_ATTRS __riscv_cv_mac_mac(long a, long b, long c) { + return __builtin_riscv_cv_mac_mac(a, b, c); +} + +static __inline__ long __DEFAULT_FN_ATTRS __riscv_cv_mac_msu(long a, long b, long c) { + return __builtin_riscv_cv_mac_msu(a, b, c); +} + +#define __riscv_cv_mac_muluN(rs1, rs2, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_muluN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulhhuN(rs1, rs2, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_mulhhuN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulsN(rs1, rs2, SHIFT) \ + (long) __builtin_riscv_cv_mac_mulsN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulhhsN(rs1, rs2, SHIFT) \ + (long) __builtin_riscv_cv_mac_mulhhsN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_muluRN(rs1, rs2, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_muluRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulhhuRN(rs1, rs2, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_mulhhuRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulsRN(rs1, rs2, SHIFT) \ + (long) __builtin_riscv_cv_mac_mulsRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_mulhhsRN(rs1, rs2, SHIFT) \ + (long) __builtin_riscv_cv_mac_mulhhsRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_macuN(rs1, rs2, rD, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_macuN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_machhuN(rs1, rs2, rD, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_machhuN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_macsN(rs1, rs2, rD, SHIFT) \ + (long) __builtin_riscv_cv_mac_macsN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_machhsN(rs1, rs2, rD, SHIFT) \ + (long) __builtin_riscv_cv_mac_machhsN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_macuRN(rs1, rs2, rD, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_macuRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_machhuRN(rs1, rs2, rD, SHIFT) \ + (unsigned long) __builtin_riscv_cv_mac_machhuRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_macsRN(rs1, rs2, rD, SHIFT) \ + (long) __builtin_riscv_cv_mac_macsRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#define __riscv_cv_mac_machhsRN(rs1, rs2, rD, SHIFT) \ + (long) __builtin_riscv_cv_mac_machhsRN((unsigned long) (rs1), \ + (unsigned long) (rs2), (unsigned long) (rD), \ + (const uint8_t) (SHIFT)) + +#endif // defined(__riscv_xcvmac) + +#if defined(__cplusplus) +} +#endif + +#endif // define __RISCV_COREV_MAC_H diff --git a/clang/test/CodeGen/RISCV/corev-intrinsics/mac-c-api.c b/clang/test/CodeGen/RISCV/corev-intrinsics/mac-c-api.c new file mode 100644 index 000000000000..c68859c9362c --- /dev/null +++ b/clang/test/CodeGen/RISCV/corev-intrinsics/mac-c-api.c @@ -0,0 +1,114 @@ +// RUN: %clang_cc1 -triple riscv32 -target-feature +xcvmac -emit-llvm %s -o - \ +// RUN: | FileCheck %s + +#include +#include + +// CHECK-LABEL: @test_mac_mac +// CHECK: @llvm.riscv.cv.mac.mac +int32_t test_mac_mac(int32_t x, int32_t y, int32_t z) { + return __riscv_cv_mac_mac(x, y, z); +} + +// CHECK-LABEL: @test_mac_msu +// CHECK: @llvm.riscv.cv.mac.msu +int32_t test_mac_msu(int32_t x, int32_t y, int32_t z) { + return __riscv_cv_mac_msu(x, y, z); +} + +// CHECK-LABEL: @test_mac_muluN +// CHECK: @llvm.riscv.cv.mac.muluN +uint32_t test_mac_muluN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_muluN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulhhuN +// CHECK: @llvm.riscv.cv.mac.mulhhuN +uint32_t test_mac_mulhhuN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulhhuN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulsN +// CHECK: @llvm.riscv.cv.mac.mulsN +int32_t test_mac_mulsN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulsN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulhhsN +// CHECK: @llvm.riscv.cv.mac.mulhhsN +int32_t test_mac_mulhhsN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulhhsN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_muluRN +// CHECK: @llvm.riscv.cv.mac.muluRN +uint32_t test_mac_muluRN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_muluRN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulhhuRN +// CHECK: @llvm.riscv.cv.mac.mulhhuRN +uint32_t test_mac_mulhhuRN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulhhuRN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulsRN +// CHECK: @llvm.riscv.cv.mac.mulsRN +int32_t test_mac_mulsRN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulsRN(x, y, 0); +} + +// CHECK-LABEL: @test_mac_mulhhsRN +// CHECK: @llvm.riscv.cv.mac.mulhhsRN +int32_t test_mac_mulhhsRN(uint32_t x, uint32_t y) { + return __riscv_cv_mac_mulhhsRN(x, y, 0); +} + +// 16-bit x 16-bit multiply-accumulate +// CHECK-LABEL: @test_mac_macuN +// CHECK: @llvm.riscv.cv.mac.macuN +uint32_t test_mac_macuN(uint32_t x, uint32_t y, uint32_t z) { + return __riscv_cv_mac_macuN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_machhuN +// CHECK: @llvm.riscv.cv.mac.machhuN +uint32_t test_mac_machhuN(uint32_t x, uint32_t y, uint32_t z) { + return __riscv_cv_mac_machhuN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_macsN +// CHECK: @llvm.riscv.cv.mac.macsN +int32_t test_mac_macsN(uint32_t x, uint32_t y, int32_t z) { + return __riscv_cv_mac_macsN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_machhsN +// CHECK: @llvm.riscv.cv.mac.machhsN +int32_t test_mac_machhsN(uint32_t x, uint32_t y, int32_t z) { + return __riscv_cv_mac_machhsN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_macuRN +// CHECK: @llvm.riscv.cv.mac.macuRN +uint32_t test_mac_macuRN(uint32_t x, uint32_t y, uint32_t z) { + return __riscv_cv_mac_macuRN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_machhuRN +// CHECK: @llvm.riscv.cv.mac.machhuRN +uint32_t test_mac_machhuRN(uint32_t x, uint32_t y, uint32_t z) { + return __riscv_cv_mac_machhuRN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_macsRN +// CHECK: @llvm.riscv.cv.mac.macsRN +int32_t test_mac_macsRN(uint32_t x, uint32_t y, int32_t z) { + return __riscv_cv_mac_macsRN(x, y, z, 0); +} + +// CHECK-LABEL: @test_mac_machhsRN +// CHECK: @llvm.riscv.cv.mac.machhsRN +int32_t test_mac_machhsRN(uint32_t x, uint32_t y, int32_t z) { + return __riscv_cv_mac_machhsRN(x, y, z, 0); +}