Skip to content

[TVMScript] Output elif where possible#13433

Merged
junrushao merged 1 commit intoapache:mainfrom
Lunderberg:tvmscript_elif
Nov 19, 2022
Merged

[TVMScript] Output elif where possible#13433
junrushao merged 1 commit intoapache:mainfrom
Lunderberg:tvmscript_elif

Conversation

@Lunderberg
Copy link
Contributor

@Lunderberg Lunderberg commented Nov 18, 2022

When a TIR IfThenElseNode::else_case is itself a IfThenElse node, it may be printed as elif instead of else: if .... The elif syntax was already handled on the parsing side, so this commit only changes the TVMScript printer.

This change also incidentally altered the printer's output for an else block whose condition is True. Previously, these were dropped from the output altogether. Now, they are retained in the else block. Removing no-op statements should be done by a transformation pass, not a printer/parser round trip, so this change in behavior makes sense to keep.

Below are shown the same prim func, printed before and after this change.

# Before
@T.prim_func
def func(i: T.int32):
    # body
    if i == 0:
        T.evaluate(0)
    else:
        if i == 1:
            T.evaluate(1)
        else:
            if i == 2:
                T.evaluate(2)
            else:
                T.evaluate(3)

# After
@T.prim_func
def func(i: T.int32):
    # body
    if i == 0:
        T.evaluate(0)
    elif i == 1:
        T.evaluate(1)
    elif i == 2:
        T.evaluate(2)
    else:
        T.evaluate(3)

When a TIR `IfThenElseNode::else_case` is itself a `IfThenElse` node,
it may be printed as `elif` instead of `else: if ...`.  The `elif`
syntax was already handled on the parsing side, so this commit only
changes the TVMScript printer.

This change also incidentally altered the printer's output for an else
block whose condition is `True`.  Previously, these were dropped from
the output altogether.  Now, they are retained in the else block.
Removing no-op statements should be done by a transformation pass, not
a printer/parser round trip, so this change in behavior makes sense to
keep.
@tvm-bot
Copy link
Collaborator

tvm-bot commented Nov 18, 2022

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

  • No users to tag found in teams: tvmscript See #10317 for details
  • Built docs for commit 918b2a9 can be found here.

Generated by tvm-bot

Copy link
Contributor

@yelite yelite left a comment

Choose a reason for hiding this comment

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

This looks good! Thanks for making this improvement!

doc << Doc::NewLine();
doc << "else:" << Doc::Indent(4, Doc::NewLine() << PrintBody(op->else_case.value()));

Optional<Stmt> else_case = op->else_case;

Choose a reason for hiding this comment

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

would that be possible that we can add a comment here or a code snippet in the PR to show the elif print before and after this change. Otherwise LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly! Added a quick example to the PR description.

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

LGTM!

@junrushao junrushao merged commit 76a6e71 into apache:main Nov 19, 2022
@Lunderberg Lunderberg deleted the tvmscript_elif branch November 22, 2022 17:20
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
When a TIR `IfThenElseNode::else_case` is itself a `IfThenElse` node,
it may be printed as `elif` instead of `else: if ...`.  The `elif`
syntax was already handled on the parsing side, so this commit only
changes the TVMScript printer.

This change also incidentally altered the printer's output for an else
block whose condition is `True`.  Previously, these were dropped from
the output altogether.  Now, they are retained in the else block.
Removing no-op statements should be done by a transformation pass, not
a printer/parser round trip, so this change in behavior makes sense to
keep.
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.

5 participants