Skip to content

[MLIR][Tensor] Fix Chained tensor.cast canonicalization pattern #113551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,18 +433,23 @@ struct ChainedTensorCast : public OpRewritePattern<CastOp> {

// We can remove the intermediate cast if joining all three produces the
// same result as just joining the source and result shapes.
auto firstJoin =
joinShapes(joinShapes(sourceType, intermediateType), resultType);
auto firstJoin = joinShapes(sourceType, intermediateType);

// The join might not exist if the cast sequence would fail at runtime.
if (!firstJoin)
return failure();

auto secondJoin = joinShapes(firstJoin, resultType);

// The join might not exist if the cast sequence would fail at runtime.
if (!secondJoin)
return failure();

// The newJoin always exists if the above join exists, it might just contain
// less information. If so, we cannot drop the intermediate cast, as doing
// so would remove runtime checks.
auto newJoin = joinShapes(sourceType, resultType);
if (firstJoin != newJoin)
if (secondJoin != newJoin)
return failure();

rewriter.replaceOpWithNewOp<CastOp>(tensorCast, resultType,
Expand Down
Loading