-
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
Assigning to kernel argument leads to no error but wrong behavior #5619
Comments
I'd advocate for non-mutable function parameters unless it's a pointer/template type, and should make this an error. (ndarrays and fields parameters can be considered as a reference semantically) |
|
I once made it immutable in #3105 but this broke some internal code because we allowed them to be mutable in the past, and the PR was reverted very soon. If we do make this change this time, we may need to notify the users in the release note. |
OK... Thanks for the info! Sounds like we need a deeper discussion on this. If we take the mutable solution and make it watertight, do we need much more work? For example, allowing atomic adds to the argument sounds like a lot of work (you may need a pointer to the argument list?): @ti.kernel
def foo(a: ti.i32):
for i in range(10):
a += i
print(a) If the amount of work to make the arguments mutable is huge, we'd rather make it immutable. |
If it's internal code we are breaking we should fix them ourselves. I'm not exactly sure how this is done? In the IR we have ArgLoad but I don't think it works the same way as allocas. I think we should just make it a compiler warning at first and keep it what it is in the codegen, and then make it an error some versions later |
The solution turns out to be pretty simple: we can assign the argument to a new variable. |
Yeah, that could be a solution. But I'm afraid that may lead to some confusion: if one thread modifies that variable, is that visible to other threads? Users will assume the kernel arguments are shared among threads (since it's kernel-level) instead of being thread-local. What about serial offloaded tasks? E.g., @ti.kernel
def foo(a: ti.i32):
for i in range(10):
a = i
print(a) # What should be the result here? The other thing I worry about is register usage on GPUs. We may need an extra optimization pass to eliminate these introduced temporary variables. |
If we make
Yeah. Writing this may take a while. It's possible that I can finish writing the pass before 1.1.0 releases (2 days left) but I can't 100% guarantee. The fastest temporary solution is to throw a warning (or error?) when user alter a scalar argument in non-outermost scope. |
According to offline discussions, we decide to make kernel arguments immutable. |
Related issue = fixes #5619 <!-- Thank you for your contribution! If it is your first time contributing to Taichi, please read our Contributor Guidelines: https://docs.taichi-lang.org/docs/contributor_guide - Please always prepend your PR title with tags such as [CUDA], [Lang], [Doc], [Example]. For a complete list of valid PR tags, please check out https://github.com/taichi-dev/taichi/blob/master/misc/prtags.json. - Use upper-case tags (e.g., [Metal]) for PRs that change public APIs. Otherwise, please use lower-case tags (e.g., [metal]). - More details: https://docs.taichi-lang.org/docs/contributor_guide#pr-title-format-and-tags - Please fill in the issue number that this PR relates to. - If your PR fixes the issue **completely**, use the `close` or `fixes` prefix so that GitHub automatically closes the issue when the PR is merged. For example, Related issue = close #2345 - If the PR does not belong to any existing issue, free to leave it blank. --> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Describe the bug
The code below
Should either emits an error (kernel argument cannot be assigned), or print
1
. But it prints0
:The text was updated successfully, but these errors were encountered: