llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[GlobalISel] maxScalarEltSameAsIf returns the other slot's whole type, which fails mutationIsSane when the lane counts differ

Open nodecoda opened this issue 1 month ago • 0 comments

LegalizeRuleSet::maxScalarEltSameAsIf decides whether to narrow by comparing element widths (correct), but the mutation it returns is the other slot's whole type, lane count included:

// llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h:1210
LegalizeRuleSet &maxScalarEltSameAsIf(LegalityPredicate Predicate,
                                      unsigned TypeIdx, unsigned SmallTypeIdx) {
  typeIdx(TypeIdx);
  return narrowScalarIf(
      [=](const LegalityQuery &Query) {
        return Query.Types[SmallTypeIdx].getScalarSizeInBits() <
                   Query.Types[TypeIdx].getScalarSizeInBits() &&
               Predicate(Query);
      },
      [=](const LegalityQuery &Query) {
        LLT T = Query.Types[SmallTypeIdx];   // <- whole type, not just the size
        return std::make_pair(TypeIdx, T);
      });
}

mutationIsSane() requires a NarrowScalar on a vector to keep the element count. Handing back a type with a different element count can never satisfy that, and when TypeIdx is a scalar the "narrowed" type is a vector.

Reproduction (verified)

Public API only — no target, no MIR, no table. This is the tail of AArch64's G_CTPOP rule set in isolation:

TEST(LegalizerInfoTest, MaxScalarEltSameAsIfLaneCountMismatch) {
  LegalizeRuleSet RS;
  RS.maxScalarEltSameAsIf([](const LegalityQuery &) { return true; }, 1, 0);
  // LegalityQuery holds an ArrayRef, so the array has to outlive the query.
  LLT Types[] = {LLT::fixed_vector(16, 8), LLT::fixed_vector(2, 32)};
  RS.apply(LegalityQuery(/*Opcode=*/0, Types));
}

Result (debug build, line 203 on main):

GlobalISelTests: llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp:203:
  llvm::LegalizeActionStep llvm::LegalizeRuleSet::apply(...):
  Assertion `mutationIsSane(Rule, Query, Mutation) &&
  "legality mutation invalid for match"' failed.

The mutation it tried to return was <16 x s8> for slot 1: the element size was correct (32 -> 8) but the lane count came along (2 -> 16).

Reachability

Not reachable from IR: for G_CTPOP the result and the operand always have the same shape, and the predicate is false when the element sizes are equal. It is reachable from anything that enumerates the legality space by multiplying the two slots' domains — a differential tester, a table generator, a fuzzer. That is how it was found: an AArch64 legality-table self-check that asks every (result, operand) pair.

Measured on that product domain (292 points): 66 of them go through this rule and are rejected as insane.

Why it should still be fixed

  • mutationIsSane is compiled out under NDEBUG (LegalizerInfo.cpp:100), so a release build does not crash here — it hands LegalizerHelper a step that claims NarrowScalar and carries a wider/differently-shaped type.
  • The fix is one line, and it makes the rule agree with its own predicate.

Suggested fix

Use the size, not the type, matching what minScalarEltSameAsIf already does:

-          LLT T = Query.Types[SmallTypeIdx];
+          LLT T = Query.Types[TypeIdx].changeElementSize(
+              Query.Types[SmallTypeIdx].getScalarSizeInBits());
           return std::make_pair(TypeIdx, T);

Measured with the patch:

  • the query above becomes NarrowScalar -> <2 x s8>: 2 lanes kept, element 32 -> 8;
  • the whole 292-point AArch64 product domain answers without an assertion;
  • no change on real code: -run-pass=legalizer output for 615 AArch64 and 177 X86 MIR files is byte-identical with and without the patch.

Relation to existing work

  • #220802 / #235976: the sibling bug in minScalarSameAs/maxScalarSameAs, where the predicate compares an element size against a total size. #235976 fixes that one and is orthogonal to this.
  • #236651: a pointer reaching changeElementSize in the same rule family.
  • This report is deliberately only the maxScalarEltSameAsIf half.

AI tool use

Found by a legality-space enumerator we are building; this report and the repro were drafted with Codex CLI assistance, reviewed and verified by the submitter.

nodecoda avatar Nov 01 '26 01:11 nodecoda