Add Block::to_vector - #9384
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
|
||
| std::vector<Stmt> Block::to_vector(const Stmt &s) { | ||
| std::vector<Stmt> result; | ||
| // Blocks are right-leaning: 'first' is never itself a Block. |
There was a problem hiding this comment.
Not through a Block::make indeed. But someone could just set first and rest manually. An assert wouldn't hurt.
There was a problem hiding this comment.
There's actually no way to do that, unless someone is manually saying "new Block", but if they're doing that sort of thing, all bets are off. Maybe we can make operator new for IR nodes private to IR.cpp somehow. There are lots of places where we assume invariants that are created in a ::make method (e.g. a child Expr of an Add is always defined and has the same type as the parent)
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:
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:
LoopCarryhad its own recursiveblock_to_vectorpair; deleted.Prefetchhad a recursivetraverse_blocktemplate; deleted.HoistPrefetches::visit(Block)now collects into two vectors and concatenates.AsyncProducers, both inmake_producer_consumerand inExpandAcquireNodes::visit(Block).RemoveUndef::visit(Block). It no longer shares an unchanged suffix when something did change, but still returnsopuntouched 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, andSerialization.test_correctness passes locally apart from the known LLVM 23 sve2 failure in simd_op_check.