Skip to content

Add Block::to_vector - #9384

Open
abadams wants to merge 1 commit into
mainfrom
abadams/block_to_vector
Open

Add Block::to_vector#9384
abadams wants to merge 1 commit into
mainfrom
abadams/block_to_vector

Conversation

@abadams

@abadams abadams commented Aug 24, 2026

Copy link
Copy Markdown
Member

Blocks are guaranteed to be right-leaning, but several places were unpacking a Block tree into a vector of non-Block Stmts by hand, some of them recursively. This adds a single helper:

/** The inverse of the vector form of make. Unpacks a Stmt into the
 * sequence of non-Block Stmts it runs. An undefined Stmt unpacks to an
 * empty vector, and a non-Block Stmt unpacks to a vector of size one. */
static std::vector<Stmt> to_vector(const Stmt &s);

It's a static taking a Stmt rather than a member on Block, because every call site starts from a Stmt that may or may not be a Block, and that makes it a true inverse of Block::make(const std::vector<Stmt> &).

Call sites converted:

  • LoopCarry had its own recursive block_to_vector pair; deleted.
  • Prefetch had a recursive traverse_block template; deleted. HoistPrefetches::visit(Block) now collects into two vectors and concatenates.
  • AsyncProducers, both in make_producer_consumer and in ExpandAcquireNodes::visit(Block).
  • RemoveUndef::visit(Block). It no longer shares an unchanged suffix when something did change, but still returns op untouched when nothing changed, and still returns an undefined Stmt when everything was removed.
  • StmtToHTML::print_block_stmt.

Left alone are the places that walk a block chain conditionally or partially rather than unpacking it: Simplify_Stmts, CodeGen_LLVM's leading-assert collection, LICM, Profiling, InjectHostDevBufferCopies, and Serialization.

test_correctness passes locally apart from the known LLVM 23 sve2 failure in simd_op_check.

Adds a helper that unpacks a Stmt into the sequence of non-Block Stmts it
runs, and uses it in the places that were doing this ad-hoc: LoopCarry
(which had its own recursive block_to_vector), Prefetch (which had a
recursive traverse_block template), AsyncProducers, RemoveUndef, and
StmtToHTML.

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

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.11%. Comparing base (c049b16) to head (dd9ecf9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9384      +/-   ##
==========================================
+ Coverage   70.03%   70.11%   +0.07%     
==========================================
  Files         261      261              
  Lines       79223    79190      -33     
  Branches    19312    19305       -7     
==========================================
+ Hits        55487    55524      +37     
+ Misses      17923    17893      -30     
+ Partials     5813     5773      -40     

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

Comment thread src/IR.cpp

std::vector<Stmt> Block::to_vector(const Stmt &s) {
std::vector<Stmt> result;
// Blocks are right-leaning: 'first' is never itself a Block.

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.

Not through a Block::make indeed. But someone could just set first and rest manually. An assert wouldn't hurt.

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