Skip to content

[Codegen][CPU] Fold reshapes into map_store per reassociation group. - #24535

Merged
bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-map-store-per-group-fold
Jun 5, 2026
Merged

bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-map-store-per-group-fold

Conversation

@bjacob

@bjacob bjacob commented May 25, 2026

Copy link
Copy Markdown
Collaborator

foldReshapeIntoMapStore composed an expand/collapse reshape into a map_store's index transformation via one global linearize over the source shape plus delinearize over the result shape. Under dynamic shapes that delinearize lowers to a dynamic integer division per element.

Fold each reassociation group independently instead: an expand_shape splits one source index by just that group's result sizes; a collapse_shape merges one group of source indices. The split/merge factors are then per-group -- typically static tile sizes -- so the scalarized transformation is static shifts/masks with no per-element division.

Speeds up the i8 data-tiled matmul's C-encode dispatch; i8 inner_tiled matmul, 4096x4096 on Zen 4: 52.9ms -> 46.9ms.

Also document, at MapStoreOpVectorizationModel::isVectorizable, why a CPU-tiled map_store reaches the vectorizer dynamically shaped and scalarizes -- which is precisely what makes this per-group, division-free fold matter -- while the same op vectorizes on GPU. Comment only, no behavior change.

Progress towards #24515.

@bjacob
bjacob marked this pull request as ready for review June 1, 2026 15:27
@AGindinson
AGindinson requested a review from egebeysel June 3, 2026 13:02
@bjacob
bjacob force-pushed the users/bjacob/cpu-i8-mma-16x16x2 branch from a8e703f to e880789 Compare June 3, 2026 18:46
@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch from fd5a727 to ba0c333 Compare June 3, 2026 18:46

@egebeysel egebeysel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me, but I might be missing some context here. Does this only change the behavior on the CPU and not GPU?

@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch from ba0c333 to b57e003 Compare June 5, 2026 14:54
@bjacob
bjacob force-pushed the users/bjacob/cpu-i8-mma-16x16x2 branch from e880789 to 9816c54 Compare June 5, 2026 15:11
@bjacob
bjacob requested a review from jtuyls as a code owner June 5, 2026 15:11
@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch from b57e003 to c3a3ad5 Compare June 5, 2026 15:11

@egebeysel egebeysel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, LGTM % one test comment.

@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch from c3a3ad5 to e9aee6a Compare June 5, 2026 15:26

@egebeysel egebeysel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just the test comments Claude generated are a bit too verbose this time :)


// -----

// Pre-PR (now-removed) global form: one `affine.delinearize_index` over the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we rewrite these comments from a higher level perspective without mentioning pre-PR, or implementation level details, just mentioning that the per-group fold avoids the divsi in both the positive and negative tests?

@bjacob bjacob Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same treatment in the latest push. The function-level comment now reads "Negative case for contrast: a global affine.delinearize_index over a fully dynamic result shape — the per-group fold avoids producing this IR," and the CHECK-line comment is one sentence: "Scalarizes to runtime-divisor arith.divsi/remsi (divisors are arith.muli products of the dynamic factors) — the cost the per-group fold avoids." Same trim on the positive test (@lowered_per_group_no_division) and on the top-of-file EXPAND RUN-line comment.


// -----

// Pre-PR vs. new lowering -- contrast the post-`affine-expand-index-ops` shape

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, can be much shorter and just mention what the per-group (de-)linearize enables/avoids.

@bjacob bjacob Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in the latest push — the block is now four lines, framed in terms of what the per-group fold does (lowered body is division-free; uses muli/addi by the static tile size) and what it avoids (runtime-divisor arith.divsi/remsi from a global delinearize_index over the full dynamic shape). No "pre-PR" or foldReshapeIntoMapStore references.

Base automatically changed from users/bjacob/cpu-i8-mma-16x16x2 to main June 5, 2026 16:20
@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch 3 times, most recently from 2f3d68f to 47fb47a Compare June 5, 2026 16:59
`foldReshapeIntoMapStore` composed an expand/collapse reshape into a
`map_store`'s index transformation via one global `linearize` over the
source shape plus `delinearize` over the result shape. Under dynamic
shapes that `delinearize` lowers to a dynamic integer division per
element.

Fold each reassociation group independently instead: an `expand_shape`
splits one source index by just that group's result sizes; a
`collapse_shape` merges one group of source indices. The split/merge
factors are then per-group -- typically static tile sizes -- so the
scalarized transformation is static shifts/masks with no per-element
division.

Speeds up the i8 data-tiled matmul's C-encode dispatch; i8 inner_tiled
matmul, 4096x4096 on Zen 4: 52.9ms -> 46.9ms.

Also document, at `MapStoreOpVectorizationModel::isVectorizable`, why a
CPU-tiled `map_store` reaches the vectorizer dynamically shaped and
scalarizes -- which is precisely what makes this per-group, division-free
fold matter -- while the same op vectorizes on GPU. Comment only, no
behavior change.

Progress towards #24515.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
@bjacob
bjacob force-pushed the users/bjacob/cpu-map-store-per-group-fold branch from 47fb47a to 7598a04 Compare June 5, 2026 18:00
@bjacob
bjacob merged commit c1c962b into main Jun 5, 2026
66 of 67 checks passed
@bjacob
bjacob deleted the users/bjacob/cpu-map-store-per-group-fold branch June 5, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants