Skip to content

[RISCV] Implement Clang Builtins for XAndesPerf Extension #147018

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

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/BuiltinsRISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,8 @@ def pause : RISCVBuiltin<"void()">;
// XCV extensions.
//===----------------------------------------------------------------------===//
include "clang/Basic/BuiltinsRISCVXCV.td"

//===----------------------------------------------------------------------===//
// XAndes extensions.
//===----------------------------------------------------------------------===//
include "clang/Basic/BuiltinsRISCVXAndes.td"
29 changes: 29 additions & 0 deletions clang/include/clang/Basic/BuiltinsRISCVXAndes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//==- BuiltinsRISCVXAndes.td - RISC-V Andes Builtin database -----*- C++ -*-==//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the Andes-specific builtin function database. Users of
// this file must define the BUILTIN macro to make use of this information.
//
//===----------------------------------------------------------------------===//

class RISCVXAndesBuiltin<string prototype, string features = ""> : TargetBuiltin {
let Spellings = ["__builtin_riscv_nds_" # NAME];
let Prototype = prototype;
let Features = features;
}

let Attributes = [NoThrow, Const] in {
//===----------------------------------------------------------------------===//
// XAndesPerf extension.
//===----------------------------------------------------------------------===//

def ffb : RISCVXAndesBuiltin<"long int(unsigned long int, unsigned long int)", "xandesperf">;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have 32/64 versions just like clz, orc.b?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, we did not plan to provide both 32-bit and 64-bit versions for RV64. The 32-bit version would be only for RV32, and the 64-bit version is for RV64.

def ffzmism : RISCVXAndesBuiltin<"long int(unsigned long int, unsigned long int)", "xandesperf">;
def ffmism : RISCVXAndesBuiltin<"long int(unsigned long int, unsigned long int)", "xandesperf">;
def flmism : RISCVXAndesBuiltin<"long int(unsigned long int, unsigned long int)", "xandesperf">;
} // Attributes = [NoThrow, Const]
18 changes: 18 additions & 0 deletions clang/lib/CodeGen/TargetBuiltins/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,24 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
ID = Intrinsic::riscv_cv_alu_subuRN;
break;

// XAndesPerf
case RISCV::BI__builtin_riscv_nds_ffb:
IntrinsicTypes = {ResultType};
ID = Intrinsic::riscv_nds_ffb;
break;
case RISCV::BI__builtin_riscv_nds_ffzmism:
IntrinsicTypes = {ResultType};
ID = Intrinsic::riscv_nds_ffzmism;
break;
case RISCV::BI__builtin_riscv_nds_ffmism:
IntrinsicTypes = {ResultType};
ID = Intrinsic::riscv_nds_ffmism;
break;
case RISCV::BI__builtin_riscv_nds_flmism:
IntrinsicTypes = {ResultType};
ID = Intrinsic::riscv_nds_flmism;
break;

// Vector builtins are handled from here.
#include "clang/Basic/riscv_vector_builtin_cg.inc"

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ set(ppc_htm_files
set(riscv_files
riscv_bitmanip.h
riscv_corev_alu.h
riscv_nds.h
riscv_crypto.h
riscv_ntlh.h
sifive_vector.h
Expand Down
47 changes: 47 additions & 0 deletions clang/lib/Headers/riscv_nds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*===---- riscv_nds.h - Andes 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_NDS_H
#define __RISCV_NDS_H

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

#if defined(__riscv_xandesperf)

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

static __inline__ long __DEFAULT_FN_ATTRS __riscv_nds_ffb(unsigned long a,
unsigned long b) {
return __builtin_riscv_nds_ffb(a, b);
}

static __inline__ long __DEFAULT_FN_ATTRS __riscv_nds_ffzmism(unsigned long a,
unsigned long b) {
return __builtin_riscv_nds_ffzmism(a, b);
}

static __inline__ long __DEFAULT_FN_ATTRS __riscv_nds_ffmism(unsigned long a,
unsigned long b) {
return __builtin_riscv_nds_ffmism(a, b);
}

static __inline__ long __DEFAULT_FN_ATTRS __riscv_nds_flmism(unsigned long a,
unsigned long b) {
return __builtin_riscv_nds_flmism(a, b);
}

#endif // defined(__riscv_nds)

#if defined(__cplusplus)
}
#endif

#endif // define __RISCV_NDS_H
65 changes: 65 additions & 0 deletions clang/test/CodeGen/RISCV/riscv-xandesperf-c-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
// RUN: %clang_cc1 -triple riscv32 -target-feature +xandesperf -emit-llvm %s -o - \
// RUN: -disable-O0-optnone | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=CHECK-RV32
// RUN: %clang_cc1 -triple riscv64 -target-feature +xandesperf -emit-llvm %s -o - \
// RUN: -disable-O0-optnone | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=CHECK-RV64

#include <riscv_nds.h>

// CHECK-RV32-LABEL: @test_ffb(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffb.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffb(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffb.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffb(unsigned long a, unsigned long b) {
return __riscv_nds_ffb(a, b);
}

// CHECK-RV32-LABEL: @test_ffzmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffzmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffzmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffzmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffzmism(unsigned long a, unsigned long b) {
return __riscv_nds_ffzmism(a, b);
}

