Skip to content

Commit e25b30d

Browse files
committed
[RISCV] Add vendor-defined XTHeadBa (address-generation) extension
The vendor-defined XTHeadBa (predating the standard Zba extension) extension adds an address-generation instruction (th.addsl) with similar semantics as sh[123]add from Zba. It is supported by the C9xx cores (e.g., found in the wild in the Allwinner D1) by Alibaba T-Head. The current (as of this commit) public documentation for XTHeadBa is available from: https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf Support for these instructions has already landed in GNU Binutils: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=8254c3d2c94ae5458095ea6c25446ba89134b9da Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D143029
1 parent a13645c commit e25b30d

File tree

11 files changed

+788
-0
lines changed

11 files changed

+788
-0
lines changed

llvm/docs/RISCVUsage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ It is our intention to follow the naming conventions described in `riscv-non-isa
169169

170170
The current vendor extensions supported are:
171171

172+
``XTHeadBa``
173+
LLVM implements `the THeadBa (address-generation) vendor-defined instructions specified in <https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf>`_ by T-HEAD of Alibaba. Instructions are prefixed with `th.` as described in the specification.
174+
172175
``XTHeadVdot``
173176
LLVM implements `version 1.0.0 of the THeadV-family custom instructions specification <https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.0/xthead-2022-12-04-2.2.0.pdf>`_ by T-HEAD of Alibaba. All instructions are prefixed with `th.` as described in the specification, and the riscv-toolchain-convention document linked above.
174177

llvm/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Changes to the RISC-V Backend
108108
* Zca, Zcf, and Zcd extensions were upgraded to version 1.0.1.
109109
* vsetvli intrinsics no longer have side effects. They may now be combined,
110110
moved, deleted, etc. by optimizations.
111+
* Adds support for the vendor-defined XTHeadBa (address-generation) extension.
111112

112113
Changes to the WebAssembly Backend
113114
----------------------------------

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static const RISCVSupportedExtension SupportedExtensions[] = {
109109
{"svinval", RISCVExtensionVersion{1, 0}},
110110

111111
// vendor-defined ('X') extensions
112+
{"xtheadba", RISCVExtensionVersion{1, 0}},
112113
{"xtheadvdot", RISCVExtensionVersion{1, 0}},
113114
{"xventanacondops", RISCVExtensionVersion{1, 0}},
114115
};

llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,13 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
471471
if (Result != MCDisassembler::Fail)
472472
return Result;
473473
}
474+
if (STI.getFeatureBits()[RISCV::FeatureVendorXTHeadBa]) {
475+
LLVM_DEBUG(dbgs() << "Trying XTHeadBa custom opcode table:\n");
476+
Result = decodeInstruction(DecoderTableTHeadBa32, MI, Insn, Address, this,
477+
STI);
478+
if (Result != MCDisassembler::Fail)
479+
return Result;
480+
}
474481
if (STI.getFeatureBits()[RISCV::FeatureVendorXTHeadVdot]) {
475482
LLVM_DEBUG(dbgs() << "Trying XTHeadVdot custom opcode table:\n");
476483
Result =

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ def HasVendorXVentanaCondOps : Predicate<"Subtarget->hasVendorXVentanaCondOps()"
463463
AssemblerPredicate<(all_of FeatureVendorXVentanaCondOps),
464464
"'XVentanaCondOps' (Ventana Conditional Ops)">;
465465

466+
def FeatureVendorXTHeadBa
467+
: SubtargetFeature<"xtheadba", "HasVendorXTHeadBa", "true",
468+
"'xtheadba' (T-Head address calculation instructions)">;
469+
def HasVendorXTHeadBa : Predicate<"Subtarget->hasVendorXTHeadBa()">,
470+
AssemblerPredicate<(all_of FeatureVendorXTHeadBa),
471+
"'xtheadba' (T-Head address calculation instructions)">;
472+
466473
def FeatureVendorXTHeadVdot
467474
: SubtargetFeature<"xtheadvdot", "HasVendorXTHeadVdot", "true",
468475
"'xtheadvdot' (T-Head Vector Extensions for Dot)",

llvm/lib/Target/RISCV/RISCVInstrInfoXTHead.td

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ class THVdotALUrVX<bits<6> funct6, RISCVVFormat opv, string opcodestr>
4343
opcodestr, "$vd, $rs1, $vs2$vm">;
4444
} // hasSideEffects = 0, mayLoad = 0, mayStore = 0
4545

46+
let Predicates = [HasVendorXTHeadBa], DecoderNamespace = "THeadBa",
47+
hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
48+
class THShiftALU_rri<bits<3> funct3, string opcodestr>
49+
: RVInstR<0, funct3, OPC_CUSTOM_0, (outs GPR:$rd),
50+
(ins GPR:$rs1, GPR:$rs2, uimm2:$uimm2),
51+
opcodestr, "$rd, $rs1, $rs2, $uimm2"> {
52+
bits<2> uimm2;
53+
let Inst{31-27} = 0;
54+
let Inst{26-25} = uimm2;
55+
}
56+
4657
//===----------------------------------------------------------------------===//
4758
// Combination of instruction classes.
4859
// Use these multiclasses to define instructions more easily.
@@ -59,6 +70,11 @@ multiclass THVdotVMAQA<string opcodestr, bits<6> funct6> {
5970
//===----------------------------------------------------------------------===//
6071
// Instructions
6172
//===----------------------------------------------------------------------===//
73+
let Predicates = [HasVendorXTHeadBa] in {
74+
def TH_ADDSL : THShiftALU_rri<0b001, "th.addsl">,
75+
Sched<[WriteSHXADD, ReadSHXADD, ReadSHXADD]>;
76+
} // Predicates = [HasVendorXTHeadBa]
77+
6278
let Predicates = [HasVendorXTHeadVdot],
6379
Constraints = "@earlyclobber $vd",
6480
RVVConstraint = WidenV in {
@@ -134,6 +150,80 @@ multiclass VPatTernaryVMAQA_VV_VX<string intrinsic, string instruction,
134150
//===----------------------------------------------------------------------===//
135151
// Pseudo-instructions and codegen patterns
136152
//===----------------------------------------------------------------------===//
153+
let Predicates = [HasVendorXTHeadBa] in {
154+
def : Pat<(add GPR:$rs1, (shl GPR:$rs2, uimm2:$uimm2)),
155+
(TH_ADDSL GPR:$rs1, GPR:$rs2, uimm2:$uimm2)>;
156+
157+
// Reuse complex patterns from StdExtZba
158+
def : Pat<(add sh1add_op:$rs1, non_imm12:$rs2),
159+
(TH_ADDSL GPR:$rs2, sh1add_op:$rs1, 1)>;
160+
def : Pat<(add sh2add_op:$rs1, non_imm12:$rs2),
161+
(TH_ADDSL GPR:$rs2, sh2add_op:$rs1, 2)>;
162+
def : Pat<(add sh3add_op:$rs1, non_imm12:$rs2),
163+
(TH_ADDSL GPR:$rs2, sh3add_op:$rs1, 3)>;
164+
165+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 6)), GPR:$rs2),
166+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 1), 1)>;
167+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 10)), GPR:$rs2),
168+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 2), 1)>;
169+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 18)), GPR:$rs2),
170+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 3), 1)>;
171+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 12)), GPR:$rs2),
172+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 1), 2)>;
173+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 20)), GPR:$rs2),
174+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 2), 2)>;
175+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 36)), GPR:$rs2),
176+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 3), 2)>;
177+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 24)), GPR:$rs2),
178+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 1), 3)>;
179+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 40)), GPR:$rs2),
180+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 2), 3)>;
181+
def : Pat<(add (mul_oneuse GPR:$rs1, (XLenVT 72)), GPR:$rs2),
182+
(TH_ADDSL GPR:$rs2, (TH_ADDSL GPR:$rs1, GPR:$rs1, 3), 3)>;
183+
184+
def : Pat<(add GPR:$r, CSImm12MulBy4:$i),
185+
(TH_ADDSL GPR:$r, (ADDI X0, (SimmShiftRightBy2XForm CSImm12MulBy4:$i)), 2)>;
186+
def : Pat<(add GPR:$r, CSImm12MulBy8:$i),
187+
(TH_ADDSL GPR:$r, (ADDI X0, (SimmShiftRightBy3XForm CSImm12MulBy8:$i)), 3)>;
188+
189+
def : Pat<(mul GPR:$r, C3LeftShift:$i),
190+
(SLLI (TH_ADDSL GPR:$r, GPR:$r, 1),
191+
(TrailingZeros C3LeftShift:$i))>;
192+
def : Pat<(mul GPR:$r, C5LeftShift:$i),
193+
(SLLI (TH_ADDSL GPR:$r, GPR:$r, 2),
194+
(TrailingZeros C5LeftShift:$i))>;
195+
def : Pat<(mul GPR:$r, C9LeftShift:$i),
196+
(SLLI (TH_ADDSL GPR:$r, GPR:$r, 3),
197+
(TrailingZeros C9LeftShift:$i))>;
198+
199+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 11)),
200+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 2), 1)>;
201+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 19)),
202+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 3), 1)>;
203+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 13)),
204+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 1), 2)>;
205+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 21)),
206+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 2), 2)>;
207+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 37)),
208+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 3), 2)>;
209+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 25)),
210+
(TH_ADDSL (TH_ADDSL GPR:$r, GPR:$r, 2), (TH_ADDSL GPR:$r, GPR:$r, 2), 2)>;
211+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 41)),
212+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 2), 3)>;
213+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 73)),
214+
(TH_ADDSL GPR:$r, (TH_ADDSL GPR:$r, GPR:$r, 3), 3)>;
215+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 27)),
216+
(TH_ADDSL (TH_ADDSL GPR:$r, GPR:$r, 3), (TH_ADDSL GPR:$r, GPR:$r, 3), 1)>;
217+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 45)),
218+
(TH_ADDSL (TH_ADDSL GPR:$r, GPR:$r, 3), (TH_ADDSL GPR:$r, GPR:$r, 3), 2)>;
219+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 81)),
220+
(TH_ADDSL (TH_ADDSL GPR:$r, GPR:$r, 3), (TH_ADDSL GPR:$r, GPR:$r, 3), 3)>;
221+
222+
def : Pat<(mul_const_oneuse GPR:$r, (XLenVT 200)),
223+
(SLLI (TH_ADDSL (TH_ADDSL GPR:$r, GPR:$r, 2),
224+
(TH_ADDSL GPR:$r, GPR:$r, 2), 2), 3)>;
225+
} // Predicates = [HasVendorXTHeadBa]
226+
137227
defm PseudoTHVdotVMAQA : VPseudoVMAQA_VV_VX;
138228
defm PseudoTHVdotVMAQAU : VPseudoVMAQA_VV_VX;
139229
defm PseudoTHVdotVMAQASU : VPseudoVMAQA_VV_VX;

llvm/test/CodeGen/RISCV/attributes.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
; RUN: llc -mtriple=riscv64 -mattr=+svpbmt %s -o - | FileCheck --check-prefix=RV64SVPBMT %s
8787
; RUN: llc -mtriple=riscv64 -mattr=+svinval %s -o - | FileCheck --check-prefix=RV64SVINVAL %s
8888
; RUN: llc -mtriple=riscv64 -mattr=+xventanacondops %s -o - | FileCheck --check-prefix=RV64XVENTANACONDOPS %s
89+
; RUN: llc -mtriple=riscv64 -mattr=+xtheadba %s -o - | FileCheck --check-prefix=RV64XTHEADBA %s
8990
; RUN: llc -mtriple=riscv64 -mattr=+xtheadvdot %s -o - | FileCheck --check-prefix=RV64XTHEADVDOT %s
9091
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zawrs %s -o - | FileCheck --check-prefix=RV64ZAWRS %s
9192
; RUN: llc -mtriple=riscv64 -mattr=+experimental-ztso %s -o - | FileCheck --check-prefix=RV64ZTSO %s
@@ -180,6 +181,7 @@
180181
; RV64SVPBMT: .attribute 5, "rv64i2p0_svpbmt1p0"
181182
; RV64SVINVAL: .attribute 5, "rv64i2p0_svinval1p0"
182183
; RV64XVENTANACONDOPS: .attribute 5, "rv64i2p0_xventanacondops1p0"
184+
; RV64XTHEADBA: .attribute 5, "rv64i2p0_xtheadba1p0"
183185
; RV64XTHEADVDOT: .attribute 5, "rv64i2p0_f2p0_d2p0_v1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0_xtheadvdot1p0"
184186
; RV64ZTSO: .attribute 5, "rv64i2p0_ztso0p1"
185187
; RV64ZCA: .attribute 5, "rv64i2p0_zca1p0"

0 commit comments

Comments
 (0)