Skip to content

[Bugfix] Fix threadIdx variable lookup by thread_tag instead of position in ThreadSync#1829

Merged
LeiWang1999 merged 5 commits intomainfrom
fix-thread-sync-threadidx-lookup
Feb 9, 2026
Merged

[Bugfix] Fix threadIdx variable lookup by thread_tag instead of position in ThreadSync#1829
LeiWang1999 merged 5 commits intomainfrom
fix-thread-sync-threadidx-lookup

Conversation

@LeiWang1999
Copy link
Member

@LeiWang1999 LeiWang1999 commented Feb 9, 2026

Added condition expression visiting in constr_visitor.h to ensure buffer accesses in if-conditions are properly recorded before applying guards

Summary by CodeRabbit

  • Bug Fixes

    • Ensured buffer accesses inside conditional expressions are captured correctly by visiting conditions before applying branch guards.
  • Tests

    • Added test coverage validating thread-synchronization hoisting with shared memory in loops and non-uniform conditional branches.

… memory

This commit introduces a new test case to verify the correct behavior of sync hoisting when a non-uniform if statement is present inside a loop that utilizes shared memory. The test ensures that the synchronization occurs before the if statement, confirming the expected transformation in the module's intermediate representation.
This commit removes the optional thread variable lookup by tag and replaces it with direct indexing based on the expected position of threadIdx variables. This change addresses a bug where the last three elements of env_threads_ were incorrectly assumed to always correspond to threadIdx.x/y/z, improving the accuracy of thread variable access in the synchronization logic.
@github-actions
Copy link

github-actions bot commented Feb 9, 2026

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 9, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

The visitor for if-then-else expressions now visits the condition expression unguarded before applying branch guards, ensuring condition buffer accesses are recorded; a test was added to validate thread-sync hoisting for non-uniform ifs inside loops using shared (thread) memory.

Changes

Cohort / File(s) Summary
Constraint Visitor Modification
src/transform/common/constr_visitor.h
VisitIfThenElseExpr now visits the condition unguarded before applying guards to true/false branches, so condition buffer accesses are pushed to the constraint stack.
Thread Sync Test Addition
testing/python/transform/test_tilelang_transform_thread_sync.py
Added test_sync_hoist_non_uniform_if_in_loop_with_shared_memory (appears duplicated) to assert storage sync is hoisted before non-uniform if inside a loop using shared memory.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • kurisu6912
  • SiriusNEO
  • silentCoder-dev

Poem

🐰 I nibble guards and peek the road ahead,
I note each buffer where my paws have tread,
A sync I tuck before branches diverge,
Hoisted and tidy—now threads safely merge. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main bugfix: correcting threadIdx variable lookup methodology in ThreadSync, which is evidenced by the refactoring in thread_storage_sync.cc switching from optional tag-based lookup to direct position-based indexing.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-thread-sync-threadidx-lookup

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes ThreadSync-related guard/sync placement by ensuring buffer accesses in if-conditions are visited/recorded prior to applying guards, and adds a regression test for hoisting sync before a non-uniform if inside a loop.

Changes:

  • Update ConstrVisitor to visit IfThenElse condition expressions before creating/applying guards.
  • Add a CUDA test covering sync hoisting with shared memory reads in a loop/conditional.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
testing/python/transform/test_tilelang_transform_thread_sync.py Adds a regression test ensuring shared-memory sync is hoisted before a non-uniform if inside a loop.
src/transform/common/constr_visitor.h Ensures condition expressions are visited unguarded so their buffer accesses are recorded before guard application.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@LeiWang1999 LeiWang1999 merged commit f8b4d9e into main Feb 9, 2026
0 of 3 checks passed
@LeiWang1999 LeiWang1999 deleted the fix-thread-sync-threadidx-lookup branch February 11, 2026 15:25
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.

1 participant