Skip to content

Commit aea8133

Browse files
committed
[RISCV][MC] Add MC support of Zibi experimental extension
This adds the MC support of Zibi v0.1 experimental extension. References: https://lf-riscv.atlassian.net/wiki/spaces/USXX/pages/599261201/Branch+with+Immediate+Zibi+Ratification+Plan https://lf-riscv.atlassian.net/browse/RVS-3828
1 parent 9382a95 commit aea8133

File tree

17 files changed

+218
-0
lines changed

17 files changed

+218
-0
lines changed

clang/test/Driver/print-supported-extensions-riscv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
// CHECK-EMPTY:
206206
// CHECK-NEXT: Experimental extensions
207207
// CHECK-NEXT: p 0.14 'P' ('Base P' (Packed SIMD))
208+
// CHECK-NEXT: zibi 0.1 'Zibi' (Branch with Immediate)
208209
// CHECK-NEXT: zicfilp 1.0 'Zicfilp' (Landing pad)
209210
// CHECK-NEXT: zicfiss 1.0 'Zicfiss' (Shadow stack)
210211
// CHECK-NEXT: zalasr 0.1 'Zalasr' (Load-Acquire and Store-Release Instructions)

clang/test/Preprocessor/riscv-target-features.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
// CHECK-NOT: __riscv_zfinx {{.*$}}
123123
// CHECK-NOT: __riscv_zhinx {{.*$}}
124124
// CHECK-NOT: __riscv_zhinxmin {{.*$}}
125+
// CHECK-NOT: __riscv_zibi {{.*$}}
125126
// CHECK-NOT: __riscv_zic64b {{.*$}}
126127
// CHECK-NOT: __riscv_zicbom {{.*$}}
127128
// CHECK-NOT: __riscv_zicbop {{.*$}}
@@ -1029,6 +1030,14 @@
10291030
// RUN: -o - | FileCheck --check-prefix=CHECK-ZHINXMIN-EXT %s
10301031
// CHECK-ZHINXMIN-EXT: __riscv_zhinxmin 1000000{{$}}
10311032

1033+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1034+
// RUN: -march=rv32i_zibi0p1 -E -dM %s \
1035+
// RUN: -o - | FileCheck --check-prefix=CHECK-ZIBI-EXT %s
1036+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1037+
// RUN: -march=rv64i_zibi0p1 -E -dM %s \
1038+
// RUN: -o - | FileCheck --check-prefix=CHECK-ZIBI-EXT %s
1039+
// CHECK-ZIBI-EXT: __riscv_zibi
1040+
10321041
// RUN: %clang --target=riscv32-unknown-linux-gnu \
10331042
// RUN: -march=rv32izic64b -E -dM %s \
10341043
// RUN: -o - | FileCheck --check-prefix=CHECK-ZIC64B-EXT %s

llvm/docs/RISCVUsage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ The primary goal of experimental support is to assist in the process of ratifica
326326
``experimental-zalasr``
327327
LLVM implements the `0.0.5 draft specification <https://github.com/mehnadnerd/riscv-zalasr>`__.
328328

329+
``experimental-zibi``
330+
LLVM implements the `0.1 release specification <https://lf-riscv.atlassian.net/wiki/spaces/USXX/pages/599261201/Branch+with+Immediate+Zibi+Ratification+Plan>`__.
331+
329332
``experimental-zicfilp``, ``experimental-zicfiss``
330333
LLVM implements the `1.0 release specification <https://github.com/riscv/riscv-cfi/releases/tag/v1.0>`__.
331334

llvm/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ Changes to the RISC-V Backend
177177
extension.
178178
* Adds experimental assembler support for the Qualcomm uC 'Xqciio` (External Input Output)
179179
extension.
180+
* Adds experimental assembler and code generation support for the 'Zibi` (Branch with Immediate)
181+
extension.
180182
* Adds assembler support for the 'Zilsd` (Load/Store Pair Instructions)
181183
extension.
182184
* Adds assembler support for the 'Zclsd` (Compressed Load/Store Pair Instructions)

llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ struct RISCVOperand final : public MCParsedAsmOperand {
744744
return isUImmPred([](int64_t Imm) { return Imm != 0 && isUInt<5>(Imm); });
745745
}
746746

