Skip to content

[RISCV] Add support for handling one tied operand in the source instruction for compress patterns #143660

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 5 commits into from
Jun 19, 2025
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
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfoXqci.td
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,11 @@ def : CompressPat<(QC_E_ADDI X2, X2, simm10_lsb0000nonzero:$imm),
(C_ADDI16SP X2, simm10_lsb0000nonzero:$imm)>;
} // let isCompressOnly = true, Predicates = [HasVendorXqcilia, IsRV32]

let Predicates = [HasVendorXqciac, IsRV32] in {
def : CompressPat<(QC_MULIADD GPRC:$rd, GPRC:$rs1, uimm5:$imm5),
(QC_C_MULIADD GPRC:$rd, GPRC:$rs1, uimm5:$imm5)>;
}

let isCompressOnly = true, Predicates = [HasVendorXqcibi, IsRV32] in {
def : CompressPat<(QC_E_BEQI GPRNoX0:$rs1, simm5nonzero:$imm5, bare_simm13_lsb0:$imm12),
(QC_BEQI GPRNoX0:$rs1, simm5nonzero:$imm5, bare_simm13_lsb0:$imm12)>;
Expand Down
21 changes: 16 additions & 5 deletions llvm/test/MC/RISCV/xqciac-valid.s
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
# Xqciac - Qualcomm uC Load-Store Address Calculation Extension
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-xqciac -M no-aliases -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK-ENC,CHECK-INST %s
# RUN: | FileCheck -check-prefixes=CHECK-ENC,CHECK-INST,CHECK-NOALIAS %s
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-xqciac < %s \
# RUN: | llvm-objdump --mattr=+experimental-xqciac -M no-aliases --no-print-imm-hex -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s
# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-xqciac -show-encoding \
# RUN: | FileCheck -check-prefixes=CHECK-ENC,CHECK-INST %s
# RUN: | FileCheck -check-prefixes=CHECK-ENC,CHECK-INST,CHECK-ALIAS %s
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-xqciac < %s \
# RUN: | llvm-objdump --mattr=+experimental-xqciac --no-print-imm-hex -d - \
# RUN: | FileCheck -check-prefix=CHECK-INST %s

# CHECK-INST: qc.c.muliadd a0, a1, 0
# CHECK-NOALIAS: qc.c.muliadd a0, a1, 0
# CHECK-ALIAS: qc.muliadd a0, a1, 0
# CHECK-ENC: encoding: [0x8a,0x21]
qc.c.muliadd x10, x11, 0

# CHECK-INST: qc.c.muliadd a0, a1, 31
# CHECK-NOALIAS: qc.c.muliadd a0, a1, 31
# CHECK-ALIAS: qc.muliadd a0, a1, 31
# CHECK-ENC: encoding: [0xea,0x3d]
qc.c.muliadd x10, x11, 31

# CHECK-INST: qc.c.muliadd a0, a1, 16
# CHECK-NOALIAS: qc.c.muliadd a0, a1, 16
# CHECK-ALIAS: qc.muliadd a0, a1, 16
# CHECK-ENC: encoding: [0xaa,0x21]
qc.c.muliadd x10, x11, 16

Expand Down Expand Up @@ -47,3 +50,11 @@ qc.shladd x10, x11, x12, 4
# CHECK-INST: qc.shladd a0, a1, a2, 31
# CHECK-ENC: encoding: [0x0b,0xb5,0xc5,0x7e]
qc.shladd x10, x11, x12, 31

# Check that compress pattern for qc.muliadd works

# CHECK-NOALIAS: qc.c.muliadd a0, a1, 16
# CHECK-ALIAS: qc.muliadd a0, a1, 16
# CHECK-ENC: encoding: [0xaa,0x21]
qc.muliadd x10, x11, 16

58 changes: 42 additions & 16 deletions llvm/utils/TableGen/CompressInstEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
#include <limits>
#include <set>
#include <vector>
using namespace llvm;
Expand Down Expand Up @@ -123,10 +124,10 @@ class CompressInstEmitter {
const RecordKeeper &Records;
const CodeGenTarget Target;
std::vector<CompressPat> CompressPatterns;

void addDagOperandMapping(const Record *Rec, const DagInit *Dag,
const CodeGenInstruction &Inst,
IndexedMap<OpData> &OperandMap, bool IsSourceInst);
IndexedMap<OpData> &OperandMap, bool IsSourceInst,
unsigned *SourceLastTiedOpPtr);
void evaluateCompressPat(const Record *Compress);
void emitCompressInstEmitter(raw_ostream &OS, EmitterType EType);
bool validateTypes(const Record *DagOpType, const Record *InstOpType,
Expand All @@ -143,7 +144,8 @@ class CompressInstEmitter {
IndexedMap<OpData> &SourceOperandMap,
IndexedMap<OpData> &DestOperandMap,
StringMap<unsigned> &SourceOperands,
const CodeGenInstruction &DestInst);
const CodeGenInstruction &DestInst,
unsigned SourceLastTiedOp);

public:
CompressInstEmitter(const RecordKeeper &R) : Records(R), Target(R) {}
Expand Down Expand Up @@ -206,7 +208,8 @@ void CompressInstEmitter::addDagOperandMapping(const Record *Rec,
const DagInit *Dag,
const CodeGenInstruction &Inst,
IndexedMap<OpData> &OperandMap,
bool IsSourceInst) {
bool IsSourceInst,
unsigned *SourceLastTiedOpPtr) {
unsigned NumMIOperands = 0;
for (const auto &Op : Inst.Operands)
NumMIOperands += Op.MINumOperands;
Expand All @@ -219,12 +222,16 @@ void CompressInstEmitter::addDagOperandMapping(const Record *Rec,
// are represented.
unsigned TiedCount = 0;
unsigned OpNo = 0;
if (IsSourceInst)
*SourceLastTiedOpPtr = std::numeric_limits<unsigned int>::max();
for (const auto &Opnd : Inst.Operands) {
int TiedOpIdx = Opnd.getTiedRegister();
if (-1 != TiedOpIdx) {
// Set the entry in OperandMap for the tied operand we're skipping.
OperandMap[OpNo].Kind = OperandMap[TiedOpIdx].Kind;
OperandMap[OpNo].Data = OperandMap[TiedOpIdx].Data;
if (IsSourceInst)
*SourceLastTiedOpPtr = OpNo;
++OpNo;
++TiedCount;
continue;
Expand Down Expand Up @@ -289,15 +296,23 @@ void CompressInstEmitter::addDagOperandMapping(const Record *Rec,
static bool verifyDagOpCount(const CodeGenInstruction &Inst, const DagInit *Dag,
bool IsSource) {
unsigned NumMIOperands = 0;
for (const auto &Op : Inst.Operands)

// Use this to count number of tied Operands in Source Inst in this function.
// This counter is required here to error out when there is a Source
// Inst with two or more tied operands.
unsigned SourceInstTiedOpCount = 0;
for (const auto &Op : Inst.Operands) {
NumMIOperands += Op.MINumOperands;
if (Op.getTiedRegister() != -1)
SourceInstTiedOpCount++;
}

if (Dag->getNumArgs() == NumMIOperands)
return true;

// Source instructions are non compressed instructions and don't have tied
// operands.
if (IsSource)
// Source instructions are non compressed instructions and have at most one
// tied operand.
if (IsSource && (SourceInstTiedOpCount >= 2))
PrintFatalError(Inst.TheDef->getLoc(),
"Input operands for Inst '" + Inst.TheDef->getName() +
"' and input Dag operand count mismatch");
Expand Down Expand Up @@ -381,7 +396,8 @@ void CompressInstEmitter::createDagOperandMapping(
void CompressInstEmitter::createInstOperandMapping(
const Record *Rec, const DagInit *SourceDag, const DagInit *DestDag,
IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap,
StringMap<unsigned> &SourceOperands, const CodeGenInstruction &DestInst) {
StringMap<unsigned> &SourceOperands, const CodeGenInstruction &DestInst,
unsigned SourceLastTiedOp) {
// TiedCount keeps track of the number of operands skipped in Inst
// operands list to get to the corresponding Dag operand.
unsigned TiedCount = 0;
Expand Down Expand Up @@ -422,10 +438,18 @@ void CompressInstEmitter::createInstOperandMapping(
assert(DestDag->getArgNameStr(DagArgIdx) ==
SourceDag->getArgNameStr(SourceOp->getValue()) &&
"Incorrect operand mapping detected!\n");
DestOperandMap[OpNo].Data.Operand = SourceOp->getValue();
SourceOperandMap[SourceOp->getValue()].Data.Operand = OpNo;
LLVM_DEBUG(dbgs() << " " << SourceOp->getValue() << " ====> " << OpNo
<< "\n");

// Following four lines ensure the correct handling of a single tied
// operand in the Source Inst. SourceDagOp points to the position of
// appropriate Dag argument which is not correct in presence of tied
// operand in the Source Inst and must be incremented by 1 to reflect
// correct position of the operand in Source Inst
unsigned SourceDagOp = SourceOp->getValue();
if (SourceDagOp >= SourceLastTiedOp)
SourceDagOp++;
DestOperandMap[OpNo].Data.Operand = SourceDagOp;
SourceOperandMap[SourceDagOp].Data.Operand = OpNo;
LLVM_DEBUG(dbgs() << " " << SourceDagOp << " ====> " << OpNo << "\n");
}
}
}
Expand Down Expand Up @@ -484,23 +508,25 @@ void CompressInstEmitter::evaluateCompressPat(const Record *Rec) {
// Fill the mapping from the source to destination instructions.

IndexedMap<OpData> SourceOperandMap;
unsigned SourceLastTiedOp; // postion of the last tied operand in Source Inst
// Create a mapping between source Dag operands and source Inst operands.
addDagOperandMapping(Rec, SourceDag, SourceInst, SourceOperandMap,
/*IsSourceInst*/ true);
/*IsSourceInst*/ true, &SourceLastTiedOp);

IndexedMap<OpData> DestOperandMap;
// Create a mapping between destination Dag operands and destination Inst
// operands.
addDagOperandMapping(Rec, DestDag, DestInst, DestOperandMap,
/*IsSourceInst*/ false);
/*IsSourceInst*/ false, nullptr);

StringMap<unsigned> SourceOperands;
StringMap<unsigned> DestOperands;
createDagOperandMapping(Rec, SourceOperands, DestOperands, SourceDag, DestDag,
SourceOperandMap);
// Create operand mapping between the source and destination instructions.
createInstOperandMapping(Rec, SourceDag, DestDag, SourceOperandMap,
DestOperandMap, SourceOperands, DestInst);
DestOperandMap, SourceOperands, DestInst,
SourceLastTiedOp);

// Get the target features for the CompressPat.
std::vector<const Record *> PatReqFeatures;
Expand Down