Skip to content

Commit 6f8c6d1

Browse files
committed
[RISCV] Add Xqccmp Assembly Support
Xqccmp is a new spec by Qualcomm that makes a vendor-specific effort to solve the push/pop + frame pointers issue. Broadly, it takes the Zcmp instructions and reverse the order they push/pop registers in, which ends up matching the frame pointer convention. This extension adds a new instruction not present in Xqccmp, `qc.cm.pushfp`, which will set `fp` to the incoming `sp` value after it has pushed the registers in rlist. This change duplicates the Zcmp implementation, with minor changes to mnemonics (for the `qc.` prefix), predicates, and the addition of `qc.cm.pushfp`. There is also new logic to prevent combining Xqccmp and Zcmp. Xqccmp is kept separate to Xqci for decoding/encoding etc, as the specs are separate today. Specification: https://github.com/quic/riscv-unified-db/releases/tag/Xqccmp_extension-0.1.0
1 parent f58fde5 commit 6f8c6d1

File tree

13 files changed

+808
-1
lines changed

13 files changed

+808
-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
@@ -193,6 +193,7 @@
193193
// CHECK-NEXT: smctr 1.0 'Smctr' (Control Transfer Records Machine Level)
194194
// CHECK-NEXT: ssctr 1.0 'Ssctr' (Control Transfer Records Supervisor Level)
195195
// CHECK-NEXT: svukte 0.3 'Svukte' (Address-Independent Latency of User-Mode Faults to Supervisor Addresses)
196+
// CHECK-NEXT: xqccmp 0.1 'Xqccmp' (Qualcomm 16-bit Push/Pop and Double Moves)
196197
// CHECK-NEXT: xqcia 0.2 'Xqcia' (Qualcomm uC Arithmetic Extension)
197198
// CHECK-NEXT: xqciac 0.3 'Xqciac' (Qualcomm uC Load-Store Address Calculation Extension)
198199
// CHECK-NEXT: xqcicli 0.2 'Xqcicli' (Qualcomm uC Conditional Load Immediate Extension)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,7 @@ bool RISCVAsmParser::validateInstruction(MCInst &Inst,
36403640
}
36413641
}
36423642

