Skip to content

[Mips] Remove unused function templates (NFC) - #202971

Merged
vgvassilev merged 2 commits into
llvm:mainfrom
flash1729:wunused-template-mips
Jun 29, 2026
Merged

vgvassilev merged 2 commits into
llvm:mainfrom
flash1729:wunused-template-mips

Conversation

@flash1729

Copy link
Copy Markdown
Member

A few function templates in the Mips MC layer have no callers anywhere, so they never get instantiated and trip -Wunused-template. They all live in .cpp files, so this is plain dead-code removal.

MipsELFObjectWriter.cpp: copy_if_else and find_best lost their only user when sortRelocs was rewritten in #104723. The FindBestPredicateResult enum existed only for find_best, so it goes too.

MipsDisassembler.cpp: DecodeDAHIDATIMMR6 isn't referenced by any DecoderMethod. The r6 DAHI/DATI instructions decode through its sibling DecodeDAHIDATI, which is what the generated tables actually call.

NFC: removes uninstantiated templates only.

Part of #202945.

A few function templates in the Mips MC layer have no callers anywhere, so they
never get instantiated and trip `-Wunused-template`. They all live in `.cpp`
files, so this is plain dead-code removal.

`MipsELFObjectWriter.cpp`: `copy_if_else` and `find_best` lost their only user
when `sortRelocs` was rewritten in llvm#104723. The `FindBestPredicateResult` enum
existed only for `find_best`, so it goes too.

`MipsDisassembler.cpp`: `DecodeDAHIDATIMMR6` isn't referenced by any
`DecoderMethod`. The r6 DAHI/DATI instructions decode through its sibling
`DecodeDAHIDATI`, which is what the generated tables actually call.

NFC: removes uninstantiated templates only.

Part of llvm#202945.
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-mips

Author: Aditya Medhane (flash1729)

Changes

A few function templates in the Mips MC layer have no callers anywhere, so they never get instantiated and trip -Wunused-template. They all live in .cpp files, so this is plain dead-code removal.

MipsELFObjectWriter.cpp: copy_if_else and find_best lost their only user when sortRelocs was rewritten in #104723. The FindBestPredicateResult enum existed only for find_best, so it goes too.

MipsDisassembler.cpp: DecodeDAHIDATIMMR6 isn't referenced by any DecoderMethod. The r6 DAHI/DATI instructions decode through its sibling DecodeDAHIDATI, which is what the generated tables actually call.

NFC: removes uninstantiated templates only.

Part of #202945.


Full diff: https://github.com/llvm/llvm-project/pull/202971.diff

2 Files Affected:

  • (modified) llvm/lib/Target/Mips/Disassembler/MipsDisassembler.cpp (-15)
  • (modified) llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp (-55)
diff --git a/llvm/lib/Target/Mips/Disassembler/MipsDisassembler.cpp b/llvm/lib/Target/Mips/Disassembler/MipsDisassembler.cpp
index 808ff67428c12..bbd614fc64423 100644
--- a/llvm/lib/Target/Mips/Disassembler/MipsDisassembler.cpp
+++ b/llvm/lib/Target/Mips/Disassembler/MipsDisassembler.cpp
@@ -338,21 +338,6 @@ static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
   return MCDisassembler::Success;
 }
 
-template <typename InsnType>
-static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn,
-                                       uint64_t Address,
-                                       const MCDisassembler *Decoder) {
-  InsnType Rs = fieldFromInstruction(insn, 16, 5);
-  InsnType Imm = fieldFromInstruction(insn, 0, 16);
-  MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
-                                       Rs)));
-  MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
-                                       Rs)));
-  MI.addOperand(MCOperand::createImm(Imm));
-
-  return MCDisassembler::Success;
-}
-
 template <typename InsnType>
 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
                                    const MCDisassembler *Decoder) {
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
index 680d27927ba36..4defb4f12db69 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
@@ -54,63 +54,8 @@ class MipsELFObjectWriter : public MCELFObjectTargetWriter {
   void sortRelocs(std::vector<ELFRelocationEntry> &Relocs) override;
 };
 
-/// The possible results of the Predicate function used by find_best.
-enum FindBestPredicateResult {
-  FindBest_NoMatch = 0,  ///< The current element is not a match.
-  FindBest_Match,        ///< The current element is a match but better ones are
-                         ///  possible.
-  FindBest_PerfectMatch, ///< The current element is an unbeatable match.
-};
-
 } // end anonymous namespace
 
