Skip to content

[Codegen] Crop partial reads instead of padding by a negative amount - #25330

Merged
jschuhmacher merged 2 commits into
mainfrom
users/jschuhmacher/pad_crop_unread_inputs
Dec 1, 2026
Merged

jschuhmacher merged 2 commits into
mainfrom
users/jschuhmacher/pad_crop_unread_inputs

Conversation

@jschuhmacher

Copy link
Copy Markdown
Contributor

When padding an op to tile-aligned sizes, an input can be larger than the
region the padded loops read, for example when a strided window leaves the last
column unread. Padding it then needs a negative amount, and compilation fails:

'tensor.pad' op padding must be non-negative, got -1

Such inputs are now cropped with tensor.extract_slice to the region that is
read, and then padded. The new cropAndPadTilingInterfaceOp helper is used by
GPUPadConvs and TensorMaskingOpInterface, so the fix applies to every
backend.

Assisted-by: Claude Code

@jschuhmacher
jschuhmacher marked this pull request as ready for review November 24, 2026 16:45
@jschuhmacher jschuhmacher changed the title [Codegen] Crop partly read inputs instead of padding them by a negative amount [Codegen] Crop partial reads instead of padding by a negative amount Nov 24, 2026

@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.

Why do we not have any regression tests? If the existing ones do not cover the additional behavior, I think it'd be nice to add some. Besides that LGTM % a few things my agent found that look reasonable.

if (failed(paddedShape)) {
continue;
}
Value cropped = createCropToPaddedShape(builder, op.getLoc(), input->get(),

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.

What guarantees that *paddedShape covers every element the op reads? With eight iterations, d1 floordiv 16 reads element zero every time, but the standalone masking pass crops tensor<1x1xf32> to tensor<1x0xf32> here and still succeeds. Could we bail out before changing any operands for maps the shape helper doesn’t support, and add this as a negative lit test?


FailureOr<linalg::PadTilingInterfaceResult> result =
linalg::rewriteAsPaddedOp(b, tilingInterfaceOp, options);
cropAndPadTilingInterfaceOp(b, tilingInterfaceOp, options);

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.

If the helper declines that map, LinalgGenericOpInterface::getMaskedImplementation still dereferences result->paddedOp. Can we check failed(result) there and return failure(), like the online attention model does? The outer pass already handles skipping the op

Padding an operation to tile-aligned sizes sizes each input to the region the
padded loops read. A strided or dilated window can leave trailing elements of an
input unread, so that region can be smaller than the input. The padding then
needed a negative amount, which tensor.pad rejects, and compilation failed, for
example for grouped convolutions on gfx942. Such inputs are now cropped to the
region that is read before they are padded, wherever IREE pads operations
through the TilingInterface.

Signed-off-by: Jelle Schuhmacher <[email protected]>

Assisted-by: Claude Code
Assisted-by: Claude Code
Signed-off-by: Jelle Schühmacher <[email protected]>
@jschuhmacher
jschuhmacher force-pushed the users/jschuhmacher/pad_crop_unread_inputs branch from 3c02921 to 650fc13 Compare November 27, 2026 14:53
@jschuhmacher

Copy link
Copy Markdown
Contributor Author

Thanks @egebeysel!

@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!

@jschuhmacher
jschuhmacher merged commit e45c109 into main Dec 1, 2026
60 checks passed
@jschuhmacher
jschuhmacher deleted the users/jschuhmacher/pad_crop_unread_inputs branch December 1, 2026 08:44
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