[llvm] [Mips] Fix clang crashes when assembling invalid MIPS beql instructions with --arch=mips (PR #156413)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 1 23:47:22 PDT 2025


https://github.com/yingopq created https://github.com/llvm/llvm-project/pull/156413

>From clang version 4, mips append new instruction BeqImm and BEQLImmMacro, the second operand of instruction format is imm64:$imm. When Mips process `beql $t0, ($t0), 1`, it think the second operand was an imm, so match success. Then mips backend process expandBranchImm, check the Operand(1) was not imm, reported failure.
We can strengthen the instruction matching restrictions.

Fix #151453.

>From 3d53abfab7cba259f4ac7168d48ddc46c8a7e41a Mon Sep 17 00:00:00 2001
From: Ying Huang <ying.huang at oss.cipunited.com>
Date: Mon, 1 Sep 2025 22:38:25 -0400
Subject: [PATCH] [Mips] Fix clang crashes when assembling invalid MIPS beql
 instructions with --arch=mips

>From clang version 4, mips append new instruction BeqImm and
BEQLImmMacro, the second operand of instruction format is imm64:$imm.
When Mips process `beql $t0, ($t0), 1`, it think the second operand
was an imm, so match success. Then mips backend process expandBranchImm,
check the Operand(1) was not imm, reported failure.
We can strengthen the instruction matching restrictions.

Fix #151453.
---
 llvm/lib/Target/Mips/MipsInstrInfo.td | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/Mips/MipsInstrInfo.td b/llvm/lib/Target/Mips/MipsInstrInfo.td
index a124e84e9ca5f..0d6fa25fb8025 100644
--- a/llvm/lib/Target/Mips/MipsInstrInfo.td
+++ b/llvm/lib/Target/Mips/MipsInstrInfo.td
@@ -855,7 +855,15 @@ def calltarget  : Operand<iPTR> {
   let PrintMethod = "printJumpOperand";
 }
 
-def imm64: Operand<i64>;
+def ConstantImmAsmOperandClass : AsmOperandClass {
+  let Name = "ConstantImm";
+  let PredicateMethod = "isConstantImm";
+  let RenderMethod = "addImmOperands";
+}
+
+def imm64: Operand<i64> {
+  let ParserMatchClass = ConstantImmAsmOperandClass;
+}
 
 def simm19_lsl2 : Operand<i32> {
   let EncoderMethod = "getSimm19Lsl2Encoding";



More information about the llvm-commits mailing list