You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are quite a few recent issues caused by insertion point management for imperative code, so I would like to open a thread to explain and discuss the problem.
HeteroCL code example
This is an example with an if and an else if branch, and two conditions.
Note that in this case, the condition operations are inserted after the scf.if operation. The condition of second if %7 is used before defined, which causes an error.
The insertion point issue
Concisely, this issue is caused by resetting the operation insertion point when the program exits with hcl.if_() scope, and the condition expression is evaluated before the program entering the with hcl.elif() scope. An insertion point is a position we insert operations in the IR, for example, at the beginning or end of a block, or after an operation. When we build a if operation with a with hcl.if_() scope, we set the insertion point to the then block of the scf.if operation. When we exit the with hcl.if_() scope, we need to reset the insertion point back to after the scf.if operation. Here is an example to illustrate this point:
withhcl.if(cond):
hcl.print(...) # insertion point should be set inside `scf.if`'s `then` blockhcl.compute(...) # insertion point should be set after the `scf.if` operation, so these operations don't get inserted to `scf.if`
However, due to the evaluation order of Python, when we have a if + elif statement like this:
withhcl.if(cond1):
passwithhcl.elif(cond2):
pass
cond2 is evaluated before the program jumps to with hcl.elif. At this point when cond2 is evaluated, the insertion point is set to "after scf.if operation", hence the wrong IR above.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
There are quite a few recent issues caused by insertion point management for imperative code, so I would like to open a thread to explain and discuss the problem.
HeteroCL code example
This is an example with an if and an else if branch, and two conditions.
We implement such else if branch as a nested if in the first if's else branch, so the IR should look like this:
Note that the operations that evaluate the second if's condition should be inserted inside the else block.
However, because of the insertion point issue, we get IR like this:
Note that in this case, the condition operations are inserted after the
scf.if
operation. The condition of second if%7
is used before defined, which causes an error.The insertion point issue
Concisely, this issue is caused by resetting the operation insertion point when the program exits
with hcl.if_()
scope, and thecondition
expression is evaluated before the program entering thewith hcl.elif()
scope. An insertion point is a position we insert operations in the IR, for example, at the beginning or end of a block, or after an operation. When we build a if operation with awith hcl.if_()
scope, we set the insertion point to thethen
block of thescf.if
operation. When we exit thewith hcl.if_()
scope, we need to reset the insertion point back to after thescf.if
operation. Here is an example to illustrate this point:However, due to the evaluation order of Python, when we have a if + elif statement like this:
cond2
is evaluated before the program jumps towith hcl.elif
. At this point whencond2
is evaluated, the insertion point is set to "afterscf.if
operation", hence the wrong IR above.Beta Was this translation helpful? Give feedback.
All reactions