Skip to content
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

[TIR] Additional Stmt/Expr simplication rules #11373

Merged
merged 5 commits into from
May 26, 2022

Conversation

Lunderberg
Copy link
Contributor

These came about after investigating some simplifications that may be useful for handling of padding introduced during padded layout transformations.

  • Enabled simplification of A[i] = A[i] + 0 into no-op. This was a bug introduced in [TE][TIR] Implement layout transformations, non-flat memory buffers #9727, which applied this rewrite only to A[i] = A[i], and not to statements which simplify to A[i] = A[i]. Regression test added to prevent reoccurrence of this bug.

  • Enabled simplification of x - x to zero for floating point types. Previously, this simplification was applied only for data types that could be used as buffer indices.

@tkonolige
Copy link
Contributor

x - x = 0 isn't valid for all IEEE floating point numbers. Specifically Inf - Inf = Nan and NaN - NaN = NaN. Not sure if we care about these edge cases though.

@Lunderberg
Copy link
Contributor Author

Good point, and I hadn't considered Nan. Looking through other operators, it looks like there are existing simplifications that would also change NaN propagation (e.g. Simplifying x==y || x!=y to true, or simplifying !(x <= y) to y < x), so it wouldn't break any existing guarantees.

if (IsIndexType(op->dtype)) {
// Index rules
// cancelation rules
Copy link
Member

Choose a reason for hiding this comment

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

The integer simplifcation should be fast path (and clean, hopefully without Nans), it would be better to put new rules in else branch even if it means some duplications, since (recursively) checking side effects is expensive.
Doing floating point simplification is probably fine, but given the possibility of introducing additional types, it might be safer to say something like

if (IsIndexType(op->dtype)) {
  old rules
} else if (op->dtype.is_float())
  new rules
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds reasonable, and updated as requested.

- Enabled simplification of `A[i] = A[i] + 0` into no-op.  This was a
  bug introduced in apache#9727, which
  applied this rewrite only to `A[i] = A[i]`, and not to statements
  which simplify to `A[i] = A[i]`.  Regression test added to prevent
  reoccurrence of this bug.

- Enabled simplification of `x - x` to zero for floating point types.
  Previously, this simplification was applied only for data types that
  could be used as buffer indices.
@Lunderberg Lunderberg force-pushed the tir_simplify_rules branch from 23bdc17 to bd7e8cd Compare May 25, 2022 15:06
@Lunderberg
Copy link
Contributor Author

Retriggering CI following #11456.

Comment on lines 259 to 266
// cancelation rules
TVM_TRY_REWRITE_IF(x - x, ZeroWithTypeLike(x),
SideEffect(x.Eval()) <= CallEffectKind::kReadState);
TVM_TRY_REWRITE_IF((x + y) - y, x, SideEffect(y.Eval()) <= CallEffectKind::kReadState);
TVM_TRY_REWRITE_IF((x + y) - x, y, SideEffect(x.Eval()) <= CallEffectKind::kReadState);
TVM_TRY_REWRITE_IF(x - (y + x), 0 - y, SideEffect(x.Eval()) <= CallEffectKind::kReadState);
TVM_TRY_REWRITE_IF(x - (x + y), 0 - y, SideEffect(x.Eval()) <= CallEffectKind::kReadState);

Copy link
Member

Choose a reason for hiding this comment

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

should this be removed since you've added the else branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the catch, and they are now removed.

@vinx13 vinx13 merged commit 52df2e8 into apache:main May 26, 2022
@Lunderberg Lunderberg deleted the tir_simplify_rules branch May 26, 2022 16:56
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.

3 participants