Halide
AutoScheduleUtils.h
Go to the documentation of this file.
1 #ifndef HALIDE_INTERNAL_AUTO_SCHEDULE_UTILS_H
2 #define HALIDE_INTERNAL_AUTO_SCHEDULE_UTILS_H
3 
4 /** \file
5  *
6  * Defines util functions that used by auto scheduler.
7  */
8 
9 #include <limits>
10 #include <set>
11 
12 #include "Bounds.h"
13 #include "Definition.h"
14 #include "IRVisitor.h"
15 #include "Interval.h"
16 
17 namespace Halide {
18 namespace Internal {
19 
20 typedef std::map<std::string, Interval> DimBounds;
21 
23 
24 /** Visitor for keeping track of functions that are directly called and the
25  * arguments with which they are called. */
26 class FindAllCalls : public IRVisitor {
27  using IRVisitor::visit;
28 
29  void visit(const Call *call) override {
30  if (call->call_type == Call::Halide || call->call_type == Call::Image) {
31  funcs_called.insert(call->name);
32  call_args.emplace_back(call->name, call->args);
33  }
34  for (const auto &arg : call->args) {
35  arg.accept(this);
36  }
37  }
38 
39 public:
40  std::set<std::string> funcs_called;
41  std::vector<std::pair<std::string, std::vector<Expr>>> call_args;
42 };
43 
44 /** Return an int representation of 's'. Throw an error on failure. */
45 int string_to_int(const std::string &s);
46 
47 /** Substitute every variable in an Expr or a Stmt with its estimate
48  * if specified. */
49 //@{
52 //@}
53 
54 /** Return the size of an interval. Return an undefined expr if the interval
55  * is unbounded. */
56 Expr get_extent(const Interval &i);
57 
58 /** Return the size of an n-d box. */
59 Expr box_size(const Box &b);
60 
61 /** Helper function to print the bounds of a region. */
62 void disp_regions(const std::map<std::string, Box> &regions);
63 
64 /** Return the corresponding definition of a function given the stage. This
65  * will throw an assertion if the function is an extern function (Extern Func
66  * does not have definition). */
67 Definition get_stage_definition(const Function &f, int stage_num);
68 
69 /** Return the corresponding loop dimensions of a function given the stage.
70  * For extern Func, this will return a list of size 1 containing the
71  * dummy __outermost loop dimension. */
72 std::vector<Dim> &get_stage_dims(const Function &f, int stage_num);
73 
74 /** Add partial load costs to the corresponding function in the result costs. */
75 void combine_load_costs(std::map<std::string, Expr> &result,
76  const std::map<std::string, Expr> &partial);
77 
78 /** Return the required bounds of an intermediate stage (f, stage_num) of
79  * function 'f' given the bounds of the pure dimensions. */
80 DimBounds get_stage_bounds(const Function &f, int stage_num, const DimBounds &pure_bounds);
81 
82 /** Return the required bounds for all the stages of the function 'f'. Each entry
83  * in the returned vector corresponds to a stage. */
84 std::vector<DimBounds> get_stage_bounds(const Function &f, const DimBounds &pure_bounds);
85 
86 /** Recursively inline all the functions in the set 'inlines' into the
87  * expression 'e' and return the resulting expression. If 'order' is
88  * passed, inlining will be done in the reverse order of function realization
89  * to avoid extra inlining works. */
90 Expr perform_inline(Expr e, const std::map<std::string, Function> &env,
91  const std::set<std::string> &inlines = std::set<std::string>(),
92  const std::vector<std::string> &order = std::vector<std::string>());
93 
94 /** Return all functions that are directly called by a function stage (f, stage). */
95 std::set<std::string> get_parents(Function f, int stage);
96 
97 /** Return value of element within a map. This will assert if the element is not
98  * in the map. */
99 // @{
100 template<typename K, typename V>
101 V get_element(const std::map<K, V> &m, const K &key) {
102  const auto &iter = m.find(key);
103  internal_assert(iter != m.end());
104  return iter->second;
105 }
106 
107 template<typename K, typename V>
108 V &get_element(std::map<K, V> &m, const K &key) {
109  const auto &iter = m.find(key);
110  internal_assert(iter != m.end());
111  return iter->second;
112 }
113 // @}
114 
115 /** If the cost of computing a Func is about the same as calling the Func,
116  * inline the Func. Return true of any of the Funcs is inlined. */
117 bool inline_all_trivial_functions(const std::vector<Function> &outputs,
118  const std::vector<std::string> &order,
119  const std::map<std::string, Function> &env);
120 
121 /** Determine if a Func (order[index]) is only consumed by another single Func
122  * in element-wise manner. If it is, return the name of the consumer Func;
123  * otherwise, return an empty string. */
124 std::string is_func_called_element_wise(const std::vector<std::string> &order, size_t index,
125  const std::map<std::string, Function> &env);
126 
127 /** Inline a Func if its values are only consumed by another single Func in
128  * element-wise manner. */
129 bool inline_all_element_wise_functions(const std::vector<Function> &outputs,
130  const std::vector<std::string> &order,
131  const std::map<std::string, Function> &env);
132 
134 
135 } // namespace Internal
136 } // namespace Halide
137 
138 #endif
internal_assert
#define internal_assert(c)
Definition: Errors.h:19
Halide::Internal::IRVisitor::visit
virtual void visit(const IntImm *)
Halide::Internal::FindAllCalls::call_args
std::vector< std::pair< std::string, std::vector< Expr > > > call_args
Definition: AutoScheduleUtils.h:41
Definition.h
Halide::Internal::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
Halide::Internal::FindAllCalls
Visitor for keeping track of functions that are directly called and the arguments with which they are...
Definition: AutoScheduleUtils.h:26
Halide::min
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:584
Bounds.h
Halide::Internal::substitute_var_estimates
Expr substitute_var_estimates(Expr e)
Substitute every variable in an Expr or a Stmt with its estimate if specified.
Halide::Internal::DimBounds
std::map< std::string, Interval > DimBounds
Definition: AutoScheduleUtils.h:20
Halide::Internal::get_parents
std::set< std::string > get_parents(Function f, int stage)
Return all functions that are directly called by a function stage (f, stage).
Halide::Internal::Call::Halide
@ Halide
A call to a Func.
Definition: IR.h:489
Halide::Internal::Definition
A Function definition which can either represent a init or an update definition.
Definition: Definition.h:38
Halide::Internal::FindAllCalls::funcs_called
std::set< std::string > funcs_called
Definition: AutoScheduleUtils.h:40
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:418
Halide::Internal::get_extent
Expr get_extent(const Interval &i)
Return the size of an interval.
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::Internal::Call::Image
@ Image
A load from an input image.
Definition: IR.h:485
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::is_func_called_element_wise
std::string is_func_called_element_wise(const std::vector< std::string > &order, size_t index, const std::map< std::string, Function > &env)
Determine if a Func (order[index]) is only consumed by another single Func in element-wise manner.
Halide::Internal::get_element
V get_element(const std::map< K, V > &m, const K &key)
Return value of element within a map.
Definition: AutoScheduleUtils.h:101
IRVisitor.h
Halide::Internal::get_stage_dims
std::vector< Dim > & get_stage_dims(const Function &f, int stage_num)
Return the corresponding loop dimensions of a function given the stage.
Halide::Internal::inline_all_trivial_functions
bool inline_all_trivial_functions(const std::vector< Function > &outputs, const std::vector< std::string > &order, const std::map< std::string, Function > &env)
If the cost of computing a Func is about the same as calling the Func, inline the Func.
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:22
Halide::Internal::box_size
Expr box_size(const Box &b)
Return the size of an n-d box.
Halide::Internal::Call::name
std::string name
Definition: IR.h:483
Halide::Internal::perform_inline
Expr perform_inline(Expr e, const std::map< std::string, Function > &env, const std::set< std::string > &inlines=std::set< std::string >(), const std::vector< std::string > &order=std::vector< std::string >())
Recursively inline all the functions in the set 'inlines' into the expression 'e' and return the resu...
Halide::Internal::Interval
A class to represent ranges of Exprs.
Definition: Interval.h:14
Halide::Internal::unknown
const int64_t unknown
Definition: AutoScheduleUtils.h:22
Halide::Internal::combine_load_costs
void combine_load_costs(std::map< std::string, Expr > &result, const std::map< std::string, Expr > &partial)
Add partial load costs to the corresponding function in the result costs.
Halide::Internal::Call::args
std::vector< Expr > args
Definition: IR.h:484
Halide::Internal::Function
A reference-counted handle to Halide's internal representation of a function.
Definition: Function.h:39
Halide::Internal::Call
A function call.
Definition: IR.h:482
Halide::Internal::get_stage_bounds
DimBounds get_stage_bounds(const Function &f, int stage_num, const DimBounds &pure_bounds)
Return the required bounds of an intermediate stage (f, stage_num) of function 'f' given the bounds o...
Halide::Internal::disp_regions
void disp_regions(const std::map< std::string, Box > &regions)
Helper function to print the bounds of a region.
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
Interval.h
Halide::Internal::Call::call_type
CallType call_type
Definition: IR.h:493
Halide::Internal::inline_all_element_wise_functions
bool inline_all_element_wise_functions(const std::vector< Function > &outputs, const std::vector< std::string > &order, const std::map< std::string, Function > &env)
Inline a Func if its values are only consumed by another single Func in element-wise manner.
Halide::Internal::propagate_estimate_test
void propagate_estimate_test()
Halide::Internal::string_to_int
int string_to_int(const std::string &s)
Return an int representation of 's'.
Halide::Internal::Box
Represents the bounds of a region of arbitrary dimension.
Definition: Bounds.h:53
Halide::Internal::get_stage_definition
Definition get_stage_definition(const Function &f, int stage_num)
Return the corresponding definition of a function given the stage.