747+
bool isUImm5Zibi() const {
748+
return isUImmPred(
749+
[](int64_t Imm) { return (Imm != 0 && isUInt<5>(Imm)) || Imm == -1; });
750+
}
751+
747752
bool isUImm5GT3() const {
748753
return isUImmPred([](int64_t Imm) { return isUInt<5>(Imm) && Imm > 3; });
749754
}
@@ -1470,6 +1475,10 @@ bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
14701475
return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
14711476
case Match_InvalidUImm5NonZero:
14721477
return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
1478+
case Match_InvalidUImm5Zibi:
1479+
return generateImmOutOfRangeError(
1480+
Operands, ErrorInfo, -1, (1 << 5) - 1,
1481+
"immediate must be non-zero in the range");
14731482
case Match_InvalidUImm5GT3:
14741483
return generateImmOutOfRangeError(Operands, ErrorInfo, 4, (1 << 5) - 1);
14751484
case Match_InvalidUImm5Plus1:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,14 @@ static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
457457
return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
458458
}
459459

460+
static DecodeStatus decodeUImmZibiOperand(MCInst &Inst, uint32_t Imm,
461+
int64_t Address,
462+
const MCDisassembler *Decoder) {
463+
assert(isUInt<5>(Imm) && "Invalid immediate");
464+
Inst.addOperand(MCOperand::createImm(Imm ? Imm : -1LL));
465+
return MCDisassembler::Success;
466+
}
467+
460468
static DecodeStatus
461469
decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address,
462470
const MCDisassembler *Decoder) {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ enum OperandType : unsigned {
301301
OPERAND_UIMM4,
302302
OPERAND_UIMM5,
303303
OPERAND_UIMM5_NONZERO,
304+
OPERAND_UIMM5_ZIBI,
304305
OPERAND_UIMM5_GT3,
305306
OPERAND_UIMM5_PLUS1,
306307
OPERAND_UIMM5_GE6_PLUS1,

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ class RISCVMCCodeEmitter : public MCCodeEmitter {
9797
SmallVectorImpl<MCFixup> &Fixups,
9898
const MCSubtargetInfo &STI) const;
9999

100+
uint64_t getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
101+
SmallVectorImpl<MCFixup> &Fixups,
102+
const MCSubtargetInfo &STI) const;
103+
100104
uint64_t getImmOpValue(const MCInst &MI, unsigned OpNo,
101105
SmallVectorImpl<MCFixup> &Fixups,
102106
const MCSubtargetInfo &STI) const;
@@ -545,6 +549,19 @@ RISCVMCCodeEmitter::getImmOpValueAsrN(const MCInst &MI, unsigned OpNo,
545549
return getImmOpValue(MI, OpNo, Fixups, STI);
546550
}
547551

552+
uint64_t
553+
RISCVMCCodeEmitter::getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
554+
SmallVectorImpl<MCFixup> &Fixups,
555+
const MCSubtargetInfo &STI) const {
556+
const MCOperand &MO = MI.getOperand(OpNo);
557+
assert(MO.isImm() && "Zibi operand must be an immediate");
558+
int64_t Res = MO.getImm();
559+
if (Res == -1)
560+
return 0;
561+
562+
return Res;
563+
}
564+
548565
uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
549566
SmallVectorImpl<MCFixup> &Fixups,
550567
const MCSubtargetInfo &STI) const {

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def FeatureStdExtE
7878
: RISCVExtension<2, 0, "Embedded Instruction Set with 16 GPRs">,
7979
RISCVExtensionBitmask<0, 4>;
8080

81+
def FeatureStdExtZibi
82+
: RISCVExperimentalExtension<0, 1, "Branch with Immediate">;
83+
def HasStdExtZibi : Predicate<"Subtarget->hasStdExtZibi()">,
84+
AssemblerPredicate<(all_of FeatureStdExtZibi),
85+
"'Zibi' (Branch with Immediate)">;
86+
8187
def FeatureStdExtZic64b
8288
: RISCVExtension<1, 0, "Cache Block Size Is 64 Bytes">;
8389

llvm/lib/Target/RISCV/RISCVInstrFormats.td

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,22 @@ class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
496496
let Inst{6-0} = opcode.Value;
497497
}
498498

499+
class RVInstBIMM<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
500+
string opcodestr, string argstr>
501+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
502+
bits<12> imm12;
503+
bits<5> cimm;
504+
bits<5> rs1;
505+
let Inst{31} = imm12{11};
506+
let Inst{30-25} = imm12{9-4};
507+
let Inst{24-20} = cimm;
508+
let Inst{19-15} = rs1;
509+
let Inst{14-12} = funct3;
510+
let Inst{11-8} = imm12{3-0};
511+
let Inst{7} = imm12{10};
512+
let Inst{6-0} = opcode.Value;
513+
}
514+
499515
class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
500516
string argstr>
501517
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {

0 commit comments

Comments
 (0)