[llvm] [MIPatternMatch] Fix incorrect argument type of m_Type (PR #121074)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 24 15:59:43 PST 2024


https://github.com/mshockwave created https://github.com/llvm/llvm-project/pull/121074

m_Type is supposed to extract the underlying value type (equality type comparison is covered by m_SpecificType), therefore it should take a LLT reference as its argument rather than passing by value.

This is an oversight of de256478e61d6488db751689af82d280ba114a6f, which refactors out a good chunk of LLT reference usages. And it's just so happen that (for some reasons) no one is using m_Type and no test was covering it.

>From 823270bb68312556fbfc2f158fee8b9341b800d5 Mon Sep 17 00:00:00 2001
From: Min Hsu <min.hsu at sifive.com>
Date: Tue, 24 Dec 2024 15:51:24 -0800
Subject: [PATCH] [MIPatternMatch] Fix incorrect argument type of m_Type

m_Type should extract the underlying value type, therefore it should
take a LLT reference as its argument rather than passing by value.

This is an oversight of de256478e61d6488db751689af82d280ba114a6f, which
refactors out a good chunk of LLT reference usages. And it's just so
happen that (for some reasons) no one is using m_Type and no test
covered it either.
---
 llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h  | 4 ++--
 llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
index ea6ed322e9b192..80d1fef7533c9f 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
@@ -338,7 +338,7 @@ template <> struct bind_helper<MachineInstr *> {
 };
 
 template <> struct bind_helper<LLT> {
-  static bool bind(const MachineRegisterInfo &MRI, LLT Ty, Register Reg) {
+  static bool bind(const MachineRegisterInfo &MRI, LLT &Ty, Register Reg) {
     Ty = MRI.getType(Reg);
     if (Ty.isValid())
       return true;
@@ -368,7 +368,7 @@ template <typename Class> struct bind_ty {
 
 inline bind_ty<Register> m_Reg(Register &R) { return R; }
 inline bind_ty<MachineInstr *> m_MInstr(MachineInstr *&MI) { return MI; }
-inline bind_ty<LLT> m_Type(LLT Ty) { return Ty; }
+inline bind_ty<LLT> m_Type(LLT &Ty) { return Ty; }
 inline bind_ty<CmpInst::Predicate> m_Pred(CmpInst::Predicate &P) { return P; }
 inline operand_type_match m_Pred() { return operand_type_match(); }
 
diff --git a/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp b/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
index 59a86fa5646f36..bcaa321e49c10c 100644
--- a/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp
@@ -576,6 +576,11 @@ TEST_F(AArch64GISelMITest, MatchMiscellaneous) {
   auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
   Register Reg = MIBAdd.getReg(0);
 
+  // Extract the type.
+  LLT Ty;
+  EXPECT_TRUE(mi_match(Reg, *MRI, m_GAdd(m_Type(Ty), m_Reg())));
+  EXPECT_EQ(Ty, s64);
+
   // Only one use of Reg.
   B.buildCast(LLT::pointer(0, 32), MIBAdd);
   EXPECT_TRUE(mi_match(Reg, *MRI, m_OneUse(m_GAdd(m_Reg(), m_Reg()))));



More information about the llvm-commits mailing list