Skip to content

[RISCV][MC] Add MC layer support for the experimental zabha extension #80005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/docs/RISCVUsage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ LLVM supports (to various degrees) a number of experimental extensions. All exp

The primary goal of experimental support is to assist in the process of ratification by providing an existence proof of an implementation, and simplifying efforts to validate the value of a proposed extension against large code bases. Experimental extensions are expected to either transition to ratified status, or be eventually removed. The decision on whether to accept an experimental extension is currently done on an entirely case by case basis; if you want to propose one, attending the bi-weekly RISC-V sync-up call is strongly advised.

``experimental-zabha``
LLVM implements assembler support for the `v1.0-rc1 draft specification <https://github.com/riscv/riscv-zabha/tree/v1.0-rc1>`_.

``experimental-zacas``
LLVM implements the `1.0-rc1 draft specification <https://github.com/riscv/riscv-zacas/releases/tag/v1.0-rc1>`_.

Expand Down
1 change: 1 addition & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Changes to the RISC-V Backend
-----------------------------

* Support for the Zicond extension is no longer experimental.
* Added assembler/disassembler support for the experimental Zabha (Byte and Halfword Atomic Memory Operations) extension.

Changes to the WebAssembly Backend
----------------------------------
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Support/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ static const RISCVSupportedExtension SupportedExtensions[] = {
// clang-format off
static const RISCVSupportedExtension SupportedExperimentalExtensions[] = {
{"zaamo", {0, 2}},
{"zabha", {1, 0}},
{"zacas", {1, 0}},
{"zalrsc", {0, 2}},

Expand Down Expand Up @@ -1006,6 +1007,7 @@ static const char *ImpliedExtsXSfvfnrclipxfqf[] = {"zve32f"};
static const char *ImpliedExtsXSfvfwmaccqqq[] = {"zvfbfmin"};
static const char *ImpliedExtsXSfvqmaccdod[] = {"zve32x"};
static const char *ImpliedExtsXSfvqmaccqoq[] = {"zve32x"};
static const char *ImpliedExtsZabha[] = {"a"};
static const char *ImpliedExtsZacas[] = {"a"};
static const char *ImpliedExtsZcb[] = {"zca"};
static const char *ImpliedExtsZcd[] = {"d", "zca"};
Expand Down Expand Up @@ -1080,6 +1082,7 @@ static constexpr ImpliedExtsEntry ImpliedExts[] = {
{{"xsfvqmaccdod"}, {ImpliedExtsXSfvqmaccdod}},
{{"xsfvqmaccqoq"}, {ImpliedExtsXSfvqmaccqoq}},
{{"xtheadvdot"}, {ImpliedExtsXTHeadVdot}},
{{"zabha"}, {ImpliedExtsZabha}},
{{"zacas"}, {ImpliedExtsZacas}},
{{"zcb"}, {ImpliedExtsZcb}},
{{"zcd"}, {ImpliedExtsZcd}},
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/RISCV/RISCVFeatures.td
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ def HasStdExtAOrZaamo
"'A' (Atomic Instructions) or "
"'Zaamo' (Atomic Memory Operations)">;

def FeatureStdExtZabha
: SubtargetFeature<"experimental-zabha", "HasStdExtZabha", "true",
"'Zabha' (Byte and Halfword Atomic Memory Operations)">;
def HasStdExtZabha : Predicate<"Subtarget->hasStdExtZabha()">,
AssemblerPredicate<(all_of FeatureStdExtZabha),
"'Zabha' (Byte and Halfword Atomic Memory Operations)">;

def FeatureStdExtZacas
: SubtargetFeature<"experimental-zacas", "HasStdExtZacas", "true",
"'Zacas' (Atomic Compare-And-Swap Instructions)">;
Expand Down
51 changes: 51 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfoZa.td
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// extensions:
// - Zawrs (v1.0) : Wait-on-Reservation-Set.
// - Zacas (v1.0-rc1) : Atomic Compare-and-Swap.
// - Zabha (v1.0-rc1) : Byte and Halfword Atomic Memory Operations.
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -134,3 +135,53 @@ let Predicates = [HasStdExtZawrs] in {
def WRS_NTO : WRSInst<0b000000001101, "wrs.nto">, Sched<[]>;
def WRS_STO : WRSInst<0b000000011101, "wrs.sto">, Sched<[]>;
} // Predicates = [HasStdExtZawrs]

//===----------------------------------------------------------------------===//
// Zabha (Byte and Halfword Atomic Memory Operations)
//===----------------------------------------------------------------------===//

let Predicates = [HasStdExtZabha] in {
defm AMOSWAP_B : AMO_rr_aq_rl<0b00001, 0b000, "amoswap.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOADD_B : AMO_rr_aq_rl<0b00000, 0b000, "amoadd.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOXOR_B : AMO_rr_aq_rl<0b00100, 0b000, "amoxor.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOAND_B : AMO_rr_aq_rl<0b01100, 0b000, "amoand.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOOR_B : AMO_rr_aq_rl<0b01000, 0b000, "amoor.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOMIN_B : AMO_rr_aq_rl<0b10000, 0b000, "amomin.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOMAX_B : AMO_rr_aq_rl<0b10100, 0b000, "amomax.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOMINU_B : AMO_rr_aq_rl<0b11000, 0b000, "amominu.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;
defm AMOMAXU_B : AMO_rr_aq_rl<0b11100, 0b000, "amomaxu.b">,
Sched<[WriteAtomicB, ReadAtomicBA, ReadAtomicBD]>;

defm AMOSWAP_H : AMO_rr_aq_rl<0b00001, 0b001, "amoswap.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOADD_H : AMO_rr_aq_rl<0b00000, 0b001, "amoadd.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOXOR_H : AMO_rr_aq_rl<0b00100, 0b001, "amoxor.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOAND_H : AMO_rr_aq_rl<0b01100, 0b001, "amoand.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOOR_H : AMO_rr_aq_rl<0b01000, 0b001, "amoor.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOMIN_H : AMO_rr_aq_rl<0b10000, 0b001, "amomin.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOMAX_H : AMO_rr_aq_rl<0b10100, 0b001, "amomax.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOMINU_H : AMO_rr_aq_rl<0b11000, 0b001, "amominu.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
defm AMOMAXU_H : AMO_rr_aq_rl<0b11100, 0b001, "amomaxu.h">,
Sched<[WriteAtomicH, ReadAtomicHA, ReadAtomicHD]>;
}

// If Zacas extension is also implemented, Zabha further provides AMOCAS.[B|H].
let Predicates = [HasStdExtZabha, HasStdExtZacas] in {
defm AMOCAS_B : AMO_cas_aq_rl<0b00101, 0b000, "amocas.b", GPR>;
defm AMOCAS_H : AMO_cas_aq_rl<0b00101, 0b001, "amocas.h", GPR>;
}
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVSchedRocket.td
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def : ReadAdvance<ReadFClass64, 0>;
//===----------------------------------------------------------------------===//
// Unsupported extensions
defm : UnsupportedSchedV;
defm : UnsupportedSchedZabha;
defm : UnsupportedSchedZba;
defm : UnsupportedSchedZbb;
defm : UnsupportedSchedZbc;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVSchedSiFive7.td
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ foreach mx = SchedMxList in {

//===----------------------------------------------------------------------===//
// Unsupported extensions
defm : UnsupportedSchedZabha;
defm : UnsupportedSchedZbc;
defm : UnsupportedSchedZbkb;
defm : UnsupportedSchedZbkx;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVSchedSiFiveP400.td
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ def : ReadAdvance<ReadSingleBitImm, 0>;

//===----------------------------------------------------------------------===//
// Unsupported extensions
defm : UnsupportedSchedZabha;
defm : UnsupportedSchedZbc;
defm : UnsupportedSchedZbkb;
defm : UnsupportedSchedZbkx;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVSchedSyntacoreSCR1.td
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def : ReadAdvance<ReadSFBALU, 0>;
//===----------------------------------------------------------------------===//
// Unsupported extensions
defm : UnsupportedSchedV;
defm : UnsupportedSchedZabha;
defm : UnsupportedSchedZba;
defm : UnsupportedSchedZbb;
defm : UnsupportedSchedZbc;
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Target/RISCV/RISCVSchedule.td
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def WriteSTB : SchedWrite; // Store byte
def WriteSTH : SchedWrite; // Store half-word
def WriteSTW : SchedWrite; // Store word
def WriteSTD : SchedWrite; // Store double-word
def WriteAtomicB : SchedWrite; //Atomic memory operation byte size
def WriteAtomicH : SchedWrite; //Atomic memory operation halfword size
def WriteAtomicW : SchedWrite; //Atomic memory operation word size
def WriteAtomicD : SchedWrite; //Atomic memory operation double word size
def WriteAtomicLDW : SchedWrite; // Atomic load word
Expand Down Expand Up @@ -135,6 +137,10 @@ def ReadIDiv : SchedRead;
def ReadIDiv32 : SchedRead;
def ReadIMul : SchedRead;
def ReadIMul32 : SchedRead;
def ReadAtomicBA : SchedRead;
def ReadAtomicBD : SchedRead;
def ReadAtomicHA : SchedRead;
def ReadAtomicHD : SchedRead;
def ReadAtomicWA : SchedRead;
def ReadAtomicWD : SchedRead;
def ReadAtomicDA : SchedRead;
Expand Down Expand Up @@ -271,6 +277,18 @@ def : ReadAdvance<ReadFRoundF16, 0>;
} // Unsupported = true
}

multiclass UnsupportedSchedZabha {
let Unsupported = true in {
def : WriteRes<WriteAtomicB, []>;
def : WriteRes<WriteAtomicH, []>;

def : ReadAdvance<ReadAtomicBA, 0>;
def : ReadAdvance<ReadAtomicBD, 0>;
def : ReadAdvance<ReadAtomicHA, 0>;
def : ReadAdvance<ReadAtomicHD, 0>;
} // Unsupported = true
}

// Include the scheduler resources for other instruction extensions.
include "RISCVScheduleZb.td"
include "RISCVScheduleV.td"
4 changes: 4 additions & 0 deletions llvm/test/CodeGen/RISCV/attributes.ll
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zacas %s -o - | FileCheck --check-prefix=RV32ZACAS %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zalrsc %s -o - | FileCheck --check-prefix=RV32ZALRSC %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfilp %s -o - | FileCheck --check-prefix=RV32ZICFILP %s
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zabha %s -o - | FileCheck --check-prefix=RV32ZABHA %s

; RUN: llc -mtriple=riscv64 %s -o - | FileCheck %s
; RUN: llc -mtriple=riscv64 -mattr=+m %s -o - | FileCheck --check-prefixes=CHECK,RV64M %s
Expand Down Expand Up @@ -201,6 +202,7 @@
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zacas %s -o - | FileCheck --check-prefix=RV64ZACAS %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zalrsc %s -o - | FileCheck --check-prefix=RV64ZALRSC %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp %s -o - | FileCheck --check-prefix=RV64ZICFILP %s
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zabha %s -o - | FileCheck --check-prefix=RV64ZABHA %s

; CHECK: .attribute 4, 16

Expand Down Expand Up @@ -300,6 +302,7 @@
; RV32ZACAS: .attribute 5, "rv32i2p1_a2p1_zacas1p0"
; RV32ZALRSC: .attribute 5, "rv32i2p1_zalrsc0p2"
; RV32ZICFILP: .attribute 5, "rv32i2p1_zicfilp0p4"
; RV32ZABHA: .attribute 5, "rv32i2p1_a2p1_zabha1p0"

; RV64M: .attribute 5, "rv64i2p1_m2p0"
; RV64ZMMUL: .attribute 5, "rv64i2p1_zmmul1p0"
Expand Down Expand Up @@ -403,6 +406,7 @@
; RV64ZACAS: .attribute 5, "rv64i2p1_a2p1_zacas1p0"
; RV64ZALRSC: .attribute 5, "rv64i2p1_zalrsc0p2"
; RV64ZICFILP: .attribute 5, "rv64i2p1_zicfilp0p4"
; RV64ZABHA: .attribute 5, "rv64i2p1_a2p1_zabha1p0"

define i32 @addi(i32 %a) {
%1 = add i32 %a, 1
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/MC/RISCV/rvzabha-invalid.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# RUN: not llvm-mc -triple riscv32 -mattr=+experimental-zabha < %s 2>&1 | FileCheck %s
# RUN: not llvm-mc -triple riscv64 -mattr=+experimental-zabha < %s 2>&1 | FileCheck %s

# Final operand must have parentheses
amoswap.b a1, a2, a3 # CHECK: :[[@LINE]]:19: error: expected '(' or optional integer offset
amomin.b a1, a2, 1 # CHECK: :[[@LINE]]:20: error: expected '(' after optional integer offset
amomin.b a1, a2, 1(a3) # CHECK: :[[@LINE]]:18: error: optional integer offset must be 0

# Only .aq, .rl, and .aqrl suffixes are valid
amoxor.b.rlqa a2, a3, (a4) # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
amoor.b.aq.rl a4, a5, (a6) # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
amoor.b. a4, a5, (a6) # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic

# Non-zero offsets not supported for the third operand (rs1).
amocas.b a1, a3, 1(a5) # CHECK: :[[@LINE]]:18: error: optional integer offset must be 0
amocas.h a0, a2, 2(a5) # CHECK: :[[@LINE]]:18: error: optional integer offset must be 0
Loading