Skip to content

Add inductive Funcs - #9717

Draft
abadams wants to merge 11 commits into
abadams/slide_directivefrom
abadams/inductive_funcs
Draft

abadams wants to merge 11 commits into
abadams/slide_directivefrom
abadams/inductive_funcs

Conversation

@abadams

@abadams abadams commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

This is basically #8878 but on top of the rewrite of sliding window, which makes inductive funcs behave better.

Copilot says:

Stacked on #9633 (abadams/slide_directive), which is stacked on #9648. Opened as a draft to get CI coverage.

Adds inductive Funcs: pure definitions that refer to themselves at earlier values of some of their pure Vars, with the recursion guarded by a select that names the base case:

Func f(Int(32), "f");
f(x) = select(x <= 0, in(0), in(x) + f(x - 1));

Unlike the equivalent RDom scan, such a Func can be computed at its consumer, slid, and folded down to the few values the recurrence still reads.

  • Function classifies the Func and each of its Vars as inductive. Inductive Vars get a new DimType::InductiveVar, which can't be parallelized, vectorized, or reordered against another loop derived from the same Var.
  • Bounds inference expands the region required of an inductive Func to reach its base case (Inductive.cpp).
  • Inductive Funcs can't be inlined or be pipeline outputs. An inductive update's RVars must be nested inside its inductive Vars.
  • Ring buffering rewrites a Func's calls to itself inside its own stores.
  • Storage folding measures the footprint of a window that warms up on the first iteration at the loop min, so a cascade of inductive Funcs sliding over a split dimension doesn't need a fold factor larger than the window it keeps.
  • New apps/iir_cascade: a cascade of second-order IIR sections written as inductive Funcs, each sliding over the samples and keeping its last three values in registers.

Much of this code comes from @stevenraphael's #8878. He is credited as a co-author on the commits.

abadams and others added 10 commits November 4, 2026 13:26
A Func may now be defined in terms of itself at earlier values of some of
its pure Vars, with the recursion guarded by a select that names the base
case, e.g.

    Func f(Int(32), "f");
    f(x) = select(x <= 0, in(0), in(x) + f(x - 1));

Unlike the equivalent RDom scan, such a Func can be computed at its
consumer, slid, and folded down to the few values the recurrence still
reads. A Func that refers to itself must be declared with an explicit type.

- Function classifies the Func and each of its Vars as inductive, and gives
  inductive Vars a new DimType, InductiveVar, which can't be parallelized,
  vectorized, or reordered against another loop derived from the same Var.
- Bounds inference expands the region required of an inductive Func to
  reach its base case (Inductive.cpp).
- Inductive Funcs can't be inlined or be pipeline outputs. An inductive
  update's RVars must be nested inside its inductive Vars.
- Ring buffering rewrites a Func's calls to itself inside its own stores.

Extracted from abadams/inductive_benchmarks, which builds on PR #8878.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…t the loop min

A window that warms up on the first iteration of a loop computes a larger
region on that iteration only, so storage folding measures that region's
extent at the loop min rather than over the whole loop. Otherwise a
cascade of inductive Funcs sliding over a split dimension needs a fold
factor larger than the window it keeps.

Add tests of cascades of second-order inductive Funcs, folded, and
sliding over a split dimension.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Each section is a second-order recurrence over the samples, written as
an inductive Func. The sections slide over the samples, and the last
three values of each are kept in registers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
expand_to_include_base_case ran BaseCaseSolver, a tree visitor, over the
result of substitute_in_all_lets, which is exponential in the depth of a
let chain whose values reuse earlier lets. Have the solver track the
enclosing lets instead, rewrapping the ones each condition or recursive
argument uses. Lets whose values refer to the Func are checked where they
are used, once per select arm.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.70787% with 58 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (abadams/slide_directive@b409fec). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/Function.cpp 80.48% 15 Missing and 9 partials ⚠️
src/Func.cpp 60.00% 12 Missing and 8 partials ⚠️
src/ScheduleFunctions.cpp 76.92% 2 Missing and 4 partials ⚠️
src/IRPrinter.cpp 0.00% 3 Missing ⚠️
src/Inductive.cpp 98.31% 0 Missing and 2 partials ⚠️
src/BoundsInference.cpp 91.66% 0 Missing and 1 partial ⚠️
src/IR.cpp 75.00% 0 Missing and 1 partial ⚠️
src/ParallelRVar.cpp 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##             abadams/slide_directive    #9717   +/-   ##
==========================================================
  Coverage                           ?   72.96%           
==========================================================
  Files                              ?      268           
  Lines                              ?    84625           
  Branches                           ?    20846           
==========================================================
  Hits                               ?    61743           
  Misses                             ?    16289           
  Partials                           ?     6593           

☔ 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 added this pull request to stack #9659 November 24, 2026 05:02
@alexreinking

alexreinking commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

If any of this code is @stevenraphael's, you should add a Co-authored-by for it, or fix the commit authorship outright. Commit 65a9349 looks particularly similar.

@abadams

abadams commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Yes, the squashed commit into main is going to need a co-authored-by. I think this is Steven and my code all mixed together at this point.

Much of the inductive Funcs implementation comes from #8878.

Co-authored-by: Steven Raphael <stevenraphael44@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@abadams

abadams commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

To guard against forgetting to do that I added an empty commit with a co-authored-by in it so that it automatically gets added to the final squashed commit.

This branch has not been deployed

No deployments
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