Halide  12.0.1
Halide compiler and libraries
Generator.h
Go to the documentation of this file.
1 #ifndef HALIDE_GENERATOR_H_
2 #define HALIDE_GENERATOR_H_
3 
4 /** \file
5  *
6  * Generator is a class used to encapsulate the building of Funcs in user
7  * pipelines. A Generator is agnostic to JIT vs AOT compilation; it can be used for
8  * either purpose, but is especially convenient to use for AOT compilation.
9  *
10  * A Generator explicitly declares the Inputs and Outputs associated for a given
11  * pipeline, and (optionally) separates the code for constructing the outputs from the code from
12  * scheduling them. For instance:
13  *
14  * \code
15  * class Blur : public Generator<Blur> {
16  * public:
17  * Input<Func> input{"input", UInt(16), 2};
18  * Output<Func> output{"output", UInt(16), 2};
19  * void generate() {
20  * blur_x(x, y) = (input(x, y) + input(x+1, y) + input(x+2, y))/3;
21  * blur_y(x, y) = (blur_x(x, y) + blur_x(x, y+1) + blur_x(x, y+2))/3;
22  * output(x, y) = blur(x, y);
23  * }
24  * void schedule() {
25  * blur_y.split(y, y, yi, 8).parallel(y).vectorize(x, 8);
26  * blur_x.store_at(blur_y, y).compute_at(blur_y, yi).vectorize(x, 8);
27  * }
28  * private:
29  * Var x, y, xi, yi;
30  * Func blur_x, blur_y;
31  * };
32  * \endcode
33  *
34  * Halide can compile a Generator into the correct pipeline by introspecting these
35  * values and constructing an appropriate signature based on them.
36  *
37  * A Generator provides implementations of two methods:
38  *
39  * - generate(), which must fill in all Output Func(s); it may optionally also do scheduling
40  * if no schedule() method is present.
41  * - schedule(), which (if present) should contain all scheduling code.
42  *
43  * Inputs can be any C++ scalar type:
44  *
45  * \code
46  * Input<float> radius{"radius"};
47  * Input<int32_t> increment{"increment"};
48  * \endcode
49  *
50  * An Input<Func> is (essentially) like an ImageParam, except that it may (or may
51  * not) not be backed by an actual buffer, and thus has no defined extents.
52  *
53  * \code
54  * Input<Func> input{"input", Float(32), 2};
55  * \endcode
56  *
57  * You can optionally make the type and/or dimensions of Input<Func> unspecified,
58  * in which case the value is simply inferred from the actual Funcs passed to them.
59  * Of course, if you specify an explicit Type or Dimension, we still require the
60  * input Func to match, or a compilation error results.
61  *
62  * \code
63  * Input<Func> input{ "input", 3 }; // require 3-dimensional Func,
64  * // but leave Type unspecified
65  * \endcode
66  *
67  * A Generator must explicitly list the output(s) it produces:
68  *
69  * \code
70  * Output<Func> output{"output", Float(32), 2};
71  * \endcode
72  *
73  * You can specify an output that returns a Tuple by specifying a list of Types:
74  *
75  * \code
76  * class Tupler : Generator<Tupler> {
77  * Input<Func> input{"input", Int(32), 2};
78  * Output<Func> output{"output", {Float(32), UInt(8)}, 2};
79  * void generate() {
80  * Var x, y;
81  * Expr a = cast<float>(input(x, y));
82  * Expr b = cast<uint8_t>(input(x, y));
83  * output(x, y) = Tuple(a, b);
84  * }
85  * };
86  * \endcode
87  *
88  * You can also specify Output<X> for any scalar type (except for Handle types);
89  * this is merely syntactic sugar on top of a zero-dimensional Func, but can be
90  * quite handy, especially when used with multiple outputs:
91  *
92  * \code
93  * Output<float> sum{"sum"}; // equivalent to Output<Func> {"sum", Float(32), 0}
94  * \endcode
95  *
96  * As with Input<Func>, you can optionally make the type and/or dimensions of an
97  * Output<Func> unspecified; any unspecified types must be resolved via an
98  * implicit GeneratorParam in order to use top-level compilation.
99  *
100  * You can also declare an *array* of Input or Output, by using an array type
101  * as the type parameter:
102  *
103  * \code
104  * // Takes exactly 3 images and outputs exactly 3 sums.
105  * class SumRowsAndColumns : Generator<SumRowsAndColumns> {
106  * Input<Func[3]> inputs{"inputs", Float(32), 2};
107  * Input<int32_t[2]> extents{"extents"};
108  * Output<Func[3]> sums{"sums", Float(32), 1};
109  * void generate() {
110  * assert(inputs.size() == sums.size());
111  * // assume all inputs are same extent
112  * Expr width = extent[0];
113  * Expr height = extent[1];
114  * for (size_t i = 0; i < inputs.size(); ++i) {
115  * RDom r(0, width, 0, height);
116  * sums[i]() = 0.f;
117  * sums[i]() += inputs[i](r.x, r.y);
118  * }
119  * }
120  * };
121  * \endcode
122  *
123  * You can also leave array size unspecified, with some caveats:
124  * - For ahead-of-time compilation, Inputs must have a concrete size specified
125  * via a GeneratorParam at build time (e.g., pyramid.size=3)
126  * - For JIT compilation via a Stub, Inputs array sizes will be inferred
127  * from the vector passed.
128  * - For ahead-of-time compilation, Outputs may specify a concrete size
129  * via a GeneratorParam at build time (e.g., pyramid.size=3), or the
130  * size can be specified via a resize() method.
131  *
132  * \code
133  * class Pyramid : public Generator<Pyramid> {
134  * public:
135  * GeneratorParam<int32_t> levels{"levels", 10};
136  * Input<Func> input{ "input", Float(32), 2 };
137  * Output<Func[]> pyramid{ "pyramid", Float(32), 2 };
138  * void generate() {
139  * pyramid.resize(levels);
140  * pyramid[0](x, y) = input(x, y);
141  * for (int i = 1; i < pyramid.size(); i++) {
142  * pyramid[i](x, y) = (pyramid[i-1](2*x, 2*y) +
143  * pyramid[i-1](2*x+1, 2*y) +
144  * pyramid[i-1](2*x, 2*y+1) +
145  * pyramid[i-1](2*x+1, 2*y+1))/4;
146  * }
147  * }
148  * };
149  * \endcode
150  *
151  * A Generator can also be customized via compile-time parameters (GeneratorParams),
152  * which affect code generation.
153  *
154  * GeneratorParams, Inputs, and Outputs are (by convention) always
155  * public and always declared at the top of the Generator class, in the order
156  *
157  * \code
158  * GeneratorParam(s)
159  * Input<Func>(s)
160  * Input<non-Func>(s)
161  * Output<Func>(s)
162  * \endcode
163  *
164  * Note that the Inputs and Outputs will appear in the C function call in the order
165  * they are declared. All Input<Func> and Output<Func> are represented as halide_buffer_t;
166  * all other Input<> are the appropriate C++ scalar type. (GeneratorParams are
167  * always referenced by name, not position, so their order is irrelevant.)
168  *
169  * All Inputs and Outputs must have explicit names, and all such names must match
170  * the regex [A-Za-z][A-Za-z_0-9]* (i.e., essentially a C/C++ variable name, with
171  * some extra restrictions on underscore use). By convention, the name should match
172  * the member-variable name.
173  *
174  * You can dynamically add Inputs and Outputs to your Generator via adding a
175  * configure() method; if present, it will be called before generate(). It can
176  * examine GeneratorParams but it may not examine predeclared Inputs or Outputs;
177  * the only thing it should do is call add_input<>() and/or add_output<>(), or call
178  * set_type()/set_dimensions()/set_array_size() on an Input or Output with an unspecified type.
179  * Added inputs will be appended (in order) after predeclared Inputs but before
180  * any Outputs; added outputs will be appended after predeclared Outputs.
181  *
182  * Note that the pointers returned by add_input() and add_output() are owned
183  * by the Generator and will remain valid for the Generator's lifetime; user code
184  * should not attempt to delete or free them.
185  *
186  * \code
187  * class MultiSum : public Generator<MultiSum> {
188  * public:
189  * GeneratorParam<int32_t> input_count{"input_count", 10};
190  * Output<Func> output{ "output", Float(32), 2 };
191  *
192  * void configure() {
193  * for (int i = 0; i < input_count; ++i) {
194  * extra_inputs.push_back(
195  * add_input<Func>("input_" + std::to_string(i), Float(32), 2);
196  * }
197  * }
198  *
199  * void generate() {
200  * Expr sum = 0.f;
201  * for (int i = 0; i < input_count; ++i) {
202  * sum += (*extra_inputs)[i](x, y);
203  * }
204  * output(x, y) = sum;
205  * }
206  * private:
207  * std::vector<Input<Func>* extra_inputs;
208  * };
209  * \endcode
210  *
211  * All Generators have three GeneratorParams that are implicitly provided
212  * by the base class:
213  *
214  * GeneratorParam<Target> target{"target", Target()};
215  * GeneratorParam<bool> auto_schedule{"auto_schedule", false};
216  * GeneratorParam<MachineParams> machine_params{"machine_params", MachineParams::generic()};
217  *
218  * - 'target' is the Halide::Target for which the Generator is producing code.
219  * It is read-only during the Generator's lifetime, and must not be modified;
220  * its value should always be filled in by the calling code: either the Halide
221  * build system (for ahead-of-time compilation), or ordinary C++ code
222  * (for JIT compilation).
223  * - 'auto_schedule' indicates whether the auto-scheduler should be run for this
224  * Generator:
225  * - if 'false', the Generator should schedule its Funcs as it sees fit.
226  * - if 'true', the Generator should only provide estimate()s for its Funcs,
227  * and not call any other scheduling methods.
228  * - 'machine_params' is only used if auto_schedule is true; it is ignored
229  * if auto_schedule is false. It provides details about the machine architecture
230  * being targeted which may be used to enhance the automatically-generated
231  * schedule.
232  *
233  * Generators are added to a global registry to simplify AOT build mechanics; this
234  * is done by simply using the HALIDE_REGISTER_GENERATOR macro at global scope:
235  *
236  * \code
237  * HALIDE_REGISTER_GENERATOR(ExampleGen, jit_example)
238  * \endcode
239  *
240  * The registered name of the Generator is provided must match the same rules as
241  * Input names, above.
242  *
243  * Note that the class name of the generated Stub class will match the registered
244  * name by default; if you want to vary it (typically, to include namespaces),
245  * you can add it as an optional third argument:
246  *
247  * \code
248  * HALIDE_REGISTER_GENERATOR(ExampleGen, jit_example, SomeNamespace::JitExampleStub)
249  * \endcode
250  *
251  * Note that a Generator is always executed with a specific Target assigned to it,
252  * that you can access via the get_target() method. (You should *not* use the
253  * global get_target_from_environment(), etc. methods provided in Target.h)
254  *
255  * (Note that there are older variations of Generator that differ from what's
256  * documented above; these are still supported but not described here. See
257  * https://github.com/halide/Halide/wiki/Old-Generator-Documentation for
258  * more information.)
259  */
260 
261 #include <algorithm>
262 #include <functional>
263 #include <iterator>
264 #include <limits>
265 #include <memory>
266 #include <mutex>
267 #include <set>
268 #include <sstream>
269 #include <string>
270 #include <type_traits>
271 #include <utility>
272 #include <vector>
273 
274 #include "ExternalCode.h"
275 #include "Func.h"
276 #include "ImageParam.h"
277 #include "Introspection.h"
278 #include "ObjectInstanceRegistry.h"
279 #include "Target.h"
280 
281 namespace Halide {
282 
283 template<typename T>
284 class Buffer;
285 
286 namespace Internal {
287 
289 
290 /**
291  * ValueTracker is an internal utility class that attempts to track and flag certain
292  * obvious Stub-related errors at Halide compile time: it tracks the constraints set
293  * on any Parameter-based argument (i.e., Input<Buffer> and Output<Buffer>) to
294  * ensure that incompatible values aren't set.
295  *
296  * e.g.: if a Generator A requires stride[0] == 1,
297  * and Generator B uses Generator A via stub, but requires stride[0] == 4,
298  * we should be able to detect this at Halide compilation time, and fail immediately,
299  * rather than producing code that fails at runtime and/or runs slowly due to
300  * vectorization being unavailable.
301  *
302  * We do this by tracking the active values at entrance and exit to all user-provided
303  * Generator methods (build()/generate()/schedule()); if we ever find more than two unique
304  * values active, we know we have a potential conflict. ("two" here because the first
305  * value is the default value for a given constraint.)
306  *
307  * Note that this won't catch all cases:
308  * -- JIT compilation has no way to check for conflicts at the top-level
309  * -- constraints that match the default value (e.g. if dim(0).set_stride(1) is the
310  * first value seen by the tracker) will be ignored, so an explicit requirement set
311  * this way can be missed
312  *
313  * Nevertheless, this is likely to be much better than nothing when composing multiple
314  * layers of Stubs in a single fused result.
315  */
317 private:
318  std::map<std::string, std::vector<std::vector<Expr>>> values_history;
319  const size_t max_unique_values;
320 
321 public:
322  explicit ValueTracker(size_t max_unique_values = 2)
323  : max_unique_values(max_unique_values) {
324  }
325  void track_values(const std::string &name, const std::vector<Expr> &values);
326 };
327 
328 std::vector<Expr> parameter_constraints(const Parameter &p);
329 
330 template<typename T>
331 HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map<std::string, T> &enum_map, const T &t) {
332  for (const auto &key_value : enum_map) {
333  if (t == key_value.second) {
334  return key_value.first;
335  }
336  }
337  user_error << "Enumeration value not found.\n";
338  return "";
339 }
340 
341 template<typename T>
342 T enum_from_string(const std::map<std::string, T> &enum_map, const std::string &s) {
343  auto it = enum_map.find(s);
344  user_assert(it != enum_map.end()) << "Enumeration value not found: " << s << "\n";
345  return it->second;
346 }
347 
348 extern const std::map<std::string, Halide::Type> &get_halide_type_enum_map();
349 inline std::string halide_type_to_enum_string(const Type &t) {
351 }
352 
353 // Convert a Halide Type into a string representation of its C source.
354 // e.g., Int(32) -> "Halide::Int(32)"
355 std::string halide_type_to_c_source(const Type &t);
356 
357 // Convert a Halide Type into a string representation of its C Source.
358 // e.g., Int(32) -> "int32_t"
359 std::string halide_type_to_c_type(const Type &t);
360 
361 /** generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() +
362  * compile_to_files(); it can be trivially wrapped by a "real" main() to produce a
363  * command-line utility for ahead-of-time filter compilation. */
364 int generate_filter_main(int argc, char **argv, std::ostream &cerr);
365 
366 // select_type<> is to std::conditional as switch is to if:
367 // it allows a multiway compile-time type definition via the form
368 //
369 // select_type<cond<condition1, type1>,
370 // cond<condition2, type2>,
371 // ....
372 // cond<conditionN, typeN>>::type
373 //
374 // Note that the conditions are evaluated in order; the first evaluating to true
375 // is chosen.
376 //
377 // Note that if no conditions evaluate to true, the resulting type is illegal
378 // and will produce a compilation error. (You can provide a default by simply
379 // using cond<true, SomeType> as the final entry.)
380 template<bool B, typename T>
381 struct cond {
382  static constexpr bool value = B;
383  using type = T;
384 };
385 
386 template<typename First, typename... Rest>
387 struct select_type : std::conditional<First::value, typename First::type, typename select_type<Rest...>::type> {};
388 
389 template<typename First>
390 struct select_type<First> { using type = typename std::conditional<First::value, typename First::type, void>::type; };
391 
392 class GeneratorBase;
393 class GeneratorParamInfo;
394 
396 public:
397  explicit GeneratorParamBase(const std::string &name);
399 
400  inline const std::string &name() const {
401  return name_;
402  }
403 
404  // overload the set() function to call the right virtual method based on type.
405  // This allows us to attempt to set a GeneratorParam via a
406  // plain C++ type, even if we don't know the specific templated
407  // subclass. Attempting to set the wrong type will assert.
408  // Notice that there is no typed setter for Enums, for obvious reasons;
409  // setting enums in an unknown type must fallback to using set_from_string.
410  //
411  // It's always a bit iffy to use macros for this, but IMHO it clarifies the situation here.
412 #define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
413  virtual void set(const TYPE &new_value) = 0;
414 
430 
431 #undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
432 
433  // Add overloads for string and char*
434  void set(const std::string &new_value) {
435  set_from_string(new_value);
436  }
437  void set(const char *new_value) {
438  set_from_string(std::string(new_value));
439  }
440 
441 protected:
442  friend class GeneratorBase;
443  friend class GeneratorParamInfo;
444  friend class StubEmitter;
445 
446  void check_value_readable() const;
447  void check_value_writable() const;
448 
449  // All GeneratorParams are settable from string.
450  virtual void set_from_string(const std::string &value_string) = 0;
451 
452  virtual std::string call_to_string(const std::string &v) const = 0;
453  virtual std::string get_c_type() const = 0;
454 
455  virtual std::string get_type_decls() const {
456  return "";
457  }
458 
459  virtual std::string get_default_value() const = 0;
460 
461  virtual bool is_synthetic_param() const {
462  return false;
463  }
464 
465  virtual bool is_looplevel_param() const {
466  return false;
467  }
468 
469  void fail_wrong_type(const char *type);
470 
471 private:
472  const std::string name_;
473 
474  // Generator which owns this GeneratorParam. Note that this will be null
475  // initially; the GeneratorBase itself will set this field when it initially
476  // builds its info about params. However, since it (generally) isn't
477  // appropriate for GeneratorParam<> to be declared outside of a Generator,
478  // all reasonable non-testing code should expect this to be non-null.
479  GeneratorBase *generator{nullptr};
480 
481 public:
486 };
487 
488 // This is strictly some syntactic sugar to suppress certain compiler warnings.
489 template<typename FROM, typename TO>
490 struct Convert {
491  template<typename TO2 = TO, typename std::enable_if<!std::is_same<TO2, bool>::value>::type * = nullptr>
492  inline static TO2 value(const FROM &from) {
493  return static_cast<TO2>(from);
494  }
495 
496  template<typename TO2 = TO, typename std::enable_if<std::is_same<TO2, bool>::value>::type * = nullptr>
497  inline static TO2 value(const FROM &from) {
498  return from != 0;
499  }
500 };
501 
502 template<typename T>
504 public:
505  using type = T;
506 
507  GeneratorParamImpl(const std::string &name, const T &value)
509  }
510 
511  T value() const {
512  this->check_value_readable();
513  return value_;
514  }
515 
516  operator T() const {
517  return this->value();
518  }
519 
520  operator Expr() const {
521  return make_const(type_of<T>(), this->value());
522  }
523 
524 #define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
525  void set(const TYPE &new_value) override { \
526  typed_setter_impl<TYPE>(new_value, #TYPE); \
527  }
528 
544 
545 #undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
546 
547  // Overload for std::string.
548  void set(const std::string &new_value) {
550  value_ = new_value;
551  }
552 
553 protected:
554  virtual void set_impl(const T &new_value) {
556  value_ = new_value;
557  }
558 
559  // Needs to be protected to allow GeneratorParam<LoopLevel>::set() override
561 
562 private:
563  // If FROM->T is not legal, fail
564  template<typename FROM, typename std::enable_if<
565  !std::is_convertible<FROM, T>::value>::type * = nullptr>
566  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &, const char *msg) {
567  fail_wrong_type(msg);
568  }
569 
570  // If FROM and T are identical, just assign
571  template<typename FROM, typename std::enable_if<
572  std::is_same<FROM, T>::value>::type * = nullptr>
573  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
575  value_ = value;
576  }
577 
578  // If both FROM->T and T->FROM are legal, ensure it's lossless
579  template<typename FROM, typename std::enable_if<
580  !std::is_same<FROM, T>::value &&
581  std::is_convertible<FROM, T>::value &&
582  std::is_convertible<T, FROM>::value>::type * = nullptr>
583  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
585  const T t = Convert<FROM, T>::value(value);
586  const FROM value2 = Convert<T, FROM>::value(t);
587  if (value2 != value) {
588  fail_wrong_type(msg);
589  }
590  value_ = t;
591  }
592 
593  // If FROM->T is legal but T->FROM is not, just assign
594  template<typename FROM, typename std::enable_if<
595  !std::is_same<FROM, T>::value &&
596  std::is_convertible<FROM, T>::value &&
597  !std::is_convertible<T, FROM>::value>::type * = nullptr>
598  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
600  value_ = value;
601  }
602 };
603 
604 // Stubs for type-specific implementations of GeneratorParam, to avoid
605 // many complex enable_if<> statements that were formerly spread through the
606 // implementation. Note that not all of these need to be templated classes,
607 // (e.g. for GeneratorParam_Target, T == Target always), but are declared
608 // that way for symmetry of declaration.
609 template<typename T>
611 public:
612  GeneratorParam_Target(const std::string &name, const T &value)
613  : GeneratorParamImpl<T>(name, value) {
614  }
615 
616  void set_from_string(const std::string &new_value_string) override {
617  this->set(Target(new_value_string));
618  }
619 
620  std::string get_default_value() const override {
621  return this->value().to_string();
622  }
623 
624  std::string call_to_string(const std::string &v) const override {
625  std::ostringstream oss;
626  oss << v << ".to_string()";
627  return oss.str();
628  }
629 
630  std::string get_c_type() const override {
631  return "Target";
632  }
633 };
634 
635 template<typename T>
637 public:
638  GeneratorParam_MachineParams(const std::string &name, const T &value)
639  : GeneratorParamImpl<T>(name, value) {
640  }
641 
642  void set_from_string(const std::string &new_value_string) override {
643  this->set(MachineParams(new_value_string));
644  }
645 
646  std::string get_default_value() const override {
647  return this->value().to_string();
648  }
649 
650  std::string call_to_string(const std::string &v) const override {
651  std::ostringstream oss;
652  oss << v << ".to_string()";
653  return oss.str();
654  }
655 
656  std::string get_c_type() const override {
657  return "MachineParams";
658  }
659 };
660 
661 class GeneratorParam_LoopLevel : public GeneratorParamImpl<LoopLevel> {
662 public:
663  GeneratorParam_LoopLevel(const std::string &name, const LoopLevel &value)
665  }
666 
668 
669  void set(const LoopLevel &value) override {
670  // Don't call check_value_writable(): It's OK to set a LoopLevel after generate().
671  // check_value_writable();
672 
673  // This looks odd, but is deliberate:
674 
675  // First, mutate the existing contents to match the value passed in,
676  // so that any existing usage of the LoopLevel now uses the newer value.
677  // (Strictly speaking, this is really only necessary if this method
678  // is called after generate(): before generate(), there is no usage
679  // to be concerned with.)
680  value_.set(value);
681 
682  // Then, reset the value itself so that it points to the same LoopLevelContents
683  // as the value passed in. (Strictly speaking, this is really only
684  // useful if this method is called before generate(): afterwards, it's
685  // too late to alter the code to refer to a different LoopLevelContents.)
686  value_ = value;
687  }
688 
689  void set_from_string(const std::string &new_value_string) override {
690  if (new_value_string == "root") {
691  this->set(LoopLevel::root());
692  } else if (new_value_string == "inlined") {
693  this->set(LoopLevel::inlined());
694  } else {
695  user_error << "Unable to parse " << this->name() << ": " << new_value_string;
696  }
697  }
698 
699  std::string get_default_value() const override {
700  // This is dodgy but safe in this case: we want to
701  // see what the value of our LoopLevel is *right now*,
702  // so we make a copy and lock the copy so we can inspect it.
703  // (Note that ordinarily this is a bad idea, since LoopLevels
704  // can be mutated later on; however, this method is only
705  // called by the Generator infrastructure, on LoopLevels that
706  // will never be mutated, so this is really just an elaborate way
707  // to avoid runtime assertions.)
708  LoopLevel copy;
709  copy.set(this->value());
710  copy.lock();
711  if (copy.is_inlined()) {
712  return "LoopLevel::inlined()";
713  } else if (copy.is_root()) {
714  return "LoopLevel::root()";
715  } else {
717  return "";
718  }
719  }
720 
721  std::string call_to_string(const std::string &v) const override {
723  return std::string();
724  }
725 
726  std::string get_c_type() const override {
727  return "LoopLevel";
728  }
729 
730  bool is_looplevel_param() const override {
731  return true;
732  }
733 };
734 
735 template<typename T>
737 public:
738  GeneratorParam_Arithmetic(const std::string &name,
739  const T &value,
740  const T &min = std::numeric_limits<T>::lowest(),
741  const T &max = std::numeric_limits<T>::max())
742  : GeneratorParamImpl<T>(name, value), min(min), max(max) {
743  // call set() to ensure value is clamped to min/max
744  this->set(value);
745  }
746 
747  void set_impl(const T &new_value) override {
748  user_assert(new_value >= min && new_value <= max) << "Value out of range: " << new_value;
750  }
751 
752  void set_from_string(const std::string &new_value_string) override {
753  std::istringstream iss(new_value_string);
754  T t;
755  // All one-byte ints int8 and uint8 should be parsed as integers, not chars --
756  // including 'char' itself. (Note that sizeof(bool) is often-but-not-always-1,
757  // so be sure to exclude that case.)
758  if (sizeof(T) == sizeof(char) && !std::is_same<T, bool>::value) {
759  int i;
760  iss >> i;
761  t = (T)i;
762  } else {
763  iss >> t;
764  }
765  user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << new_value_string;
766  this->set(t);
767  }
768 
769  std::string get_default_value() const override {
770  std::ostringstream oss;
771  oss << this->value();
772  if (std::is_same<T, float>::value) {
773  // If the constant has no decimal point ("1")
774  // we must append one before appending "f"
775  if (oss.str().find('.') == std::string::npos) {
776  oss << ".";
777  }
778  oss << "f";
779  }
780  return oss.str();
781  }
782 
783  std::string call_to_string(const std::string &v) const override {
784  std::ostringstream oss;
785  oss << "std::to_string(" << v << ")";
786  return oss.str();
787  }
788 
789  std::string get_c_type() const override {
790  std::ostringstream oss;
791  if (std::is_same<T, float>::value) {
792  return "float";
793  } else if (std::is_same<T, double>::value) {
794  return "double";
795  } else if (std::is_integral<T>::value) {
796  if (std::is_unsigned<T>::value) {
797  oss << "u";
798  }
799  oss << "int" << (sizeof(T) * 8) << "_t";
800  return oss.str();
801  } else {
802  user_error << "Unknown arithmetic type\n";
803  return "";
804  }
805  }
806 
807 private:
808  const T min, max;
809 };
810 
811 template<typename T>
813 public:
814  GeneratorParam_Bool(const std::string &name, const T &value)
816  }
817 
818  void set_from_string(const std::string &new_value_string) override {
819  bool v = false;
820  if (new_value_string == "true" || new_value_string == "True") {
821  v = true;
822  } else if (new_value_string == "false" || new_value_string == "False") {
823  v = false;
824  } else {
825  user_assert(false) << "Unable to parse bool: " << new_value_string;
826  }
827  this->set(v);
828  }
829 
830  std::string get_default_value() const override {
831  return this->value() ? "true" : "false";
832  }
833 
834  std::string call_to_string(const std::string &v) const override {
835  std::ostringstream oss;
836  oss << "std::string((" << v << ") ? \"true\" : \"false\")";
837  return oss.str();
838  }
839 
840  std::string get_c_type() const override {
841  return "bool";
842  }
843 };
844 
845 template<typename T>
847 public:
848  GeneratorParam_Enum(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
849  : GeneratorParamImpl<T>(name, value), enum_map(enum_map) {
850  }
851 
852  // define a "set" that takes our specific enum (but don't hide the inherited virtual functions)
854 
855  template<typename T2 = T, typename std::enable_if<!std::is_same<T2, Type>::value>::type * = nullptr>
856  void set(const T &e) {
857  this->set_impl(e);
858  }
859 
860  void set_from_string(const std::string &new_value_string) override {
861  auto it = enum_map.find(new_value_string);
862  user_assert(it != enum_map.end()) << "Enumeration value not found: " << new_value_string;
863  this->set_impl(it->second);
864  }
865 
866  std::string call_to_string(const std::string &v) const override {
867  return "Enum_" + this->name() + "_map().at(" + v + ")";
868  }
869 
870  std::string get_c_type() const override {
871  return "Enum_" + this->name();
872  }
873 
874  std::string get_default_value() const override {
875  return "Enum_" + this->name() + "::" + enum_to_string(enum_map, this->value());
876  }
877 
878  std::string get_type_decls() const override {
879  std::ostringstream oss;
880  oss << "enum class Enum_" << this->name() << " {\n";
881  for (auto key_value : enum_map) {
882  oss << " " << key_value.first << ",\n";
883  }
884  oss << "};\n";
885  oss << "\n";
886 
887  // TODO: since we generate the enums, we could probably just use a vector (or array!) rather than a map,
888  // since we can ensure that the enum values are a nice tight range.
889  oss << "inline HALIDE_NO_USER_CODE_INLINE const std::map<Enum_" << this->name() << ", std::string>& Enum_" << this->name() << "_map() {\n";
890  oss << " static const std::map<Enum_" << this->name() << ", std::string> m = {\n";
891  for (auto key_value : enum_map) {
892  oss << " { Enum_" << this->name() << "::" << key_value.first << ", \"" << key_value.first << "\"},\n";
893  }
894  oss << " };\n";
895  oss << " return m;\n";
896  oss << "};\n";
897  return oss.str();
898  }
899 
900 private:
901  const std::map<std::string, T> enum_map;
902 };
903 
904 template<typename T>
906 public:
907  GeneratorParam_Type(const std::string &name, const T &value)
909  }
910 
911  std::string call_to_string(const std::string &v) const override {
912  return "Halide::Internal::halide_type_to_enum_string(" + v + ")";
913  }
914 
915  std::string get_c_type() const override {
916  return "Type";
917  }
918 
919  std::string get_default_value() const override {
920  return halide_type_to_c_source(this->value());
921  }
922 
923  std::string get_type_decls() const override {
924  return "";
925  }
926 };
927 
928 template<typename T>
930 public:
931  GeneratorParam_String(const std::string &name, const std::string &value)
932  : GeneratorParamImpl<T>(name, value) {
933  }
934  void set_from_string(const std::string &new_value_string) override {
935  this->set(new_value_string);
936  }
937 
938  std::string get_default_value() const override {
939  return "\"" + this->value() + "\"";
940  }
941 
942  std::string call_to_string(const std::string &v) const override {
943  return v;
944  }
945 
946  std::string get_c_type() const override {
947  return "std::string";
948  }
949 };
950 
951 template<typename T>
953  typename select_type<
962 
963 } // namespace Internal
964 
965 /** GeneratorParam is a templated class that can be used to modify the behavior
966  * of the Generator at code-generation time. GeneratorParams are commonly
967  * specified in build files (e.g. Makefile) to customize the behavior of
968  * a given Generator, thus they have a very constrained set of types to allow
969  * for efficient specification via command-line flags. A GeneratorParam can be:
970  * - any float or int type.
971  * - bool
972  * - enum
973  * - Halide::Target
974  * - Halide::Type
975  * - std::string
976  * Please don't use std::string unless there's no way to do what you want with some
977  * other type; in particular, don't use this if you can use enum instead.
978  * All GeneratorParams have a default value. Arithmetic types can also
979  * optionally specify min and max. Enum types must specify a string-to-value
980  * map.
981  *
982  * Halide::Type is treated as though it were an enum, with the mappings:
983  *
984  * "int8" Halide::Int(8)
985  * "int16" Halide::Int(16)
986  * "int32" Halide::Int(32)
987  * "uint8" Halide::UInt(8)
988  * "uint16" Halide::UInt(16)
989  * "uint32" Halide::UInt(32)
990  * "float32" Halide::Float(32)
991  * "float64" Halide::Float(64)
992  *
993  * No vector Types are currently supported by this mapping.
994  *
995  */
996 template<typename T>
998 public:
999  template<typename T2 = T, typename std::enable_if<!std::is_same<T2, std::string>::value>::type * = nullptr>
1000  GeneratorParam(const std::string &name, const T &value)
1001  : Internal::GeneratorParamImplBase<T>(name, value) {
1002  }
1003 
1004  GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
1005  : Internal::GeneratorParamImplBase<T>(name, value, min, max) {
1006  }
1007 
1008  GeneratorParam(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
1009  : Internal::GeneratorParamImplBase<T>(name, value, enum_map) {
1010  }
1011 
1012  GeneratorParam(const std::string &name, const std::string &value)
1013  : Internal::GeneratorParamImplBase<T>(name, value) {
1014  }
1015 };
1016 
1017 /** Addition between GeneratorParam<T> and any type that supports operator+ with T.
1018  * Returns type of underlying operator+. */
1019 // @{
1020 template<typename Other, typename T>
1021 auto operator+(const Other &a, const GeneratorParam<T> &b) -> decltype(a + (T)b) {
1022  return a + (T)b;
1023 }
1024 template<typename Other, typename T>
1025 auto operator+(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a + b) {
1026  return (T)a + b;
1027 }
1028 // @}
1029 
1030 /** Subtraction between GeneratorParam<T> and any type that supports operator- with T.
1031  * Returns type of underlying operator-. */
1032 // @{
1033 template<typename Other, typename T>
1034 auto operator-(const Other &a, const GeneratorParam<T> &b) -> decltype(a - (T)b) {
1035  return a - (T)b;
1036 }
1037 template<typename Other, typename T>
1038 auto operator-(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a - b) {
1039  return (T)a - b;
1040 }
1041 // @}
1042 
1043 /** Multiplication between GeneratorParam<T> and any type that supports operator* with T.
1044  * Returns type of underlying operator*. */
1045 // @{
1046 template<typename Other, typename T>
1047 auto operator*(const Other &a, const GeneratorParam<T> &b) -> decltype(a * (T)b) {
1048  return a * (T)b;
1049 }
1050 template<typename Other, typename T>
1051 auto operator*(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a * b) {
1052  return (T)a * b;
1053 }
1054 // @}
1055 
1056 /** Division between GeneratorParam<T> and any type that supports operator/ with T.
1057  * Returns type of underlying operator/. */
1058 // @{
1059 template<typename Other, typename T>
1060 auto operator/(const Other &a, const GeneratorParam<T> &b) -> decltype(a / (T)b) {
1061  return a / (T)b;
1062 }
1063 template<typename Other, typename T>
1064 auto operator/(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a / b) {
1065  return (T)a / b;
1066 }
1067 // @}
1068 
1069 /** Modulo between GeneratorParam<T> and any type that supports operator% with T.
1070  * Returns type of underlying operator%. */
1071 // @{
1072 template<typename Other, typename T>
1073 auto operator%(const Other &a, const GeneratorParam<T> &b) -> decltype(a % (T)b) {
1074  return a % (T)b;
1075 }
1076 template<typename Other, typename T>
1077 auto operator%(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a % b) {
1078  return (T)a % b;
1079 }
1080 // @}
1081 
1082 /** Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
1083  * Returns type of underlying operator>. */
1084 // @{
1085 template<typename Other, typename T>
1086 auto operator>(const Other &a, const GeneratorParam<T> &b) -> decltype(a > (T)b) {
1087  return a > (T)b;
1088 }
1089 template<typename Other, typename T>
1090 auto operator>(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a > b) {
1091  return (T)a > b;
1092 }
1093 // @}
1094 
1095 /** Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
1096  * Returns type of underlying operator<. */
1097 // @{
1098 template<typename Other, typename T>
1099 auto operator<(const Other &a, const GeneratorParam<T> &b) -> decltype(a < (T)b) {
1100  return a < (T)b;
1101 }
1102 template<typename Other, typename T>
1103 auto operator<(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a < b) {
1104  return (T)a < b;
1105 }
1106 // @}
1107 
1108 /** Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with T.
1109  * Returns type of underlying operator>=. */
1110 // @{
1111 template<typename Other, typename T>
1112 auto operator>=(const Other &a, const GeneratorParam<T> &b) -> decltype(a >= (T)b) {
1113  return a >= (T)b;
1114 }
1115 template<typename Other, typename T>
1116 auto operator>=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a >= b) {
1117  return (T)a >= b;
1118 }
1119 // @}
1120 
1121 /** Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
1122  * Returns type of underlying operator<=. */
1123 // @{
1124 template<typename Other, typename T>
1125 auto operator<=(const Other &a, const GeneratorParam<T> &b) -> decltype(a <= (T)b) {
1126  return a <= (T)b;
1127 }
1128 template<typename Other, typename T>
1129 auto operator<=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a <= b) {
1130  return (T)a <= b;
1131 }
1132 // @}
1133 
1134 /** Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
1135  * Returns type of underlying operator==. */
1136 // @{
1137 template<typename Other, typename T>
1138 auto operator==(const Other &a, const GeneratorParam<T> &b) -> decltype(a == (T)b) {
1139  return a == (T)b;
1140 }
1141 template<typename Other, typename T>
1142 auto operator==(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a == b) {
1143  return (T)a == b;
1144 }
1145 // @}
1146 
1147 /** Inequality comparison between between GeneratorParam<T> and any type that supports operator!= with T.
1148  * Returns type of underlying operator!=. */
1149 // @{
1150 template<typename Other, typename T>
1151 auto operator!=(const Other &a, const GeneratorParam<T> &b) -> decltype(a != (T)b) {
1152  return a != (T)b;
1153 }
1154 template<typename Other, typename T>
1155 auto operator!=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a != b) {
1156  return (T)a != b;
1157 }
1158 // @}
1159 
1160 /** Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
1161  * Returns type of underlying operator&&. */
1162 // @{
1163 template<typename Other, typename T>
1164 auto operator&&(const Other &a, const GeneratorParam<T> &b) -> decltype(a && (T)b) {
1165  return a && (T)b;
1166 }
1167 template<typename Other, typename T>
1168 auto operator&&(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a && b) {
1169  return (T)a && b;
1170 }
1171 template<typename T>
1172 auto operator&&(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a && (T)b) {
1173  return (T)a && (T)b;
1174 }
1175 // @}
1176 
1177 /** Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
1178  * Returns type of underlying operator||. */
1179 // @{
1180 template<typename Other, typename T>
1181 auto operator||(const Other &a, const GeneratorParam<T> &b) -> decltype(a || (T)b) {
1182  return a || (T)b;
1183 }
1184 template<typename Other, typename T>
1185 auto operator||(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a || b) {
1186  return (T)a || b;
1187 }
1188 template<typename T>
1189 auto operator||(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a || (T)b) {
1190  return (T)a || (T)b;
1191 }
1192 // @}
1193 
1194 /* min and max are tricky as the language support for these is in the std
1195  * namespace. In order to make this work, forwarding functions are used that
1196  * are declared in a namespace that has std::min and std::max in scope.
1197  */
1198 namespace Internal {
1199 namespace GeneratorMinMax {
1200 
1201 using std::max;
1202 using std::min;
1203 
1204 template<typename Other, typename T>
1205 auto min_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(min(a, (T)b)) {
1206  return min(a, (T)b);
1207 }
1208 template<typename Other, typename T>
1209 auto min_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(min((T)a, b)) {
1210  return min((T)a, b);
1211 }
1212 
1213 template<typename Other, typename T>
1214 auto max_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(max(a, (T)b)) {
1215  return max(a, (T)b);
1216 }
1217 template<typename Other, typename T>
1218 auto max_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(max((T)a, b)) {
1219  return max((T)a, b);
1220 }
1221 
1222 } // namespace GeneratorMinMax
1223 } // namespace Internal
1224 
1225 /** Compute minimum between GeneratorParam<T> and any type that supports min with T.
1226  * Will automatically import std::min. Returns type of underlying min call. */
1227 // @{
1228 template<typename Other, typename T>
1229 auto min(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1231 }
1232 template<typename Other, typename T>
1233 auto min(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1235 }
1236 // @}
1237 
1238 /** Compute the maximum value between GeneratorParam<T> and any type that supports max with T.
1239  * Will automatically import std::max. Returns type of underlying max call. */
1240 // @{
1241 template<typename Other, typename T>
1242 auto max(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1244 }
1245 template<typename Other, typename T>
1246 auto max(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1248 }
1249 // @}
1250 
1251 /** Not operator for GeneratorParam */
1252 template<typename T>
1253 auto operator!(const GeneratorParam<T> &a) -> decltype(!(T)a) {
1254  return !(T)a;
1255 }
1256 
1257 namespace Internal {
1258 
1259 template<typename T2>
1260 class GeneratorInput_Buffer;
1261 
1262 enum class IOKind { Scalar,
1263  Function,
1264  Buffer };
1265 
1266 /**
1267  * StubInputBuffer is the placeholder that a Stub uses when it requires
1268  * a Buffer for an input (rather than merely a Func or Expr). It is constructed
1269  * to allow only two possible sorts of input:
1270  * -- Assignment of an Input<Buffer<>>, with compatible type and dimensions,
1271  * essentially allowing us to pipe a parameter from an enclosing Generator to an internal Stub.
1272  * -- Assignment of a Buffer<>, with compatible type and dimensions,
1273  * causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
1274  */
1275 template<typename T = void>
1277  friend class StubInput;
1278  template<typename T2>
1280 
1281  Parameter parameter_;
1282 
1284  : parameter_(p) {
1285  // Create an empty 1-element buffer with the right runtime typing and dimensions,
1286  // which we'll use only to pass to can_convert_from() to verify this
1287  // Parameter is compatible with our constraints.
1288  Buffer<> other(p.type(), nullptr, std::vector<int>(p.dimensions(), 1));
1290  }
1291 
1292  template<typename T2>
1293  HALIDE_NO_USER_CODE_INLINE static Parameter parameter_from_buffer(const Buffer<T2> &b) {
1294  internal_assert(b.defined());
1296  Parameter p(b.type(), true, b.dimensions());
1297  p.set_buffer(b);
1298  return p;
1299  }
1300 
1301 public:
1302  StubInputBuffer() = default;
1303 
1304  // *not* explicit -- this ctor should only be used when you want
1305  // to pass a literal Buffer<> for a Stub Input; this Buffer<> will be
1306  // compiled into the Generator's product, rather than becoming
1307  // a runtime Parameter.
1308  template<typename T2>
1310  : parameter_(parameter_from_buffer(b)) {
1311  }
1312 };
1313 
1315 protected:
1317  std::shared_ptr<GeneratorBase> generator;
1318 
1319  void check_scheduled(const char *m) const;
1321 
1323  explicit StubOutputBufferBase(const Func &f, const std::shared_ptr<GeneratorBase> &generator);
1324 
1325 public:
1326  Realization realize(std::vector<int32_t> sizes);
1327 
1328  template<typename... Args>
1329  Realization realize(Args &&...args) {
1330  check_scheduled("realize");
1331  return f.realize(std::forward<Args>(args)..., get_target());
1332  }
1333 
1334  template<typename Dst>
1335  void realize(Dst dst) {
1336  check_scheduled("realize");
1337  f.realize(dst, get_target());
1338  }
1339 };
1340 
1341 /**
1342  * StubOutputBuffer is the placeholder that a Stub uses when it requires
1343  * a Buffer for an output (rather than merely a Func). It is constructed
1344  * to allow only two possible sorts of things:
1345  * -- Assignment to an Output<Buffer<>>, with compatible type and dimensions,
1346  * essentially allowing us to pipe a parameter from the result of a Stub to an
1347  * enclosing Generator
1348  * -- Realization into a Buffer<>; this is useful only in JIT compilation modes
1349  * (and shouldn't be usable otherwise)
1350  *
1351  * It is deliberate that StubOutputBuffer is not (easily) convertible to Func.
1352  */
1353 template<typename T = void>
1355  template<typename T2>
1357  friend class GeneratorStub;
1358  explicit StubOutputBuffer(const Func &f, const std::shared_ptr<GeneratorBase> &generator)
1360  }
1361 
1362 public:
1363  StubOutputBuffer() = default;
1364 };
1365 
1366 // This is a union-like class that allows for convenient initialization of Stub Inputs
1367 // via C++11 initializer-list syntax; it is only used in situations where the
1368 // downstream consumer will be able to explicitly check that each value is
1369 // of the expected/required kind.
1370 class StubInput {
1371  const IOKind kind_;
1372  // Exactly one of the following fields should be defined:
1373  const Parameter parameter_;
1374  const Func func_;
1375  const Expr expr_;
1376 
1377 public:
1378  // *not* explicit.
1379  template<typename T2>
1381  : kind_(IOKind::Buffer), parameter_(b.parameter_), func_(), expr_() {
1382  }
1383  StubInput(const Func &f)
1384  : kind_(IOKind::Function), parameter_(), func_(f), expr_() {
1385  }
1386  StubInput(const Expr &e)
1387  : kind_(IOKind::Scalar), parameter_(), func_(), expr_(e) {
1388  }
1389 
1390 private:
1391  friend class GeneratorInputBase;
1392 
1393  IOKind kind() const {
1394  return kind_;
1395  }
1396 
1397  Parameter parameter() const {
1398  internal_assert(kind_ == IOKind::Buffer);
1399  return parameter_;
1400  }
1401 
1402  Func func() const {
1404  return func_;
1405  }
1406 
1407  Expr expr() const {
1408  internal_assert(kind_ == IOKind::Scalar);
1409  return expr_;
1410  }
1411 };
1412 
1413 /** GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<>
1414  * instantiations; it is not part of the public API and should never be
1415  * used directly by user code.
1416  *
1417  * Every GIOBase instance can be either a single value or an array-of-values;
1418  * each of these values can be an Expr or a Func. (Note that for an
1419  * array-of-values, the types/dimensions of all values in the array must match.)
1420  *
1421  * A GIOBase can have multiple Types, in which case it represents a Tuple.
1422  * (Note that Tuples are currently only supported for GeneratorOutput, but
1423  * it is likely that GeneratorInput will be extended to support Tuple as well.)
1424  *
1425  * The array-size, type(s), and dimensions can all be left "unspecified" at
1426  * creation time, in which case they may assume values provided by a Stub.
1427  * (It is important to note that attempting to use a GIOBase with unspecified
1428  * values will assert-fail; you must ensure that all unspecified values are
1429  * filled in prior to use.)
1430  */
1431 class GIOBase {
1432 public:
1433  bool array_size_defined() const;
1434  size_t array_size() const;
1435  virtual bool is_array() const;
1436 
1437  const std::string &name() const;
1438  IOKind kind() const;
1439 
1440  bool types_defined() const;
1441  const std::vector<Type> &types() const;
1442  Type type() const;
1443 
1444  bool dims_defined() const;
1445  int dims() const;
1446 
1447  const std::vector<Func> &funcs() const;
1448  const std::vector<Expr> &exprs() const;
1449 
1450  virtual ~GIOBase() = default;
1451 
1452  void set_type(const Type &type);
1454  void set_array_size(int size);
1455 
1456 protected:
1458  const std::string &name,
1459  IOKind kind,
1460  const std::vector<Type> &types,
1461  int dims);
1462 
1463  friend class GeneratorBase;
1464  friend class GeneratorParamInfo;
1465 
1466  mutable int array_size_; // always 1 if is_array() == false.
1467  // -1 if is_array() == true but unspecified.
1468 
1469  const std::string name_;
1470  const IOKind kind_;
1471  mutable std::vector<Type> types_; // empty if type is unspecified
1472  mutable int dims_; // -1 if dim is unspecified
1473 
1474  // Exactly one of these will have nonzero length
1475  std::vector<Func> funcs_;
1476  std::vector<Expr> exprs_;
1477 
1478  // Generator which owns this Input or Output. Note that this will be null
1479  // initially; the GeneratorBase itself will set this field when it initially
1480  // builds its info about params. However, since it isn't
1481  // appropriate for Input<> or Output<> to be declared outside of a Generator,
1482  // all reasonable non-testing code should expect this to be non-null.
1484 
1485  std::string array_name(size_t i) const;
1486 
1487  virtual void verify_internals();
1488 
1489  void check_matching_array_size(size_t size) const;
1490  void check_matching_types(const std::vector<Type> &t) const;
1491  void check_matching_dims(int d) const;
1492 
1493  template<typename ElemType>
1494  const std::vector<ElemType> &get_values() const;
1495 
1496  void check_gio_access() const;
1497 
1498  virtual void check_value_writable() const = 0;
1499 
1500  virtual const char *input_or_output() const = 0;
1501 
1502 private:
1503  template<typename T>
1505 
1506 public:
1507  GIOBase(const GIOBase &) = delete;
1508  GIOBase &operator=(const GIOBase &) = delete;
1509  GIOBase(GIOBase &&) = delete;
1510  GIOBase &operator=(GIOBase &&) = delete;
1511 };
1512 
1513 template<>
1514 inline const std::vector<Expr> &GIOBase::get_values<Expr>() const {
1515  return exprs();
1516 }
1517 
1518 template<>
1519 inline const std::vector<Func> &GIOBase::get_values<Func>() const {
1520  return funcs();
1521 }
1522 
1523 class GeneratorInputBase : public GIOBase {
1524 protected:
1526  const std::string &name,
1527  IOKind kind,
1528  const std::vector<Type> &t,
1529  int d);
1530 
1531  GeneratorInputBase(const std::string &name, IOKind kind, const std::vector<Type> &t, int d);
1532 
1533  friend class GeneratorBase;
1534  friend class GeneratorParamInfo;
1535 
1536  std::vector<Parameter> parameters_;
1537 
1539 
1541  void set_inputs(const std::vector<StubInput> &inputs);
1542 
1543  virtual void set_def_min_max();
1544 
1545  void verify_internals() override;
1546 
1547  friend class StubEmitter;
1548 
1549  virtual std::string get_c_type() const = 0;
1550 
1551  void check_value_writable() const override;
1552 
1553  const char *input_or_output() const override {
1554  return "Input";
1555  }
1556 
1557  void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent);
1558  void set_estimates_impl(const Region &estimates);
1559 
1560 public:
1562 };
1563 
1564 template<typename T, typename ValueType>
1566 protected:
1567  using TBase = typename std::remove_all_extents<T>::type;
1568 
1569  bool is_array() const override {
1570  return std::is_array<T>::value;
1571  }
1572 
1573  template<typename T2 = T, typename std::enable_if<
1574  // Only allow T2 not-an-array
1575  !std::is_array<T2>::value>::type * = nullptr>
1576  GeneratorInputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
1577  : GeneratorInputBase(name, kind, t, d) {
1578  }
1579 
1580  template<typename T2 = T, typename std::enable_if<
1581  // Only allow T2[kSomeConst]
1582  std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
1583  GeneratorInputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
1584  : GeneratorInputBase(std::extent<T2, 0>::value, name, kind, t, d) {
1585  }
1586 
1587  template<typename T2 = T, typename std::enable_if<
1588  // Only allow T2[]
1589  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
1590  GeneratorInputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
1591  : GeneratorInputBase(-1, name, kind, t, d) {
1592  }
1593 
1594 public:
1595  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1596  size_t size() const {
1597  this->check_gio_access();
1598  return get_values<ValueType>().size();
1599  }
1600 
1601  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1602  const ValueType &operator[](size_t i) const {
1603  this->check_gio_access();
1604  return get_values<ValueType>()[i];
1605  }
1606 
1607  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1608  const ValueType &at(size_t i) const {
1609  this->check_gio_access();
1610  return get_values<ValueType>().at(i);
1611  }
1612 
1613  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1614  typename std::vector<ValueType>::const_iterator begin() const {
1615  this->check_gio_access();
1616  return get_values<ValueType>().begin();
1617  }
1618 
1619  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1620  typename std::vector<ValueType>::const_iterator end() const {
1621  this->check_gio_access();
1622  return get_values<ValueType>().end();
1623  }
1624 };
1625 
1626 // When forwarding methods to ImageParam, Func, etc., we must take
1627 // care with the return types: many of the methods return a reference-to-self
1628 // (e.g., ImageParam&); since we create temporaries for most of these forwards,
1629 // returning a ref will crater because it refers to a now-defunct section of the
1630 // stack. Happily, simply removing the reference is solves this, since all of the
1631 // types in question satisfy the property of copies referring to the same underlying
1632 // structure (returning references is just an optimization). Since this is verbose
1633 // and used in several places, we'll use a helper macro:
1634 #define HALIDE_FORWARD_METHOD(Class, Method) \
1635  template<typename... Args> \
1636  inline auto Method(Args &&...args)->typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
1637  return this->template as<Class>().Method(std::forward<Args>(args)...); \
1638  }
1639 
1640 #define HALIDE_FORWARD_METHOD_CONST(Class, Method) \
1641  template<typename... Args> \
1642  inline auto Method(Args &&...args) const-> \
1643  typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
1644  this->check_gio_access(); \
1645  return this->template as<Class>().Method(std::forward<Args>(args)...); \
1646  }
1647 
1648 template<typename T>
1650 private:
1652 
1653 protected:
1654  using TBase = typename Super::TBase;
1655 
1656  friend class ::Halide::Func;
1657  friend class ::Halide::Stage;
1658 
1659  std::string get_c_type() const override {
1660  if (TBase::has_static_halide_type) {
1661  return "Halide::Internal::StubInputBuffer<" +
1662  halide_type_to_c_type(TBase::static_halide_type()) +
1663  ">";
1664  } else {
1665  return "Halide::Internal::StubInputBuffer<>";
1666  }
1667  }
1668 
1669  template<typename T2>
1670  inline T2 as() const {
1671  return (T2) * this;
1672  }
1673 
1674 public:
1675  GeneratorInput_Buffer(const std::string &name)
1676  : Super(name, IOKind::Buffer,
1677  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1678  -1) {
1679  }
1680 
1681  GeneratorInput_Buffer(const std::string &name, const Type &t, int d = -1)
1682  : Super(name, IOKind::Buffer, {t}, d) {
1683  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1684  }
1685 
1686  GeneratorInput_Buffer(const std::string &name, int d)
1687  : Super(name, IOKind::Buffer, TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{}, d) {
1688  }
1689 
1690  template<typename... Args>
1691  Expr operator()(Args &&...args) const {
1692  this->check_gio_access();
1693  return Func(*this)(std::forward<Args>(args)...);
1694  }
1695 
1696  Expr operator()(std::vector<Expr> args) const {
1697  this->check_gio_access();
1698  return Func(*this)(std::move(args));
1699  }
1700 
1701  template<typename T2>
1702  operator StubInputBuffer<T2>() const {
1703  user_assert(!this->is_array()) << "Cannot assign an array type to a non-array type for Input " << this->name();
1704  return StubInputBuffer<T2>(this->parameters_.at(0));
1705  }
1706 
1707  operator Func() const {
1708  this->check_gio_access();
1709  return this->funcs().at(0);
1710  }
1711 
1712  operator ExternFuncArgument() const {
1713  this->check_gio_access();
1714  return ExternFuncArgument(this->parameters_.at(0));
1715  }
1716 
1718  this->check_gio_access();
1719  this->set_estimate_impl(var, min, extent);
1720  return *this;
1721  }
1722 
1724  this->check_gio_access();
1725  this->set_estimates_impl(estimates);
1726  return *this;
1727  }
1728 
1729  Func in() {
1730  this->check_gio_access();
1731  return Func(*this).in();
1732  }
1733 
1734  Func in(const Func &other) {
1735  this->check_gio_access();
1736  return Func(*this).in(other);
1737  }
1738 
1739  Func in(const std::vector<Func> &others) {
1740  this->check_gio_access();
1741  return Func(*this).in(others);
1742  }
1743 
1744  operator ImageParam() const {
1745  this->check_gio_access();
1746  user_assert(!this->is_array()) << "Cannot convert an Input<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
1747  return ImageParam(this->parameters_.at(0), Func(*this));
1748  }
1749 
1750  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1751  size_t size() const {
1752  this->check_gio_access();
1753  return this->parameters_.size();
1754  }
1755 
1756  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1757  ImageParam operator[](size_t i) const {
1758  this->check_gio_access();
1759  return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1760  }
1761 
1762  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1763  ImageParam at(size_t i) const {
1764  this->check_gio_access();
1765  return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1766  }
1767 
1768  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1769  typename std::vector<ImageParam>::const_iterator begin() const {
1770  user_error << "Input<Buffer<>>::begin() is not supported.";
1771  return {};
1772  }
1773 
1774  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1775  typename std::vector<ImageParam>::const_iterator end() const {
1776  user_error << "Input<Buffer<>>::end() is not supported.";
1777  return {};
1778  }
1779 
1780  /** Forward methods to the ImageParam. */
1781  // @{
1784  HALIDE_FORWARD_METHOD_CONST(ImageParam, host_alignment)
1785  HALIDE_FORWARD_METHOD(ImageParam, set_host_alignment)
1796  HALIDE_FORWARD_METHOD_CONST(ImageParam, add_trace_tag)
1797  // }@
1798 };
1799 
1800 template<typename T>
1801 class GeneratorInput_Func : public GeneratorInputImpl<T, Func> {
1802 private:
1804 
1805 protected:
1806  using TBase = typename Super::TBase;
1807 
1808  std::string get_c_type() const override {
1809  return "Func";
1810  }
1811 
1812  template<typename T2>
1813  inline T2 as() const {
1814  return (T2) * this;
1815  }
1816 
1817 public:
1818  GeneratorInput_Func(const std::string &name, const Type &t, int d)
1819  : Super(name, IOKind::Function, {t}, d) {
1820  }
1821 
1822  // unspecified type
1823  GeneratorInput_Func(const std::string &name, int d)
1824  : Super(name, IOKind::Function, {}, d) {
1825  }
1826 
1827  // unspecified dimension
1828  GeneratorInput_Func(const std::string &name, const Type &t)
1829  : Super(name, IOKind::Function, {t}, -1) {
1830  }
1831 
1832  // unspecified type & dimension
1833  GeneratorInput_Func(const std::string &name)
1834  : Super(name, IOKind::Function, {}, -1) {
1835  }
1836 
1837  GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
1838  : Super(array_size, name, IOKind::Function, {t}, d) {
1839  }
1840 
1841  // unspecified type
1842  GeneratorInput_Func(size_t array_size, const std::string &name, int d)
1843  : Super(array_size, name, IOKind::Function, {}, d) {
1844  }
1845 
1846  // unspecified dimension
1847  GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
1848  : Super(array_size, name, IOKind::Function, {t}, -1) {
1849  }
1850 
1851  // unspecified type & dimension
1852  GeneratorInput_Func(size_t array_size, const std::string &name)
1853  : Super(array_size, name, IOKind::Function, {}, -1) {
1854  }
1855 
1856  template<typename... Args>
1857  Expr operator()(Args &&...args) const {
1858  this->check_gio_access();
1859  return this->funcs().at(0)(std::forward<Args>(args)...);
1860  }
1861 
1862  Expr operator()(const std::vector<Expr> &args) const {
1863  this->check_gio_access();
1864  return this->funcs().at(0)(args);
1865  }
1866 
1867  operator Func() const {
1868  this->check_gio_access();
1869  return this->funcs().at(0);
1870  }
1871 
1872  operator ExternFuncArgument() const {
1873  this->check_gio_access();
1874  return ExternFuncArgument(this->parameters_.at(0));
1875  }
1876 
1878  this->check_gio_access();
1879  this->set_estimate_impl(var, min, extent);
1880  return *this;
1881  }
1882 
1884  this->check_gio_access();
1885  this->set_estimates_impl(estimates);
1886  return *this;
1887  }
1888 
1889  Func in() {
1890  this->check_gio_access();
1891  return Func(*this).in();
1892  }
1893 
1894  Func in(const Func &other) {
1895  this->check_gio_access();
1896  return Func(*this).in(other);
1897  }
1898 
1899  Func in(const std::vector<Func> &others) {
1900  this->check_gio_access();
1901  return Func(*this).in(others);
1902  }
1903 
1904  /** Forward const methods to the underlying Func. (Non-const methods
1905  * aren't available for Input<Func>.) */
1906  // @{
1909  HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
1910  HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
1911  HALIDE_FORWARD_METHOD_CONST(Func, output_types)
1914  HALIDE_FORWARD_METHOD_CONST(Func, update_args)
1915  HALIDE_FORWARD_METHOD_CONST(Func, update_value)
1916  HALIDE_FORWARD_METHOD_CONST(Func, update_values)
1919  // }@
1920 };
1921 
1922 template<typename T>
1924 private:
1926 
1927  static_assert(std::is_same<typename std::remove_all_extents<T>::type, Expr>::value, "GeneratorInput_DynamicScalar is only legal to use with T=Expr for now");
1928 
1929 protected:
1930  std::string get_c_type() const override {
1931  return "Expr";
1932  }
1933 
1934 public:
1935  explicit GeneratorInput_DynamicScalar(const std::string &name)
1936  : Super(name, IOKind::Scalar, {}, 0) {
1937  user_assert(!std::is_array<T>::value) << "Input<Expr[]> is not allowed";
1938  }
1939 
1940  /** You can use this Input as an expression in a halide
1941  * function definition */
1942  operator Expr() const {
1943  this->check_gio_access();
1944  return this->exprs().at(0);
1945  }
1946 
1947  /** Using an Input as the argument to an external stage treats it
1948  * as an Expr */
1949  operator ExternFuncArgument() const {
1950  this->check_gio_access();
1951  return ExternFuncArgument(this->exprs().at(0));
1952  }
1953 
1954  void set_estimate(const Expr &value) {
1955  this->check_gio_access();
1956  for (Parameter &p : this->parameters_) {
1957  p.set_estimate(value);
1958  }
1959  }
1960 };
1961 
1962 template<typename T>
1964 private:
1966 
1967 protected:
1968  using TBase = typename Super::TBase;
1969 
1970  const TBase def_{TBase()};
1972 
1973  void set_def_min_max() override {
1974  for (Parameter &p : this->parameters_) {
1975  p.set_scalar<TBase>(def_);
1977  }
1978  }
1979 
1980  std::string get_c_type() const override {
1981  return "Expr";
1982  }
1983 
1984  // Expr() doesn't accept a pointer type in its ctor; add a SFINAE adapter
1985  // so that pointer (aka handle) Inputs will get cast to uint64.
1986  template<typename TBase2 = TBase, typename std::enable_if<!std::is_pointer<TBase2>::value>::type * = nullptr>
1987  static Expr TBaseToExpr(const TBase2 &value) {
1988  return cast<TBase>(Expr(value));
1989  }
1990 
1991  template<typename TBase2 = TBase, typename std::enable_if<std::is_pointer<TBase2>::value>::type * = nullptr>
1992  static Expr TBaseToExpr(const TBase2 &value) {
1993  user_assert(value == 0) << "Zero is the only legal default value for Inputs which are pointer types.\n";
1994  return Expr();
1995  }
1996 
1997 public:
1998  explicit GeneratorInput_Scalar(const std::string &name)
1999  : Super(name, IOKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2000  }
2001 
2002  GeneratorInput_Scalar(const std::string &name, const TBase &def)
2003  : Super(name, IOKind::Scalar, {type_of<TBase>()}, 0), def_(def), def_expr_(TBaseToExpr(def)) {
2004  }
2005 
2007  const std::string &name)
2008  : Super(array_size, name, IOKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2009  }
2010 
2012  const std::string &name,
2013  const TBase &def)
2014  : Super(array_size, name, IOKind::Scalar, {type_of<TBase>()}, 0), def_(def), def_expr_(TBaseToExpr(def)) {
2015  }
2016 
2017  /** You can use this Input as an expression in a halide
2018  * function definition */
2019  operator Expr() const {
2020  this->check_gio_access();
2021  return this->exprs().at(0);
2022  }
2023 
2024  /** Using an Input as the argument to an external stage treats it
2025  * as an Expr */
2026  operator ExternFuncArgument() const {
2027  this->check_gio_access();
2028  return ExternFuncArgument(this->exprs().at(0));
2029  }
2030 
2031  template<typename T2 = T, typename std::enable_if<std::is_pointer<T2>::value>::type * = nullptr>
2032  void set_estimate(const TBase &value) {
2033  this->check_gio_access();
2034  user_assert(value == nullptr) << "nullptr is the only valid estimate for Input<PointerType>";
2035  Expr e = reinterpret(type_of<T2>(), cast<uint64_t>(0));
2036  for (Parameter &p : this->parameters_) {
2037  p.set_estimate(e);
2038  }
2039  }
2040 
2041  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value && !std::is_pointer<T2>::value>::type * = nullptr>
2042  void set_estimate(const TBase &value) {
2043  this->check_gio_access();
2044  Expr e = Expr(value);
2045  if (std::is_same<T2, bool>::value) {
2046  e = cast<bool>(e);
2047  }
2048  for (Parameter &p : this->parameters_) {
2049  p.set_estimate(e);
2050  }
2051  }
2052 
2053  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2054  void set_estimate(size_t index, const TBase &value) {
2055  this->check_gio_access();
2056  Expr e = Expr(value);
2057  if (std::is_same<T2, bool>::value) {
2058  e = cast<bool>(e);
2059  }
2060  this->parameters_.at(index).set_estimate(e);
2061  }
2062 };
2063 
2064 template<typename T>
2066 private:
2068 
2069 protected:
2070  using TBase = typename Super::TBase;
2071 
2072  const Expr min_, max_;
2073 
2074  void set_def_min_max() override {
2076  // Don't set min/max for bool
2077  if (!std::is_same<TBase, bool>::value) {
2078  for (Parameter &p : this->parameters_) {
2079  if (min_.defined()) {
2080  p.set_min_value(min_);
2081  }
2082  if (max_.defined()) {
2083  p.set_max_value(max_);
2084  }
2085  }
2086  }
2087  }
2088 
2089 public:
2090  explicit GeneratorInput_Arithmetic(const std::string &name)
2091  : Super(name), min_(Expr()), max_(Expr()) {
2092  }
2093 
2094  GeneratorInput_Arithmetic(const std::string &name,
2095  const TBase &def)
2096  : Super(name, def), min_(Expr()), max_(Expr()) {
2097  }
2098 
2100  const std::string &name)
2101  : Super(array_size, name), min_(Expr()), max_(Expr()) {
2102  }
2103 
2105  const std::string &name,
2106  const TBase &def)
2107  : Super(array_size, name, def), min_(Expr()), max_(Expr()) {
2108  }
2109 
2110  GeneratorInput_Arithmetic(const std::string &name,
2111  const TBase &def,
2112  const TBase &min,
2113  const TBase &max)
2114  : Super(name, def), min_(min), max_(max) {
2115  }
2116 
2118  const std::string &name,
2119  const TBase &def,
2120  const TBase &min,
2121  const TBase &max)
2122  : Super(array_size, name, def), min_(min), max_(max) {
2123  }
2124 };
2125 
2126 template<typename>
2127 struct type_sink { typedef void type; };
2128 
2129 template<typename T2, typename = void>
2130 struct has_static_halide_type_method : std::false_type {};
2131 
2132 template<typename T2>
2133 struct has_static_halide_type_method<T2, typename type_sink<decltype(T2::static_halide_type())>::type> : std::true_type {};
2134 
2135 template<typename T, typename TBase = typename std::remove_all_extents<T>::type>
2137  typename select_type<
2143 
2144 } // namespace Internal
2145 
2146 template<typename T>
2148 private:
2150 
2151 protected:
2152  using TBase = typename Super::TBase;
2153 
2154  // Trick to avoid ambiguous ctor between Func-with-dim and int-with-default-value;
2155  // since we can't use std::enable_if on ctors, define the argument to be one that
2156  // can only be properly resolved for TBase=Func.
2157  struct Unused;
2159  typename Internal::select_type<
2163 
2164 public:
2165  explicit GeneratorInput(const std::string &name)
2166  : Super(name) {
2167  }
2168 
2169  GeneratorInput(const std::string &name, const TBase &def)
2170  : Super(name, def) {
2171  }
2172 
2173  GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
2174  : Super(array_size, name, def) {
2175  }
2176 
2177  GeneratorInput(const std::string &name,
2178  const TBase &def, const TBase &min, const TBase &max)
2179  : Super(name, def, min, max) {
2180  }
2181 
2182  GeneratorInput(size_t array_size, const std::string &name,
2183  const TBase &def, const TBase &min, const TBase &max)
2184  : Super(array_size, name, def, min, max) {
2185  }
2186 
2187  GeneratorInput(const std::string &name, const Type &t, int d)
2188  : Super(name, t, d) {
2189  }
2190 
2191  GeneratorInput(const std::string &name, const Type &t)
2192  : Super(name, t) {
2193  }
2194 
2195  // Avoid ambiguity between Func-with-dim and int-with-default
2196  GeneratorInput(const std::string &name, IntIfNonScalar d)
2197  : Super(name, d) {
2198  }
2199 
2200  GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
2201  : Super(array_size, name, t, d) {
2202  }
2203 
2204  GeneratorInput(size_t array_size, const std::string &name, const Type &t)
2205  : Super(array_size, name, t) {
2206  }
2207 
2208  // Avoid ambiguity between Func-with-dim and int-with-default
2209  //template <typename T2 = T, typename std::enable_if<std::is_same<TBase, Func>::value>::type * = nullptr>
2210  GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
2211  : Super(array_size, name, d) {
2212  }
2213 
2214  GeneratorInput(size_t array_size, const std::string &name)
2215  : Super(array_size, name) {
2216  }
2217 };
2218 
2219 namespace Internal {
2220 
2222 protected:
2223  template<typename T2, typename std::enable_if<std::is_same<T2, Func>::value>::type * = nullptr>
2225  static_assert(std::is_same<T2, Func>::value, "Only Func allowed here");
2227  internal_assert(exprs_.empty());
2228  user_assert(funcs_.size() == 1) << "Use [] to access individual Funcs in Output<Func[]>";
2229  return funcs_[0];
2230  }
2231 
2232 public:
2233  /** Forward schedule-related methods to the underlying Func. */
2234  // @{
2235  HALIDE_FORWARD_METHOD(Func, add_trace_tag)
2236  HALIDE_FORWARD_METHOD(Func, align_bounds)
2237  HALIDE_FORWARD_METHOD(Func, align_extent)
2238  HALIDE_FORWARD_METHOD(Func, align_storage)
2240  HALIDE_FORWARD_METHOD(Func, bound)
2241  HALIDE_FORWARD_METHOD(Func, bound_extent)
2242  HALIDE_FORWARD_METHOD(Func, compute_at)
2243  HALIDE_FORWARD_METHOD(Func, compute_inline)
2244  HALIDE_FORWARD_METHOD(Func, compute_root)
2245  HALIDE_FORWARD_METHOD(Func, compute_with)
2246  HALIDE_FORWARD_METHOD(Func, copy_to_device)
2247  HALIDE_FORWARD_METHOD(Func, copy_to_host)
2248  HALIDE_FORWARD_METHOD(Func, define_extern)
2250  HALIDE_FORWARD_METHOD(Func, fold_storage)
2253  HALIDE_FORWARD_METHOD(Func, gpu_blocks)
2254  HALIDE_FORWARD_METHOD(Func, gpu_single_thread)
2255  HALIDE_FORWARD_METHOD(Func, gpu_threads)
2256  HALIDE_FORWARD_METHOD(Func, gpu_tile)
2257  HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
2258  HALIDE_FORWARD_METHOD(Func, hexagon)
2260  HALIDE_FORWARD_METHOD(Func, memoize)
2261  HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
2262  HALIDE_FORWARD_METHOD_CONST(Func, output_types)
2264  HALIDE_FORWARD_METHOD(Func, parallel)
2265  HALIDE_FORWARD_METHOD(Func, prefetch)
2267  HALIDE_FORWARD_METHOD(Func, rename)
2268  HALIDE_FORWARD_METHOD(Func, reorder)
2269  HALIDE_FORWARD_METHOD(Func, reorder_storage)
2271  HALIDE_FORWARD_METHOD(Func, serial)
2272  HALIDE_FORWARD_METHOD(Func, set_estimate)
2273  HALIDE_FORWARD_METHOD(Func, specialize)
2274  HALIDE_FORWARD_METHOD(Func, specialize_fail)
2275  HALIDE_FORWARD_METHOD(Func, split)
2276  HALIDE_FORWARD_METHOD(Func, store_at)
2277  HALIDE_FORWARD_METHOD(Func, store_root)
2279  HALIDE_FORWARD_METHOD(Func, trace_stores)
2280  HALIDE_FORWARD_METHOD(Func, unroll)
2281  HALIDE_FORWARD_METHOD(Func, update)
2282  HALIDE_FORWARD_METHOD_CONST(Func, update_args)
2283  HALIDE_FORWARD_METHOD_CONST(Func, update_value)
2284  HALIDE_FORWARD_METHOD_CONST(Func, update_values)
2287  HALIDE_FORWARD_METHOD(Func, vectorize)
2288  // }@
2289 
2290 #undef HALIDE_OUTPUT_FORWARD
2291 #undef HALIDE_OUTPUT_FORWARD_CONST
2292 
2293 protected:
2295  const std::string &name,
2296  IOKind kind,
2297  const std::vector<Type> &t,
2298  int d);
2299 
2300  GeneratorOutputBase(const std::string &name,
2301  IOKind kind,
2302  const std::vector<Type> &t,
2303  int d);
2304 
2305  friend class GeneratorBase;
2306  friend class StubEmitter;
2307 
2309  void resize(size_t size);
2310 
2311  virtual std::string get_c_type() const {
2312  return "Func";
2313  }
2314 
2315  void check_value_writable() const override;
2316 
2317  const char *input_or_output() const override {
2318  return "Output";
2319  }
2320 
2321 public:
2323 };
2324 
2325 template<typename T>
2327 protected:
2328  using TBase = typename std::remove_all_extents<T>::type;
2329  using ValueType = Func;
2330 
2331  bool is_array() const override {
2332  return std::is_array<T>::value;
2333  }
2334 
2335  template<typename T2 = T, typename std::enable_if<
2336  // Only allow T2 not-an-array
2337  !std::is_array<T2>::value>::type * = nullptr>
2338  GeneratorOutputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
2339  : GeneratorOutputBase(name, kind, t, d) {
2340  }
2341 
2342  template<typename T2 = T, typename std::enable_if<
2343  // Only allow T2[kSomeConst]
2344  std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
2345  GeneratorOutputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
2346  : GeneratorOutputBase(std::extent<T2, 0>::value, name, kind, t, d) {
2347  }
2348 
2349  template<typename T2 = T, typename std::enable_if<
2350  // Only allow T2[]
2351  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2352  GeneratorOutputImpl(const std::string &name, IOKind kind, const std::vector<Type> &t, int d)
2353  : GeneratorOutputBase(-1, name, kind, t, d) {
2354  }
2355 
2356 public:
2357  template<typename... Args, typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2358  FuncRef operator()(Args &&...args) const {
2359  this->check_gio_access();
2360  return get_values<ValueType>().at(0)(std::forward<Args>(args)...);
2361  }
2362 
2363  template<typename ExprOrVar, typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2364  FuncRef operator()(std::vector<ExprOrVar> args) const {
2365  this->check_gio_access();
2366  return get_values<ValueType>().at(0)(args);
2367  }
2368 
2369  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2370  operator Func() const {
2371  this->check_gio_access();
2372  return get_values<ValueType>().at(0);
2373  }
2374 
2375  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2376  operator Stage() const {
2377  this->check_gio_access();
2378  return get_values<ValueType>().at(0);
2379  }
2380 
2381  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2382  size_t size() const {
2383  this->check_gio_access();
2384  return get_values<ValueType>().size();
2385  }
2386 
2387  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2388  const ValueType &operator[](size_t i) const {
2389  this->check_gio_access();
2390  return get_values<ValueType>()[i];
2391  }
2392 
2393  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2394  const ValueType &at(size_t i) const {
2395  this->check_gio_access();
2396  return get_values<ValueType>().at(i);
2397  }
2398 
2399  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2400  typename std::vector<ValueType>::const_iterator begin() const {
2401  this->check_gio_access();
2402  return get_values<ValueType>().begin();
2403  }
2404 
2405  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2406  typename std::vector<ValueType>::const_iterator end() const {
2407  this->check_gio_access();
2408  return get_values<ValueType>().end();
2409  }
2410 
2411  template<typename T2 = T, typename std::enable_if<
2412  // Only allow T2[]
2413  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2414  void resize(size_t size) {
2415  this->check_gio_access();
2417  }
2418 };
2419 
2420 template<typename T>
2422 private:
2423  using Super = GeneratorOutputImpl<T>;
2424 
2425  HALIDE_NO_USER_CODE_INLINE void assign_from_func(const Func &f) {
2426  this->check_value_writable();
2427 
2428  internal_assert(f.defined());
2429 
2430  if (this->types_defined()) {
2431  const auto &my_types = this->types();
2432  user_assert(my_types.size() == f.output_types().size())
2433  << "Cannot assign Func \"" << f.name()
2434  << "\" to Output \"" << this->name() << "\"\n"
2435  << "Output " << this->name()
2436  << " is declared to have " << my_types.size() << " tuple elements"
2437  << " but Func " << f.name()
2438  << " has " << f.output_types().size() << " tuple elements.\n";
2439  for (size_t i = 0; i < my_types.size(); i++) {
2440  user_assert(my_types[i] == f.output_types().at(i))
2441  << "Cannot assign Func \"" << f.name()
2442  << "\" to Output \"" << this->name() << "\"\n"
2443  << (my_types.size() > 1 ? "In tuple element " + std::to_string(i) + ", " : "")
2444  << "Output " << this->name()
2445  << " has declared type " << my_types[i]
2446  << " but Func " << f.name()
2447  << " has type " << f.output_types().at(i) << "\n";
2448  }
2449  }
2450  if (this->dims_defined()) {
2451  user_assert(f.dimensions() == this->dims())
2452  << "Cannot assign Func \"" << f.name()
2453  << "\" to Output \"" << this->name() << "\"\n"
2454  << "Output " << this->name()
2455  << " has declared dimensionality " << this->dims()
2456  << " but Func " << f.name()
2457  << " has dimensionality " << f.dimensions() << "\n";
2458  }
2459 
2460  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2461  user_assert(!this->funcs_.at(0).defined());
2462  this->funcs_[0] = f;
2463  }
2464 
2465 protected:
2466  using TBase = typename Super::TBase;
2467 
2468  static std::vector<Type> my_types(const std::vector<Type> &t) {
2469  if (TBase::has_static_halide_type) {
2470  user_assert(t.empty()) << "Cannot pass a Type argument for an Output<Buffer> with a non-void static type\n";
2471  return std::vector<Type>{TBase::static_halide_type()};
2472  }
2473  return t;
2474  }
2475 
2476  GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t = {}, int d = -1)
2477  : Super(name, IOKind::Buffer, my_types(t), d) {
2478  }
2479 
2480  GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t = {}, int d = -1)
2481  : Super(array_size, name, IOKind::Buffer, my_types(t), d) {
2482  }
2483 
2484  HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override {
2485  if (TBase::has_static_halide_type) {
2486  return "Halide::Internal::StubOutputBuffer<" +
2487  halide_type_to_c_type(TBase::static_halide_type()) +
2488  ">";
2489  } else {
2490  return "Halide::Internal::StubOutputBuffer<>";
2491  }
2492  }
2493 
2494  template<typename T2, typename std::enable_if<!std::is_same<T2, Func>::value>::type * = nullptr>
2496  return (T2) * this;
2497  }
2498 
2499 public:
2500  // Allow assignment from a Buffer<> to an Output<Buffer<>>;
2501  // this allows us to use a statically-compiled buffer inside a Generator
2502  // to assign to an output.
2503  // TODO: This used to take the buffer as a const ref. This no longer works as
2504  // using it in a Pipeline might change the dev field so it is currently
2505  // not considered const. We should consider how this really ought to work.
2506  template<typename T2>
2508  this->check_gio_access();
2509  this->check_value_writable();
2510 
2511  user_assert(T::can_convert_from(buffer))
2512  << "Cannot assign to the Output \"" << this->name()
2513  << "\": the expression is not convertible to the same Buffer type and/or dimensions.\n";
2514 
2515  if (this->types_defined()) {
2516  user_assert(Type(buffer.type()) == this->type())
2517  << "Output " << this->name() << " should have type=" << this->type() << " but saw type=" << Type(buffer.type()) << "\n";
2518  }
2519  if (this->dims_defined()) {
2520  user_assert(buffer.dimensions() == this->dims())
2521  << "Output " << this->name() << " should have dim=" << this->dims() << " but saw dim=" << buffer.dimensions() << "\n";
2522  }
2523 
2524  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2525  user_assert(!this->funcs_.at(0).defined());
2526  this->funcs_.at(0)(_) = buffer(_);
2527 
2528  return *this;
2529  }
2530 
2531  // Allow assignment from a StubOutputBuffer to an Output<Buffer>;
2532  // this allows us to pipeline the results of a Stub to the results
2533  // of the enclosing Generator.
2534  template<typename T2>
2536  this->check_gio_access();
2537  assign_from_func(stub_output_buffer.f);
2538  return *this;
2539  }
2540 
2541  // Allow assignment from a Func to an Output<Buffer>;
2542  // this allows us to use helper functions that return a plain Func
2543  // to simply set the output(s) without needing a wrapper Func.
2545  this->check_gio_access();
2546  assign_from_func(f);
2547  return *this;
2548  }
2549 
2550  operator OutputImageParam() const {
2551  this->check_gio_access();
2552  user_assert(!this->is_array()) << "Cannot convert an Output<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
2553  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2554  return this->funcs_.at(0).output_buffer();
2555  }
2556 
2557  // 'perfect forwarding' won't work with initializer lists,
2558  // so hand-roll our own forwarding method for set_estimates,
2559  // rather than using HALIDE_FORWARD_METHOD.
2561  this->as<OutputImageParam>().set_estimates(estimates);
2562  return *this;
2563  }
2564 
2565  /** Forward methods to the OutputImageParam. */
2566  // @{
2570  HALIDE_FORWARD_METHOD(OutputImageParam, set_host_alignment)
2580  // }@
2581 };
2582 
2583 template<typename T>
2585 private:
2586  using Super = GeneratorOutputImpl<T>;
2587 
2588  HALIDE_NO_USER_CODE_INLINE Func &get_assignable_func_ref(size_t i) {
2589  internal_assert(this->exprs_.empty() && this->funcs_.size() > i);
2590  return this->funcs_.at(i);
2591  }
2592 
2593 protected:
2594  using TBase = typename Super::TBase;
2595 
2596  GeneratorOutput_Func(const std::string &name)
2597  : Super(name, IOKind::Function, std::vector<Type>{}, -1) {
2598  }
2599 
2600  GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t, int d = -1)
2601  : Super(name, IOKind::Function, t, d) {
2602  }
2603 
2604  GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2605  : Super(array_size, name, IOKind::Function, t, d) {
2606  }
2607 
2608 public:
2609  // Allow Output<Func> = Func
2610  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2612  this->check_gio_access();
2613  this->check_value_writable();
2614 
2615  // Don't bother verifying the Func type, dimensions, etc., here:
2616  // That's done later, when we produce the pipeline.
2617  get_assignable_func_ref(0) = f;
2618  return *this;
2619  }
2620 
2621  // Allow Output<Func[]> = Func
2622  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2623  Func &operator[](size_t i) {
2624  this->check_gio_access();
2625  this->check_value_writable();
2626  return get_assignable_func_ref(i);
2627  }
2628 
2629  // Allow Func = Output<Func[]>
2630  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2631  const Func &operator[](size_t i) const {
2632  this->check_gio_access();
2633  return Super::operator[](i);
2634  }
2635 
2636  GeneratorOutput_Func<T> &set_estimate(const Var &var, const Expr &min, const Expr &extent) {
2637  this->check_gio_access();
2638  internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2639  for (Func &f : this->funcs_) {
2640  f.set_estimate(var, min, extent);
2641  }
2642  return *this;
2643  }
2644 
2646  this->check_gio_access();
2647  internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2648  for (Func &f : this->funcs_) {
2649  f.set_estimates(estimates);
2650  }
2651  return *this;
2652  }
2653 };
2654 
2655 template<typename T>
2657 private:
2658  using Super = GeneratorOutputImpl<T>;
2659 
2660 protected:
2661  using TBase = typename Super::TBase;
2662 
2663  explicit GeneratorOutput_Arithmetic(const std::string &name)
2664  : Super(name, IOKind::Function, {type_of<TBase>()}, 0) {
2665  }
2666 
2667  GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
2668  : Super(array_size, name, IOKind::Function, {type_of<TBase>()}, 0) {
2669  }
2670 };
2671 
2672 template<typename T, typename TBase = typename std::remove_all_extents<T>::type>
2674  typename select_type<
2678 
2679 } // namespace Internal
2680 
2681 template<typename T>
2683 private:
2685 
2686 protected:
2687  using TBase = typename Super::TBase;
2688 
2689 public:
2690  explicit GeneratorOutput(const std::string &name)
2691  : Super(name) {
2692  }
2693 
2694  explicit GeneratorOutput(const char *name)
2695  : GeneratorOutput(std::string(name)) {
2696  }
2697 
2698  GeneratorOutput(size_t array_size, const std::string &name)
2699  : Super(array_size, name) {
2700  }
2701 
2702  GeneratorOutput(const std::string &name, int d)
2703  : Super(name, {}, d) {
2704  }
2705 
2706  GeneratorOutput(const std::string &name, const Type &t, int d)
2707  : Super(name, {t}, d) {
2708  }
2709 
2710  GeneratorOutput(const std::string &name, const std::vector<Type> &t, int d)
2711  : Super(name, t, d) {
2712  }
2713 
2714  GeneratorOutput(size_t array_size, const std::string &name, int d)
2715  : Super(array_size, name, {}, d) {
2716  }
2717 
2718  GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
2719  : Super(array_size, name, {t}, d) {
2720  }
2721 
2722  GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2723  : Super(array_size, name, t, d) {
2724  }
2725 
2726  // TODO: This used to take the buffer as a const ref. This no longer works as
2727  // using it in a Pipeline might change the dev field so it is currently
2728  // not considered const. We should consider how this really ought to work.
2729  template<typename T2>
2731  Super::operator=(buffer);
2732  return *this;
2733  }
2734 
2735  template<typename T2>
2737  Super::operator=(stub_output_buffer);
2738  return *this;
2739  }
2740 
2742  Super::operator=(f);
2743  return *this;
2744  }
2745 };
2746 
2747 namespace Internal {
2748 
2749 template<typename T>
2750 T parse_scalar(const std::string &value) {
2751  std::istringstream iss(value);
2752  T t;
2753  iss >> t;
2754  user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << value;
2755  return t;
2756 }
2757 
2758 std::vector<Type> parse_halide_type_list(const std::string &types);
2759 
2761  Dim,
2762  ArraySize };
2763 
2764 // This is a type of GeneratorParam used internally to create 'synthetic' params
2765 // (e.g. image.type, image.dim); it is not possible for user code to instantiate it.
2766 template<typename T>
2768 public:
2769  void set_from_string(const std::string &new_value_string) override {
2770  // If error_msg is not empty, this is unsettable:
2771  // display error_msg as a user error.
2772  if (!error_msg.empty()) {
2773  user_error << error_msg;
2774  }
2775  set_from_string_impl<T>(new_value_string);
2776  }
2777 
2778  std::string get_default_value() const override {
2780  return std::string();
2781  }
2782 
2783  std::string call_to_string(const std::string &v) const override {
2785  return std::string();
2786  }
2787 
2788  std::string get_c_type() const override {
2790  return std::string();
2791  }
2792 
2793  bool is_synthetic_param() const override {
2794  return true;
2795  }
2796 
2797 private:
2798  friend class GeneratorParamInfo;
2799 
2800  static std::unique_ptr<Internal::GeneratorParamBase> make(
2801  GeneratorBase *generator,
2802  const std::string &generator_name,
2803  const std::string &gpname,
2804  GIOBase &gio,
2805  SyntheticParamType which,
2806  bool defined) {
2807  std::string error_msg = defined ? "Cannot set the GeneratorParam " + gpname + " for " + generator_name + " because the value is explicitly specified in the C++ source." : "";
2808  return std::unique_ptr<GeneratorParam_Synthetic<T>>(
2809  new GeneratorParam_Synthetic<T>(gpname, gio, which, error_msg));
2810  }
2811 
2812  GeneratorParam_Synthetic(const std::string &name, GIOBase &gio, SyntheticParamType which, const std::string &error_msg = "")
2813  : GeneratorParamImpl<T>(name, T()), gio(gio), which(which), error_msg(error_msg) {
2814  }
2815 
2816  template<typename T2 = T, typename std::enable_if<std::is_same<T2, ::Halide::Type>::value>::type * = nullptr>
2817  void set_from_string_impl(const std::string &new_value_string) {
2819  gio.types_ = parse_halide_type_list(new_value_string);
2820  }
2821 
2822  template<typename T2 = T, typename std::enable_if<std::is_integral<T2>::value>::type * = nullptr>
2823  void set_from_string_impl(const std::string &new_value_string) {
2824  if (which == SyntheticParamType::Dim) {
2825  gio.dims_ = parse_scalar<T2>(new_value_string);
2826  } else if (which == SyntheticParamType::ArraySize) {
2827  gio.array_size_ = parse_scalar<T2>(new_value_string);
2828  } else {
2830  }
2831  }
2832 
2833  GIOBase &gio;
2834  const SyntheticParamType which;
2835  const std::string error_msg;
2836 };
2837 
2838 class GeneratorStub;
2839 
2840 } // namespace Internal
2841 
2842 /** GeneratorContext is a base class that is used when using Generators (or Stubs) directly;
2843  * it is used to allow the outer context (typically, either a Generator or "top-level" code)
2844  * to specify certain information to the inner context to ensure that inner and outer
2845  * Generators are compiled in a compatible way.
2846  *
2847  * If you are using this at "top level" (e.g. with the JIT), you can construct a GeneratorContext
2848  * with a Target:
2849  * \code
2850  * auto my_stub = MyStub(
2851  * GeneratorContext(get_target_from_environment()),
2852  * // inputs
2853  * { ... },
2854  * // generator params
2855  * { ... }
2856  * );
2857  * \endcode
2858  *
2859  * Note that all Generators inherit from GeneratorContext, so if you are using a Stub
2860  * from within a Generator, you can just pass 'this' for the GeneratorContext:
2861  * \code
2862  * struct SomeGen : Generator<SomeGen> {
2863  * void generate() {
2864  * ...
2865  * auto my_stub = MyStub(
2866  * this, // GeneratorContext
2867  * // inputs
2868  * { ... },
2869  * // generator params
2870  * { ... }
2871  * );
2872  * ...
2873  * }
2874  * };
2875  * \endcode
2876  */
2878 public:
2879  using ExternsMap = std::map<std::string, ExternalCode>;
2880 
2881  explicit GeneratorContext(const Target &t,
2882  bool auto_schedule = false,
2884  virtual ~GeneratorContext() = default;
2885 
2886  inline Target get_target() const {
2887  return target;
2888  }
2889  inline bool get_auto_schedule() const {
2890  return auto_schedule;
2891  }
2893  return machine_params;
2894  }
2895 
2896  /** Generators can register ExternalCode objects onto
2897  * themselves. The Generator infrastructure will arrange to have
2898  * this ExternalCode appended to the Module that is finally
2899  * compiled using the Generator. This allows encapsulating
2900  * functionality that depends on external libraries or handwritten
2901  * code for various targets. The name argument should match the
2902  * name of the ExternalCode block and is used to ensure the same
2903  * code block is not duplicated in the output. Halide does not do
2904  * anything other than to compare names for equality. To guarantee
2905  * uniqueness in public code, we suggest using a Java style
2906  * inverted domain name followed by organization specific
2907  * naming. E.g.:
2908  * com.yoyodyne.overthruster.0719acd19b66df2a9d8d628a8fefba911a0ab2b7
2909  *
2910  * See test/generator/external_code_generator.cpp for example use. */
2911  inline std::shared_ptr<ExternsMap> get_externs_map() const {
2912  return externs_map;
2913  }
2914 
2915  template<typename T>
2916  inline std::unique_ptr<T> create() const {
2917  return T::create(*this);
2918  }
2919 
2920  template<typename T, typename... Args>
2921  inline std::unique_ptr<T> apply(const Args &...args) const {
2922  auto t = this->create<T>();
2923  t->apply(args...);
2924  return t;
2925  }
2926 
2927 protected:
2931  std::shared_ptr<ExternsMap> externs_map;
2932  std::shared_ptr<Internal::ValueTracker> value_tracker;
2933 
2935  : GeneratorContext(Target()) {
2936  }
2937 
2938  virtual void init_from_context(const Halide::GeneratorContext &context);
2939 
2940  inline std::shared_ptr<Internal::ValueTracker> get_value_tracker() const {
2941  return value_tracker;
2942  }
2943 
2944 public:
2949 };
2950 
2952  // Names in this class are only intended for use in derived classes.
2953 protected:
2954  // Import a consistent list of Halide names that can be used in
2955  // Halide generators without qualification.
2973  using Var = Halide::Var;
2974  template<typename T>
2975  static Expr cast(Expr e) {
2976  return Halide::cast<T>(e);
2977  }
2978  static inline Expr cast(Halide::Type t, Expr e) {
2979  return Halide::cast(t, std::move(e));
2980  }
2981  template<typename T>
2983  template<typename T = void>
2985  template<typename T>
2987  static inline Type Bool(int lanes = 1) {
2988  return Halide::Bool(lanes);
2989  }
2990  static inline Type Float(int bits, int lanes = 1) {
2991  return Halide::Float(bits, lanes);
2992  }
2993  static inline Type Int(int bits, int lanes = 1) {
2994  return Halide::Int(bits, lanes);
2995  }
2996  static inline Type UInt(int bits, int lanes = 1) {
2997  return Halide::UInt(bits, lanes);
2998  }
2999 };
3000 
3001 namespace Internal {
3002 
3003 template<typename... Args>
3004 struct NoRealizations : std::false_type {};
3005 
3006 template<>
3007 struct NoRealizations<> : std::true_type {};
3008 
3009 template<typename T, typename... Args>
3010 struct NoRealizations<T, Args...> {
3011  static const bool value = !std::is_convertible<T, Realization>::value && NoRealizations<Args...>::value;
3012 };
3013 
3014 class GeneratorStub;
3015 
3016 // Note that these functions must never return null:
3017 // if they cannot return a valid Generator, they must assert-fail.
3018 using GeneratorFactory = std::function<std::unique_ptr<GeneratorBase>(const GeneratorContext &)>;
3019 
3021  std::string string_value;
3023 
3024  StringOrLoopLevel() = default;
3025  /*not-explicit*/ StringOrLoopLevel(const char *s)
3026  : string_value(s) {
3027  }
3028  /*not-explicit*/ StringOrLoopLevel(const std::string &s)
3029  : string_value(s) {
3030  }
3031  /*not-explicit*/ StringOrLoopLevel(const LoopLevel &loop_level)
3032  : loop_level(loop_level) {
3033  }
3034 };
3035 using GeneratorParamsMap = std::map<std::string, StringOrLoopLevel>;
3036 
3038  // names used across all params, inputs, and outputs.
3039  std::set<std::string> names;
3040 
3041  // Ordered-list of non-null ptrs to GeneratorParam<> fields.
3042  std::vector<Internal::GeneratorParamBase *> filter_generator_params;
3043 
3044  // Ordered-list of non-null ptrs to Input<> fields.
3045  std::vector<Internal::GeneratorInputBase *> filter_inputs;
3046 
3047  // Ordered-list of non-null ptrs to Output<> fields; empty if old-style Generator.
3048  std::vector<Internal::GeneratorOutputBase *> filter_outputs;
3049 
3050  // list of synthetic GP's that we dynamically created; this list only exists to simplify
3051  // lifetime management, and shouldn't be accessed directly outside of our ctor/dtor,
3052  // regardless of friend access.
3053  std::vector<std::unique_ptr<Internal::GeneratorParamBase>> owned_synthetic_params;
3054 
3055  // list of dynamically-added inputs and outputs, here only for lifetime management.
3056  std::vector<std::unique_ptr<Internal::GIOBase>> owned_extras;
3057 
3058 public:
3059  friend class GeneratorBase;
3060 
3061  GeneratorParamInfo(GeneratorBase *generator, size_t size);
3062 
3063  const std::vector<Internal::GeneratorParamBase *> &generator_params() const {
3064  return filter_generator_params;
3065  }
3066  const std::vector<Internal::GeneratorInputBase *> &inputs() const {
3067  return filter_inputs;
3068  }
3069  const std::vector<Internal::GeneratorOutputBase *> &outputs() const {
3070  return filter_outputs;
3071  }
3072 };
3073 
3075 public:
3076  ~GeneratorBase() override;
3077 
3079 
3080  /** Given a data type, return an estimate of the "natural" vector size
3081  * for that data type when compiling for the current target. */
3083  return get_target().natural_vector_size(t);
3084  }
3085 
3086  /** Given a data type, return an estimate of the "natural" vector size
3087  * for that data type when compiling for the current target. */
3088  template<typename data_t>
3089  int natural_vector_size() const {
3090  return get_target().natural_vector_size<data_t>();
3091  }
3092 
3093  void emit_cpp_stub(const std::string &stub_file_path);
3094 
3095  // Call build() and produce a Module for the result.
3096  // If function_name is empty, generator_name() will be used for the function.
3097  Module build_module(const std::string &function_name = "",
3099 
3100  /**
3101  * Build a module that is suitable for using for gradient descent calculation in TensorFlow or PyTorch.
3102  *
3103  * Essentially:
3104  * - A new Pipeline is synthesized from the current Generator (according to the rules below)
3105  * - The new Pipeline is autoscheduled (if autoscheduling is requested, but it would be odd not to do so)
3106  * - The Pipeline is compiled to a Module and returned
3107  *
3108  * The new Pipeline is adjoint to the original; it has:
3109  * - All the same inputs as the original, in the same order
3110  * - Followed by one grad-input for each original output
3111  * - Followed by one output for each unique pairing of original-output + original-input.
3112  * (For the common case of just one original-output, this amounts to being one output for each original-input.)
3113  */
3114  Module build_gradient_module(const std::string &function_name);
3115 
3116  /**
3117  * set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler
3118  * in many cases, as it constructs the relevant entries for the vector for you, which
3119  * is often a bit unintuitive at present. The arguments are passed in Input<>-declaration-order,
3120  * and the types must be compatible. Array inputs are passed as std::vector<> of the relevant type.
3121  *
3122  * Note: at present, scalar input types must match *exactly*, i.e., for Input<uint8_t>, you
3123  * must pass an argument that is actually uint8_t; an argument that is int-that-will-fit-in-uint8
3124  * will assert-fail at Halide compile time.
3125  */
3126  template<typename... Args>
3127  void set_inputs(const Args &...args) {
3128  // set_inputs_vector() checks this too, but checking it here allows build_inputs() to avoid out-of-range checks.
3129  GeneratorParamInfo &pi = this->param_info();
3130  user_assert(sizeof...(args) == pi.inputs().size())
3131  << "Expected exactly " << pi.inputs().size()
3132  << " inputs but got " << sizeof...(args) << "\n";
3133  set_inputs_vector(build_inputs(std::forward_as_tuple<const Args &...>(args...), make_index_sequence<sizeof...(Args)>{}));
3134  }
3135 
3136  Realization realize(std::vector<int32_t> sizes) {
3137  this->check_scheduled("realize");
3138  return get_pipeline().realize(std::move(sizes), get_target());
3139  }
3140 
3141  // Only enable if none of the args are Realization; otherwise we can incorrectly
3142  // select this method instead of the Realization-as-outparam variant
3143  template<typename... Args, typename std::enable_if<NoRealizations<Args...>::value>::type * = nullptr>
3144  Realization realize(Args &&...args) {
3145  this->check_scheduled("realize");
3146  return get_pipeline().realize(std::forward<Args>(args)..., get_target());
3147  }
3148 
3150  this->check_scheduled("realize");
3152  }
3153 
3154  // Return the Pipeline that has been built by the generate() method.
3155  // This method can only be used from a Generator that has a generate()
3156  // method (vs a build() method), and currently can only be called from
3157  // the schedule() method. (This may be relaxed in the future to allow
3158  // calling from generate() as long as all Outputs have been defined.)
3160 
3161  // Create Input<Buffer> or Input<Func> with dynamic type
3162  template<typename T,
3163  typename std::enable_if<!std::is_arithmetic<T>::value>::type * = nullptr>
3164  GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3166  auto *p = new GeneratorInput<T>(name, t, dimensions);
3167  p->generator = this;
3168  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3169  param_info_ptr->filter_inputs.push_back(p);
3170  return p;
3171  }
3172 
3173  // Create a Input<Buffer> or Input<Func> with compile-time type
3174  template<typename T,
3175  typename std::enable_if<T::has_static_halide_type>::type * = nullptr>
3176  GeneratorInput<T> *add_input(const std::string &name, int dimensions) {
3178  auto *p = new GeneratorInput<T>(name, dimensions);
3179  p->generator = this;
3180  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3181  param_info_ptr->filter_inputs.push_back(p);
3182  return p;
3183  }
3184 
3185  // Create Input<scalar>
3186  template<typename T,
3187  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3188  GeneratorInput<T> *add_input(const std::string &name) {
3190  auto *p = new GeneratorInput<T>(name);
3191  p->generator = this;
3192  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3193  param_info_ptr->filter_inputs.push_back(p);
3194  return p;
3195  }
3196 
3197  // Create Input<Expr> with dynamic type
3198  template<typename T,
3199  typename std::enable_if<std::is_same<T, Expr>::value>::type * = nullptr>
3200  GeneratorInput<T> *add_input(const std::string &name, const Type &type) {
3202  auto *p = new GeneratorInput<Expr>(name);
3203  p->generator = this;
3204  p->set_type(type);
3205  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3206  param_info_ptr->filter_inputs.push_back(p);
3207  return p;
3208  }
3209 
3210  // Create Output<Buffer> or Output<Func> with dynamic type
3211  template<typename T,
3212  typename std::enable_if<!std::is_arithmetic<T>::value>::type * = nullptr>
3213  GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3215  auto *p = new GeneratorOutput<T>(name, t, dimensions);
3216  p->generator = this;
3217  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3218  param_info_ptr->filter_outputs.push_back(p);
3219  return p;
3220  }
3221 
3222  // Create a Output<Buffer> or Output<Func> with compile-time type
3223  template<typename T,
3224  typename std::enable_if<T::has_static_halide_type>::type * = nullptr>
3225  GeneratorOutput<T> *add_output(const std::string &name, int dimensions) {
3227  auto *p = new GeneratorOutput<T>(name, dimensions);
3228  p->generator = this;
3229  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3230  param_info_ptr->filter_outputs.push_back(p);
3231  return p;
3232  }
3233 
3234  template<typename... Args>
3235  HALIDE_NO_USER_CODE_INLINE void add_requirement(Expr condition, Args &&...args) {
3236  get_pipeline().add_requirement(condition, std::forward<Args>(args)...);
3237  }
3238 
3241  }
3242 
3243 protected:
3244  GeneratorBase(size_t size, const void *introspection_helper);
3245  void set_generator_names(const std::string &registered_name, const std::string &stub_name);
3246 
3247  void init_from_context(const Halide::GeneratorContext &context) override;
3248 
3249  virtual Pipeline build_pipeline() = 0;
3250  virtual void call_configure() = 0;
3251  virtual void call_generate() = 0;
3252  virtual void call_schedule() = 0;
3253 
3254  void track_parameter_values(bool include_outputs);
3255 
3256  void pre_build();
3257  void post_build();
3264 
3265  template<typename T>
3267 
3268  template<typename T>
3270 
3271  // A Generator's creation and usage must go in a certain phase to ensure correctness;
3272  // the state machine here is advanced and checked at various points to ensure
3273  // this is the case.
3274  enum Phase {
3275  // Generator has just come into being.
3277 
3278  // Generator has had its configure() method called. (For Generators without
3279  // a configure() method, this phase will be skipped and will advance
3280  // directly to InputsSet.)
3282 
3283  // All Input<>/Param<> fields have been set. (Applicable only in JIT mode;
3284  // in AOT mode, this can be skipped, going Created->GenerateCalled directly.)
3286 
3287  // Generator has had its generate() method called. (For Generators with
3288  // a build() method instead of generate(), this phase will be skipped
3289  // and will advance directly to ScheduleCalled.)
3291 
3292  // Generator has had its schedule() method (if any) called.
3294  } phase{Created};
3295 
3296  void check_exact_phase(Phase expected_phase) const;
3297  void check_min_phase(Phase expected_phase) const;
3298  void advance_phase(Phase new_phase);
3299 
3301 
3302 private:
3304  friend class GeneratorParamBase;
3305  friend class GIOBase;
3306  friend class GeneratorInputBase;
3307  friend class GeneratorOutputBase;
3308  friend class GeneratorParamInfo;
3309  friend class GeneratorStub;
3310  friend class StubOutputBufferBase;
3311 
3312  const size_t size;
3313 
3314  // Lazily-allocated-and-inited struct with info about our various Params.
3315  // Do not access directly: use the param_info() getter.
3316  std::unique_ptr<GeneratorParamInfo> param_info_ptr;
3317 
3318  mutable std::shared_ptr<ExternsMap> externs_map;
3319 
3320  bool inputs_set{false};
3321  std::string generator_registered_name, generator_stub_name;
3322  Pipeline pipeline;
3323 
3324  // Return our GeneratorParamInfo.
3325  GeneratorParamInfo &param_info();
3326 
3327  Internal::GeneratorOutputBase *find_output_by_name(const std::string &name);
3328 
3329  void check_scheduled(const char *m) const;
3330 
3331  void build_params(bool force = false);
3332 
3333  // Provide private, unimplemented, wrong-result-type methods here
3334  // so that Generators don't attempt to call the global methods
3335  // of the same name by accident: use the get_target() method instead.
3336  void get_host_target();
3339 
3340  // Return the output with the given name.
3341  // If the output is singular (a non-array), return a vector of size 1.
3342  // If no such name exists (or is non-array), assert.
3343  // This method never returns undefined Funcs.
3344  std::vector<Func> get_outputs(const std::string &n);
3345 
3346  void set_inputs_vector(const std::vector<std::vector<StubInput>> &inputs);
3347 
3348  static void check_input_is_singular(Internal::GeneratorInputBase *in);
3349  static void check_input_is_array(Internal::GeneratorInputBase *in);
3350  static void check_input_kind(Internal::GeneratorInputBase *in, Internal::IOKind kind);
3351 
3352  // Allow Buffer<> if:
3353  // -- we are assigning it to an Input<Buffer<>> (with compatible type and dimensions),
3354  // causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
3355  // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Buffer<>.
3356  template<typename T>
3357  std::vector<StubInput> build_input(size_t i, const Buffer<T> &arg) {
3358  auto *in = param_info().inputs().at(i);
3359  check_input_is_singular(in);
3360  const auto k = in->kind();
3361  if (k == Internal::IOKind::Buffer) {
3362  Halide::Buffer<> b = arg;
3363  StubInputBuffer<> sib(b);
3364  StubInput si(sib);
3365  return {si};
3366  } else if (k == Internal::IOKind::Function) {
3367  Halide::Func f(arg.name() + "_im");
3368  f(Halide::_) = arg(Halide::_);
3369  StubInput si(f);
3370  return {si};
3371  } else {
3372  check_input_kind(in, Internal::IOKind::Buffer); // just to trigger assertion
3373  return {};
3374  }
3375  }
3376 
3377  // Allow Input<Buffer<>> if:
3378  // -- we are assigning it to another Input<Buffer<>> (with compatible type and dimensions),
3379  // allowing us to simply pipe a parameter from an enclosing Generator to the Invoker.
3380  // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Input<Buffer<>>.
3381  template<typename T>
3382  std::vector<StubInput> build_input(size_t i, const GeneratorInput<Buffer<T>> &arg) {
3383  auto *in = param_info().inputs().at(i);
3384  check_input_is_singular(in);
3385  const auto k = in->kind();
3386  if (k == Internal::IOKind::Buffer) {
3387  StubInputBuffer<> sib = arg;
3388  StubInput si(sib);
3389  return {si};
3390  } else if (k == Internal::IOKind::Function) {
3391  Halide::Func f = arg.funcs().at(0);
3392  StubInput si(f);
3393  return {si};
3394  } else {
3395  check_input_kind(in, Internal::IOKind::Buffer); // just to trigger assertion
3396  return {};
3397  }
3398  }
3399 
3400  // Allow Func iff we are assigning it to an Input<Func> (with compatible type and dimensions).
3401  std::vector<StubInput> build_input(size_t i, const Func &arg) {
3402  auto *in = param_info().inputs().at(i);
3403  check_input_kind(in, Internal::IOKind::Function);
3404  check_input_is_singular(in);
3405  const Halide::Func &f = arg;
3406  StubInput si(f);
3407  return {si};
3408  }
3409 
3410  // Allow vector<Func> iff we are assigning it to an Input<Func[]> (with compatible type and dimensions).
3411  std::vector<StubInput> build_input(size_t i, const std::vector<Func> &arg) {
3412  auto *in = param_info().inputs().at(i);
3413  check_input_kind(in, Internal::IOKind::Function);
3414  check_input_is_array(in);
3415  // My kingdom for a list comprehension...
3416  std::vector<StubInput> siv;
3417  siv.reserve(arg.size());
3418  for (const auto &f : arg) {
3419  siv.emplace_back(f);
3420  }
3421  return siv;
3422  }
3423 
3424  // Expr must be Input<Scalar>.
3425  std::vector<StubInput> build_input(size_t i, const Expr &arg) {
3426  auto *in = param_info().inputs().at(i);
3427  check_input_kind(in, Internal::IOKind::Scalar);
3428  check_input_is_singular(in);
3429  StubInput si(arg);
3430  return {si};
3431  }
3432 
3433  // (Array form)
3434  std::vector<StubInput> build_input(size_t i, const std::vector<Expr> &arg) {
3435  auto *in = param_info().inputs().at(i);
3436  check_input_kind(in, Internal::IOKind::Scalar);
3437  check_input_is_array(in);
3438  std::vector<StubInput> siv;
3439  siv.reserve(arg.size());
3440  for (const auto &value : arg) {
3441  siv.emplace_back(value);
3442  }
3443  return siv;
3444  }
3445 
3446  // Any other type must be convertible to Expr and must be associated with an Input<Scalar>.
3447  // Use is_arithmetic since some Expr conversions are explicit.
3448  template<typename T,
3449  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3450  std::vector<StubInput> build_input(size_t i, const T &arg) {
3451  auto *in = param_info().inputs().at(i);
3452  check_input_kind(in, Internal::IOKind::Scalar);
3453  check_input_is_singular(in);
3454  // We must use an explicit Expr() ctor to preserve the type
3455  Expr e(arg);
3456  StubInput si(e);
3457  return {si};
3458  }
3459 
3460  // (Array form)
3461  template<typename T,
3462  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3463  std::vector<StubInput> build_input(size_t i, const std::vector<T> &arg) {
3464  auto *in = param_info().inputs().at(i);
3465  check_input_kind(in, Internal::IOKind::Scalar);
3466  check_input_is_array(in);
3467  std::vector<StubInput> siv;
3468  siv.reserve(arg.size());
3469  for (const auto &value : arg) {
3470  // We must use an explicit Expr() ctor to preserve the type;
3471  // otherwise, implicit conversions can downgrade (e.g.) float -> int
3472  Expr e(value);
3473  siv.emplace_back(e);
3474  }
3475  return siv;
3476  }
3477 
3478  template<typename... Args, size_t... Indices>
3479  std::vector<std::vector<StubInput>> build_inputs(const std::tuple<const Args &...> &t, index_sequence<Indices...>) {
3480  return {build_input(Indices, std::get<Indices>(t))...};
3481  }
3482 
3483 public:
3484  GeneratorBase(const GeneratorBase &) = delete;
3486  GeneratorBase(GeneratorBase &&that) = delete;
3488 };
3489 
3491 public:
3492  static void register_factory(const std::string &name, GeneratorFactory generator_factory);
3493  static void unregister_factory(const std::string &name);
3494  static std::vector<std::string> enumerate();
3495  // Note that this method will never return null:
3496  // if it cannot return a valid Generator, it should assert-fail.
3497  static std::unique_ptr<GeneratorBase> create(const std::string &name,
3498  const Halide::GeneratorContext &context);
3499 
3500 private:
3501  using GeneratorFactoryMap = std::map<const std::string, GeneratorFactory>;
3502 
3503  GeneratorFactoryMap factories;
3504  std::mutex mutex;
3505 
3506  static GeneratorRegistry &get_registry();
3507 
3508  GeneratorRegistry() = default;
3509 
3510 public:
3515 };
3516 
3517 } // namespace Internal
3518 
3519 template<class T>
3521 protected:
3523  : Internal::GeneratorBase(sizeof(T),
3524  Internal::Introspection::get_introspection_helper<T>()) {
3525  }
3526 
3527 public:
3528  static std::unique_ptr<T> create(const Halide::GeneratorContext &context) {
3529  // We must have an object of type T (not merely GeneratorBase) to call a protected method,
3530  // because CRTP is a weird beast.
3531  auto g = std::unique_ptr<T>(new T());
3532  g->init_from_context(context);
3533  return g;
3534  }
3535 
3536  // This is public but intended only for use by the HALIDE_REGISTER_GENERATOR() macro.
3537  static std::unique_ptr<T> create(const Halide::GeneratorContext &context,
3538  const std::string &registered_name,
3539  const std::string &stub_name) {
3540  auto g = create(context);
3541  g->set_generator_names(registered_name, stub_name);
3542  return g;
3543  }
3544 
3547 
3548  template<typename... Args>
3549  void apply(const Args &...args) {
3550 #ifndef _MSC_VER
3551  // VS2015 apparently has some SFINAE issues, so this can inappropriately
3552  // trigger there. (We'll still fail when generate() is called, just
3553  // with a less-helpful error message.)
3554  static_assert(has_generate_method<T>::value, "apply() is not supported for old-style Generators.");
3555 #endif
3556  call_configure();
3557  set_inputs(args...);
3558  call_generate();
3559  call_schedule();
3560  }
3561 
3562 private:
3563  // std::is_member_function_pointer will fail if there is no member of that name,
3564  // so we use a little SFINAE to detect if there are method-shaped members.
3565  template<typename>
3566  struct type_sink { typedef void type; };
3567 
3568  template<typename T2, typename = void>
3569  struct has_configure_method : std::false_type {};
3570 
3571  template<typename T2>
3572  struct has_configure_method<T2, typename type_sink<decltype(std::declval<T2>().configure())>::type> : std::true_type {};
3573 
3574  template<typename T2, typename = void>
3575  struct has_generate_method : std::false_type {};
3576 
3577  template<typename T2>
3578  struct has_generate_method<T2, typename type_sink<decltype(std::declval<T2>().generate())>::type> : std::true_type {};
3579 
3580  template<typename T2, typename = void>
3581  struct has_schedule_method : std::false_type {};
3582 
3583  template<typename T2>
3584  struct has_schedule_method<T2, typename type_sink<decltype(std::declval<T2>().schedule())>::type> : std::true_type {};
3585 
3586  template<typename T2 = T,
3587  typename std::enable_if<!has_generate_method<T2>::value>::type * = nullptr>
3588 
3589  // Implementations for build_pipeline_impl(), specialized on whether we
3590  // have build() or generate()/schedule() methods.
3591 
3592  // MSVC apparently has some weirdness with the usual sfinae tricks
3593  // for detecting method-shaped things, so we can't actually use
3594  // the helpers above outside of static_assert. Instead we make as
3595  // many overloads as we can exist, and then use C++'s preference
3596  // for treating a 0 as an int rather than a double to choose one
3597  // of them.
3598  Pipeline build_pipeline_impl(double) {
3599  static_assert(!has_configure_method<T2>::value, "The configure() method is ignored if you define a build() method; use generate() instead.");
3600  static_assert(!has_schedule_method<T2>::value, "The schedule() method is ignored if you define a build() method; use generate() instead.");
3601  pre_build();
3602  Pipeline p = ((T *)this)->build();
3603  post_build();
3604  return p;
3605  }
3606 
3607  template<typename T2 = T,
3608  typename = decltype(std::declval<T2>().generate())>
3609  Pipeline build_pipeline_impl(int) {
3610  // No: configure() must be called prior to this
3611  // (and in fact, prior to calling set_inputs).
3612  //
3613  // ((T *)this)->call_configure_impl(0, 0);
3614 
3615  ((T *)this)->call_generate_impl(0);
3616  ((T *)this)->call_schedule_impl(0, 0);
3617  return get_pipeline();
3618  }
3619 
3620  // Implementations for call_configure_impl(), specialized on whether we
3621  // have build() or configure()/generate()/schedule() methods.
3622 
3623  void call_configure_impl(double, double) {
3624  pre_configure();
3625  // Called as a side effect for build()-method Generators; quietly do nothing
3626  // (except for pre_configure(), to advance the phase).
3627  post_configure();
3628  }
3629 
3630  template<typename T2 = T,
3631  typename = decltype(std::declval<T2>().generate())>
3632  void call_configure_impl(double, int) {
3633  // Generator has a generate() method but no configure() method. This is ok. Just advance the phase.
3634  pre_configure();
3635  static_assert(!has_configure_method<T2>::value, "Did not expect a configure method here.");
3636  post_configure();
3637  }
3638 
3639  template<typename T2 = T,
3640  typename = decltype(std::declval<T2>().generate()),
3641  typename = decltype(std::declval<T2>().configure())>
3642  void call_configure_impl(int, int) {
3643  T *t = (T *)this;
3644  static_assert(std::is_void<decltype(t->configure())>::value, "configure() must return void");
3645  pre_configure();
3646  t->configure();
3647  post_configure();
3648  }
3649 
3650  // Implementations for call_generate_impl(), specialized on whether we
3651  // have build() or configure()/generate()/schedule() methods.
3652 
3653  void call_generate_impl(double) {
3654  user_error << "Unimplemented";
3655  }
3656 
3657  template<typename T2 = T,
3658  typename = decltype(std::declval<T2>().generate())>
3659  void call_generate_impl(int) {
3660  T *t = (T *)this;
3661  static_assert(std::is_void<decltype(t->generate())>::value, "generate() must return void");
3662  pre_generate();
3663  t->generate();
3664  post_generate();
3665  }
3666 
3667  // Implementations for call_schedule_impl(), specialized on whether we
3668  // have build() or configure()generate()/schedule() methods.
3669 
3670  void call_schedule_impl(double, double) {
3671  user_error << "Unimplemented";
3672  }
3673 
3674  template<typename T2 = T,
3675  typename = decltype(std::declval<T2>().generate())>
3676  void call_schedule_impl(double, int) {
3677  // Generator has a generate() method but no schedule() method. This is ok. Just advance the phase.
3678  pre_schedule();
3679  post_schedule();
3680  }
3681 
3682  template<typename T2 = T,
3683  typename = decltype(std::declval<T2>().generate()),
3684  typename = decltype(std::declval<T2>().schedule())>
3685  void call_schedule_impl(int, int) {
3686  T *t = (T *)this;
3687  static_assert(std::is_void<decltype(t->schedule())>::value, "schedule() must return void");
3688  pre_schedule();
3689  t->schedule();
3690  post_schedule();
3691  }
3692 
3693 protected:
3695  return this->build_pipeline_impl(0);
3696  }
3697 
3698  void call_configure() override {
3699  this->call_configure_impl(0, 0);
3700  }
3701 
3702  void call_generate() override {
3703  this->call_generate_impl(0);
3704  }
3705 
3706  void call_schedule() override {
3707  this->call_schedule_impl(0, 0);
3708  }
3709 
3710 private:
3713  friend class ::Halide::GeneratorContext;
3714 
3715 public:
3716  Generator(const Generator &) = delete;
3717  Generator &operator=(const Generator &) = delete;
3718  Generator(Generator &&that) = delete;
3719  Generator &operator=(Generator &&that) = delete;
3720 };
3721 
3722 namespace Internal {
3723 
3725 public:
3726  RegisterGenerator(const char *registered_name, GeneratorFactory generator_factory);
3727 };
3728 
3730 public:
3732  const GeneratorFactory &generator_factory);
3733 
3735  const GeneratorFactory &generator_factory,
3736  const GeneratorParamsMap &generator_params,
3737  const std::vector<std::vector<Internal::StubInput>> &inputs);
3738  std::vector<std::vector<Func>> generate(const GeneratorParamsMap &generator_params,
3739  const std::vector<std::vector<Internal::StubInput>> &inputs);
3740 
3741  // Output(s)
3742  std::vector<Func> get_outputs(const std::string &n) const {
3743  return generator->get_outputs(n);
3744  }
3745 
3746  template<typename T2>
3747  std::vector<T2> get_output_buffers(const std::string &n) const {
3748  auto v = generator->get_outputs(n);
3749  std::vector<T2> result;
3750  for (auto &o : v) {
3751  result.push_back(T2(o, generator));
3752  }
3753  return result;
3754  }
3755 
3756  static std::vector<StubInput> to_stub_input_vector(const Expr &e) {
3757  return {StubInput(e)};
3758  }
3759 
3760  static std::vector<StubInput> to_stub_input_vector(const Func &f) {
3761  return {StubInput(f)};
3762  }
3763 
3764  template<typename T = void>
3765  static std::vector<StubInput> to_stub_input_vector(const StubInputBuffer<T> &b) {
3766  return {StubInput(b)};
3767  }
3768 
3769  template<typename T>
3770  static std::vector<StubInput> to_stub_input_vector(const std::vector<T> &v) {
3771  std::vector<StubInput> r;
3772  std::copy(v.begin(), v.end(), std::back_inserter(r));
3773  return r;
3774  }
3775 
3776  struct Names {
3777  std::vector<std::string> generator_params, inputs, outputs;
3778  };
3779  Names get_names() const;
3780 
3781  std::shared_ptr<GeneratorBase> generator;
3782 };
3783 
3784 } // namespace Internal
3785 
3786 } // namespace Halide
3787 
3788 // Define this namespace at global scope so that anonymous namespaces won't
3789 // defeat our static_assert check; define a dummy type inside so we can
3790 // check for type aliasing injected by anonymous namespace usage
3792 struct halide_global_ns;
3793 };
3794 
3795 #define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
3796  namespace halide_register_generator { \
3797  struct halide_global_ns; \
3798  namespace GEN_REGISTRY_NAME##_ns { \
3799  std::unique_ptr<Halide::Internal::GeneratorBase> factory(const Halide::GeneratorContext &context); \
3800  std::unique_ptr<Halide::Internal::GeneratorBase> factory(const Halide::GeneratorContext &context) { \
3801  return GEN_CLASS_NAME::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
3802  } \
3803  } \
3804  static auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
3805  } \
3806  static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
3807  "HALIDE_REGISTER_GENERATOR must be used at global scope");
3808 
3809 #define _HALIDE_REGISTER_GENERATOR2(GEN_CLASS_NAME, GEN_REGISTRY_NAME) \
3810  _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, GEN_REGISTRY_NAME)
3811 
3812 #define _HALIDE_REGISTER_GENERATOR3(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
3813  _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME)
3814 
3815 // MSVC has a broken implementation of variadic macros: it expands __VA_ARGS__
3816 // as a single token in argument lists (rather than multiple tokens).
3817 // Jump through some hoops to work around this.
3818 #define __HALIDE_REGISTER_ARGCOUNT_IMPL(_1, _2, _3, COUNT, ...) \
3819  COUNT
3820 
3821 #define _HALIDE_REGISTER_ARGCOUNT_IMPL(ARGS) \
3822  __HALIDE_REGISTER_ARGCOUNT_IMPL ARGS
3823 
3824 #define _HALIDE_REGISTER_ARGCOUNT(...) \
3825  _HALIDE_REGISTER_ARGCOUNT_IMPL((__VA_ARGS__, 3, 2, 1, 0))
3826 
3827 #define ___HALIDE_REGISTER_CHOOSER(COUNT) \
3828  _HALIDE_REGISTER_GENERATOR##COUNT
3829 
3830 #define __HALIDE_REGISTER_CHOOSER(COUNT) \
3831  ___HALIDE_REGISTER_CHOOSER(COUNT)
3832 
3833 #define _HALIDE_REGISTER_CHOOSER(COUNT) \
3834  __HALIDE_REGISTER_CHOOSER(COUNT)
3835 
3836 #define _HALIDE_REGISTER_GENERATOR_PASTE(A, B) \
3837  A B
3838 
3839 #define HALIDE_REGISTER_GENERATOR(...) \
3840  _HALIDE_REGISTER_GENERATOR_PASTE(_HALIDE_REGISTER_CHOOSER(_HALIDE_REGISTER_ARGCOUNT(__VA_ARGS__)), (__VA_ARGS__))
3841 
3842 // HALIDE_REGISTER_GENERATOR_ALIAS() can be used to create an an alias-with-a-particular-set-of-param-values
3843 // for a given Generator in the build system. Normally, you wouldn't want to do this;
3844 // however, some existing Halide clients have build systems that make it challenging to
3845 // specify GeneratorParams inside the build system, and this allows a somewhat simpler
3846 // customization route for them. It's highly recommended you don't use this for new code.
3847 //
3848 // The final argument is really an initializer-list of GeneratorParams, in the form
3849 // of an initializer-list for map<string, string>:
3850 //
3851 // { { "gp-name", "gp-value"} [, { "gp2-name", "gp2-value" }] }
3852 //
3853 // It is specified as a variadic template argument to allow for the fact that the embedded commas
3854 // would otherwise confuse the preprocessor; since (in this case) all we're going to do is
3855 // pass it thru as-is, this is fine (and even MSVC's 'broken' __VA_ARGS__ should be OK here).
3856 #define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
3857  namespace halide_register_generator { \
3858  struct halide_global_ns; \
3859  namespace ORIGINAL_REGISTRY_NAME##_ns { \
3860  std::unique_ptr<Halide::Internal::GeneratorBase> factory(const Halide::GeneratorContext &context); \
3861  } \
3862  namespace GEN_REGISTRY_NAME##_ns { \
3863  std::unique_ptr<Halide::Internal::GeneratorBase> factory(const Halide::GeneratorContext &context); \
3864  std::unique_ptr<Halide::Internal::GeneratorBase> factory(const Halide::GeneratorContext &context) { \
3865  auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
3866  g->set_generator_param_values(__VA_ARGS__); \
3867  return g; \
3868  } \
3869  } \
3870  static auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
3871  } \
3872  static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
3873  "HALIDE_REGISTER_GENERATOR_ALIAS must be used at global scope");
3874 
3875 #endif // HALIDE_GENERATOR_H_
#define internal_error
Definition: Errors.h:23
#define user_error
Definition: Errors.h:7
#define internal_assert(c)
Definition: Errors.h:19
#define user_assert(c)
Definition: Errors.h:15
Defines Func - the front-end handle on a halide function, and related classes.
#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE)
Definition: Generator.h:524
#define HALIDE_FORWARD_METHOD(Class, Method)
Definition: Generator.h:1634
#define HALIDE_FORWARD_METHOD_CONST(Class, Method)
Definition: Generator.h:1640
#define HALIDE_ALWAYS_INLINE
Definition: HalideRuntime.h:38
Classes for declaring image parameters to halide pipelines.
Defines methods for introspecting in C++.
Provides a single global registry of Generators, GeneratorParams, and Params indexed by this pointer.
Defines the structure that describes a Halide target.
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:45
A Halide::Buffer is a named shared reference to a Halide::Runtime::Buffer.
Definition: Buffer.h:115
bool defined() const
Check if this Buffer refers to an existing Buffer.
Definition: Buffer.h:374
Type type() const
Definition: Buffer.h:520
const std::string & name() const
Definition: Buffer.h:360
Helper class for identifying purpose of an Expr passed to memoize.
Definition: Func.h:666
A halide function.
Definition: Func.h:681
bool defined() const
Does this function have at least a pure definition.
int dimensions() const
The dimensionality (number of arguments) of this function.
Func & set_estimates(const Region &estimates)
Set (min, extent) estimates for all dimensions in the Func at once; this is equivalent to calling set...
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target(), const ParamMap &param_map=ParamMap::empty_map())
Evaluate this function over some rectangular domain and return the resulting buffer or buffers.
const std::string & name() const
The name of this function, either given during construction, or automatically generated.
const std::vector< Type > & output_types() const
Get the types of the outputs of this Func.
Func in(const Func &f)
Creates and returns a new identity Func that wraps this Func.
Func & set_estimate(const Var &var, const Expr &min, const Expr &extent)
Statically declare the range over which the function will be evaluated in the general case.
A fragment of front-end syntax of the form f(x, y, z), where x, y, z are Vars or Exprs.
Definition: Func.h:472
GeneratorContext is a base class that is used when using Generators (or Stubs) directly; it is used t...
Definition: Generator.h:2877
GeneratorContext(GeneratorContext &&)=delete
std::map< std::string, ExternalCode > ExternsMap
Definition: Generator.h:2879
std::shared_ptr< Internal::ValueTracker > value_tracker
Definition: Generator.h:2932
std::unique_ptr< T > apply(const Args &...args) const
Definition: Generator.h:2921
GeneratorContext & operator=(const GeneratorContext &)=delete
GeneratorParam< bool > auto_schedule
Definition: Generator.h:2929
virtual void init_from_context(const Halide::GeneratorContext &context)
std::unique_ptr< T > create() const
Definition: Generator.h:2916
bool get_auto_schedule() const
Definition: Generator.h:2889
std::shared_ptr< Internal::ValueTracker > get_value_tracker() const
Definition: Generator.h:2940
std::shared_ptr< ExternsMap > get_externs_map() const
Generators can register ExternalCode objects onto themselves.
Definition: Generator.h:2911
Target get_target() const
Definition: Generator.h:2886
GeneratorParam< MachineParams > machine_params
Definition: Generator.h:2930
GeneratorContext(const Target &t, bool auto_schedule=false, const MachineParams &machine_params=MachineParams::generic())
virtual ~GeneratorContext()=default
std::shared_ptr< ExternsMap > externs_map
Definition: Generator.h:2931
GeneratorContext & operator=(GeneratorContext &&)=delete
GeneratorContext(const GeneratorContext &)=delete
GeneratorParam< Target > target
Definition: Generator.h:2928
MachineParams get_machine_params() const
Definition: Generator.h:2892
void call_generate() override
Definition: Generator.h:3702
Generator(Generator &&that)=delete
void call_schedule() override
Definition: Generator.h:3706
static std::unique_ptr< T > create(const Halide::GeneratorContext &context)
Definition: Generator.h:3528
Generator & operator=(Generator &&that)=delete
Generator & operator=(const Generator &)=delete
void apply(const Args &...args)
Definition: Generator.h:3549
static std::unique_ptr< T > create(const Halide::GeneratorContext &context, const std::string &registered_name, const std::string &stub_name)
Definition: Generator.h:3537
void call_configure() override
Definition: Generator.h:3698
Pipeline build_pipeline() override
Definition: Generator.h:3694
Generator(const Generator &)=delete
typename Internal::select_type< Internal::cond< Internal::has_static_halide_type_method< TBase >::value, int >, Internal::cond< std::is_same< TBase, Func >::value, int >, Internal::cond< true, Unused > >::type IntIfNonScalar
Definition: Generator.h:2162
GeneratorInput(size_t array_size, const std::string &name, const Type &t)
Definition: Generator.h:2204
GeneratorInput(const std::string &name, const TBase &def)
Definition: Generator.h:2169
GeneratorInput(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2177
typename Super::TBase TBase
Definition: Generator.h:2152
GeneratorInput(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2182
GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
Definition: Generator.h:2210
GeneratorInput(const std::string &name, const Type &t)
Definition: Generator.h:2191
GeneratorInput(size_t array_size, const std::string &name)
Definition: Generator.h:2214
GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:2200
GeneratorInput(const std::string &name)
Definition: Generator.h:2165
GeneratorInput(const std::string &name, const Type &t, int d)
Definition: Generator.h:2187
GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2173
GeneratorInput(const std::string &name, IntIfNonScalar d)
Definition: Generator.h:2196
typename Super::TBase TBase
Definition: Generator.h:2687
GeneratorOutput(const std::string &name)
Definition: Generator.h:2690
GeneratorOutput(const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2710
GeneratorOutput(const char *name)
Definition: Generator.h:2694
GeneratorOutput(size_t array_size, const std::string &name, int d)
Definition: Generator.h:2714
GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:2718
GeneratorOutput(const std::string &name, const Type &t, int d)
Definition: Generator.h:2706
GeneratorOutput< T > & operator=(const Internal::StubOutputBuffer< T2 > &stub_output_buffer)
Definition: Generator.h:2736
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2722
GeneratorOutput(const std::string &name, int d)
Definition: Generator.h:2702
GeneratorOutput(size_t array_size, const std::string &name)
Definition: Generator.h:2698
GeneratorOutput< T > & operator=(const Func &f)
Definition: Generator.h:2741
GeneratorOutput< T > & operator=(Buffer< T2 > &buffer)
Definition: Generator.h:2730
GeneratorParam is a templated class that can be used to modify the behavior of the Generator at code-...
Definition: Generator.h:997
GeneratorParam(const std::string &name, const std::string &value)
Definition: Generator.h:1012
GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
Definition: Generator.h:1004
GeneratorParam(const std::string &name, const T &value)
Definition: Generator.h:1000
GeneratorParam(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition: Generator.h:1008
An Image parameter to a halide pipeline.
Definition: ImageParam.h:23
A reference-counted handle to Halide's internal representation of a function.
Definition: Function.h:38
GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<> instantiations; it is not pa...
Definition: Generator.h:1431
size_t array_size() const
const std::vector< Func > & funcs() const
GIOBase & operator=(const GIOBase &)=delete
void check_matching_dims(int d) const
GIOBase(size_t array_size, const std::string &name, IOKind kind, const std::vector< Type > &types, int dims)
bool array_size_defined() const
virtual const char * input_or_output() const =0
GIOBase & operator=(GIOBase &&)=delete
std::vector< Type > types_
Definition: Generator.h:1471
const std::vector< ElemType > & get_values() const
void check_matching_types(const std::vector< Type > &t) const
const std::vector< Type > & types() const
std::string array_name(size_t i) const
virtual void check_value_writable() const =0
GIOBase(const GIOBase &)=delete
void check_matching_array_size(size_t size) const
const std::vector< Expr > & exprs() const
GIOBase(GIOBase &&)=delete
void check_gio_access() const
void set_dimensions(int dims)
void set_array_size(int size)
std::vector< Func > funcs_
Definition: Generator.h:1475
const std::string name_
Definition: Generator.h:1469
const std::string & name() const
virtual bool is_array() const
virtual void verify_internals()
virtual ~GIOBase()=default
std::vector< Expr > exprs_
Definition: Generator.h:1476
void set_type(const Type &type)
GeneratorBase * generator
Definition: Generator.h:1483
GeneratorInput< T > * add_input(const std::string &name, const Type &t, int dimensions)
Definition: Generator.h:3164
GeneratorBase(size_t size, const void *introspection_helper)
void init_from_context(const Halide::GeneratorContext &context) override
HALIDE_NO_USER_CODE_INLINE void add_requirement(Expr condition, Args &&...args)
Definition: Generator.h:3235
GeneratorInput< T > * add_input(const std::string &name)
Definition: Generator.h:3188
Realization realize(Args &&...args)
Definition: Generator.h:3144
Module build_module(const std::string &function_name="", LinkageType linkage_type=LinkageType::ExternalPlusMetadata)
GeneratorBase(const GeneratorBase &)=delete
GeneratorInput< T > * add_input(const std::string &name, const Type &type)
Definition: Generator.h:3200
int natural_vector_size() const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Definition: Generator.h:3089
GeneratorInput< T > * add_input(const std::string &name, int dimensions)
Definition: Generator.h:3176
void check_exact_phase(Phase expected_phase) const
void check_min_phase(Phase expected_phase) const
void realize(Realization r)
Definition: Generator.h:3149
enum Halide::Internal::GeneratorBase::Phase Created
void set_generator_names(const std::string &registered_name, const std::string &stub_name)
Realization realize(std::vector< int32_t > sizes)
Definition: Generator.h:3136
GeneratorOutput< T > * add_output(const std::string &name, const Type &t, int dimensions)
Definition: Generator.h:3213
Module build_gradient_module(const std::string &function_name)
Build a module that is suitable for using for gradient descent calculation in TensorFlow or PyTorch.
GeneratorBase(GeneratorBase &&that)=delete
GeneratorBase & operator=(const GeneratorBase &)=delete
GeneratorOutput< T > * add_output(const std::string &name, int dimensions)
Definition: Generator.h:3225
void emit_cpp_stub(const std::string &stub_file_path)
virtual Pipeline build_pipeline()=0
void set_inputs(const Args &...args)
set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler in many cas...
Definition: Generator.h:3127
void set_generator_param_values(const GeneratorParamsMap &params)
int natural_vector_size(Halide::Type t) const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Definition: Generator.h:3082
void track_parameter_values(bool include_outputs)
void advance_phase(Phase new_phase)
GeneratorBase & operator=(GeneratorBase &&that)=delete
GeneratorInput_Arithmetic(size_t array_size, const std::string &name)
Definition: Generator.h:2099
GeneratorInput_Arithmetic(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2110
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2104
GeneratorInput_Arithmetic(const std::string &name)
Definition: Generator.h:2090
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2117
GeneratorInput_Arithmetic(const std::string &name, const TBase &def)
Definition: Generator.h:2094
std::vector< ImageParam >::const_iterator begin() const
Definition: Generator.h:1769
std::string get_c_type() const override
Definition: Generator.h:1659
GeneratorInput_Buffer< T > & set_estimate(Var var, Expr min, Expr extent)
Definition: Generator.h:1717
GeneratorInput_Buffer(const std::string &name)
Definition: Generator.h:1675
GeneratorInput_Buffer(const std::string &name, const Type &t, int d=-1)
Definition: Generator.h:1681
Expr operator()(std::vector< Expr > args) const
Definition: Generator.h:1696
std::vector< ImageParam >::const_iterator end() const
Definition: Generator.h:1775
Expr operator()(Args &&...args) const
Definition: Generator.h:1691
Func in(const std::vector< Func > &others)
Definition: Generator.h:1739
GeneratorInput_Buffer< T > & set_estimates(const Region &estimates)
Definition: Generator.h:1723
ImageParam operator[](size_t i) const
Definition: Generator.h:1757
ImageParam at(size_t i) const
Definition: Generator.h:1763
GeneratorInput_Buffer(const std::string &name, int d)
Definition: Generator.h:1686
std::string get_c_type() const override
Definition: Generator.h:1930
GeneratorInput_DynamicScalar(const std::string &name)
Definition: Generator.h:1935
GeneratorInput_Func(size_t array_size, const std::string &name, int d)
Definition: Generator.h:1842
Expr operator()(Args &&...args) const
Definition: Generator.h:1857
Func in(const std::vector< Func > &others)
Definition: Generator.h:1899
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:1837
GeneratorInput_Func< T > & set_estimate(Var var, Expr min, Expr extent)
Definition: Generator.h:1877
GeneratorInput_Func(const std::string &name, int d)
Definition: Generator.h:1823
GeneratorInput_Func(const std::string &name, const Type &t)
Definition: Generator.h:1828
GeneratorInput_Func< T > & set_estimates(const Region &estimates)
Definition: Generator.h:1883
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
Definition: Generator.h:1847
Expr operator()(const std::vector< Expr > &args) const
Definition: Generator.h:1862
Func in(const Func &other)
Definition: Generator.h:1894
std::string get_c_type() const override
Definition: Generator.h:1808
GeneratorInput_Func(const std::string &name, const Type &t, int d)
Definition: Generator.h:1818
GeneratorInput_Func(const std::string &name)
Definition: Generator.h:1833
GeneratorInput_Func(size_t array_size, const std::string &name)
Definition: Generator.h:1852
GeneratorInput_Scalar(size_t array_size, const std::string &name)
Definition: Generator.h:2006
static Expr TBaseToExpr(const TBase2 &value)
Definition: Generator.h:1987
void set_estimate(const TBase &value)
Definition: Generator.h:2032
void set_estimate(size_t index, const TBase &value)
Definition: Generator.h:2054
GeneratorInput_Scalar(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2011
GeneratorInput_Scalar(const std::string &name)
Definition: Generator.h:1998
GeneratorInput_Scalar(const std::string &name, const TBase &def)
Definition: Generator.h:2002
std::string get_c_type() const override
Definition: Generator.h:1980
GeneratorInputBase(const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
const char * input_or_output() const override
Definition: Generator.h:1553
void set_inputs(const std::vector< StubInput > &inputs)
virtual std::string get_c_type() const =0
void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent)
std::vector< Parameter > parameters_
Definition: Generator.h:1536
GeneratorInputBase(size_t array_size, const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
void set_estimates_impl(const Region &estimates)
void check_value_writable() const override
bool is_array() const override
Definition: Generator.h:1569
std::vector< ValueType >::const_iterator end() const
Definition: Generator.h:1620
const ValueType & operator[](size_t i) const
Definition: Generator.h:1602
std::vector< ValueType >::const_iterator begin() const
Definition: Generator.h:1614
const ValueType & at(size_t i) const
Definition: Generator.h:1608
typename std::remove_all_extents< T >::type TBase
Definition: Generator.h:1567
GeneratorInputImpl(const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
Definition: Generator.h:1576
GeneratorOutput_Arithmetic(const std::string &name)
Definition: Generator.h:2663
GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
Definition: Generator.h:2667
GeneratorOutput_Buffer< T > & set_estimates(const Region &estimates)
Definition: Generator.h:2560
GeneratorOutput_Buffer< T > & operator=(const Func &f)
Definition: Generator.h:2544
HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override
Definition: Generator.h:2484
GeneratorOutput_Buffer< T > & operator=(const StubOutputBuffer< T2 > &stub_output_buffer)
Definition: Generator.h:2535
HALIDE_NO_USER_CODE_INLINE GeneratorOutput_Buffer< T > & operator=(Buffer< T2 > &buffer)
Definition: Generator.h:2507
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t={}, int d=-1)
Definition: Generator.h:2480
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition: Generator.h:2495
static std::vector< Type > my_types(const std::vector< Type > &t)
Definition: Generator.h:2468
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t={}, int d=-1)
Definition: Generator.h:2476
GeneratorOutput_Func< T > & set_estimate(const Var &var, const Expr &min, const Expr &extent)
Definition: Generator.h:2636
GeneratorOutput_Func(const std::string &name)
Definition: Generator.h:2596
GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2604
GeneratorOutput_Func< T > & set_estimates(const Region &estimates)
Definition: Generator.h:2645
GeneratorOutput_Func< T > & operator=(const Func &f)
Definition: Generator.h:2611
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t, int d=-1)
Definition: Generator.h:2600
const Func & operator[](size_t i) const
Definition: Generator.h:2631
GeneratorOutputBase(size_t array_size, const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
Forward schedule-related methods to the underlying Func.
GeneratorOutputBase(const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
virtual std::string get_c_type() const
Definition: Generator.h:2311
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition: Generator.h:2224
const char * input_or_output() const override
Definition: Generator.h:2317
void check_value_writable() const override
GeneratorOutputImpl(const std::string &name, IOKind kind, const std::vector< Type > &t, int d)
Definition: Generator.h:2338
std::vector< ValueType >::const_iterator begin() const
Definition: Generator.h:2400
const ValueType & at(size_t i) const
Definition: Generator.h:2394
const ValueType & operator[](size_t i) const
Definition: Generator.h:2388
bool is_array() const override
Definition: Generator.h:2331
std::vector< ValueType >::const_iterator end() const
Definition: Generator.h:2406
FuncRef operator()(std::vector< ExprOrVar > args) const
Definition: Generator.h:2364
typename std::remove_all_extents< T >::type TBase
Definition: Generator.h:2328
FuncRef operator()(Args &&...args) const
Definition: Generator.h:2358
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:752
std::string get_c_type() const override
Definition: Generator.h:789
GeneratorParam_Arithmetic(const std::string &name, const T &value, const T &min=std::numeric_limits< T >::lowest(), const T &max=std::numeric_limits< T >::max())
Definition: Generator.h:738
std::string get_default_value() const override
Definition: Generator.h:769
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:783
void set_impl(const T &new_value) override
Definition: Generator.h:747
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:818
std::string get_default_value() const override
Definition: Generator.h:830
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:834
GeneratorParam_Bool(const std::string &name, const T &value)
Definition: Generator.h:814
std::string get_c_type() const override
Definition: Generator.h:840
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:866
std::string get_default_value() const override
Definition: Generator.h:874
GeneratorParam_Enum(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition: Generator.h:848
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:860
std::string get_c_type() const override
Definition: Generator.h:870
std::string get_type_decls() const override
Definition: Generator.h:878
GeneratorParam_LoopLevel(const std::string &name, const LoopLevel &value)
Definition: Generator.h:663
std::string get_c_type() const override
Definition: Generator.h:726
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:721
bool is_looplevel_param() const override
Definition: Generator.h:730
void set(const LoopLevel &value) override
Definition: Generator.h:669
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:689
std::string get_default_value() const override
Definition: Generator.h:699
GeneratorParam_MachineParams(const std::string &name, const T &value)
Definition: Generator.h:638
std::string get_c_type() const override
Definition: Generator.h:656
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:650
std::string get_default_value() const override
Definition: Generator.h:646
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:642
GeneratorParam_String(const std::string &name, const std::string &value)
Definition: Generator.h:931
std::string get_c_type() const override
Definition: Generator.h:946
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:934
std::string get_default_value() const override
Definition: Generator.h:938
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:942
bool is_synthetic_param() const override
Definition: Generator.h:2793
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:2783
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:2769
std::string get_default_value() const override
Definition: Generator.h:2778
std::string get_c_type() const override
Definition: Generator.h:2788
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:616
GeneratorParam_Target(const std::string &name, const T &value)
Definition: Generator.h:612
std::string get_c_type() const override
Definition: Generator.h:630
std::string get_default_value() const override
Definition: Generator.h:620
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:624
std::string get_type_decls() const override
Definition: Generator.h:923
std::string get_c_type() const override
Definition: Generator.h:915
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:911
std::string get_default_value() const override
Definition: Generator.h:919
GeneratorParam_Type(const std::string &name, const T &value)
Definition: Generator.h:907
const std::string & name() const
Definition: Generator.h:400
virtual bool is_synthetic_param() const
Definition: Generator.h:461
GeneratorParamBase(GeneratorParamBase &&)=delete
GeneratorParamBase & operator=(const GeneratorParamBase &)=delete
virtual std::string call_to_string(const std::string &v) const =0
void fail_wrong_type(const char *type)
GeneratorParamBase & operator=(GeneratorParamBase &&)=delete
virtual std::string get_type_decls() const
Definition: Generator.h:455
GeneratorParamBase(const std::string &name)
virtual std::string get_default_value() const =0
void set(const std::string &new_value)
Definition: Generator.h:434
GeneratorParamBase(const GeneratorParamBase &)=delete
virtual std::string get_c_type() const =0
virtual bool is_looplevel_param() const
Definition: Generator.h:465
virtual void set_from_string(const std::string &value_string)=0
void set(const char *new_value)
Definition: Generator.h:437
void set(const std::string &new_value)
Definition: Generator.h:548
GeneratorParamImpl(const std::string &name, const T &value)
Definition: Generator.h:507
virtual void set_impl(const T &new_value)
Definition: Generator.h:554
const std::vector< Internal::GeneratorParamBase * > & generator_params() const
Definition: Generator.h:3063
const std::vector< Internal::GeneratorInputBase * > & inputs() const
Definition: Generator.h:3066
GeneratorParamInfo(GeneratorBase *generator, size_t size)
const std::vector< Internal::GeneratorOutputBase * > & outputs() const
Definition: Generator.h:3069
GeneratorRegistry & operator=(const GeneratorRegistry &)=delete
GeneratorRegistry(const GeneratorRegistry &)=delete
static std::unique_ptr< GeneratorBase > create(const std::string &name, const Halide::GeneratorContext &context)
GeneratorRegistry(GeneratorRegistry &&that)=delete
GeneratorRegistry & operator=(GeneratorRegistry &&that)=delete
static std::vector< std::string > enumerate()
static void register_factory(const std::string &name, GeneratorFactory generator_factory)
static void unregister_factory(const std::string &name)
static std::vector< StubInput > to_stub_input_vector(const Expr &e)
Definition: Generator.h:3756
std::vector< std::vector< Func > > generate(const GeneratorParamsMap &generator_params, const std::vector< std::vector< Internal::StubInput >> &inputs)
GeneratorStub(const GeneratorContext &context, const GeneratorFactory &generator_factory, const GeneratorParamsMap &generator_params, const std::vector< std::vector< Internal::StubInput >> &inputs)
std::vector< Func > get_outputs(const std::string &n) const
Definition: Generator.h:3742
static std::vector< StubInput > to_stub_input_vector(const Func &f)
Definition: Generator.h:3760
std::shared_ptr< GeneratorBase > generator
Definition: Generator.h:3781
GeneratorStub(const GeneratorContext &context, const GeneratorFactory &generator_factory)
static std::vector< StubInput > to_stub_input_vector(const StubInputBuffer< T > &b)
Definition: Generator.h:3765
std::vector< T2 > get_output_buffers(const std::string &n) const
Definition: Generator.h:3747
static std::vector< StubInput > to_stub_input_vector(const std::vector< T > &v)
Definition: Generator.h:3770
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:29
void set_buffer(const Buffer< void > &b)
If the parameter is a buffer parameter, set its current value.
HALIDE_NO_USER_CODE_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:91
void set_default_value(const Expr &e)
Get and set the default values for scalar parameters.
Type type() const
Get the type of this parameter.
void set_min_value(const Expr &e)
Get and set constraints for scalar parameters.
int dimensions() const
Get the dimensionality of this parameter.
void set_max_value(const Expr &e)
RegisterGenerator(const char *registered_name, GeneratorFactory generator_factory)
StubInputBuffer is the placeholder that a Stub uses when it requires a Buffer for an input (rather th...
Definition: Generator.h:1276
StubInputBuffer(const Buffer< T2 > &b)
Definition: Generator.h:1309
StubInput(const StubInputBuffer< T2 > &b)
Definition: Generator.h:1380
StubInput(const Expr &e)
Definition: Generator.h:1386
StubInput(const Func &f)
Definition: Generator.h:1383
Realization realize(Args &&...args)
Definition: Generator.h:1329
StubOutputBufferBase(const Func &f, const std::shared_ptr< GeneratorBase > &generator)
std::shared_ptr< GeneratorBase > generator
Definition: Generator.h:1317
void check_scheduled(const char *m) const
Realization realize(std::vector< int32_t > sizes)
StubOutputBuffer is the placeholder that a Stub uses when it requires a Buffer for an output (rather ...
Definition: Generator.h:1354
ValueTracker is an internal utility class that attempts to track and flag certain obvious Stub-relate...
Definition: Generator.h:316
void track_values(const std::string &name, const std::vector< Expr > &values)
ValueTracker(size_t max_unique_values=2)
Definition: Generator.h:322
A reference to a site in a Halide statement at the top of the body of a particular for loop.
Definition: Schedule.h:153
static LoopLevel root()
Construct a special LoopLevel value which represents the location outside of all for loops.
static LoopLevel inlined()
Construct a special LoopLevel value that implies that a function should be inlined away.
void set(const LoopLevel &other)
Mutate our contents to match the contents of 'other'.
bool is_root() const
bool is_inlined() const
LoopLevel & lock()
A halide module.
Definition: Module.h:135
static Type Bool(int lanes=1)
Definition: Generator.h:2987
static Expr cast(Expr e)
Definition: Generator.h:2975
static Expr cast(Halide::Type t, Expr e)
Definition: Generator.h:2978
static Type UInt(int bits, int lanes=1)
Definition: Generator.h:2996
static Type Int(int bits, int lanes=1)
Definition: Generator.h:2993
static Type Float(int bits, int lanes=1)
Definition: Generator.h:2990
Halide::Pipeline Pipeline
Definition: Generator.h:2965
A handle on the output buffer of a pipeline.
A scalar parameter to a halide pipeline.
Definition: Param.h:22
A class representing a Halide pipeline.
Definition: Pipeline.h:97
void add_requirement(const Expr &condition, std::vector< Expr > &error)
Add a top-level precondition to the generated pipeline, expressed as a boolean Expr.
void trace_pipeline()
Generate begin_pipeline and end_pipeline tracing calls for this pipeline.
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target(), const ParamMap &param_map=ParamMap::empty_map())
See Func::realize.
A multi-dimensional domain over which to iterate.
Definition: RDom.h:193
A reduction variable represents a single dimension of a reduction domain (RDom).
Definition: RDom.h:29
A Realization is a vector of references to existing Buffer objects.
Definition: Realization.h:21
A single definition of a Func.
Definition: Func.h:70
Create a small array of Exprs for defining and calling functions with multiple outputs.
Definition: Tuple.h:18
A Halide variable, to be used when defining functions.
Definition: Var.h:19
auto max_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(max(a,(T) b))
Definition: Generator.h:1214
auto min_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(min(a,(T) b))
Definition: Generator.h:1205
const void * get_introspection_helper()
Return the address of a global with type T *.
Definition: Introspection.h:50
std::string halide_type_to_enum_string(const Type &t)
Definition: Generator.h:349
typename select_type< cond< std::is_same< T, Target >::value, GeneratorParam_Target< T > >, cond< std::is_same< T, MachineParams >::value, GeneratorParam_MachineParams< T > >, cond< std::is_same< T, LoopLevel >::value, GeneratorParam_LoopLevel >, cond< std::is_same< T, std::string >::value, GeneratorParam_String< T > >, cond< std::is_same< T, Type >::value, GeneratorParam_Type< T > >, cond< std::is_same< T, bool >::value, GeneratorParam_Bool< T > >, cond< std::is_arithmetic< T >::value, GeneratorParam_Arithmetic< T > >, cond< std::is_enum< T >::value, GeneratorParam_Enum< T > >>::type GeneratorParamImplBase
Definition: Generator.h:961
Expr make_const(Type t, int64_t val)
Construct an immediate of the given type from any numeric C++ type.
std::string halide_type_to_c_source(const Type &t)
std::function< std::unique_ptr< GeneratorBase >(const GeneratorContext &)> GeneratorFactory
Definition: Generator.h:3018
std::vector< Type > parse_halide_type_list(const std::string &types)
make_integer_sequence< size_t, N > make_index_sequence
Definition: Util.h:402
HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map< std::string, T > &enum_map, const T &t)
Definition: Generator.h:331
std::map< std::string, StringOrLoopLevel > GeneratorParamsMap
Definition: Generator.h:3035
std::string halide_type_to_c_type(const Type &t)
std::vector< Expr > parameter_constraints(const Parameter &p)
std::string print_loop_nest(const std::vector< Function > &output_funcs)
Emit some simple pseudocode that shows the structure of the loop nest specified by this pipeline's sc...
typename select_type< cond< has_static_halide_type_method< TBase >::value, GeneratorOutput_Buffer< T > >, cond< std::is_same< TBase, Func >::value, GeneratorOutput_Func< T > >, cond< std::is_arithmetic< TBase >::value, GeneratorOutput_Arithmetic< T > >>::type GeneratorOutputImplBase
Definition: Generator.h:2677
typename select_type< cond< has_static_halide_type_method< TBase >::value, GeneratorInput_Buffer< T > >, cond< std::is_same< TBase, Func >::value, GeneratorInput_Func< T > >, cond< std::is_arithmetic< TBase >::value, GeneratorInput_Arithmetic< T > >, cond< std::is_scalar< TBase >::value, GeneratorInput_Scalar< T > >, cond< std::is_same< TBase, Expr >::value, GeneratorInput_DynamicScalar< T > >>::type GeneratorInputImplBase
Definition: Generator.h:2142
int generate_filter_main(int argc, char **argv, std::ostream &cerr)
generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() + compile_to_files(); ...
T parse_scalar(const std::string &value)
Definition: Generator.h:2750
const std::map< std::string, Halide::Type > & get_halide_type_enum_map()
T enum_from_string(const std::map< std::string, T > &enum_map, const std::string &s)
Definition: Generator.h:342
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
auto operator>=(const Other &a, const GeneratorParam< T > &b) -> decltype(a >=(T) b)
Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with...
Definition: Generator.h:1112
Target get_host_target()
Return the target corresponding to the host machine.
Type UInt(int bits, int lanes=1)
Constructing an unsigned integer type.
Definition: Type.h:503
Expr reinterpret(Type t, Expr e)
Reinterpret the bits of one value as another type.
Type Float(int bits, int lanes=1)
Construct a floating-point type.
Definition: Type.h:508
auto operator==(const Other &a, const GeneratorParam< T > &b) -> decltype(a==(T) b)
Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
Definition: Generator.h:1138
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition: Module.h:48
@ ExternalPlusMetadata
Visible externally. Argument metadata and an argv wrapper are also generated.
@ Internal
Not visible externally, similar to 'static' linkage in C.
auto operator<(const Other &a, const GeneratorParam< T > &b) -> decltype(a<(T) b)
Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
Definition: Generator.h:1099
auto operator*(const Other &a, const GeneratorParam< T > &b) -> decltype(a *(T) b)
Multiplication between GeneratorParam<T> and any type that supports operator* with T.
Definition: Generator.h:1047
auto operator||(const Other &a, const GeneratorParam< T > &b) -> decltype(a||(T) b)
Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
Definition: Generator.h:1181
PrefetchBoundStrategy
Different ways to handle accesses outside the original extents in a prefetch.
auto min(const GeneratorParam< T > &a, const Other &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b))
Definition: Generator.h:1233
auto operator-(const Other &a, const GeneratorParam< T > &b) -> decltype(a -(T) b)
Subtraction between GeneratorParam<T> and any type that supports operator- with T.
Definition: Generator.h:1034
Expr cast(Expr a)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:387
auto operator!(const GeneratorParam< T > &a) -> decltype(!(T) a)
Not operator for GeneratorParam.
Definition: Generator.h:1253
TailStrategy
Different ways to handle a tail case in a split when the factor does not provably divide the extent.
Definition: Schedule.h:32
Type Int(int bits, int lanes=1)
Constructing a signed integer type.
Definition: Type.h:498
auto operator+(const Other &a, const GeneratorParam< T > &b) -> decltype(a+(T) b)
Addition between GeneratorParam<T> and any type that supports operator+ with T.
Definition: Generator.h:1021
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:578
auto operator&&(const Other &a, const GeneratorParam< T > &b) -> decltype(a &&(T) b)
Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
Definition: Generator.h:1164
auto operator%(const Other &a, const GeneratorParam< T > &b) -> decltype(a %(T) b)
Modulo between GeneratorParam<T> and any type that supports operator% with T.
Definition: Generator.h:1073
NameMangling
An enum to specify calling convention for extern stages.
Definition: Function.h:24
Target get_jit_target_from_environment()
Return the target that Halide will use for jit-compilation.
auto operator<=(const Other &a, const GeneratorParam< T > &b) -> decltype(a<=(T) b)
Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
Definition: Generator.h:1125
Target get_target_from_environment()
Return the target that Halide will use.
auto operator>(const Other &a, const GeneratorParam< T > &b) -> decltype(a >(T) b)
Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
Definition: Generator.h:1086
auto operator!=(const Other &a, const GeneratorParam< T > &b) -> decltype(a !=(T) b)
Inequality comparison between between GeneratorParam<T> and any type that supports operator!...
Definition: Generator.h:1151
Type Bool(int lanes=1)
Construct a boolean type.
Definition: Type.h:518
std::vector< Range > Region
A multi-dimensional box.
Definition: Expr.h:343
auto operator/(const Other &a, const GeneratorParam< T > &b) -> decltype(a/(T) b)
Division between GeneratorParam<T> and any type that supports operator/ with T.
Definition: Generator.h:1060
Expr max(const FuncRef &a, const FuncRef &b)
Definition: Func.h:581
auto max(const GeneratorParam< T > &a, const Other &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b))
Definition: Generator.h:1246
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:346
char * dst
Definition: printer.h:32
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
signed __INT32_TYPE__ int32_t
unsigned __INT8_TYPE__ uint8_t
unsigned __INT16_TYPE__ uint16_t
unsigned __INT32_TYPE__ uint32_t
signed __INT16_TYPE__ int16_t
signed __INT8_TYPE__ int8_t
A fragment of Halide syntax.
Definition: Expr.h:256
An argument to an extern-defined Func.
static TO2 value(const FROM &from)
Definition: Generator.h:492
std::vector< std::string > inputs
Definition: Generator.h:3777
std::vector< std::string > outputs
Definition: Generator.h:3777
std::vector< std::string > generator_params
Definition: Generator.h:3777
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:161
StringOrLoopLevel(const LoopLevel &loop_level)
Definition: Generator.h:3031
StringOrLoopLevel(const std::string &s)
Definition: Generator.h:3028
static constexpr bool value
Definition: Generator.h:382
typename std::conditional< First::value, typename First::type, void >::type type
Definition: Generator.h:390
A struct representing the machine parameters to generate the auto-scheduled code for.
Definition: Pipeline.h:31
static MachineParams generic()
Default machine parameters for generic CPU architecture.
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
int natural_vector_size(const Halide::Type &t) const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Types in the halide type system.
Definition: Type.h:269