3643-
if (Opcode == RISCV::CM_MVSA01) {
3643+
if (Opcode == RISCV::CM_MVSA01 || Opcode == RISCV::QC_CM_MVSA01) {
36443644
MCRegister Rd1 = Inst.getOperand(0).getReg();
36453645
MCRegister Rd2 = Inst.getOperand(1).getReg();
36463646
if (Rd1 == Rd2) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
745745
"Qualcomm uC Conditional Move 16bit");
746746
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqciint, DecoderTableXqciint16,
747747
"Qualcomm uC Interrupts 16bit");
748+
TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqccmp, DecoderTableXqccmp16,
749+
"Xqccmp (Qualcomm 16-bit Push/Pop & Double Move Instructions)");
748750
TRY_TO_DECODE_AND_ADD_SP(STI.hasFeature(RISCV::FeatureVendorXwchc),
749751
DecoderTableXwchc16, "WCH QingKe XW");
750752
TRY_TO_DECODE_AND_ADD_SP(true, DecoderTable16,

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,14 @@ def HasVendorXqcilo
13741374
AssemblerPredicate<(all_of FeatureVendorXqcilo),
13751375
"'Xqcilo' (Qualcomm uC Large Offset Load Store Extension)">;
13761376

1377+
def FeatureVendorXqccmp
1378+
: RISCVExperimentalExtension<0, 1,
1379+
"Qualcomm 16-bit Push/Pop and Double Moves",
1380+
[FeatureStdExtZca]>;
1381+
def HasVendorXqccmp : Predicate<"Subtarget->hasVendorXqccmp()">,
1382+
AssemblerPredicate<(all_of FeatureVendorXqccmp),
1383+
"'Xqccmp' (Qualcomm 16-bit Push/Pop and Double Moves)">;
1384+
13771385
// Rivos Extension(s)
13781386

13791387
def FeatureVendorXRivosVizip

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,7 @@ include "RISCVInstrInfoSFB.td"
21472147
include "RISCVInstrInfoXCV.td"
21482148
include "RISCVInstrInfoXwch.td"
21492149
include "RISCVInstrInfoXqci.td"
2150+
include "RISCVInstrInfoXqccmp.td"
21502151
include "RISCVInstrInfoXMips.td"
21512152
include "RISCVInstrInfoXRivos.td"
21522153

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//===---------------- RISCVInstrInfoXqccmp.td --------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file describes Qualcomm's Xqccmp extension.
10+
//
11+
// Xqccmp is broadly equivalent to (and incompatible with) Zcmp except the
12+
// following changes:
13+
//
14+
// - The registers are pushed in the opposite order, so `ra` and `fp` are
15+
// closest to the incoming stack pointer (to be compatible with the
16+
// frame-pointer convention), and
17+
//
18+
// - There is a new `qc.cm.pushfp` instruction which is `qc.cm.push` but it sets
19+
// `fp` to the incoming stack pointer value, as expected by the frame-pointer
20+
// convention.
21+
//
22+
//===----------------------------------------------------------------------===//
23+
24+
//===----------------------------------------------------------------------===//
25+
// Operand and SDNode transformation definitions.
26+
//===----------------------------------------------------------------------===//
27+
28+
//===----------------------------------------------------------------------===//
29+
// Instruction Formats
30+
//===----------------------------------------------------------------------===//
31+
32+
//===----------------------------------------------------------------------===//
33+
// Instruction Class Templates
34+
//===----------------------------------------------------------------------===//
35+
36+
//===----------------------------------------------------------------------===//
37+
// Instructions
38+
//===----------------------------------------------------------------------===//
39+
40+
// Zcmp
41+
let DecoderNamespace = "Xqccmp", Predicates = [HasVendorXqccmp],
42+
hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
43+
let Defs = [X10, X11] in
44+
def QC_CM_MVA01S : RVInst16CA<0b101011, 0b11, 0b10, (outs),
45+
(ins SR07:$rs1, SR07:$rs2), "qc.cm.mva01s", "$rs1, $rs2">,
46+
Sched<[WriteIALU, WriteIALU, ReadIALU, ReadIALU]>;
47+
48+
let Uses = [X10, X11] in
49+
def QC_CM_MVSA01 : RVInst16CA<0b101011, 0b01, 0b10, (outs SR07:$rs1, SR07:$rs2),
50+
(ins), "qc.cm.mvsa01", "$rs1, $rs2">,
51+
Sched<[WriteIALU, WriteIALU, ReadIALU, ReadIALU]>;
52+
} // DecoderNamespace = "Xqccmp", Predicates = [HasVendorXqccmp]...
53+
54+
let DecoderNamespace = "Xqccmp", Predicates = [HasVendorXqccmp] in {
55+
let hasSideEffects = 0, mayLoad = 0, mayStore = 1, Uses = [X2], Defs = [X2] in
56+
def QC_CM_PUSH : RVInstZcCPPP<0b11000, "qc.cm.push", negstackadj>,
57+
Sched<[WriteIALU, ReadIALU, ReadStoreData, ReadStoreData,
58+
ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
59+
ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
60+
ReadStoreData, ReadStoreData, ReadStoreData]>;
61+
62+
let hasSideEffects = 0, mayLoad = 0, mayStore = 1, Uses = [X2], Defs = [X2, X8] in
63+
def QC_CM_PUSHFP : RVInstZcCPPP<0b11001, "qc.cm.pushfp", negstackadj>,
64+
Sched<[WriteIALU, WriteIALU, ReadIALU, ReadStoreData, ReadStoreData,
65+
ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
66+
ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
67+
ReadStoreData, ReadStoreData, ReadStoreData]>;
68+
69+
let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isReturn = 1,
70+
Uses = [X2], Defs = [X2] in
71+
def QC_CM_POPRET : RVInstZcCPPP<0b11110, "qc.cm.popret">,
72+
Sched<[WriteIALU, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
73+
WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
74+
WriteLDW, WriteLDW, WriteLDW, WriteLDW, ReadIALU]>;
75+
76+
let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isReturn = 1,
77+
Uses = [X2], Defs = [X2, X10] in
78+
def QC_CM_POPRETZ : RVInstZcCPPP<0b11100, "qc.cm.popretz">,
79+
Sched<[WriteIALU, WriteIALU, WriteLDW, WriteLDW, WriteLDW,
80+
WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
81+
WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
82+
ReadIALU]>;
83+
84+
let hasSideEffects = 0, mayLoad = 1, mayStore = 0,
85+
Uses = [X2], Defs = [X2] in
86+
def QC_CM_POP : RVInstZcCPPP<0b11010, "qc.cm.pop">,
87+
Sched<[WriteIALU, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
88+
WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
89+
WriteLDW, WriteLDW, WriteLDW, ReadIALU]>;
90+
} // DecoderNamespace = "RVZcmp", Predicates = [HasVendorXqccmp]...
91+
92+
//===----------------------------------------------------------------------===//
93+
// Aliases
94+
//===----------------------------------------------------------------------===//
95+

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ Error RISCVISAInfo::checkDependency() {
745745
{"xqcia"}, {"xqciac"}, {"xqcicli"}, {"xqcicm"},
746746
{"xqcics"}, {"xqcicsr"}, {"xqciint"}, {"xqcilia"},
747747
{"xqcilo"}, {"xqcilsm"}, {"xqcisls"}};
748+
bool HasZcmp = Exts.count("zcmp") != 0;
749+
bool HasXqccmp = Exts.count("xqccmp") != 0;
748750

749751
if (HasI && HasE)
750752
return getIncompatibleError("i", "e");
@@ -779,6 +781,9 @@ Error RISCVISAInfo::checkDependency() {
779781
if (Exts.count(Ext.str()) && (XLen != 32))
780782
return getError("'" + Twine(Ext) + "'" + " is only supported for 'rv32'");
781783

784+
if (HasZcmp && HasXqccmp)
785+
return getIncompatibleError("zcmp", "xqccmp");
786+
782787
return Error::success();
783788
}
784789

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# RUN: not llvm-mc -triple=riscv32 -mattr=+experimental-xqccmp -M no-aliases -show-encoding < %s 2>&1 \
2+
# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
3+
4+
# CHECK-ERROR: error: invalid operand for instruction
5+
qc.cm.mvsa01 a1, a2
6+
7+
# CHECK-ERROR: error: rs1 and rs2 must be different
8+
qc.cm.mvsa01 s0, s0
9+
10+
# CHECK-ERROR: error: invalid operand for instruction
11+
qc.cm.mva01s a1, a2
12+
13+
# CHECK-ERROR: error: invalid register list, {ra, s0-s10} or {x1, x8-x9, x18-x26} is not supported
14+
qc.cm.popretz {ra, s0-s10}, 112
15+
16+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
17+
qc.cm.popretz {ra, s0-s1}, 112
18+
19+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
20+
qc.cm.push {ra}, 16
21+
22+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
23+
qc.cm.pushfp {ra, s0}, 16
24+
25+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
26+
qc.cm.pop {ra, s0-s1}, -32
27+
28+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
29+
qc.cm.push {ra}, -8
30+
31+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
32+
qc.cm.pushfp {ra, s0}, -12
33+
34+
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
35+
qc.cm.pop {ra, s0-s1}, -40

0 commit comments

Comments
 (0)