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

[Lang] Support break in native for statement #8352

Closed
wants to merge 3 commits into from

Conversation

jim19930609
Copy link
Contributor

@jim19930609 jim19930609 commented Sep 20, 2023

Issue: fix #8351

Brief Summary

🤖 Generated by Copilot at f484174

This pull request adds support for the break statement in for loops in Taichi. It modifies the IR, the LLVM code generator, and the AST lowering transform to handle the break statement correctly. It also adds a test case to tests/python/test_for_break.py to verify the functionality.

Walkthrough

🤖 Generated by Copilot at f484174

  • Implement the break statement for for loops in Taichi by lowering it to a WhileControlStmt with a constant true condition and a mask (link)
  • Add a boolean parameter is_for_loop to the WhileControlStmt constructor to indicate whether it is a break statement in a for loop (link)
  • Modify the LLVM code generator to handle the WhileControlStmt with is_for_loop set to true by jumping to the after loop block of the current for loop (link, link, link)
  • Add a field current_for_after_loop to the TaskCodeGenLLVM class to store the pointer to the after loop block of the current for loop (link)
  • Reset the current_for_after_loop pointer to null when initializing a new offloaded task to avoid using a stale pointer (link)
  • Add a test case to test_for_break.py to check the break statement in a for loop with a range iterator (link)

@netlify
Copy link

netlify bot commented Sep 20, 2023

Deploy Preview for docsite-preview canceled.

Name Link
🔨 Latest commit 4d257b6
🔍 Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/650aa585951a800007092d6c

stmts.push_back<WhileControlStmt>(while_stmt->mask, const_true);
auto mask = stmts.push_back<AllocaStmt>(PrimitiveType::i32);
auto const_stmt =
stmts.push_back<ConstStmt>(TypedConstant((int32)0xFFFFFFFF));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the mask forced to all threads? (also why is there a mask...?)

@lin-hitonami
Copy link
Contributor

lin-hitonami commented Sep 21, 2023

IIRC the for loop should be converted to while statement if there is a break inside it, and the for statement is only for perfect for loops which do not break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Taichi crashes when using break in kernel
3 participants