Skip to content

Add stream_loads and stream_stores scheduling directives#9207

Open
alexreinking wants to merge 2 commits into
mainfrom
alexreinking/non-temporal
Open

Add stream_loads and stream_stores scheduling directives#9207
alexreinking wants to merge 2 commits into
mainfrom
alexreinking/non-temporal

Conversation

@alexreinking

@alexreinking alexreinking commented Jul 14, 2026

Copy link
Copy Markdown
Member

Implements the stream_loads and stream_stores directives as proposed in #9206.

  • stream_loads - ensure that loads this stage makes are streamed. It has two modes
    • Bare .stream_loads() -- stream all direct loads after inlining, excluding self-loads.
    • g.stream_loads({f1, ..., fN}) -- stream loads from the named funcs after inlining. Naming g here is a user_error.
  • stream_stores - ensure that stores to this func are streamed on a per-stage basis. A stage can have this specialization only if all of its RVars are pure (meaning that no two stores can alias). If any stage-specialization requests streaming, a fence is placed after the specialization block. This is vacuously true for pure funcs, RDom-less updates, and stages without specializations.

This fully recovers performance of memcpy on my Apple M3.

Fixes #9206

Checklist

  • Tests added or updated (not required for docs, CI config, or typo fixes)
  • Documentation updated (if public API changed)
  • Python bindings updated (if public API changed)
  • Benchmarks are included here if the change is intended to affect performance.
  • Commits include AI attribution where applicable (see Code of Conduct)

@alexreinking

Copy link
Copy Markdown
Member Author

This implementation was super straightforward, but also super messy. It would be nice if we had an annotations system so that places that just forward those annotations don't need to be updated for every new feature.

Comment thread src/CodeGen_LLVM.cpp Outdated
Comment thread src/CodeGen_LLVM.cpp Outdated
Comment thread test/performance/memcpy.cpp Outdated
src.set_host_alignment(32);
dst.output_buffer().set_host_alignment(32);

dst.vectorize(x, 32, TailStrategy::GuardWithIf);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You'd apply it here then.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps two versions: with streaming stores and loads, and without, just as informative output when someone runs this test?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't really show "wrong" schedules in the other performance tests, do we?

Comment thread src/CodeGen_LLVM.cpp
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.77482% with 162 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@2e2336b). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/StmtToHTML.cpp 0.00% 38 Missing ⚠️
src/IRPrinter.cpp 0.00% 20 Missing and 2 partials ⚠️
src/CodeGen_LLVM.cpp 60.00% 10 Missing and 6 partials ⚠️
src/Func.cpp 60.97% 12 Missing and 4 partials ⚠️
src/IREquality.cpp 20.00% 6 Missing and 2 partials ⚠️
src/Simplify_Stmts.cpp 61.90% 5 Missing and 3 partials ⚠️
src/StorageFlattening.cpp 83.87% 4 Missing and 1 partial ⚠️
src/CodeGen_D3D12Compute_Dev.cpp 0.00% 4 Missing ⚠️
src/CodeGen_PTX_Dev.cpp 0.00% 4 Missing ⚠️
src/Derivative.cpp 0.00% 4 Missing ⚠️
... and 17 more
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #9207   +/-   ##
=======================================
  Coverage        ?   69.76%           
=======================================
  Files           ?      255           
  Lines           ?    78828           
  Branches        ?    18843           
=======================================
  Hits            ?    54997           
  Misses          ?    18267           
  Partials        ?     5564           

☔ 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.

@alexreinking alexreinking force-pushed the alexreinking/non-temporal branch from e0d2142 to 4fe8903 Compare July 15, 2026 19:25
Comment thread src/CodeGen_C.cpp
Comment on lines +1866 to +1868
} else if (op->is_intrinsic(Call::stream_store_fence)) {
// The C backend does not implement non-temporal loads/stores.
rhs << "0";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Something to discuss: what do we want to do here. Warn? Silently drop (this)?

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.

I vote warn. We're ignoring a scheduling directive.

@alexreinking alexreinking force-pushed the alexreinking/non-temporal branch from d16165a to 9d1398d Compare July 15, 2026 19:40
Co-authored-by: GPT 5.6 Sol <noreply@openai.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@alexreinking alexreinking force-pushed the alexreinking/non-temporal branch from 538dbc8 to 057fec5 Compare July 16, 2026 02:52
Comment thread src/IR.h
* resulting Load node(s) and then discards it -- it should not survive
* past that point. */
struct StreamingLoads : public StmtNode<StreamingLoads> {
std::optional<std::vector<std::string>> names;

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.

Is an empty vector meaningful, or could it be used instead of nullopt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is meaningful:

  • nullopt = stream from all loads except self
  • vector (any size) = stream from this set of loads

Therefore, an empty vector means "stream nothing". That's a valid thing to say if you're metaprogramming the schedule and some combination of parameters leads to that decision.

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.

But isn't "stream nothing" representable by just not having the IR node at all? Or might you have a stream-nothing context inside of a stream-some-stuff context? Actually, what does it even mean to have nested StreamingLoad nodes? Does the inner one override the outer, or is it the union?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It should not be possible to nest them. A Provide node is immediately surrounded by StreamingLoads/StreamingStores/Atomic in that order, with no opportunity to nest any of them. I chose that implementation over complicating the Provide and especially Call nodes.

Yes, it's possible to represent it by just removing the IR node. But that's true of constant predicates, extent-1 RDoms, etc.

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.

Maybe add the nesting rule to the comment. Agree that empty vector as stream-nothing is reasonable. Does this imply that in .stream_loads({foo}).stream_loads({}), the second overrides the first, as opposed to being a no-op? I guess it might show up in code like:

for (auto f : {a, b, c, d, e}) {
  f.compute_at(...).tile(...).vectorize(...).stream_loads(...);
}
// This specific func shouldn't actually stream any loads
c.stream_loads({})

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I think a second declaration would override a first.

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.

Add non-temporal loads/stores to Halide

3 participants