// CHECK-RV32-LABEL: @test_ffmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffmism(unsigned long a, unsigned long b) {
return __riscv_nds_ffmism(a, b);
}

// CHECK-RV32-LABEL: @test_flmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.flmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_flmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.flmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_flmism(unsigned long a, unsigned long b) {
return __riscv_nds_flmism(a, b);
}
63 changes: 63 additions & 0 deletions clang/test/CodeGen/RISCV/riscv-xandesperf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
// RUN: %clang_cc1 -triple riscv32 -target-feature +xandesperf -emit-llvm %s -o - \
// RUN: -disable-O0-optnone | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=CHECK-RV32
// RUN: %clang_cc1 -triple riscv64 -target-feature +xandesperf -emit-llvm %s -o - \
// RUN: -disable-O0-optnone | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=CHECK-RV64

// CHECK-RV32-LABEL: @test_ffb(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffb.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffb(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffb.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffb(unsigned long a, unsigned long b) {
return __builtin_riscv_nds_ffb(a, b);
}

// CHECK-RV32-LABEL: @test_ffzmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffzmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffzmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffzmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffzmism(unsigned long a, unsigned long b) {
return __builtin_riscv_nds_ffzmism(a, b);
}

// CHECK-RV32-LABEL: @test_ffmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.ffmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_ffmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.ffmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_ffmism(unsigned long a, unsigned long b) {
return __builtin_riscv_nds_ffmism(a, b);
}

// CHECK-RV32-LABEL: @test_flmism(
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.nds.flmism.i32(i32 [[A:%.*]], i32 [[B:%.*]])
// CHECK-RV32-NEXT: ret i32 [[TMP0]]
//
// CHECK-RV64-LABEL: @test_flmism(
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.nds.flmism.i64(i64 [[A:%.*]], i64 [[B:%.*]])
// CHECK-RV64-NEXT: ret i64 [[TMP0]]
//
long test_flmism(unsigned long a, unsigned long b) {
return __builtin_riscv_nds_flmism(a, b);
}
14 changes: 14 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsRISCVXAndes.td
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
//===----------------------------------------------------------------------===//

let TargetPrefix = "riscv" in {
// Andes Performance Extension
def int_riscv_nds_ffb : Intrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>, LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;
def int_riscv_nds_ffzmism : Intrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>, LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;
def int_riscv_nds_ffmism : Intrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>, LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;
def int_riscv_nds_flmism : Intrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>, LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable]>;

// Andes Vector BFloat16 Conversion Extension
def int_riscv_nds_vfwcvt_s_bf16 : RISCVConversionUnMasked;
def int_riscv_nds_vfncvt_bf16_s : RISCVConversionUnMaskedRoundingMode;
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfoXAndes.td
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,17 @@ def : Sh2AddPat<NDS_LEA_W_ZE>;
def : Sh3AddPat<NDS_LEA_D_ZE>;
} // Predicates = [HasVendorXAndesPerf, IsRV64]

