forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checks to prevent people from using negative split factors (halid…
…e#8076) * Add checks to prevent people from using negative split factors Our analysis passes assume that loop maxes are greater than loop mins, so negative split factors cause sufficient havoc that not even output bounds queries are safe. These are therefore checked on pipeline entry. This is a new way for output bounds queries to throw errors (in addition to the buffer pointers themselves being null, and maybe some buffer constraints). Testing this, I realized these errors were getting thrown twice, because the output buffer bounds query in Pipeline::realize was built around two recursive calls to realize, and both were calling the custom error handler. In addition to reporting errors in this class twice, this implies several other inefficiencies, e.g. jit call args were being prepped twice. I reworked it to be built around two calls to call_jit_code instead. Fixes halide#7938 * Add test to cmakelists * Remove pointless target arg to call_jit_code It has to be the same as the cached target in the receiving object anyway
- Loading branch information
Showing
15 changed files
with
208 additions
and
28 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "AddSplitFactorChecks.h" | ||
#include "Definition.h" | ||
#include "Function.h" | ||
#include "IR.h" | ||
#include "IROperator.h" | ||
#include "Simplify.h" | ||
|
||
namespace Halide { | ||
namespace Internal { | ||
|
||
namespace { | ||
|
||
void check_all_split_factors(const Function &f, const Definition &def, std::vector<Stmt> *stmts) { | ||
const StageSchedule &sched = def.schedule(); | ||
for (const Split &split : sched.splits()) { | ||
if (split.split_type != Split::SplitVar) { | ||
continue; | ||
} | ||
if (is_positive_const(split.factor)) { | ||
// Common-case optimization | ||
continue; | ||
} | ||
Expr positive = simplify(split.factor > 0); | ||
if (is_const_one(positive)) { | ||
// We statically proved it | ||
continue; | ||
} | ||
// We need a runtime check that says: if the condition is | ||
// entered, the split factor will be positive. We can still | ||
// assume the pipeline preconditions, because they will be | ||
// checked before this. | ||
std::ostringstream factor_str; | ||
factor_str << split.factor; | ||
Expr error = Call::make(Int(32), "halide_error_split_factor_not_positive", | ||
{f.name(), | ||
split_string(split.old_var, ".").back(), | ||
split_string(split.outer, ".").back(), | ||
split_string(split.inner, ".").back(), | ||
factor_str.str(), split.factor}, | ||
Call::Extern); | ||
stmts->push_back(AssertStmt::make(positive, error)); | ||
} | ||
|
||
for (const auto &s : def.specializations()) { | ||
check_all_split_factors(f, s.definition, stmts); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
Stmt add_split_factor_checks(const Stmt &s, const std::map<std::string, Function> &env) { | ||
// Check split factors are strictly positive | ||
std::vector<Stmt> stmts; | ||
|
||
for (const auto &p : env) { | ||
const Function &f = p.second; | ||
check_all_split_factors(f, f.definition(), &stmts); | ||
for (const auto &u : f.updates()) { | ||
check_all_split_factors(f, u, &stmts); | ||
} | ||
} | ||
|
||
stmts.push_back(s); | ||
return Block::make(stmts); | ||
} | ||
|
||
} // namespace Internal | ||
} // namespace Halide |
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,25 @@ | ||
#ifndef HALIDE_INTERNAL_ADD_SPLIT_FACTOR_CHECKS_H | ||
#define HALIDE_INTERNAL_ADD_SPLIT_FACTOR_CHECKS_H | ||
|
||
/** \file | ||
* | ||
* Defines the lowering pass that adds the assertions that all split factors are | ||
* strictly positive. | ||
*/ | ||
#include <map> | ||
|
||
#include "Expr.h" | ||
|
||
namespace Halide { | ||
namespace Internal { | ||
|
||
class Function; | ||
|
||
/** Insert checks that all split factors that depend on scalar parameters are | ||
* strictly positive. */ | ||
Stmt add_split_factor_checks(const Stmt &s, const std::map<std::string, Function> &env); | ||
|
||
} // namespace Internal | ||
} // namespace Halide | ||
|
||
#endif |
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
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
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
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
Oops, something went wrong.