Loading...
Searching...
No Matches
Bounds.h
Go to the documentation of this file.
#ifndef HALIDE_BOUNDS_H
#define HALIDE_BOUNDS_H
/** \file
* Methods for computing the upper and lower bounds of an expression,
* and the regions of a function read or written by a statement.
*/
#include "Interval.h"
#include "Scope.h"
namespace Halide {
namespace Internal {
class Function;
typedef std::map<std::pair<std::string, int>, Interval> FuncValueBounds;
const FuncValueBounds &empty_func_value_bounds();
/** Given an expression in some variables, and a map from those
* variables to their bounds (in the form of (minimum possible value,
* maximum possible value)), compute two expressions that give the
* minimum possible value and the maximum possible value of this
* expression. Max or min may be undefined expressions if the value is
* not bounded above or below. If the expression is a vector, also
* takes the bounds across the vector lanes and returns a scalar
* result.
*
* This is for tasks such as deducing the region of a buffer
* loaded by a chunk of code.
*/
Interval bounds_of_expr_in_scope(const Expr &expr,
const Scope<Interval> &scope,
const FuncValueBounds &func_bounds = empty_func_value_bounds(),
bool const_bound = false);
/** Given a varying expression, try to find a constant that is either:
* An upper bound (always greater than or equal to the expression), or
* A lower bound (always less than or equal to the expression)
* If it fails, returns an undefined Expr. */
enum class Direction { Upper,
Lower };
Expr find_constant_bound(const Expr &e, Direction d,
const Scope<Interval> &scope = Scope<Interval>::empty_scope());
/** Find bounds for a varying expression that are either constants or
* +/-inf. */
Interval find_constant_bounds(const Expr &e, const Scope<Interval> &scope);
/** Represents the bounds of a region of arbitrary dimension. Zero
* dimensions corresponds to a scalar region. */
struct Box {
/** The conditions under which this region may be touched. */
Expr used;
/** The bounds if it is touched. */
std::vector<Interval> bounds;
Box() = default;
explicit Box(size_t sz)
: bounds(sz) {
}
explicit Box(const std::vector<Interval> &b)
: bounds(b) {
}
size_t size() const {
return bounds.size();
}
bool empty() const {
return bounds.empty();
}
Interval &operator[](size_t i) {
return bounds[i];
}
const Interval &operator[](size_t i) const {
return bounds[i];
}
void resize(size_t sz) {
bounds.resize(sz);
}
void push_back(const Interval &i) {
bounds.push_back(i);
}
/** Check if the used condition is defined and not trivially true. */
bool maybe_unused() const;
friend std::ostream &operator<<(std::ostream &stream, const Box &b);
};
/** Expand box a to encompass box b */
void merge_boxes(Box &a, const Box &b);
/** Test if box a could possibly overlap box b. */
bool boxes_overlap(const Box &a, const Box &b);
/** The union of two boxes */
Box box_union(const Box &a, const Box &b);
/** The intersection of two boxes */
Box box_intersection(const Box &a, const Box &b);
/** Test if box a provably contains box b */
bool box_contains(const Box &a, const Box &b);
/** Compute rectangular domains large enough to cover all the 'Call's to each
* function that occurs within a given statement or expression. This is useful
* for figuring out what regions of things to evaluate. Respects control flow
* (e.g. encodes if statement conditions), but assumes all encountered asserts
* pass. If it encounters an assert(false) in one if branch, assumes the
* opposite if branch runs unconditionally. */
// @{
std::map<std::string, Box> boxes_required(const Expr &e,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
std::map<std::string, Box> boxes_required(Stmt s,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
// @}
/** Compute rectangular domains large enough to cover all the 'Provides's to
* each function that occurs within a given statement or expression. Handles
* asserts in the same way as boxes_required. */
// @{
std::map<std::string, Box> boxes_provided(const Expr &e,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
std::map<std::string, Box> boxes_provided(Stmt s,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
// @}
/** Compute rectangular domains large enough to cover all the 'Call's and
* 'Provides's to each function that occurs within a given statement or
* expression. Handles asserts in the same way as boxes_required. */
// @{
std::map<std::string, Box> boxes_touched(const Expr &e,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
std::map<std::string, Box> boxes_touched(Stmt s,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
// @}
/** Variants of the above that are only concerned with a single function. */
// @{
Box box_required(const Expr &e, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
Box box_required(Stmt s, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
Box box_provided(const Expr &e, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
Box box_provided(Stmt s, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
Box box_touched(const Expr &e, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
Box box_touched(Stmt s, const std::string &fn,
const Scope<Interval> &scope = Scope<Interval>::empty_scope(),
const FuncValueBounds &func_bounds = empty_func_value_bounds());
// @}
/** Compute the maximum and minimum possible value for each function
* in an environment. */
FuncValueBounds compute_function_value_bounds(const std::vector<std::string> &order,
const std::map<std::string, Function> &env);
/* Find an upper bound of bounds.max - bounds.min. */
Expr span_of_bounds(const Interval &bounds);
void bounds_test();
} // namespace Internal
} // namespace Halide
#endif
Interval.h
Defines the Interval class.
Scope.h
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
Halide::Internal::Function
A reference-counted handle to Halide's internal representation of a function.
Definition Function.h:39
Halide::Internal::Scope
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition Scope.h:94
Halide::Internal::Scope::empty_scope
static const Scope< T > & empty_scope()
A const ref to an empty scope.
Definition Scope.h:120
Halide::Internal
Definition AbstractGenerator.h:24
Halide::Internal::compute_function_value_bounds
FuncValueBounds compute_function_value_bounds(const std::vector< std::string > &order, const std::map< std::string, Function > &env)
Compute the maximum and minimum possible value for each function in an environment.
Halide::Internal::bounds_test
void bounds_test()
Halide::Internal::find_constant_bound
Expr find_constant_bound(const Expr &e, Direction d, const Scope< Interval > &scope=Scope< Interval >::empty_scope())
Halide::Internal::box_required
Box box_required(const Expr &e, const std::string &fn, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Variants of the above that are only concerned with a single function.
Halide::Internal::empty_func_value_bounds
const FuncValueBounds & empty_func_value_bounds()
Halide::Internal::boxes_touched
std::map< std::string, Box > boxes_touched(const Expr &e, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Compute rectangular domains large enough to cover all the 'Call's and 'Provides's to each function th...
Halide::Internal::Direction
Direction
Given a varying expression, try to find a constant that is either: An upper bound (always greater tha...
Definition Bounds.h:42
Halide::Internal::Direction::Upper
@ Upper
Definition Bounds.h:42
Halide::Internal::Direction::Lower
@ Lower
Definition Bounds.h:43
Halide::Internal::boxes_provided
std::map< std::string, Box > boxes_provided(const Expr &e, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Compute rectangular domains large enough to cover all the 'Provides's to each function that occurs wi...
Halide::Internal::box_intersection
Box box_intersection(const Box &a, const Box &b)
The intersection of two boxes.
Halide::Internal::box_contains
bool box_contains(const Box &a, const Box &b)
Test if box a provably contains box b.
Halide::Internal::bounds_of_expr_in_scope
Interval bounds_of_expr_in_scope(const Expr &expr, const Scope< Interval > &scope, const FuncValueBounds &func_bounds=empty_func_value_bounds(), bool const_bound=false)
Given an expression in some variables, and a map from those variables to their bounds (in the form of...
Halide::Internal::span_of_bounds
Expr span_of_bounds(const Interval &bounds)
Halide::Internal::find_constant_bounds
Interval find_constant_bounds(const Expr &e, const Scope< Interval > &scope)
Find bounds for a varying expression that are either constants or +/-inf.
Halide::Internal::box_touched
Box box_touched(const Expr &e, const std::string &fn, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Halide::Internal::FuncValueBounds
std::map< std::pair< std::string, int >, Interval > FuncValueBounds
Definition Bounds.h:17
Halide::Internal::merge_boxes
void merge_boxes(Box &a, const Box &b)
Expand box a to encompass box b.
Halide::Internal::boxes_overlap
bool boxes_overlap(const Box &a, const Box &b)
Test if box a could possibly overlap box b.
Halide::Internal::boxes_required
std::map< std::string, Box > boxes_required(const Expr &e, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Compute rectangular domains large enough to cover all the 'Call's to each function that occurs within...
Halide::Internal::box_provided
Box box_provided(const Expr &e, const std::string &fn, const Scope< Interval > &scope=Scope< Interval >::empty_scope(), const FuncValueBounds &func_bounds=empty_func_value_bounds())
Halide::Internal::box_union
Box box_union(const Box &a, const Box &b)
The union of two boxes.
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::Internal::Box
Represents the bounds of a region of arbitrary dimension.
Definition Bounds.h:53
Halide::Internal::Box::maybe_unused
bool maybe_unused() const
Check if the used condition is defined and not trivially true.
Halide::Internal::Box::Box
Box(size_t sz)
Definition Bounds.h:61
Halide::Internal::Box::Box
Box()=default
Halide::Internal::Box::operator[]
const Interval & operator[](size_t i) const
Definition Bounds.h:77
Halide::Internal::Box::size
size_t size() const
Definition Bounds.h:68
Halide::Internal::Box::used
Expr used
The conditions under which this region may be touched.
Definition Bounds.h:55
Halide::Internal::Box::push_back
void push_back(const Interval &i)
Definition Bounds.h:83
Halide::Internal::Box::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Box &b)
Halide::Internal::Box::resize
void resize(size_t sz)
Definition Bounds.h:80
Halide::Internal::Box::Box
Box(const std::vector< Interval > &b)
Definition Bounds.h:64
Halide::Internal::Box::bounds
std::vector< Interval > bounds
The bounds if it is touched.
Definition Bounds.h:58
Halide::Internal::Box::operator[]
Interval & operator[](size_t i)
Definition Bounds.h:74
Halide::Internal::Box::empty
bool empty() const
Definition Bounds.h:71
Halide::Internal::Interval
A class to represent ranges of Exprs.
Definition Interval.h:14
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427