Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV] Implement Clang Builtins for XCVmac Extension in CV32E40P #110623

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 55 additions & 17 deletions clang/include/clang/Basic/BuiltinsRISCVXCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//
//===----------------------------------------------------------------------===//

class RISCXCVBuiltin<string prototype, string features = ""> : TargetBuiltin {
class RISCVXCVBuiltin<string prototype, string features = ""> : TargetBuiltin {
let Spellings = ["__builtin_riscv_cv_" # NAME];
let Prototype = prototype;
let Features = features;
Expand All @@ -21,21 +21,59 @@ let Attributes = [NoThrow, Const] in {
//===----------------------------------------------------------------------===//
// XCValu extension.
//===----------------------------------------------------------------------===//
def alu_slet : RISCXCVBuiltin<"int(int, int)", "xcvalu">;
def alu_sletu : RISCXCVBuiltin<"int(unsigned int, unsigned int)", "xcvalu">;
def alu_exths : RISCXCVBuiltin<"int(int)", "xcvalu">;
def alu_exthz : RISCXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
def alu_extbs : RISCXCVBuiltin<"int(int)", "xcvalu">;
def alu_extbz : RISCXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
def alu_slet : RISCVXCVBuiltin<"int(int, int)", "xcvalu">;
def alu_sletu : RISCVXCVBuiltin<"int(unsigned int, unsigned int)", "xcvalu">;
def alu_exths : RISCVXCVBuiltin<"int(int)", "xcvalu">;
def alu_exthz : RISCVXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
def alu_extbs : RISCVXCVBuiltin<"int(int)", "xcvalu">;
def alu_extbz : RISCVXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;

def alu_clip : RISCXCVBuiltin<"int(int, int)", "xcvalu">;
def alu_clipu : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int)", "xcvalu">;
def alu_addN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_adduN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_addRN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_adduRN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_subN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_subuN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_subRN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_subuRN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_clip : RISCVXCVBuiltin<"int(int, int)", "xcvalu">;
def alu_clipu : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int)", "xcvalu">;
def alu_addN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_adduN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_addRN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_adduRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_subN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_subuN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
def alu_subRN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
def alu_subuRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;

//===----------------------------------------------------------------------===//
// XCVmac extension.
//===----------------------------------------------------------------------===//
def mac_mac : RISCVXCVBuiltin<"int(int, int, int)", "xcvmac">;
def mac_msu : RISCVXCVBuiltin<"int(int, int, int)", "xcvmac">;
def mac_muluN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulhhuN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulsN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulhhsN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_muluRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulhhuRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulsRN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_mulhhsRN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_macuN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_machhuN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_macsN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_machhsN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_macuRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_machhuRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_macsRN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
def mac_machhsRN : RISCVXCVBuiltin<"int(unsigned int, unsigned int, unsigned int, _Constant unsigned int)",
"xcvmac">;
} // Attributes = [NoThrow, Const]
55 changes: 55 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22389,6 +22389,61 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
case RISCV::BI__builtin_riscv_cv_alu_subuRN:
ID = Intrinsic::riscv_cv_alu_subuRN;
break;
// XCVmac
case RISCV::BI__builtin_riscv_cv_mac_mac:
ID = Intrinsic::riscv_cv_mac_mac;
break;
case RISCV::BI__builtin_riscv_cv_mac_msu:
ID = Intrinsic::riscv_cv_mac_msu;
break;
case RISCV::BI__builtin_riscv_cv_mac_muluN:
ID = Intrinsic::riscv_cv_mac_muluN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulhhuN:
ID = Intrinsic::riscv_cv_mac_mulhhuN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulsN:
ID = Intrinsic::riscv_cv_mac_mulsN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulhhsN:
ID = Intrinsic::riscv_cv_mac_mulhhsN;
break;
case RISCV::BI__builtin_riscv_cv_mac_muluRN:
ID = Intrinsic::riscv_cv_mac_muluRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulhhuRN:
ID = Intrinsic::riscv_cv_mac_mulhhuRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulsRN:
ID = Intrinsic::riscv_cv_mac_mulsRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_mulhhsRN:
ID = Intrinsic::riscv_cv_mac_mulhhsRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_macuN:
ID = Intrinsic::riscv_cv_mac_macuN;
break;
case RISCV::BI__builtin_riscv_cv_mac_machhuN:
ID = Intrinsic::riscv_cv_mac_machhuN;
break;
case RISCV::BI__builtin_riscv_cv_mac_macsN:
ID = Intrinsic::riscv_cv_mac_macsN;
break;
case RISCV::BI__builtin_riscv_cv_mac_machhsN:
ID = Intrinsic::riscv_cv_mac_machhsN;
break;
case RISCV::BI__builtin_riscv_cv_mac_macuRN:
ID = Intrinsic::riscv_cv_mac_macuRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_machhuRN:
ID = Intrinsic::riscv_cv_mac_machhuRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_macsRN:
ID = Intrinsic::riscv_cv_mac_macsRN;
break;
case RISCV::BI__builtin_riscv_cv_mac_machhsRN:
ID = Intrinsic::riscv_cv_mac_machhsRN;
break;

// Vector builtins are handled from here.
#include "clang/Basic/riscv_vector_builtin_cg.inc"
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ set(ppc_htm_files

set(riscv_files
riscv_bitmanip.h
riscv_corev_alu.h
riscv_corev_alu.h
riscv_corev_mac.h
riscv_crypto.h
riscv_ntlh.h
sifive_vector.h
Expand Down
111 changes: 111 additions & 0 deletions clang/lib/Headers/riscv_corev_mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*===---- 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 <stdint.h>

#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,
realqhc marked this conversation as resolved.
Show resolved Hide resolved
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( \
realqhc marked this conversation as resolved.
Show resolved Hide resolved
(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
19 changes: 19 additions & 0 deletions clang/lib/Sema/SemaRISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,25 @@ bool SemaRISCV::CheckBuiltinFunctionCall(const TargetInfo &TI,
// Check if rnum is in [0, 10]
case RISCV::BI__builtin_riscv_aes64ks1i:
return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 10);
// Check if shift is in [0, 31]
case RISCV::BI__builtin_riscv_cv_mac_muluN:
case RISCV::BI__builtin_riscv_cv_mac_mulhhuN:
case RISCV::BI__builtin_riscv_cv_mac_mulsN:
case RISCV::BI__builtin_riscv_cv_mac_mulhhsN:
case RISCV::BI__builtin_riscv_cv_mac_muluRN:
case RISCV::BI__builtin_riscv_cv_mac_mulhhuRN:
case RISCV::BI__builtin_riscv_cv_mac_mulsRN:
case RISCV::BI__builtin_riscv_cv_mac_mulhhsRN:
return SemaRef.BuiltinConstantArgRange(TheCall, 2, 0, 31);
case RISCV::BI__builtin_riscv_cv_mac_macuN:
case RISCV::BI__builtin_riscv_cv_mac_machhuN:
case RISCV::BI__builtin_riscv_cv_mac_macsN:
case RISCV::BI__builtin_riscv_cv_mac_machhsN:
case RISCV::BI__builtin_riscv_cv_mac_macuRN:
case RISCV::BI__builtin_riscv_cv_mac_machhuRN:
case RISCV::BI__builtin_riscv_cv_mac_macsRN:
case RISCV::BI__builtin_riscv_cv_mac_machhsRN:
return SemaRef.BuiltinConstantArgRange(TheCall, 3, 0, 31);
// Check if value range for vxrm is in [0, 3]
case RISCVVector::BI__builtin_rvv_vaaddu_vv:
case RISCVVector::BI__builtin_rvv_vaaddu_vx:
Expand Down
Loading
Loading