let Predicates = [HasVendorXAndesPerf] in {
def : Pat<(XLenVT (int_riscv_nds_ffb (XLenVT GPR:$rs1), (XLenVT GPR:$rs2))),
(NDS_FFB GPR:$rs1, GPR:$rs2)>;
def : Pat<(XLenVT (int_riscv_nds_ffzmism (XLenVT GPR:$rs1), (XLenVT GPR:$rs2))),
(NDS_FFZMISM GPR:$rs1, GPR:$rs2)>;
def : Pat<(XLenVT (int_riscv_nds_ffmism (XLenVT GPR:$rs1), (XLenVT GPR:$rs2))),
(NDS_FFMISM GPR:$rs1, GPR:$rs2)>;
def : Pat<(XLenVT (int_riscv_nds_flmism (XLenVT GPR:$rs1), (XLenVT GPR:$rs2))),
(NDS_FLMISM GPR:$rs1, GPR:$rs2)>;
} // Predicates = [HasVendorXAndesPerf]

let Predicates = [HasVendorXAndesVBFHCvt] in {
defm PseudoNDS_VFWCVT_S_BF16 : VPseudoVWCVT_S_BF16;
defm PseudoNDS_VFNCVT_BF16_S : VPseudoVNCVT_BF16_S;
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/CodeGen/RISCV/rv32xandesperf.ll
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,50 @@ define i64 @sexti32_i64_2(i32 %a) {
%1 = sext i32 %a to i64
ret i64 %1
}

; NDS.FFB/NDS.FFZMISM/NDS.FFMISM/NDS.FLMISM

declare i32 @llvm.riscv.nds.ffb.i32(i32, i32)
declare i32 @llvm.riscv.nds.ffzmism.i32(i32, i32)
declare i32 @llvm.riscv.nds.ffmism.i32(i32, i32)
declare i32 @llvm.riscv.nds.flmism.i32(i32, i32)

define i32 @ffb(i32 %a, i32 %b) nounwind {
; CHECK-LABEL: ffb:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffb a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.riscv.nds.ffb.i32(i32 %a, i32 %b)
ret i32 %0
}

define i32 @ffzmism(i32 %a, i32 %b) nounwind {
; CHECK-LABEL: ffzmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffzmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.riscv.nds.ffzmism.i32(i32 %a, i32 %b)
ret i32 %0
}

define i32 @ffmism(i32 %a, i32 %b) nounwind {
; CHECK-LABEL: ffmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.riscv.nds.ffmism.i32(i32 %a, i32 %b)
ret i32 %0
}

define i32 @flmism(i32 %a, i32 %b) nounwind {
; CHECK-LABEL: flmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.flmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.riscv.nds.flmism.i32(i32 %a, i32 %b)
ret i32 %0
}
47 changes: 47 additions & 0 deletions llvm/test/CodeGen/RISCV/rv64xandesperf.ll
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,50 @@ define i64 @sexti32_i64_2(i32 signext %a) {
%1 = sext i32 %a to i64
ret i64 %1
}

; NDS.FFB/NDS.FFZMISM/NDS.FFMISM/NDS.FLMISM

declare i64 @llvm.riscv.nds.ffb.i64(i64, i64)
declare i64 @llvm.riscv.nds.ffzmism.i64(i64, i64)
declare i64 @llvm.riscv.nds.ffmism.i64(i64, i64)
declare i64 @llvm.riscv.nds.flmism.i64(i64, i64)

define i64 @ffb(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: ffb:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffb a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.riscv.nds.ffb.i64(i64 %a, i64 %b)
ret i64 %0
}

define i64 @ffzmism(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: ffzmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffzmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.riscv.nds.ffzmism.i64(i64 %a, i64 %b)
ret i64 %0
}

define i64 @ffmism(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: ffmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.ffmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.riscv.nds.ffmism.i64(i64 %a, i64 %b)
ret i64 %0
}

define i64 @flmism(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: flmism:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: nds.flmism a0, a0, a1
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.riscv.nds.flmism.i64(i64 %a, i64 %b)
ret i64 %0
}
Loading