-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add eager_inline() directive for schedule-time inlining #9256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e92167
Add eager_inline() directive for schedule-time inlining
alexreinking 06203e8
Add variadic overload to eager_inline.
alexreinking 07cf296
Attach eager_inline() to Stage, not Func
alexreinking 86e792d
Topologically sort eager_inline() arguments
alexreinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #include "Halide.h" | ||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| using namespace Halide; | ||
|
|
||
| // eager_inline() performs the substitution immediately, so the caller's | ||
| // definition no longer references the inlined Funcs (they are inlined by value). | ||
| // Verify the numerics of a simple chained inline match a plain inlined pipeline. | ||
|
|
||
| namespace { | ||
|
|
||
| // Does the printed form of `e` contain a direct call to Func `name`? | ||
| bool mentions(const Expr &e, const std::string &name) { | ||
| std::ostringstream os; | ||
| os << e; | ||
| return os.str().find(name + "(") != std::string::npos; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| int main(int argc, char **argv) { | ||
| Var x{"x"}; | ||
| Func a{"a"}, b{"b"}, c{"c"}; | ||
| a(x) = x + 1; | ||
| b(x) = a(x) * 2; // calls a | ||
| c(x) = b(x) + a(x); // calls b (which calls a) and a directly | ||
|
|
||
| // Inline b then a into c. Inlining b splices in its call to a, which the | ||
| // subsequent inline of a then also folds. | ||
| c.eager_inline(b, a); | ||
|
|
||
| Expr c_body = c.function().definition().values()[0]; | ||
| Expr c_expected = x * 3 + 3; | ||
| internal_assert(Internal::can_prove(c_body == c_expected)) | ||
| << "eager_inline chain failed to fold all calls to a and b into c\n" | ||
| << "Saw: " << c_body << "\nExpected: " << c_expected << "\n"; | ||
|
|
||
| Buffer<int> out = c.realize({8}); | ||
| for (int i = 0; i < 8; i++) { | ||
| int ref = (i + 1) * 2 + (i + 1); | ||
| if (out(i) != ref) { | ||
| printf("eager_inline chain mismatch at %d: %d vs %d\n", i, out(i), ref); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| // eager_inline() is stage-scoped: inlining into one stage leaves the other | ||
| // definitions of the same Func untouched. | ||
| { | ||
| RDom r(0, 4); | ||
|
|
||
| // Stage-level: inline into the update only; the init definition still | ||
| // calls prod. | ||
| Func prod{"prod"}, f{"f"}; | ||
| prod(x) = x + 1; | ||
| f(x) = prod(x); // init definition calls prod | ||
| f(x) += prod(x) * r; // update definition also calls prod | ||
|
|
||
| f.update(0).eager_inline(prod); | ||
|
|
||
| internal_assert(mentions(f.function().definition().values()[0], "prod")) | ||
| << "Stage::eager_inline on update(0) should not touch the init definition\n"; | ||
| internal_assert(!mentions(f.function().update(0).values()[0], "prod")) | ||
| << "Stage::eager_inline on update(0) should have inlined prod into the update\n"; | ||
|
|
||
| // Semantics preserved: f(x) = (x+1) + sum_{r=0..3} (x+1)*r = 7*(x+1). | ||
| Buffer<int> fout = f.realize({8}); | ||
| for (int i = 0; i < 8; i++) { | ||
| if (fout(i) != 7 * (i + 1)) { | ||
| printf("stage eager_inline mismatch at %d: %d vs %d\n", i, fout(i), 7 * (i + 1)); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| // Func-level: targets the init definition only, leaving updates alone. | ||
| Func g{"g"}; | ||
| g(x) = prod(x); | ||
| g(x) += prod(x) * r; | ||
| g.eager_inline(prod); | ||
| internal_assert(!mentions(g.function().definition().values()[0], "prod")) | ||
| << "Func::eager_inline should inline prod into the init definition\n"; | ||
| internal_assert(mentions(g.function().update(0).values()[0], "prod")) | ||
| << "Func::eager_inline should not touch update definitions\n"; | ||
| } | ||
|
|
||
| printf("Success!\n"); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "Halide.h" | ||
| using namespace Halide; | ||
|
|
||
| int main(int argc, char **argv) { | ||
| Var x{"x"}; | ||
| RDom r(0, 4); | ||
| Func reduced{"reduced"}, consumer{"consumer"}; | ||
| reduced(x) = 0; | ||
| reduced(x) += r; // update definition -> not pure | ||
| consumer(x) = reduced(x); | ||
|
|
||
| // A Func with an update definition is not inlinable, so eager_inline() rejects it. | ||
| consumer.eager_inline({reduced}); | ||
|
|
||
| printf("Success!\n"); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #include "Halide.h" | ||
| using namespace Halide; | ||
|
|
||
| int main(int argc, char **argv) { | ||
| Var x{"x"}; | ||
| Func undefined_producer{"undefined_producer"}; // never given a definition | ||
| Func consumer{"consumer"}; | ||
| consumer(x) = x; | ||
|
|
||
| // An undefined Func has no body to splice in, so eager_inline() rejects it. | ||
| consumer.eager_inline({undefined_producer}); | ||
|
|
||
| printf("Success!\n"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.