Skip to content

fix(structured): correct memref pointer and wraparound lowering#43

Merged
WuXintong123 merged 1 commit into
RuyiAI-Stack:mainfrom
maochuanjun:fix/structured-to-memref-lowering
Jul 17, 2026
Merged

fix(structured): correct memref pointer and wraparound lowering#43
WuXintong123 merged 1 commit into
RuyiAI-Stack:mainfrom
maochuanjun:fix/structured-to-memref-lowering

Conversation

@maochuanjun

Copy link
Copy Markdown

Summary

Fix StructuredToMemref pointer analysis and lowering for rank-1 wraparound, non-unit and zero strides, side-by-side accesses, and masked gather/scatter paths.

The previous implementation was primarily shaped around rank-2 split pointers and unit-stride assumptions. It could compute incorrect offsets for linear or side-by-side wraparound, use invalid mask dimensions, or apply fusion patterns whose address assumptions were not valid.

Problems addressed

  • Rank-1 wraparound split pointers were not fully materialized.
  • Linear wraparound could calculate an incorrect second segment start or size, especially for non-unit or zero strides.
  • Side-by-side wraparound mixed row and column stride calculations.
  • Mask dimensions could be shorter than the pointer rank.
  • Masked reduce/store fusion could be selected for non-unit-stride pointers, where its address model is invalid.
  • Rank-1 gather/scatter lowering needed a tensor-based fallback instead of relying on the problematic memref materialization path.

Changes

  • Harden analysis helpers:

    • return nullopt for empty OpFoldResult integer queries;
    • avoid propagating an AndOp restriction through an absent structured dimension;
    • extend mask dimensions to cover the rank of GatherScatterPtr and
      MakeTensorPtr.
  • Add explicit linear wraparound support:

    • introduce the WRAP_LINEAR representation;
    • calculate rank-1 split sizes and second-segment starts safely;
    • handle runtime zero stride without invalid division;
    • materialize rank-1 split tensor pointers and casts.
  • Correct side-by-side wraparound:

    • derive offsets from the in-row column position and column stride;
    • handle zero row stride explicitly;
    • avoid treating row stride as column displacement.
  • Restrict unsafe fusions:

    • apply masked reduction and masked elementwise-store fusion only when the
      rank-1 pointer has a statically unit stride.
  • Extend split/gather/scatter lowering:

    • support rank-1 and rank-2 split-pointer rewriting;
    • add linear copy, subview, slice, and masked-store paths;
    • lower rank-1 gather/scatter through tensor.empty, linalg.fill, scf.for, and tensor.insert;
    • retain the existing memref allocation path for higher-rank cases.

Regression coverage

New tests:

  • wraparound_linear_stride.mlir

    • rank-1 linear wraparound with non-unit stride.
  • wraparound_linear_zero_stride.mlir

    • runtime zero-stride handling, including safe select and divsi paths.
  • wraparound_side_by_side_non_unit_stride.mlir

    • side-by-side accesses with non-unit column stride and zero row stride.
  • tensor_first_vector_cpu_strided_load.mlir

    • verifies that a stride-2 load does not take the masked-elementwise fusion path.

Updated tests cover:

  • rank-1 and rank-2 elementwise stores;
  • splat stores;
  • static side-by-side wraparound;
  • gather/scatter pointer lowering, including loop-contained paths;
  • rank-1 masked reduction fallback behavior.

@maochuanjun
maochuanjun force-pushed the fix/structured-to-memref-lowering branch from 8b1bd72 to 8910b4e Compare July 12, 2026 12:23
@WuXintong123

Copy link
Copy Markdown
Collaborator

The rank-1 lowering represents the access with exactly two strided memrefs, which is only correct when the sequence wraps at most once. For example, start=8, modulo=10, stride=3, size=8 should access 8,1,4,7,0,3,6,9, but the second memref accesses 1,4,7,10,13,16,19. Please either support repeated wraparound or explicitly reject/prove impossible cases before applying this lowering, and add a regression test covering a second wrap.

Scope: StructuredToMemref lowering plus supporting analysis and regression coverage.
@maochuanjun
maochuanjun force-pushed the fix/structured-to-memref-lowering branch from 8910b4e to 0cf35f5 Compare July 16, 2026 12:22
@maochuanjun

Copy link
Copy Markdown
Author

Thanks for pointing this out. I have replaced the two-memref rank-1 lowering, which could only represent one wrap, with elementwise lowering.

Also I added regression tests for repeated-wrap loads/stores, negative strides, negative initial offsets, and runtime zero stride.

PTAL,thx.

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