-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Propagate some facts about inequalities with min/max #8475
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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,78 @@ | ||
#include "Halide.h" | ||
#include "HalideRuntime.h" | ||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
using namespace Halide; | ||
|
||
int load_count = 0; | ||
|
||
// A trace that records the number of loads | ||
int my_trace(JITUserContext *user_context, const halide_trace_event_t *ev) { | ||
|
||
if (ev->event == halide_trace_load) { | ||
load_count++; | ||
} | ||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
Param<float> scale_factor_x, scale_factor_y; | ||
ImageParam input(UInt(8), 2); | ||
|
||
Var x, y; | ||
|
||
Func f; | ||
Expr upsample_x = scale_factor_x > cast<float>(1.0f); | ||
Expr upsample_y = scale_factor_y > cast<float>(1.0f); | ||
Expr upsample = upsample_x && upsample_y; | ||
Expr downsample = !upsample_x && !upsample_y; | ||
|
||
f(x, y) = select(upsample, input(cast<int>(x / 2), cast<int>(y / 2)), | ||
select(downsample, input(x * 2, y * 2), 0)); | ||
|
||
input.trace_loads(); | ||
f.jit_handlers().custom_trace = &my_trace; | ||
|
||
// Impossible condition | ||
// f.specialize(upsample && downsample); | ||
f.specialize(upsample && !downsample); | ||
f.specialize(!upsample && downsample); | ||
f.specialize(!upsample && !downsample); | ||
f.specialize_fail("Unreachable condition"); | ||
|
||
Buffer<uint8_t> img(16, 16); | ||
input.set(img); | ||
|
||
{ | ||
// In this specialization, one of the select branches should be trimmed, | ||
// resulting in one load per output pixel | ||
load_count = 0; | ||
scale_factor_x.set(2.0f); | ||
scale_factor_y.set(2.0f); | ||
Buffer<uint8_t> out = f.realize({8, 8}); | ||
assert(load_count == 64); | ||
} | ||
{ | ||
// In this specialization, no select can be trimmed, | ||
// resulting in two loads per output pixel | ||
load_count = 0; | ||
scale_factor_x.set(0.5f); | ||
scale_factor_y.set(2.0f); | ||
Buffer<uint8_t> out = f.realize({8, 8}); | ||
assert(load_count == 128); | ||
} | ||
{ | ||
// In this specialization, one of the select branches should be trimmed, | ||
// resulting in one load per output pixel | ||
load_count = 0; | ||
scale_factor_x.set(0.5f); | ||
scale_factor_y.set(0.5f); | ||
Buffer<uint8_t> out = f.realize({8, 8}); | ||
assert(load_count == 64); | ||
} | ||
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the inverse statements required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question. Is this just to get a syntactic match on the <= form? The other cases here don't do it, but I think this is not exactly the same as the other cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it was necessary i.e. if I comment out the
learn_false()
lines, the test fails, since we can't eliminate the condition in the third specialization. I added all thelearn_false()
clauses even though it may be true that only one or two are needed for this testcase.We have a rewrite that changes
downsample = !(scale_factor_x > 1.0) && !(scale_factor_y > 1.0)
intodownsample = max(scale_factor_x, scale_factor_y) <= 1.0
. Without theselearn_false()
clauses, the simplifier is unable to provemax(scale_factor_x, scale_factor_y) <= 1.0 --> !(1.0 < scale_factor_x)
and similarlymax(scale_factor_x, scale_factor_y) <= 1.0 --> !(1.0 < scale_factor_y)
, so it can't trim the conditions in that branch.