Skip to content

Commit 16877c5

Browse files
Lian Wangbenshi001
Lian Wang
authored andcommitted
[RISCV] Add bfp and bfpw intrinsic in zbf extension
Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D116994
1 parent 9979299 commit 16877c5

File tree

10 files changed

+136
-0
lines changed

10 files changed

+136
-0
lines changed

clang/include/clang/Basic/BuiltinsRISCV.def

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ TARGET_BUILTIN(__builtin_riscv_bdecompress_32, "ZiZiZi", "nc",
3333
TARGET_BUILTIN(__builtin_riscv_bdecompress_64, "WiWiWi", "nc",
3434
"experimental-zbe,64bit")
3535

36+
// Zbf extension
37+
TARGET_BUILTIN(__builtin_riscv_bfp_32, "ZiZiZi", "nc", "experimental-zbf")
38+
TARGET_BUILTIN(__builtin_riscv_bfp_64, "WiWiWi", "nc", "experimental-zbf,64bit")
39+
3640
// Zbp extension
3741
TARGET_BUILTIN(__builtin_riscv_grev_32, "ZiZiZi", "nc", "experimental-zbp")
3842
TARGET_BUILTIN(__builtin_riscv_grev_64, "WiWiWi", "nc", "experimental-zbp,64bit")

clang/lib/CodeGen/CGBuiltin.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -18830,6 +18830,8 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
1883018830
case RISCV::BI__builtin_riscv_bcompress_64:
1883118831
case RISCV::BI__builtin_riscv_bdecompress_32:
1883218832
case RISCV::BI__builtin_riscv_bdecompress_64:
18833+
case RISCV::BI__builtin_riscv_bfp_32:
18834+
case RISCV::BI__builtin_riscv_bfp_64:
1883318835
case RISCV::BI__builtin_riscv_grev_32:
1883418836
case RISCV::BI__builtin_riscv_grev_64:
1883518837
case RISCV::BI__builtin_riscv_gorc_32:
@@ -18879,6 +18881,12 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
1887918881
ID = Intrinsic::riscv_bdecompress;
1888018882
break;
1888118883

18884+
// Zbf
18885+
case RISCV::BI__builtin_riscv_bfp_32:
18886+
case RISCV::BI__builtin_riscv_bfp_64:
18887+
ID = Intrinsic::riscv_bfp;
18888+
break;
18889+
1888218890
// Zbp
1888318891
case RISCV::BI__builtin_riscv_grev_32:
1888418892
case RISCV::BI__builtin_riscv_grev_64:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2+
// RUN: %clang_cc1 -triple riscv32 -target-feature +experimental-zbf -emit-llvm %s -o - \
3+
// RUN: | FileCheck %s -check-prefix=RV32ZBF
4+
5+
// RV32ZBF-LABEL: @bfp32(
6+
// RV32ZBF-NEXT: entry:
7+
// RV32ZBF-NEXT: [[A_ADDR:%.*]] = alloca i32, align 4
8+
// RV32ZBF-NEXT: [[B_ADDR:%.*]] = alloca i32, align 4
9+
// RV32ZBF-NEXT: store i32 [[A:%.*]], i32* [[A_ADDR]], align 4
10+
// RV32ZBF-NEXT: store i32 [[B:%.*]], i32* [[B_ADDR]], align 4
11+
// RV32ZBF-NEXT: [[TMP0:%.*]] = load i32, i32* [[A_ADDR]], align 4
12+
// RV32ZBF-NEXT: [[TMP1:%.*]] = load i32, i32* [[B_ADDR]], align 4
13+
// RV32ZBF-NEXT: [[TMP2:%.*]] = call i32 @llvm.riscv.bfp.i32(i32 [[TMP0]], i32 [[TMP1]])
14+
// RV32ZBF-NEXT: ret i32 [[TMP2]]
15+
//
16+
int bfp32(int a, int b) {
17+
return __builtin_riscv_bfp_32(a, b);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2+
// RUN: %clang_cc1 -triple riscv64 -target-feature +experimental-zbf -emit-llvm %s -o - \
3+
// RUN: | FileCheck %s -check-prefix=RV64ZBF
4+
5+
// RV64ZBF-LABEL: @bfp32(
6+
// RV64ZBF-NEXT: entry:
7+
// RV64ZBF-NEXT: [[A_ADDR:%.*]] = alloca i32, align 4
8+
// RV64ZBF-NEXT: [[B_ADDR:%.*]] = alloca i32, align 4
9+
// RV64ZBF-NEXT: store i32 [[A:%.*]], i32* [[A_ADDR]], align 4
10+
// RV64ZBF-NEXT: store i32 [[B:%.*]], i32* [[B_ADDR]], align 4
11+
// RV64ZBF-NEXT: [[TMP0:%.*]] = load i32, i32* [[A_ADDR]], align 4
12+
// RV64ZBF-NEXT: [[TMP1:%.*]] = load i32, i32* [[B_ADDR]], align 4
13+
// RV64ZBF-NEXT: [[TMP2:%.*]] = call i32 @llvm.riscv.bfp.i32(i32 [[TMP0]], i32 [[TMP1]])
14+
// RV64ZBF-NEXT: ret i32 [[TMP2]]
15+
//
16+
int bfp32(int a, int b) {
17+
return __builtin_riscv_bfp_32(a, b);
18+
}
19+
20+
// RV64ZBF-LABEL: @bfp64(
21+
// RV64ZBF-NEXT: entry:
22+
// RV64ZBF-NEXT: [[A_ADDR:%.*]] = alloca i64, align 8
23+
// RV64ZBF-NEXT: [[B_ADDR:%.*]] = alloca i64, align 8
24+
// RV64ZBF-NEXT: store i64 [[A:%.*]], i64* [[A_ADDR]], align 8
25+
// RV64ZBF-NEXT: store i64 [[B:%.*]], i64* [[B_ADDR]], align 8
26+
// RV64ZBF-NEXT: [[TMP0:%.*]] = load i64, i64* [[A_ADDR]], align 8
27+
// RV64ZBF-NEXT: [[TMP1:%.*]] = load i64, i64* [[B_ADDR]], align 8
28+
// RV64ZBF-NEXT: [[TMP2:%.*]] = call i64 @llvm.riscv.bfp.i64(i64 [[TMP0]], i64 [[TMP1]])
29+
// RV64ZBF-NEXT: ret i64 [[TMP2]]
30+
//
31+
long bfp64(long a, long b) {
32+
return __builtin_riscv_bfp_64(a, b);
33+
}

llvm/include/llvm/IR/IntrinsicsRISCV.td

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ let TargetPrefix = "riscv" in {
9393
def int_riscv_bcompress : BitManipGPRGPRIntrinsics;
9494
def int_riscv_bdecompress : BitManipGPRGPRIntrinsics;
9595

96+
// Zbf
97+
def int_riscv_bfp : BitManipGPRGPRIntrinsics;
98+
9699
// Zbp
97100
def int_riscv_grev : BitManipGPRGPRIntrinsics;
98101
def int_riscv_gorc : BitManipGPRGPRIntrinsics;

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -4185,6 +4185,9 @@ SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
41854185
: RISCVISD::BDECOMPRESS;
41864186
return DAG.getNode(Opc, DL, XLenVT, Op.getOperand(1), Op.getOperand(2));
41874187
}
4188+
case Intrinsic::riscv_bfp:
4189+
return DAG.getNode(RISCVISD::BFP, DL, XLenVT, Op.getOperand(1),
4190+
Op.getOperand(2));
41884191
case Intrinsic::riscv_vmv_x_s:
41894192
assert(Op.getValueType() == XLenVT && "Unexpected VT!");
41904193
return DAG.getNode(RISCVISD::VMV_X_S, DL, Op.getValueType(),
@@ -6275,6 +6278,17 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
62756278
Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
62766279
break;
62776280
}
6281+
case Intrinsic::riscv_bfp: {
6282+
assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
6283+
"Unexpected custom legalisation");
6284+
SDValue NewOp1 =
6285+
DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
6286+
SDValue NewOp2 =
6287+
DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
6288+
SDValue Res = DAG.getNode(RISCVISD::BFPW, DL, MVT::i64, NewOp1, NewOp2);
6289+
Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
6290+
break;
6291+
}
62786292
case Intrinsic::riscv_vmv_x_s: {
62796293
EVT VT = N->getValueType(0);
62806294
MVT XLenVT = Subtarget.getXLenVT();
@@ -9699,6 +9713,8 @@ const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
96999713
NODE_NAME_CASE(SHFLW)
97009714
NODE_NAME_CASE(UNSHFL)
97019715
NODE_NAME_CASE(UNSHFLW)
9716+
NODE_NAME_CASE(BFP)
9717+
NODE_NAME_CASE(BFPW)
97029718
NODE_NAME_CASE(BCOMPRESS)
97039719
NODE_NAME_CASE(BCOMPRESSW)
97049720
NODE_NAME_CASE(BDECOMPRESS)

llvm/lib/Target/RISCV/RISCVISelLowering.h

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ enum NodeType : unsigned {
120120
BCOMPRESSW,
121121
BDECOMPRESS,
122122
BDECOMPRESSW,
123+
// The bit field place (bfp) instruction places up to XLEN/2 LSB bits from rs2
124+
// into the value in rs1. The upper bits of rs2 control the length of the bit
125+
// field and target position. The layout of rs2 is chosen in a way that makes
126+
// it possible to construct rs2 easily using pack[h] instructions and/or
127+
// andi/lui.
128+
BFP,
129+
BFPW,
123130
// Vector Extension
124131
// VMV_V_X_VL matches the semantics of vmv.v.x but includes an extra operand
125132
// for the VL value to be used for the operation.

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def riscv_shfl : SDNode<"RISCVISD::SHFL", SDTIntBinOp>;
4545
def riscv_shflw : SDNode<"RISCVISD::SHFLW", SDT_RISCVIntBinOpW>;
4646
def riscv_unshfl : SDNode<"RISCVISD::UNSHFL", SDTIntBinOp>;
4747
def riscv_unshflw: SDNode<"RISCVISD::UNSHFLW",SDT_RISCVIntBinOpW>;
48+
def riscv_bfp : SDNode<"RISCVISD::BFP", SDTIntBinOp>;
49+
def riscv_bfpw : SDNode<"RISCVISD::BFPW", SDT_RISCVIntBinOpW>;
4850
def riscv_bcompress : SDNode<"RISCVISD::BCOMPRESS", SDTIntBinOp>;
4951
def riscv_bcompressw : SDNode<"RISCVISD::BCOMPRESSW", SDT_RISCVIntBinOpW>;
5052
def riscv_bdecompress : SDNode<"RISCVISD::BDECOMPRESS", SDTIntBinOp>;
@@ -1129,3 +1131,9 @@ let Predicates = [HasStdExtZbr, IsRV64] in {
11291131
def : PatGpr<int_riscv_crc32_d, CRC32D>;
11301132
def : PatGpr<int_riscv_crc32c_d, CRC32CD>;
11311133
} // Predicates = [HasStdExtZbr, IsRV64]
1134+
1135+
let Predicates = [HasStdExtZbf] in
1136+
def : PatGprGpr<riscv_bfp, BFP>;
1137+
1138+
let Predicates = [HasStdExtZbf, IsRV64] in
1139+
def : PatGprGpr<riscv_bfpw, BFPW>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zbf -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s -check-prefix=RV32ZBF
4+
5+
declare i32 @llvm.riscv.bfp.i32(i32 %a, i32 %b)
6+
7+
define i32 @bfp32(i32 %a, i32 %b) nounwind {
8+
; RV32ZBF-LABEL: bfp32:
9+
; RV32ZBF: # %bb.0:
10+
; RV32ZBF-NEXT: bfp a0, a0, a1
11+
; RV32ZBF-NEXT: ret
12+
%tmp = call i32 @llvm.riscv.bfp.i32(i32 %a, i32 %b)
13+
ret i32 %tmp
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zbf -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s -check-prefix=RV64ZBF
4+
5+
declare i32 @llvm.riscv.bfp.i32(i32 %a, i32 %b)
6+
7+
define i32 @bfp32(i32 %a, i32 %b) nounwind {
8+
; RV64ZBF-LABEL: bfp32:
9+
; RV64ZBF: # %bb.0:
10+
; RV64ZBF-NEXT: bfpw a0, a0, a1
11+
; RV64ZBF-NEXT: ret
12+
%tmp = call i32 @llvm.riscv.bfp.i32(i32 %a, i32 %b)
13+
ret i32 %tmp
14+
}
15+
16+
declare i64 @llvm.riscv.bfp.i64(i64 %a, i64 %b)
17+
18+
define i64 @bfp64(i64 %a, i64 %b) nounwind {
19+
; RV64ZBF-LABEL: bfp64:
20+
; RV64ZBF: # %bb.0:
21+
; RV64ZBF-NEXT: bfp a0, a0, a1
22+
; RV64ZBF-NEXT: ret
23+
%tmp = call i64 @llvm.riscv.bfp.i64(i64 %a, i64 %b)
24+
ret i64 %tmp
25+
}

0 commit comments

Comments
 (0)