Skip to content

Commit a7521a8

Browse files
authored
[RISCV][MC] Add MC support of Zibi experimental extension (#127463)
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 * https://github.com/riscv/zibi/releases/tag/v0.1.0
1 parent 152d0f5 commit a7521a8

File tree

17 files changed

+219
-0
lines changed

17 files changed

+219
-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
@@ -212,6 +212,7 @@
212212
// CHECK-EMPTY:
213213
// CHECK-NEXT: Experimental extensions
214214
// CHECK-NEXT: p 0.15 'P' ('Base P' (Packed SIMD))
215+
// CHECK-NEXT: zibi 0.1 'Zibi' (Branch with Immediate)
215216
// CHECK-NEXT: zicfilp 1.0 'Zicfilp' (Landing pad)
216217
// CHECK-NEXT: zicfiss 1.0 'Zicfiss' (Shadow stack)
217218
// 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
@@ -96,6 +96,7 @@
9696
// CHECK-NOT: __riscv_zfinx {{.*$}}
9797
// CHECK-NOT: __riscv_zhinx {{.*$}}
9898
// CHECK-NOT: __riscv_zhinxmin {{.*$}}
99+
// CHECK-NOT: __riscv_zibi {{.*$}}
99100
// CHECK-NOT: __riscv_zic64b {{.*$}}
100101
// CHECK-NOT: __riscv_zicbom {{.*$}}
101102
// CHECK-NOT: __riscv_zicbop {{.*$}}
@@ -812,6 +813,14 @@
812813
// RUN: -o - | FileCheck --check-prefix=CHECK-ZHINXMIN-EXT %s
813814
// CHECK-ZHINXMIN-EXT: __riscv_zhinxmin 1000000{{$}}
814815

816+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
817+
// RUN: -march=rv32i_zibi0p1 -E -dM %s \
818+
// RUN: -o - | FileCheck --check-prefix=CHECK-ZIBI-EXT %s
819+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
820+
// RUN: -march=rv64i_zibi0p1 -E -dM %s \
821+
// RUN: -o - | FileCheck --check-prefix=CHECK-ZIBI-EXT %s
822+
// CHECK-ZIBI-EXT: __riscv_zibi
823+
815824
// RUN: %clang --target=riscv32-unknown-linux-gnu \
816825
// RUN: -march=rv32izic64b -E -dM %s \
817826
// 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
@@ -327,6 +327,9 @@ The primary goal of experimental support is to assist in the process of ratifica
327327
``experimental-zalasr``
328328
LLVM implements the `0.0.5 draft specification <https://github.com/mehnadnerd/riscv-zalasr>`__.
329329

330+
``experimental-zibi``
331+
LLVM implements the `0.1 release specification <https://github.com/riscv/zibi/releases/tag/v0.1.0>`__.
332+
330333
``experimental-zicfilp``, ``experimental-zicfiss``
331334
LLVM implements the `1.0 release specification <https://github.com/riscv/riscv-cfi/releases/tag/v1.0>`__.
332335

llvm/docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Changes to the RISC-V Backend
124124
using `$x` with an architecture string suffix is not yet supported.
125125
* Ssctr and Smctr extensions are no longer experimental.
126126
* Add support for Zvfbfa (Additional BF16 vector compute support)
127+
* Adds experimental support for the 'Zibi` (Branch with Immediate) extension.
127128

128129
Changes to the WebAssembly Backend
129130
----------------------------------

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,11 @@ struct RISCVOperand final : public MCParsedAsmOperand {
946946
return isUImmPred([](int64_t Imm) { return 4 == Imm; });
947947
}
948948

949+
bool isImm5Zibi() const {
950+
return isUImmPred(
951+
[](int64_t Imm) { return (Imm != 0 && isUInt<5>(Imm)) || Imm == -1; });
952+
}
953+
949954
bool isSImm5Plus1() const {
950955
return isSImmPred(
951956
[](int64_t Imm) { return Imm != INT64_MIN && isInt<5>(Imm - 1); });
@@ -1643,6 +1648,10 @@ bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
16431648
"operand must be a valid system register "
16441649
"name or an integer in the range");
16451650
}
1651+
case Match_InvalidImm5Zibi:
1652+
return generateImmOutOfRangeError(
1653+
Operands, ErrorInfo, -1, (1 << 5) - 1,
1654+
"immediate must be non-zero in the range");
16461655
case Match_InvalidVTypeI: {
16471656
SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
16481657
return generateVTypeError(ErrorLoc);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ static DecodeStatus decodeUImmPlus1Operand(MCInst &Inst, uint32_t Imm,
491491
return MCDisassembler::Success;
492492
}
493493

494+
static DecodeStatus decodeImmZibiOperand(MCInst &Inst, uint32_t Imm,
495+
int64_t Address,
496+
const MCDisassembler *Decoder) {
497+
assert(isUInt<5>(Imm) && "Invalid immediate");
498+
Inst.addOperand(MCOperand::createImm(Imm ? Imm : -1LL));
499+
return MCDisassembler::Success;
500+
}
501+
494502
template <unsigned N>
495503
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm,
496504
int64_t Address,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ enum OperandType : unsigned {
341341
OPERAND_UIMM64,
342342
OPERAND_THREE,
343343
OPERAND_FOUR,
344+
OPERAND_IMM5_ZIBI,
344345
OPERAND_SIMM5,
345346
OPERAND_SIMM5_NONZERO,
346347
OPERAND_SIMM5_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;
@@ -559,6 +563,19 @@ RISCVMCCodeEmitter::getImmOpValueAsrN(const MCInst &MI, unsigned OpNo,
559563
return getImmOpValue(MI, OpNo, Fixups, STI);
560564
}
561565

566+
uint64_t
567+
RISCVMCCodeEmitter::getImmOpValueZibi(const MCInst &MI, unsigned OpNo,
568+
SmallVectorImpl<MCFixup> &Fixups,
569+
const MCSubtargetInfo &STI) const {
570+
const MCOperand &MO = MI.getOperand(OpNo);
571+
assert(MO.isImm() && "Zibi operand must be an immediate");
572+
int64_t Res = MO.getImm();
573+
if (Res == -1)
574+
return 0;
575+
576+
return Res;
577+
}
578+
562579
uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
563580
SmallVectorImpl<MCFixup> &Fixups,
564581
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
@@ -498,6 +498,22 @@ class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
498498
let Inst{6-0} = opcode.Value;
499499
}
500500

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

0 commit comments

Comments
 (0)