Skip to content

Commit c44dca1

Browse files
authored
MachineVerifier: Reject extra non-register operands on instructions (#73758)
We were allowing extra immediate arguments, and only bothering to check if registers were implicit or not. Also consolidate extra operand checks in verifier, to make this testable. We had 3 different places checking if you were trying to build an instruction with more operands than allowed by the definition. We had an assertion in addOperand, a direct check in the MIRParser to avoid the assertion, and the machine verifier checks. Remove the assert and parser check so the verifier can provide a consistent verification experience, which will also handle instructions modified in place.
1 parent 2031e72 commit c44dca1

File tree

7 files changed

+38
-44
lines changed

7 files changed

+38
-44
lines changed

llvm/lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,19 +1175,10 @@ bool MIParser::parse(MachineInstr *&MI) {
11751175
MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
11761176
MI->setFlags(Flags);
11771177

1178-
unsigned NumExplicitOps = 0;
1179-
for (const auto &Operand : Operands) {
1180-
bool IsImplicitOp = Operand.Operand.isReg() && Operand.Operand.isImplicit();
1181-
if (!IsImplicitOp) {
1182-
if (!MCID.isVariadic() && NumExplicitOps >= MCID.getNumOperands() &&
1183-
!Operand.Operand.isValidExcessOperand())
1184-
return error(Operand.Begin, "too many operands for instruction");
1185-
1186-
++NumExplicitOps;
1187-
}
1188-
1178+
// Don't check the operands make sense, let the verifier catch any
1179+
// improprieties.
1180+
for (const auto &Operand : Operands)
11891181
MI->addOperand(MF, Operand.Operand);
1190-
}
11911182

11921183
if (assignRegisterTies(*MI, Operands))
11931184
return true;

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,6 @@ void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
231231
// OpNo now points as the desired insertion point. Unless this is a variadic
232232
// instruction, only implicit regs are allowed beyond MCID->getNumOperands().
233233
// RegMask operands go between the explicit and implicit operands.
234-
assert((MCID->isVariadic() || OpNo < MCID->getNumOperands() ||
235-
Op.isValidExcessOperand()) &&
236-
"Trying to add an operand to a machine instr that is already done!");
237-
238234
MachineRegisterInfo *MRI = getRegInfo();
239235

240236
// Determine if the Operands array needs to be reallocated.

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,9 +2126,9 @@ MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
21262126
}
21272127
} else if (MO->isReg() && MO->isTied())
21282128
report("Explicit operand should not be tied", MO, MONum);
2129-
} else {
2129+
} else if (!MI->isVariadic()) {
21302130
// ARM adds %reg0 operands to indicate predicates. We'll allow that.
2131-
if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
2131+
if (!MO->isValidExcessOperand())
21322132
report("Extra explicit operand on non-variadic instruction", MO, MONum);
21332133
}
21342134

llvm/lib/Target/MSP430/MSP430RegisterInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
135135
MI.setDesc(TII.get(MSP430::MOV16rr));
136136
MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
137137

138+
// Remove the now unused Offset operand.
139+
MI.removeOperand(FIOperandNum + 1);
140+
138141
if (Offset == 0)
139142
return false;
140143

llvm/test/CodeGen/MIR/AMDGPU/extra-imm-operand.mir

Lines changed: 0 additions & 13 deletions
This file was deleted.

llvm/test/CodeGen/MIR/AMDGPU/extra-reg-operand.mir

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# REQUIRES: amdgpu-registered-target
2+
# RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=none -o /dev/null %s 2>&1 | FileCheck %s
3+
4+
---
5+
name: invalid_reg_sequence
6+
tracksRegLiveness: true
7+
body: |
8+
bb.0:
9+
; CHECK: *** Bad machine code: Too few operands ***
10+
IMPLICIT_DEF
11+
12+
; FIXME: Error message misleading
13+
; CHECK: *** Bad machine code: Explicit definition must be a register ***
14+
IMPLICIT_DEF 0
15+
16+
; CHECK: *** Bad machine code: Extra explicit operand on non-variadic instruction ***
17+
%1:vgpr_32 = IMPLICIT_DEF 0
18+
19+
; CHECK: *** Bad machine code: Extra explicit operand on non-variadic instruction ***
20+
; CHECK: *** Bad machine code: Extra explicit operand on non-variadic instruction ***
21+
%2:vgpr_32 = IMPLICIT_DEF 0, 1
22+
23+
; CHECK: *** Bad machine code: Extra explicit operand on non-variadic instruction ***
24+
%3:vgpr_32 = IMPLICIT_DEF %1
25+
26+
; CHECK-NOT: Bad machine code
27+
%4:vgpr_32 = IMPLICIT_DEF implicit %1
28+
...
29+
30+

0 commit comments

Comments
 (0)