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

[X86] Recognise VPMADD52H pattern via LowerMULH for the 52-bit case

Open sangam151 opened this issue 1 month ago • 1 comments

Follow-up to #153787 (PR #236663).

Context

PR #236663 implements VPMADD52H matching for the restricted case where x * y < 2^64 — i.e., mulhu(x, y) == 0. This covers inputs up to ~32 bits and folds add(lshr(mul(x, y), 52), acc) to vpmadd52huq.

The broader case — inputs up to 52 bits, product up to 104 bits — would need to match:

add(or(shl(mulhu(x, y), 12), shr(mullo(x, y), 52)), acc)

Why it's not in the original PR

As noted by @Andarwinux in the PR review, this shape doesn't survive to combineAdd on x86:

  • vXi64 MULHU is marked Expand and scalarizes into per-lane mulq during operation legalization, before DAG combining runs.
  • The or(shl, shr) shape never appears as a vector node in the DAG.
  • A matcher for the scalarized form would be fragile and highly dependent on legalizer decisions.

Suggested approach

LowerMULH custom lowering for vXi64 — mark ISD::MULHU as Custom for v2i64/v4i64/v8i64 and emit a vector sequence using vpmuludq chains. This would let the node survive as a proper vector op, enabling either:

  1. A combineAdd matcher for the full or(shl(mulhu, 12), shr(mullo, 52)) pattern, or
  2. Direct recognition in LowerMULH itself when the surrounding add-shift-or shape matches.

Relation to existing work

  • #206983 added vXi64 MULHU/MULHS lowering via forceExpandMultiply — worth building on.
  • #169819 (RKSimon) explored this territory previously.

Testing

Would need vector tests exercising:

  • Full 52-bit inputs (product in 104 bits)
  • Negative cases (inputs not provably <=52 bits)
  • Per-lane scalarization avoidance checks

sangam151 avatar Oct 29 '26 13:10 sangam151

@llvm/issue-subscribers-backend-x86

Author: Sangam Paudel (sangam151)

Follow-up to #153787 (PR #236663).

Context

PR #236663 implements VPMADD52H matching for the restricted case where x * y &lt; 2^64 — i.e., mulhu(x, y) == 0. This covers inputs up to ~32 bits and folds add(lshr(mul(x, y), 52), acc) to vpmadd52huq.

The broader case — inputs up to 52 bits, product up to 104 bits — would need to match:

add(or(shl(mulhu(x, y), 12), shr(mullo(x, y), 52)), acc)

Why it's not in the original PR

As noted by @RKSimon in the PR review, this shape doesn't survive to combineAdd on x86:

  • vXi64 MULHU is marked Expand and scalarizes into per-lane mulq during operation legalization, before DAG combining runs.
  • The or(shl, shr) shape never appears as a vector node in the DAG.
  • A matcher for the scalarized form would be fragile and highly dependent on legalizer decisions.

Suggested approach

LowerMULH custom lowering for vXi64 — mark ISD::MULHU as Custom for v2i64/v4i64/v8i64 and emit a vector sequence using vpmuludq chains. This would let the node survive as a proper vector op, enabling either:

  1. A combineAdd matcher for the full or(shl(mulhu, 12), shr(mullo, 52)) pattern, or
  2. Direct recognition in LowerMULH itself when the surrounding add-shift-or shape matches.

Relation to existing work

  • #206983 added vXi64 MULHU/MULHS lowering via forceExpandMultiply — worth building on.
  • #169819 (RKSimon) explored this territory previously.

Testing

Would need vector tests exercising:

  • Full 52-bit inputs (product in 104 bits)
  • Negative cases (inputs not provably <=52 bits)
  • Per-lane scalarization avoidance checks