Skip to content

Keep size-one parallel loops in the task system - #9454

Open
abadams wants to merge 2 commits into
mainfrom
abadams/parallel_for_loop_size_one
Open

abadams wants to merge 2 commits into
mainfrom
abadams/parallel_for_loop_size_one

Conversation

@abadams

@abadams abadams commented Sep 17, 2026

Copy link
Copy Markdown
Member

Summary

A loop marked .parallel() is expected to be dispatched through the task system even when its extent turns out to be one. Among other things, this makes .parallel(Var::outermost()) a useful idiom for isolating a body into its own task/function without otherwise changing the loop nest.

Two passes were collapsing such loops into a serial body:

  • Simplify turned a For whose bounds prove it runs at most once into a LetStmt. It now leaves parallel loops alone.
  • RemoveLoopsOverOutermost stripped the always-size-one .__outermost loop unconditionally. It now leaves parallel loops alone too — otherwise the .parallel(Var::outermost()) idiom never reaches the task system even after the simplifier change.

Test plan

  • New correctness/parallel_size_one test installs a custom halide_do_task and asserts it fires both for a statically-size-one parallel loop (f.compute_at(g, x).parallel(x)) and for a parallel outermost loop (f.parallel(Var::outermost())).
  • Verified the test fails on the pre-change library (task handler never called) and passes after the change.

🤖 Generated with Claude Code

abadams and others added 2 commits September 17, 2026 10:20
A loop marked .parallel() is expected to be dispatched through the task
system even when its extent turns out to be one. Collapsing such a loop
into a LetStmt bypasses halide_do_task, which defeats a useful idiom:
.parallel(Var::outermost()) to isolate a body into its own function.

Don't simplify a parallel For into a serial body even when its bounds
prove it runs at most once. Adds a regression test that installs a custom
halide_do_task and confirms it fires for a statically-size-one parallel
loop.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
RemoveLoopsOverOutermost stripped the always-size-one .__outermost loop
unconditionally, so .parallel(Var::outermost()) never reached the task
system even after the simplifier stopped collapsing size-one parallel
loops. Skip parallel loops there too, and extend the regression test to
cover the outermost idiom.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@alexreinking alexreinking left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this causes performance regressions anywhere, somehow, I'd say, "if you ask for a parallel loop, don't be surprised when it when it is one."

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.98%. Comparing base (5f95e0c) to head (2885977).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9454      +/-   ##
==========================================
- Coverage   70.12%   69.98%   -0.14%     
==========================================
  Files         261      261              
  Lines       79938    79941       +3     
  Branches    19478    19481       +3     
==========================================
- Hits        56053    55944     -109     
- Misses      18056    18086      +30     
- Partials     5829     5911      +82     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants