-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Lang] Support continue
on all backends
#716
Conversation
taichi/ir/ir.h
Outdated
TI_STMT_REG_FIELDS; | ||
} | ||
|
||
bool as_return() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuanming-hu I just realized that this will require us to encapsulate the actual kernel body as a function (LLVM does this, but Metal doesn't). Otherwise when we use one thread to handle more than one elements, this could prematurely terminate the entire kernel thread.
Alternatively, do you folks think it even makes sense to support continue
at the top level? I mean Taichi users are supposed to well understand that the top-level loop as parallelized, yet continue
makes sense only for serial loops.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise when we use one thread to handle more than one element, this could prematurely terminate the entire kernel thread.
Right, sounds like we have to goto
in the source-to-source backends...The other option is to directly translate this into a continue
in the source backends. But we have to be very careful when doing that...
Alternatively, do you folks think it even makes sense to support
continue
at the top level? I mean Taichi users are supposed to well understand that the top-level loop as parallelized, yetcontinue
makes sense only for serial loops.
I think it still makes sense. For example,
for i in x:
if x[i] > 0:
# statement
# statement
if y[i] > 0:
# statement
# statement
# statement
might be more verbose than
for i in x:
if x[i] <= 0:
continue
# statement
# statement
if y[i] <= 0:
continue
# statement
# statement
# statement
(especially when we have multiple nested if
s)
We should probably document this in the documentation: continue
at the top level is only for skipping the rest of the current iteration. There's no guarantee about iteration order in a parallel setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using continue
as a shortcut of return
in top-level? Nice idea!
Alternatively, do you folks think it even makes sense to support continue at the top level? I mean Taichi users are supposed to well understand that the top-level loop as parallelized, yet continue makes sense only for serial loops.
We want to keep this easy-to-use shortcut since they acts same as the expected in python.
In comparison, we don't want break
in top-level is because it can't act same as the expected in python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other option is to directly translate this into a continue in the source backends. But we have to be very careful when doing that...
I guess this means we assume the kernel body is embedded inside a "meta" for-loop implemented by the Taichi runtime:
taichi/taichi/runtime/llvm/runtime.cpp
Lines 1074 to 1077 in 58c64ba
while (idx < end) { | |
func(context, idx); | |
idx += block_dim() * grid_dim(); | |
} |
Then I think wrapping the kernel into a function is a much more robust and cleaner approach...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, sounds like I should keep this support. Thanks guys :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think wrapping the kernel into a function is a much more robust and cleaner approach...
Right, then I guess the source-to-source backends have to support something like non-linear code emission since now parallel loop bodies are outlined into functions. Pragmatically maybe using a continue
is still the easiest way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, then I guess the source-to-source backends have to support something like non-linear code emission since now parallel loop bodies are outlined into functions.
+1
Pragmatically maybe using a continue is still the easiest way.
Right. I just realized a problem on Metal. Its range_for
doesn't use grid-stride loop, yet its struct_for
does, gahhh... 😦
taichi/ir/ir.h
Outdated
TI_STMT_REG_FIELDS; | ||
} | ||
|
||
bool as_return() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise when we use one thread to handle more than one element, this could prematurely terminate the entire kernel thread.
Right, sounds like we have to goto
in the source-to-source backends...The other option is to directly translate this into a continue
in the source backends. But we have to be very careful when doing that...
Alternatively, do you folks think it even makes sense to support
continue
at the top level? I mean Taichi users are supposed to well understand that the top-level loop as parallelized, yetcontinue
makes sense only for serial loops.
I think it still makes sense. For example,
for i in x:
if x[i] > 0:
# statement
# statement
if y[i] > 0:
# statement
# statement
# statement
might be more verbose than
for i in x:
if x[i] <= 0:
continue
# statement
# statement
if y[i] <= 0:
continue
# statement
# statement
# statement
(especially when we have multiple nested if
s)
We should probably document this in the documentation: continue
at the top level is only for skipping the rest of the current iteration. There's no guarantee about iteration order in a parallel setting.
(May be related to #689 ) This came up when I was writing this PR, would it be good if we split |
Right, ideally FrontendIR and Expressions are unique to the Taichi language, which should be translated into the CHI IR via This separation will improve modularity since now the frontend and middle-end IR (CHI) are separate into two modules. In fact, |
Related issue = #708
[Click here for the format server]