-/// Copy elements in the range [First, Last) to d1 when the predicate is true or
-/// d2 when the predicate is false. This is essentially both std::copy_if and
-/// std::remove_copy_if combined into a single pass.
-template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
-static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
-                                                    OutputIt1 d1, OutputIt2 d2,
-                                                    UnaryPredicate Predicate) {
-  for (InputIt I = First; I != Last; ++I) {
-    if (Predicate(*I)) {
-      *d1 = *I;
-      d1++;
-    } else {
-      *d2 = *I;
-      d2++;
-    }
-  }
-
-  return std::make_pair(d1, d2);
-}
-
-/// Find the best match in the range [First, Last).
-///
-/// An element matches when Predicate(X) returns FindBest_Match or
-/// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
-/// the search. BetterThan(A, B) is a comparator that returns true when A is a
-/// better match than B. The return value is the position of the best match.
-///
-/// This is similar to std::find_if but finds the best of multiple possible
-/// matches.
-template <class InputIt, class UnaryPredicate, class Comparator>
-static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
-                         Comparator BetterThan) {
-  InputIt Best = Last;
-
-  for (InputIt I = First; I != Last; ++I) {
-    unsigned Matched = Predicate(*I);
-    if (Matched != FindBest_NoMatch) {
-      if (Best == Last || BetterThan(*I, *Best))
-        Best = I;
-    }
-    if (Matched == FindBest_PerfectMatch)
-      break;
-  }
-
-  return Best;
-}
-
 /// Determine the low relocation that matches the given relocation.
 /// If the relocation does not need a low relocation then the return value
 /// is ELF::R_MIPS_NONE.

@github-actions

github-actions Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 135588 tests passed
  • 3413 tests skipped

✅ The build succeeded and all tests passed.

@flash1729

Copy link
Copy Markdown
Member Author

ping @yingopq @wzssyqa

@yingopq

yingopq commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

LGTM.

@vgvassilev
vgvassilev merged commit 9cc5827 into llvm:main Jun 29, 2026
8 checks passed
LouisLu060211 pushed a commit to LouisLu060211/llvm-project that referenced this pull request Jun 30, 2026
A few function templates in the Mips MC layer have no callers anywhere,
so they never get instantiated and trip `-Wunused-template`. They all
live in `.cpp` files, so this is plain dead-code removal.

`MipsELFObjectWriter.cpp`: `copy_if_else` and `find_best` lost their
only user when `sortRelocs` was rewritten in llvm#104723. The
`FindBestPredicateResult` enum existed only for `find_best`, so it goes
too.

`MipsDisassembler.cpp`: `DecodeDAHIDATIMMR6` isn't referenced by any
`DecoderMethod`. The r6 DAHI/DATI instructions decode through its
sibling `DecodeDAHIDATI`, which is what the generated tables actually
call.

NFC: removes uninstantiated templates only.

Part of llvm#202945.
maarcosrmz pushed a commit to maarcosrmz/llvm-project that referenced this pull request Jul 1, 2026
A few function templates in the Mips MC layer have no callers anywhere,
so they never get instantiated and trip `-Wunused-template`. They all
live in `.cpp` files, so this is plain dead-code removal.

`MipsELFObjectWriter.cpp`: `copy_if_else` and `find_best` lost their
only user when `sortRelocs` was rewritten in llvm#104723. The
`FindBestPredicateResult` enum existed only for `find_best`, so it goes
too.

`MipsDisassembler.cpp`: `DecodeDAHIDATIMMR6` isn't referenced by any
`DecoderMethod`. The r6 DAHI/DATI instructions decode through its
sibling `DecodeDAHIDATI`, which is what the generated tables actually
call.

NFC: removes uninstantiated templates only.

Part of llvm#202945.
vgvassilev pushed a commit that referenced this pull request Jul 6, 2026
Uncomment `UnusedTemplate` in the `Unused` diagnostic group so
`-Wunused-template` becomes part of `-Wall`.

`-Wunused-template` flags unused function and variable templates with
internal linkage. In a header, such a template gives every translation
unit its own internal-linkage copy, which is a latent ODR violation
(ill-formed, no diagnostic required), so enabling this surfaces a real
class of bugs and keeps the pattern from reappearing.

### Depends on

The in-tree occurrences were cleaned up first in a series of NFC PRs
tracked by #202945. This change only builds clean once those land, so CI
will be red on any area whose cleanup is not yet merged. Remaining
prerequisites:

Approved
- #202969
- #202971
- #202973
- #202974
- #206309
- #206312

Needs to be reviewed 
- #202988
- #206308

Closes #202945

cc @AaronBallman @vgvassilev
gandhi56 pushed a commit that referenced this pull request Jul 9, 2026
Uncomment `UnusedTemplate` in the `Unused` diagnostic group so
`-Wunused-template` becomes part of `-Wall`.

`-Wunused-template` flags unused function and variable templates with
internal linkage. In a header, such a template gives every translation
unit its own internal-linkage copy, which is a latent ODR violation
(ill-formed, no diagnostic required), so enabling this surfaces a real
class of bugs and keeps the pattern from reappearing.

### Depends on

The in-tree occurrences were cleaned up first in a series of NFC PRs
tracked by #202945. This change only builds clean once those land, so CI
will be red on any area whose cleanup is not yet merged. Remaining
prerequisites:

Approved
- #202969
- #202971
- #202973
- #202974
- #206309
- #206312

Needs to be reviewed 
- #202988
- #206308

Closes #202945

cc @AaronBallman @vgvassilev
@flash1729
flash1729 deleted the wunused-template-mips branch August 7, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants