Skip to content

Commit 3321010

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 3c822ea commit 3321010

File tree

17 files changed

+226
-1
lines changed

17 files changed

+226
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
// CHECK-EMPTY:
212212
// CHECK-NEXT: Experimental extensions
213213
// CHECK-NEXT: p 0.15 'P' ('Base P' (Packed SIMD))
214+
// CHECK-NEXT: zibi 0.1 'Zibi' (Branch with Immediate)
214215
// CHECK-NEXT: zicfilp 1.0 'Zicfilp' (Landing pad)
215216
// CHECK-NEXT: zicfiss 1.0 'Zicfiss' (Shadow stack)
216217
// 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://lf-riscv.atlassian.net/wiki/spaces/USXX/pages/599261201/Branch+with+Immediate+Zibi+Ratification+Plan>`__.
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Changes to the RISC-V Backend
120120
using `$x` with an architecture string suffix is not yet supported.
121121
* Ssctr and Smctr extensions are no longer experimental.
122122
* Add support for Zvfbfa (Additional BF16 vector compute support)
123-
123+
* Adds experimental support for the 'Zibi` (Branch with Immediate) extension.
124124
Changes to the WebAssembly Backend
125125
----------------------------------
126126

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

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

748+
bool isUImm5Zibi() const {
749+
return isUImmPred(
750+
[](int64_t Imm) { return (Imm != 0 && isUInt<5>(Imm)) || Imm == -1; });
751+
}
752+
748753
bool isUImm5GT3() const {
749754
return isUImmPred([](int64_t Imm) { return isUInt<5>(Imm) && Imm > 3; });
750755
}
@@ -1488,6 +1493,10 @@ bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
14881493
return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
14891494
case Match_InvalidUImm5NonZero:
14901495
return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
1496+
case Match_InvalidUImm5Zibi:
1497+
return generateImmOutOfRangeError(
1498+
Operands, ErrorInfo, -1, (1 << 5) - 1,
1499+
"immediate must be non-zero in the range");
14911500
case Match_InvalidUImm5GT3:
14921501
return generateImmOutOfRangeError(Operands, ErrorInfo, 4, (1 << 5) - 1);
14931502
case Match_InvalidUImm5Plus1:

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

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

464+
static DecodeStatus decodeUImmZibiOperand(MCInst &Inst, uint32_t Imm,
465+
int64_t Address,
466+
const MCDisassembler *Decoder) {
467+
assert(isUInt<5>(Imm) && "Invalid immediate");
468+
Inst.addOperand(MCOperand::createImm(Imm ? Imm : -1LL));
469+
return MCDisassembler::Success;
470+
}
471+
464472
static DecodeStatus
465473
decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address,
466474
const MCDisassembler *Decoder) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ enum OperandType : unsigned {
310310
OPERAND_UIMM4,
311311
OPERAND_UIMM5,
312312
OPERAND_UIMM5_NONZERO,
313+
OPERAND_UIMM5_ZIBI,
313314
OPERAND_UIMM5_GT3,
314315
OPERAND_UIMM5_PLUS1,
315316
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;
@@ -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)