Halide  17.0.2
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 two GeneratorParams that are implicitly provided
212  * by the base class:
213  *
214  * GeneratorParam<Target> target{"target", Target()};
215  * GeneratorParam<AutoschedulerParams> autoscheduler{"autoscheduler", {}}
216  *
217  * - 'target' is the Halide::Target for which the Generator is producing code.
218  * It is read-only during the Generator's lifetime, and must not be modified;
219  * its value should always be filled in by the calling code: either the Halide
220  * build system (for ahead-of-time compilation), or ordinary C++ code
221  * (for JIT compilation).
222  * - 'autoscheduler' is a string-to-string map that is used to indicates whether
223  * and how an auto-scheduler should be run for this Generator:
224  * - if empty, the Generator should schedule its Funcs as it sees fit; no autoscheduler will be run.
225  * - if the 'name' key is set, it should be one of the known autoschedulers
226  * provided with this release of Halide, which will be used to schedule
227  * the Funcs in the Generator. In this case, the Generator should only
228  * provide estimate()s for its Funcs, and not call any other scheduling methods.
229  * - Other keys may be specified in the params, on a per-autoscheduler
230  * basis, to optimize or enhance the automatically-generated schedule.
231  * See documentation for each autoscheduler for options.
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 "AbstractGenerator.h"
275 #include "Func.h"
276 #include "ImageParam.h"
277 #include "Introspection.h"
278 #include "ObjectInstanceRegistry.h"
279 #include "Target.h"
280 
281 #if !(__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
282 #error "Halide requires C++17 or later; please upgrade your compiler."
283 #endif
284 
285 namespace Halide {
286 
287 class GeneratorContext;
288 
289 namespace Internal {
290 
291 void generator_test();
292 
293 class GeneratorBase;
294 
295 std::vector<Expr> parameter_constraints(const Parameter &p);
296 
297 template<typename T>
298 HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map<std::string, T> &enum_map, const T &t) {
299  for (const auto &key_value : enum_map) {
300  if (t == key_value.second) {
301  return key_value.first;
302  }
303  }
304  user_error << "Enumeration value not found.\n";
305  return "";
306 }
307 
308 template<typename T>
309 T enum_from_string(const std::map<std::string, T> &enum_map, const std::string &s) {
310  auto it = enum_map.find(s);
311  user_assert(it != enum_map.end()) << "Enumeration value not found: " << s << "\n";
312  return it->second;
313 }
314 
315 extern const std::map<std::string, Halide::Type> &get_halide_type_enum_map();
316 inline std::string halide_type_to_enum_string(const Type &t) {
318 }
319 
320 // Convert a Halide Type into a string representation of its C source.
321 // e.g., Int(32) -> "Halide::Int(32)"
322 std::string halide_type_to_c_source(const Type &t);
323 
324 // Convert a Halide Type into a string representation of its C Source.
325 // e.g., Int(32) -> "int32_t"
326 std::string halide_type_to_c_type(const Type &t);
327 
328 /** GeneratorFactoryProvider provides a way to customize the Generators
329  * that are visible to generate_filter_main (which otherwise would just
330  * look at the global registry of C++ Generators). */
332 public:
333  GeneratorFactoryProvider() = default;
334  virtual ~GeneratorFactoryProvider() = default;
335 
336  /** Return a list of all registered Generators that are available for use
337  * with the create() method. */
338  virtual std::vector<std::string> enumerate() const = 0;
339 
340  /** Create an instance of the Generator that is registered under the given
341  * name. If the name isn't one returned by enumerate(), return nullptr
342  * rather than assert-fail; caller must check for a valid result. */
343  virtual AbstractGeneratorPtr create(const std::string &name,
344  const Halide::GeneratorContext &context) const = 0;
345 
350 };
351 
352 /** Return a GeneratorFactoryProvider that knows about all the currently-registered C++ Generators. */
354 
355 /** generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() +
356  * compile_to_files(); it can be trivially wrapped by a "real" main() to produce a
357  * command-line utility for ahead-of-time filter compilation. */
358 int generate_filter_main(int argc, char **argv);
359 
360 /** This overload of generate_filter_main lets you provide your own provider for how to enumerate and/or create
361  * the generators based on registration name; this is useful if you want to re-use the
362  * 'main' logic but avoid the global Generator registry (e.g. for bindings in languages
363  * other than C++). */
364 int generate_filter_main(int argc, char **argv, const GeneratorFactoryProvider &generator_factory_provider);
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> {
391  using type = typename std::conditional<First::value, typename First::type, void>::type;
392 };
393 
394 class GeneratorParamInfo;
395 
397 public:
398  explicit GeneratorParamBase(const std::string &name);
399  virtual ~GeneratorParamBase();
400 
401  inline const std::string &name() const {
402  return name_;
403  }
404 
405  // overload the set() function to call the right virtual method based on type.
406  // This allows us to attempt to set a GeneratorParam via a
407  // plain C++ type, even if we don't know the specific templated
408  // subclass. Attempting to set the wrong type will assert.
409  // Notice that there is no typed setter for Enums, for obvious reasons;
410  // setting enums in an unknown type must fallback to using set_from_string.
411  //
412  // It's always a bit iffy to use macros for this, but IMHO it clarifies the situation here.
413 #define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
414  virtual void set(const TYPE &new_value) = 0;
415 
431 
432 #undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
433 
434  // Add overloads for string and char*
435  void set(const std::string &new_value) {
436  set_from_string(new_value);
437  }
438  void set(const char *new_value) {
439  set_from_string(std::string(new_value));
440  }
441 
442 protected:
443  friend class GeneratorBase;
444  friend class GeneratorParamInfo;
445  friend class StubEmitter;
446 
447  void check_value_readable() const;
448  void check_value_writable() const;
449 
450  // All GeneratorParams are settable from string.
451  virtual void set_from_string(const std::string &value_string) = 0;
452 
453  virtual std::string call_to_string(const std::string &v) const = 0;
454  virtual std::string get_c_type() const = 0;
455 
456  virtual std::string get_type_decls() const {
457  return "";
458  }
459 
460  virtual std::string get_default_value() const = 0;
461 
462  virtual bool is_synthetic_param() const {
463  return false;
464  }
465 
466  virtual bool is_looplevel_param() const {
467  return false;
468  }
469 
470  void fail_wrong_type(const char *type);
471 
472 private:
473  const std::string name_;
474 
475  // Generator which owns this GeneratorParam. Note that this will be null
476  // initially; the GeneratorBase itself will set this field when it initially
477  // builds its info about params. However, since it (generally) isn't
478  // appropriate for GeneratorParam<> to be declared outside of a Generator,
479  // all reasonable non-testing code should expect this to be non-null.
480  GeneratorBase *generator{nullptr};
481 
482 public:
483  GeneratorParamBase(const GeneratorParamBase &) = delete;
487 };
488 
489 // This is strictly some syntactic sugar to suppress certain compiler warnings.
490 template<typename FROM, typename TO>
491 struct Convert {
492  template<typename TO2 = TO, typename std::enable_if<!std::is_same<TO2, bool>::value>::type * = nullptr>
493  inline static TO2 value(const FROM &from) {
494  return static_cast<TO2>(from);
495  }
496 
497  template<typename TO2 = TO, typename std::enable_if<std::is_same<TO2, bool>::value>::type * = nullptr>
498  inline static TO2 value(const FROM &from) {
499  return from != 0;
500  }
501 };
502 
503 template<typename T>
505 public:
506  using type = T;
507 
508  GeneratorParamImpl(const std::string &name, const T &value)
510  }
511 
512  T value() const {
513  this->check_value_readable();
514  return value_;
515  }
516 
517  operator T() const {
518  return this->value();
519  }
520 
521  operator Expr() const {
522  return make_const(type_of<T>(), this->value());
523  }
524 
525 #define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
526  void set(const TYPE &new_value) override { \
527  typed_setter_impl<TYPE>(new_value, #TYPE); \
528  }
529 
542  HALIDE_GENERATOR_PARAM_TYPED_SETTER(AutoschedulerParams)
545 
546 #undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
547 
548  // Overload for std::string.
549  void set(const std::string &new_value) {
551  value_ = new_value;
552  }
553 
554 protected:
555  virtual void set_impl(const T &new_value) {
557  value_ = new_value;
558  }
559 
560  // Needs to be protected to allow GeneratorParam<LoopLevel>::set() override
562 
563 private:
564  // If FROM->T is not legal, fail
565  template<typename FROM, typename std::enable_if<
566  !std::is_convertible<FROM, T>::value>::type * = nullptr>
567  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &, const char *msg) {
568  fail_wrong_type(msg);
569  }
570 
571  // If FROM and T are identical, just assign
572  template<typename FROM, typename std::enable_if<
573  std::is_same<FROM, T>::value>::type * = nullptr>
574  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
576  value_ = value;
577  }
578 
579  // If both FROM->T and T->FROM are legal, ensure it's lossless
580  template<typename FROM, typename std::enable_if<
581  !std::is_same<FROM, T>::value &&
582  std::is_convertible<FROM, T>::value &&
583  std::is_convertible<T, FROM>::value>::type * = nullptr>
584  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
586  const T t = Convert<FROM, T>::value(value);
587  const FROM value2 = Convert<T, FROM>::value(t);
588  if (value2 != value) {
589  fail_wrong_type(msg);
590  }
591  value_ = t;
592  }
593 
594  // If FROM->T is legal but T->FROM is not, just assign
595  template<typename FROM, typename std::enable_if<
596  !std::is_same<FROM, T>::value &&
597  std::is_convertible<FROM, T>::value &&
598  !std::is_convertible<T, FROM>::value>::type * = nullptr>
599  HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
601  value_ = value;
602  }
603 };
604 
605 // Stubs for type-specific implementations of GeneratorParam, to avoid
606 // many complex enable_if<> statements that were formerly spread through the
607 // implementation. Note that not all of these need to be templated classes,
608 // (e.g. for GeneratorParam_Target, T == Target always), but are declared
609 // that way for symmetry of declaration.
610 template<typename T>
612 public:
613  GeneratorParam_Target(const std::string &name, const T &value)
614  : GeneratorParamImpl<T>(name, value) {
615  }
616 
617  void set_from_string(const std::string &new_value_string) override {
618  this->set(Target(new_value_string));
619  }
620 
621  std::string get_default_value() const override {
622  return this->value().to_string();
623  }
624 
625  std::string call_to_string(const std::string &v) const override {
626  std::ostringstream oss;
627  oss << v << ".to_string()";
628  return oss.str();
629  }
630 
631  std::string get_c_type() const override {
632  return "Target";
633  }
634 };
635 
636 class GeneratorParam_AutoSchedulerParams : public GeneratorParamImpl<AutoschedulerParams> {
637 public:
639 
640  void set_from_string(const std::string &new_value_string) override;
641  std::string get_default_value() const override;
642  std::string call_to_string(const std::string &v) const override;
643  std::string get_c_type() const override;
644 
645 private:
646  friend class GeneratorBase;
647 
648  bool try_set(const std::string &key, const std::string &value);
649 };
650 
651 class GeneratorParam_LoopLevel : public GeneratorParamImpl<LoopLevel> {
652 public:
653  GeneratorParam_LoopLevel(const std::string &name, const LoopLevel &value)
655  }
656 
658 
659  void set(const LoopLevel &value) override {
660  // Don't call check_value_writable(): It's OK to set a LoopLevel after generate().
661  // check_value_writable();
662 
663  // This looks odd, but is deliberate:
664 
665  // First, mutate the existing contents to match the value passed in,
666  // so that any existing usage of the LoopLevel now uses the newer value.
667  // (Strictly speaking, this is really only necessary if this method
668  // is called after generate(): before generate(), there is no usage
669  // to be concerned with.)
670  value_.set(value);
671 
672  // Then, reset the value itself so that it points to the same LoopLevelContents
673  // as the value passed in. (Strictly speaking, this is really only
674  // useful if this method is called before generate(): afterwards, it's
675  // too late to alter the code to refer to a different LoopLevelContents.)
676  value_ = value;
677  }
678 
679  void set_from_string(const std::string &new_value_string) override {
680  if (new_value_string == "root") {
681  this->set(LoopLevel::root());
682  } else if (new_value_string == "inlined") {
683  this->set(LoopLevel::inlined());
684  } else {
685  user_error << "Unable to parse " << this->name() << ": " << new_value_string;
686  }
687  }
688 
689  std::string get_default_value() const override {
690  // This is dodgy but safe in this case: we want to
691  // see what the value of our LoopLevel is *right now*,
692  // so we make a copy and lock the copy so we can inspect it.
693  // (Note that ordinarily this is a bad idea, since LoopLevels
694  // can be mutated later on; however, this method is only
695  // called by the Generator infrastructure, on LoopLevels that
696  // will never be mutated, so this is really just an elaborate way
697  // to avoid runtime assertions.)
698  LoopLevel copy;
699  copy.set(this->value());
700  copy.lock();
701  if (copy.is_inlined()) {
702  return "LoopLevel::inlined()";
703  } else if (copy.is_root()) {
704  return "LoopLevel::root()";
705  } else {
707  return "";
708  }
709  }
710 
711  std::string call_to_string(const std::string &v) const override {
713  return std::string();
714  }
715 
716  std::string get_c_type() const override {
717  return "LoopLevel";
718  }
719 
720  bool is_looplevel_param() const override {
721  return true;
722  }
723 };
724 
725 template<typename T>
727 public:
728  GeneratorParam_Arithmetic(const std::string &name,
729  const T &value,
730  const T &min = std::numeric_limits<T>::lowest(),
731  const T &max = std::numeric_limits<T>::max())
732  : GeneratorParamImpl<T>(name, value), min(min), max(max) {
733  // call set() to ensure value is clamped to min/max
734  this->set(value);
735  }
736 
737  void set_impl(const T &new_value) override {
738  user_assert(new_value >= min && new_value <= max) << "Value out of range: " << new_value;
740  }
741 
742  void set_from_string(const std::string &new_value_string) override {
743  std::istringstream iss(new_value_string);
744  T t;
745  // All one-byte ints int8 and uint8 should be parsed as integers, not chars --
746  // including 'char' itself. (Note that sizeof(bool) is often-but-not-always-1,
747  // so be sure to exclude that case.)
748  if (sizeof(T) == sizeof(char) && !std::is_same<T, bool>::value) {
749  int i;
750  iss >> i;
751  t = (T)i;
752  } else {
753  iss >> t;
754  }
755  user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << new_value_string;
756  this->set(t);
757  }
758 
759  std::string get_default_value() const override {
760  std::ostringstream oss;
761  oss << this->value();
762  if (std::is_same<T, float>::value) {
763  // If the constant has no decimal point ("1")
764  // we must append one before appending "f"
765  if (oss.str().find('.') == std::string::npos) {
766  oss << ".";
767  }
768  oss << "f";
769  }
770  return oss.str();
771  }
772 
773  std::string call_to_string(const std::string &v) const override {
774  std::ostringstream oss;
775  oss << "std::to_string(" << v << ")";
776  return oss.str();
777  }
778 
779  std::string get_c_type() const override {
780  std::ostringstream oss;
781  if (std::is_same<T, float>::value) {
782  return "float";
783  } else if (std::is_same<T, double>::value) {
784  return "double";
785  } else if (std::is_integral<T>::value) {
786  if (std::is_unsigned<T>::value) {
787  oss << "u";
788  }
789  oss << "int" << (sizeof(T) * 8) << "_t";
790  return oss.str();
791  } else {
792  user_error << "Unknown arithmetic type\n";
793  return "";
794  }
795  }
796 
797 private:
798  const T min, max;
799 };
800 
801 template<typename T>
803 public:
804  GeneratorParam_Bool(const std::string &name, const T &value)
806  }
807 
808  void set_from_string(const std::string &new_value_string) override {
809  bool v = false;
810  if (new_value_string == "true" || new_value_string == "True") {
811  v = true;
812  } else if (new_value_string == "false" || new_value_string == "False") {
813  v = false;
814  } else {
815  user_assert(false) << "Unable to parse bool: " << new_value_string;
816  }
817  this->set(v);
818  }
819 
820  std::string get_default_value() const override {
821  return this->value() ? "true" : "false";
822  }
823 
824  std::string call_to_string(const std::string &v) const override {
825  std::ostringstream oss;
826  oss << "std::string((" << v << ") ? \"true\" : \"false\")";
827  return oss.str();
828  }
829 
830  std::string get_c_type() const override {
831  return "bool";
832  }
833 };
834 
835 template<typename T>
837 public:
838  GeneratorParam_Enum(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
839  : GeneratorParamImpl<T>(name, value), enum_map(enum_map) {
840  }
841 
842  // define a "set" that takes our specific enum (but don't hide the inherited virtual functions)
844 
845  template<typename T2 = T, typename std::enable_if<!std::is_same<T2, Type>::value>::type * = nullptr>
846  void set(const T &e) {
847  this->set_impl(e);
848  }
849 
850  void set_from_string(const std::string &new_value_string) override {
851  auto it = enum_map.find(new_value_string);
852  user_assert(it != enum_map.end()) << "Enumeration value not found: " << new_value_string;
853  this->set_impl(it->second);
854  }
855 
856  std::string call_to_string(const std::string &v) const override {
857  return "Enum_" + this->name() + "_map().at(" + v + ")";
858  }
859 
860  std::string get_c_type() const override {
861  return "Enum_" + this->name();
862  }
863 
864  std::string get_default_value() const override {
865  return "Enum_" + this->name() + "::" + enum_to_string(enum_map, this->value());
866  }
867 
868  std::string get_type_decls() const override {
869  std::ostringstream oss;
870  oss << "enum class Enum_" << this->name() << " {\n";
871  for (auto key_value : enum_map) {
872  oss << " " << key_value.first << ",\n";
873  }
874  oss << "};\n";
875  oss << "\n";
876 
877  // TODO: since we generate the enums, we could probably just use a vector (or array!) rather than a map,
878  // since we can ensure that the enum values are a nice tight range.
879  oss << "inline HALIDE_NO_USER_CODE_INLINE const std::map<Enum_" << this->name() << ", std::string>& Enum_" << this->name() << "_map() {\n";
880  oss << " static const std::map<Enum_" << this->name() << ", std::string> m = {\n";
881  for (auto key_value : enum_map) {
882  oss << " { Enum_" << this->name() << "::" << key_value.first << ", \"" << key_value.first << "\"},\n";
883  }
884  oss << " };\n";
885  oss << " return m;\n";
886  oss << "};\n";
887  return oss.str();
888  }
889 
890 private:
891  const std::map<std::string, T> enum_map;
892 };
893 
894 template<typename T>
896 public:
897  GeneratorParam_Type(const std::string &name, const T &value)
899  }
900 
901  std::string call_to_string(const std::string &v) const override {
902  return "Halide::Internal::halide_type_to_enum_string(" + v + ")";
903  }
904 
905  std::string get_c_type() const override {
906  return "Type";
907  }
908 
909  std::string get_default_value() const override {
910  return halide_type_to_c_source(this->value());
911  }
912 
913  std::string get_type_decls() const override {
914  return "";
915  }
916 };
917 
918 template<typename T>
920 public:
921  GeneratorParam_String(const std::string &name, const std::string &value)
922  : GeneratorParamImpl<T>(name, value) {
923  }
924  void set_from_string(const std::string &new_value_string) override {
925  this->set(new_value_string);
926  }
927 
928  std::string get_default_value() const override {
929  return "\"" + this->value() + "\"";
930  }
931 
932  std::string call_to_string(const std::string &v) const override {
933  return v;
934  }
935 
936  std::string get_c_type() const override {
937  return "std::string";
938  }
939 };
940 
941 template<typename T>
943  typename select_type<
944  cond<std::is_same<T, Target>::value, GeneratorParam_Target<T>>,
945  cond<std::is_same<T, LoopLevel>::value, GeneratorParam_LoopLevel>,
946  cond<std::is_same<T, std::string>::value, GeneratorParam_String<T>>,
947  cond<std::is_same<T, Type>::value, GeneratorParam_Type<T>>,
948  cond<std::is_same<T, bool>::value, GeneratorParam_Bool<T>>,
949  cond<std::is_arithmetic<T>::value, GeneratorParam_Arithmetic<T>>,
951 
952 } // namespace Internal
953 
954 /** GeneratorParam is a templated class that can be used to modify the behavior
955  * of the Generator at code-generation time. GeneratorParams are commonly
956  * specified in build files (e.g. Makefile) to customize the behavior of
957  * a given Generator, thus they have a very constrained set of types to allow
958  * for efficient specification via command-line flags. A GeneratorParam can be:
959  * - any float or int type.
960  * - bool
961  * - enum
962  * - Halide::Target
963  * - Halide::Type
964  * - std::string
965  * Please don't use std::string unless there's no way to do what you want with some
966  * other type; in particular, don't use this if you can use enum instead.
967  * All GeneratorParams have a default value. Arithmetic types can also
968  * optionally specify min and max. Enum types must specify a string-to-value
969  * map.
970  *
971  * Halide::Type is treated as though it were an enum, with the mappings:
972  *
973  * "int8" Halide::Int(8)
974  * "int16" Halide::Int(16)
975  * "int32" Halide::Int(32)
976  * "int64" Halide::Int(64)
977  * "uint8" Halide::UInt(8)
978  * "uint16" Halide::UInt(16)
979  * "uint32" Halide::UInt(32)
980  * "uint64" Halide::UInt(64)
981  * "float16" Halide::Float(16)
982  * "float32" Halide::Float(32)
983  * "float64" Halide::Float(64)
984  * "bfloat16" Halide::BFloat(16)
985  *
986  * No vector Types are currently supported by this mapping.
987  *
988  */
989 template<typename T>
991 public:
992  template<typename T2 = T, typename std::enable_if<!std::is_same<T2, std::string>::value>::type * = nullptr>
993  GeneratorParam(const std::string &name, const T &value)
994  : Internal::GeneratorParamImplBase<T>(name, value) {
995  }
996 
997  GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
998  : Internal::GeneratorParamImplBase<T>(name, value, min, max) {
999  }
1000 
1001  GeneratorParam(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
1002  : Internal::GeneratorParamImplBase<T>(name, value, enum_map) {
1003  }
1004 
1005  GeneratorParam(const std::string &name, const std::string &value)
1006  : Internal::GeneratorParamImplBase<T>(name, value) {
1007  }
1008 };
1009 
1010 /** Addition between GeneratorParam<T> and any type that supports operator+ with T.
1011  * Returns type of underlying operator+. */
1012 // @{
1013 template<typename Other, typename T>
1014 auto operator+(const Other &a, const GeneratorParam<T> &b) -> decltype(a + (T)b) {
1015  return a + (T)b;
1016 }
1017 template<typename Other, typename T>
1018 auto operator+(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a + b) {
1019  return (T)a + b;
1020 }
1021 // @}
1022 
1023 /** Subtraction between GeneratorParam<T> and any type that supports operator- with T.
1024  * Returns type of underlying operator-. */
1025 // @{
1026 template<typename Other, typename T>
1027 auto operator-(const Other &a, const GeneratorParam<T> &b) -> decltype(a - (T)b) {
1028  return a - (T)b;
1029 }
1030 template<typename Other, typename T>
1031 auto operator-(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a - b) {
1032  return (T)a - b;
1033 }
1034 // @}
1035 
1036 /** Multiplication between GeneratorParam<T> and any type that supports operator* with T.
1037  * Returns type of underlying operator*. */
1038 // @{
1039 template<typename Other, typename T>
1040 auto operator*(const Other &a, const GeneratorParam<T> &b) -> decltype(a * (T)b) {
1041  return a * (T)b;
1042 }
1043 template<typename Other, typename T>
1044 auto operator*(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a * b) {
1045  return (T)a * b;
1046 }
1047 // @}
1048 
1049 /** Division between GeneratorParam<T> and any type that supports operator/ with T.
1050  * Returns type of underlying operator/. */
1051 // @{
1052 template<typename Other, typename T>
1053 auto operator/(const Other &a, const GeneratorParam<T> &b) -> decltype(a / (T)b) {
1054  return a / (T)b;
1055 }
1056 template<typename Other, typename T>
1057 auto operator/(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a / b) {
1058  return (T)a / b;
1059 }
1060 // @}
1061 
1062 /** Modulo between GeneratorParam<T> and any type that supports operator% with T.
1063  * Returns type of underlying operator%. */
1064 // @{
1065 template<typename Other, typename T>
1066 auto operator%(const Other &a, const GeneratorParam<T> &b) -> decltype(a % (T)b) {
1067  return a % (T)b;
1068 }
1069 template<typename Other, typename T>
1070 auto operator%(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a % b) {
1071  return (T)a % b;
1072 }
1073 // @}
1074 
1075 /** Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
1076  * Returns type of underlying operator>. */
1077 // @{
1078 template<typename Other, typename T>
1079 auto operator>(const Other &a, const GeneratorParam<T> &b) -> decltype(a > (T)b) {
1080  return a > (T)b;
1081 }
1082 template<typename Other, typename T>
1083 auto operator>(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a > b) {
1084  return (T)a > b;
1085 }
1086 // @}
1087 
1088 /** Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
1089  * Returns type of underlying operator<. */
1090 // @{
1091 template<typename Other, typename T>
1092 auto operator<(const Other &a, const GeneratorParam<T> &b) -> decltype(a < (T)b) {
1093  return a < (T)b;
1094 }
1095 template<typename Other, typename T>
1096 auto operator<(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a < b) {
1097  return (T)a < b;
1098 }
1099 // @}
1100 
1101 /** Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with T.
1102  * Returns type of underlying operator>=. */
1103 // @{
1104 template<typename Other, typename T>
1105 auto operator>=(const Other &a, const GeneratorParam<T> &b) -> decltype(a >= (T)b) {
1106  return a >= (T)b;
1107 }
1108 template<typename Other, typename T>
1109 auto operator>=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a >= b) {
1110  return (T)a >= b;
1111 }
1112 // @}
1113 
1114 /** Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
1115  * Returns type of underlying operator<=. */
1116 // @{
1117 template<typename Other, typename T>
1118 auto operator<=(const Other &a, const GeneratorParam<T> &b) -> decltype(a <= (T)b) {
1119  return a <= (T)b;
1120 }
1121 template<typename Other, typename T>
1122 auto operator<=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a <= b) {
1123  return (T)a <= b;
1124 }
1125 // @}
1126 
1127 /** Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
1128  * Returns type of underlying operator==. */
1129 // @{
1130 template<typename Other, typename T>
1131 auto operator==(const Other &a, const GeneratorParam<T> &b) -> decltype(a == (T)b) {
1132  return a == (T)b;
1133 }
1134 template<typename Other, typename T>
1135 auto operator==(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a == b) {
1136  return (T)a == b;
1137 }
1138 // @}
1139 
1140 /** Inequality comparison between between GeneratorParam<T> and any type that supports operator!= with T.
1141  * Returns type of underlying operator!=. */
1142 // @{
1143 template<typename Other, typename T>
1144 auto operator!=(const Other &a, const GeneratorParam<T> &b) -> decltype(a != (T)b) {
1145  return a != (T)b;
1146 }
1147 template<typename Other, typename T>
1148 auto operator!=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a != b) {
1149  return (T)a != b;
1150 }
1151 // @}
1152 
1153 /** Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
1154  * Returns type of underlying operator&&. */
1155 // @{
1156 template<typename Other, typename T>
1157 auto operator&&(const Other &a, const GeneratorParam<T> &b) -> decltype(a && (T)b) {
1158  return a && (T)b;
1159 }
1160 template<typename Other, typename T>
1161 auto operator&&(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a && b) {
1162  return (T)a && b;
1163 }
1164 template<typename T>
1165 auto operator&&(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a && (T)b) {
1166  return (T)a && (T)b;
1167 }
1168 // @}
1169 
1170 /** Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
1171  * Returns type of underlying operator||. */
1172 // @{
1173 template<typename Other, typename T>
1174 auto operator||(const Other &a, const GeneratorParam<T> &b) -> decltype(a || (T)b) {
1175  return a || (T)b;
1176 }
1177 template<typename Other, typename T>
1178 auto operator||(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a || b) {
1179  return (T)a || b;
1180 }
1181 template<typename T>
1182 auto operator||(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a || (T)b) {
1183  return (T)a || (T)b;
1184 }
1185 // @}
1186 
1187 /* min and max are tricky as the language support for these is in the std
1188  * namespace. In order to make this work, forwarding functions are used that
1189  * are declared in a namespace that has std::min and std::max in scope.
1190  */
1191 namespace Internal {
1192 namespace GeneratorMinMax {
1193 
1194 using std::max;
1195 using std::min;
1196 
1197 template<typename Other, typename T>
1198 auto min_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(min(a, (T)b)) {
1199  return min(a, (T)b);
1200 }
1201 template<typename Other, typename T>
1202 auto min_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(min((T)a, b)) {
1203  return min((T)a, b);
1204 }
1205 
1206 template<typename Other, typename T>
1207 auto max_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(max(a, (T)b)) {
1208  return max(a, (T)b);
1209 }
1210 template<typename Other, typename T>
1211 auto max_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(max((T)a, b)) {
1212  return max((T)a, b);
1213 }
1214 
1215 } // namespace GeneratorMinMax
1216 } // namespace Internal
1217 
1218 /** Compute minimum between GeneratorParam<T> and any type that supports min with T.
1219  * Will automatically import std::min. Returns type of underlying min call. */
1220 // @{
1221 template<typename Other, typename T>
1222 auto min(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1224 }
1225 template<typename Other, typename T>
1226 auto min(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1228 }
1229 // @}
1230 
1231 /** Compute the maximum value between GeneratorParam<T> and any type that supports max with T.
1232  * Will automatically import std::max. Returns type of underlying max call. */
1233 // @{
1234 template<typename Other, typename T>
1235 auto max(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1237 }
1238 template<typename Other, typename T>
1239 auto max(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1241 }
1242 // @}
1243 
1244 /** Not operator for GeneratorParam */
1245 template<typename T>
1246 auto operator!(const GeneratorParam<T> &a) -> decltype(!(T)a) {
1247  return !(T)a;
1248 }
1249 
1250 namespace Internal {
1251 
1252 template<typename T2>
1254 
1255 /**
1256  * StubInputBuffer is the placeholder that a Stub uses when it requires
1257  * a Buffer for an input (rather than merely a Func or Expr). It is constructed
1258  * to allow only two possible sorts of input:
1259  * -- Assignment of an Input<Buffer<>>, with compatible type and dimensions,
1260  * essentially allowing us to pipe a parameter from an enclosing Generator to an internal Stub.
1261  * -- Assignment of a Buffer<>, with compatible type and dimensions,
1262  * causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
1263  */
1266  friend class StubInput;
1267  template<typename T2>
1269  template<typename T2, int D2>
1270  friend class StubInputBuffer;
1271 
1272  Parameter parameter_;
1273 
1275  : parameter_(p) {
1276  // Create an empty 1-element buffer with the right runtime typing and dimensions,
1277  // which we'll use only to pass to can_convert_from() to verify this
1278  // Parameter is compatible with our constraints.
1279  Buffer<> other(p.type(), nullptr, std::vector<int>(p.dimensions(), 1));
1281  }
1282 
1283  template<typename T2, int D2>
1284  HALIDE_NO_USER_CODE_INLINE static Parameter parameter_from_buffer(const Buffer<T2, D2> &b) {
1285  internal_assert(b.defined());
1287  Parameter p(b.type(), true, b.dimensions());
1288  p.set_buffer(b);
1289  return p;
1290  }
1291 
1292 public:
1293  StubInputBuffer() = default;
1294 
1295  // *not* explicit -- this ctor should only be used when you want
1296  // to pass a literal Buffer<> for a Stub Input; this Buffer<> will be
1297  // compiled into the Generator's product, rather than becoming
1298  // a runtime Parameter.
1299  template<typename T2, int D2>
1301  : parameter_(parameter_from_buffer(b)) {
1302  }
1303 
1304  template<typename T2>
1305  static std::vector<Parameter> to_parameter_vector(const StubInputBuffer<T2> &t) {
1306  return {t.parameter_};
1307  }
1308 
1309  template<typename T2>
1310  static std::vector<Parameter> to_parameter_vector(const std::vector<StubInputBuffer<T2>> &v) {
1311  std::vector<Parameter> r;
1312  r.reserve(v.size());
1313  for (const auto &s : v) {
1314  r.push_back(s.parameter_);
1315  }
1316  return r;
1317  }
1318 };
1319 
1320 class AbstractGenerator;
1321 
1323 protected:
1325  std::shared_ptr<AbstractGenerator> generator;
1326 
1327  Target get_target() const;
1328 
1330  explicit StubOutputBufferBase(const Func &f, const std::shared_ptr<AbstractGenerator> &generator);
1331 
1332 public:
1333  Realization realize(std::vector<int32_t> sizes);
1334 
1335  template<typename... Args>
1336  Realization realize(Args &&...args) {
1337  return f.realize(std::forward<Args>(args)..., get_target());
1338  }
1339 
1340  template<typename Dst>
1341  void realize(Dst dst) {
1342  f.realize(dst, get_target());
1343  }
1344 };
1345 
1346 /**
1347  * StubOutputBuffer is the placeholder that a Stub uses when it requires
1348  * a Buffer for an output (rather than merely a Func). It is constructed
1349  * to allow only two possible sorts of things:
1350  * -- Assignment to an Output<Buffer<>>, with compatible type and dimensions,
1351  * essentially allowing us to pipe a parameter from the result of a Stub to an
1352  * enclosing Generator
1353  * -- Realization into a Buffer<>; this is useful only in JIT compilation modes
1354  * (and shouldn't be usable otherwise)
1355  *
1356  * It is deliberate that StubOutputBuffer is not (easily) convertible to Func.
1357  */
1358 template<typename T = void>
1360  template<typename T2>
1362  explicit StubOutputBuffer(const Func &fn, const std::shared_ptr<AbstractGenerator> &gen)
1363  : StubOutputBufferBase(fn, gen) {
1364  }
1365 
1366 public:
1367  StubOutputBuffer() = default;
1368 
1369  static std::vector<StubOutputBuffer<T>> to_output_buffers(const std::vector<Func> &v,
1370  const std::shared_ptr<AbstractGenerator> &gen) {
1371  std::vector<StubOutputBuffer<T>> result;
1372  for (const Func &f : v) {
1373  result.push_back(StubOutputBuffer<T>(f, gen));
1374  }
1375  return result;
1376  }
1377 };
1378 
1379 // This is a union-like class that allows for convenient initialization of Stub Inputs
1380 // via initializer-list syntax; it is only used in situations where the
1381 // downstream consumer will be able to explicitly check that each value is
1382 // of the expected/required kind.
1383 class StubInput {
1384  const ArgInfoKind kind_;
1385  // Exactly one of the following fields should be defined:
1386  const Parameter parameter_;
1387  const Func func_;
1388  const Expr expr_;
1389 
1390 public:
1391  // *not* explicit.
1392  template<typename T2>
1394  : kind_(ArgInfoKind::Buffer), parameter_(b.parameter_), func_(), expr_() {
1395  }
1397  : kind_(ArgInfoKind::Buffer), parameter_(p), func_(), expr_() {
1398  }
1399  StubInput(const Func &f)
1400  : kind_(ArgInfoKind::Function), parameter_(), func_(f), expr_() {
1401  }
1402  StubInput(const Expr &e)
1403  : kind_(ArgInfoKind::Scalar), parameter_(), func_(), expr_(e) {
1404  }
1405 
1406  ArgInfoKind kind() const {
1407  return kind_;
1408  }
1409 
1412  return parameter_;
1413  }
1414 
1415  Func func() const {
1417  return func_;
1418  }
1419 
1420  Expr expr() const {
1422  return expr_;
1423  }
1424 };
1425 
1426 /** GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<>
1427  * instantiations; it is not part of the public API and should never be
1428  * used directly by user code.
1429  *
1430  * Every GIOBase instance can be either a single value or an array-of-values;
1431  * each of these values can be an Expr or a Func. (Note that for an
1432  * array-of-values, the types/dimensions of all values in the array must match.)
1433  *
1434  * A GIOBase can have multiple Types, in which case it represents a Tuple.
1435  * (Note that Tuples are currently only supported for GeneratorOutput, but
1436  * it is likely that GeneratorInput will be extended to support Tuple as well.)
1437  *
1438  * The array-size, type(s), and dimensions can all be left "unspecified" at
1439  * creation time, in which case they may assume values provided by a Stub.
1440  * (It is important to note that attempting to use a GIOBase with unspecified
1441  * values will assert-fail; you must ensure that all unspecified values are
1442  * filled in prior to use.)
1443  */
1444 class GIOBase {
1445 public:
1446  virtual ~GIOBase() = default;
1447 
1448  // These should only be called from configure() methods.
1449  // TODO: find a way to enforce this. Better yet, find a way to remove these.
1450  void set_type(const Type &type);
1451  void set_dimensions(int dims);
1452  void set_array_size(int size);
1453 
1454 protected:
1455  bool array_size_defined() const;
1456  size_t array_size() const;
1457  virtual bool is_array() const;
1458 
1459  const std::string &name() const;
1460  ArgInfoKind kind() const;
1461 
1462  bool gio_types_defined() const;
1463  const std::vector<Type> &gio_types() const;
1464  Type gio_type() const;
1465 
1466  bool dims_defined() const;
1467  int dims() const;
1468 
1469  const std::vector<Func> &funcs() const;
1470  const std::vector<Expr> &exprs() const;
1471 
1472  GIOBase(size_t array_size,
1473  const std::string &name,
1474  ArgInfoKind kind,
1475  const std::vector<Type> &types,
1476  int dims);
1477 
1478  friend class GeneratorBase;
1479  friend class GeneratorParamInfo;
1480 
1481  mutable int array_size_; // always 1 if is_array() == false.
1482  // -1 if is_array() == true but unspecified.
1483 
1484  const std::string name_;
1486  mutable std::vector<Type> types_; // empty if type is unspecified
1487  mutable int dims_; // -1 if dim is unspecified
1488 
1489  // Exactly one of these will have nonzero length
1490  std::vector<Func> funcs_;
1491  std::vector<Expr> exprs_;
1492 
1493  // Generator which owns this Input or Output. Note that this will be null
1494  // initially; the GeneratorBase itself will set this field when it initially
1495  // builds its info about params. However, since it isn't
1496  // appropriate for Input<> or Output<> to be declared outside of a Generator,
1497  // all reasonable non-testing code should expect this to be non-null.
1499 
1500  std::string array_name(size_t i) const;
1501 
1502  virtual void verify_internals();
1503 
1504  void check_matching_array_size(size_t size) const;
1505  void check_matching_types(const std::vector<Type> &t) const;
1506  void check_matching_dims(int d) const;
1507 
1508  template<typename ElemType>
1509  const std::vector<ElemType> &get_values() const;
1510 
1511  void check_gio_access() const;
1512 
1513  virtual void check_value_writable() const = 0;
1514 
1515  virtual const char *input_or_output() const = 0;
1516 
1517 private:
1518  template<typename T>
1520  friend class GeneratorStub;
1521 
1522 public:
1523  GIOBase(const GIOBase &) = delete;
1524  GIOBase &operator=(const GIOBase &) = delete;
1525  GIOBase(GIOBase &&) = delete;
1526  GIOBase &operator=(GIOBase &&) = delete;
1527 };
1528 
1529 template<>
1530 inline const std::vector<Expr> &GIOBase::get_values<Expr>() const {
1531  return exprs();
1532 }
1533 
1534 template<>
1535 inline const std::vector<Func> &GIOBase::get_values<Func>() const {
1536  return funcs();
1537 }
1538 
1539 class GeneratorInputBase : public GIOBase {
1540 protected:
1542  const std::string &name,
1543  ArgInfoKind kind,
1544  const std::vector<Type> &t,
1545  int d);
1546 
1547  GeneratorInputBase(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d);
1548 
1549  friend class GeneratorBase;
1550  friend class GeneratorParamInfo;
1551 
1552  std::vector<Parameter> parameters_;
1553 
1554  Parameter parameter() const;
1555 
1556  void init_internals();
1557  void set_inputs(const std::vector<StubInput> &inputs);
1558  bool inputs_set = false;
1559 
1560  virtual void set_def_min_max();
1561 
1562  void verify_internals() override;
1563 
1564  friend class StubEmitter;
1565 
1566  virtual std::string get_c_type() const = 0;
1567 
1568  void check_value_writable() const override;
1569 
1570  const char *input_or_output() const override {
1571  return "Input";
1572  }
1573 
1574  void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent);
1575  void set_estimates_impl(const Region &estimates);
1576 
1577 public:
1578  ~GeneratorInputBase() override;
1579 };
1580 
1581 template<typename T, typename ValueType>
1583 protected:
1584  using TBase = typename std::remove_all_extents<T>::type;
1585 
1586  bool is_array() const override {
1587  return std::is_array<T>::value;
1588  }
1589 
1590  template<typename T2 = T, typename std::enable_if<
1591  // Only allow T2 not-an-array
1592  !std::is_array<T2>::value>::type * = nullptr>
1593  GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1594  : GeneratorInputBase(name, kind, t, d) {
1595  }
1596 
1597  template<typename T2 = T, typename std::enable_if<
1598  // Only allow T2[kSomeConst]
1599  std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
1600  GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1601  : GeneratorInputBase(std::extent<T2, 0>::value, name, kind, t, d) {
1602  }
1603 
1604  template<typename T2 = T, typename std::enable_if<
1605  // Only allow T2[]
1606  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
1607  GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1608  : GeneratorInputBase(-1, name, kind, t, d) {
1609  }
1610 
1611 public:
1612  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1613  size_t size() const {
1614  this->check_gio_access();
1615  return get_values<ValueType>().size();
1616  }
1617 
1618  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1619  const ValueType &operator[](size_t i) const {
1620  this->check_gio_access();
1621  return get_values<ValueType>()[i];
1622  }
1623 
1624  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1625  const ValueType &at(size_t i) const {
1626  this->check_gio_access();
1627  return get_values<ValueType>().at(i);
1628  }
1629 
1630  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1631  typename std::vector<ValueType>::const_iterator begin() const {
1632  this->check_gio_access();
1633  return get_values<ValueType>().begin();
1634  }
1635 
1636  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1637  typename std::vector<ValueType>::const_iterator end() const {
1638  this->check_gio_access();
1639  return get_values<ValueType>().end();
1640  }
1641 };
1642 
1643 // When forwarding methods to ImageParam, Func, etc., we must take
1644 // care with the return types: many of the methods return a reference-to-self
1645 // (e.g., ImageParam&); since we create temporaries for most of these forwards,
1646 // returning a ref will crater because it refers to a now-defunct section of the
1647 // stack. Happily, simply removing the reference is solves this, since all of the
1648 // types in question satisfy the property of copies referring to the same underlying
1649 // structure (returning references is just an optimization). Since this is verbose
1650 // and used in several places, we'll use a helper macro:
1651 #define HALIDE_FORWARD_METHOD(Class, Method) \
1652  template<typename... Args> \
1653  inline auto Method(Args &&...args)->typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
1654  return this->template as<Class>().Method(std::forward<Args>(args)...); \
1655  }
1656 
1657 #define HALIDE_FORWARD_METHOD_CONST(Class, Method) \
1658  template<typename... Args> \
1659  inline auto Method(Args &&...args) const-> \
1660  typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
1661  this->check_gio_access(); \
1662  return this->template as<Class>().Method(std::forward<Args>(args)...); \
1663  }
1664 
1665 template<typename T>
1666 class GeneratorInput_Buffer : public GeneratorInputImpl<T, Func> {
1667 private:
1668  using Super = GeneratorInputImpl<T, Func>;
1669 
1670 protected:
1671  using TBase = typename Super::TBase;
1672 
1673  friend class ::Halide::Func;
1674  friend class ::Halide::Stage;
1675 
1676  std::string get_c_type() const override {
1677  if (TBase::has_static_halide_type) {
1678  return "Halide::Internal::StubInputBuffer<" +
1679  halide_type_to_c_type(TBase::static_halide_type()) +
1680  ">";
1681  } else {
1682  return "Halide::Internal::StubInputBuffer<>";
1683  }
1684  }
1685 
1686  template<typename T2>
1687  inline T2 as() const {
1688  return (T2) * this;
1689  }
1690 
1691 public:
1692  explicit GeneratorInput_Buffer(const std::string &name)
1694  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1695  TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
1696  }
1697 
1698  GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
1699  : Super(name, ArgInfoKind::Buffer, {t}, d) {
1700  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1701  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1702  }
1703 
1704  GeneratorInput_Buffer(const std::string &name, const Type &t)
1705  : Super(name, ArgInfoKind::Buffer, {t}, -1) {
1706  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1707  }
1708 
1709  GeneratorInput_Buffer(const std::string &name, int d)
1711  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1712  d) {
1713  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1714  }
1715 
1716  template<typename... Args>
1717  Expr operator()(Args &&...args) const {
1718  this->check_gio_access();
1719  return Func(*this)(std::forward<Args>(args)...);
1720  }
1721 
1722  Expr operator()(std::vector<Expr> args) const {
1723  this->check_gio_access();
1724  return Func(*this)(std::move(args));
1725  }
1726 
1727  template<typename T2>
1728  operator StubInputBuffer<T2>() const {
1729  user_assert(!this->is_array()) << "Cannot assign an array type to a non-array type for Input " << this->name();
1730  return StubInputBuffer<T2>(this->parameters_.at(0));
1731  }
1732 
1733  operator Func() const {
1734  this->check_gio_access();
1735  return this->funcs().at(0);
1736  }
1737 
1738  operator ExternFuncArgument() const {
1739  this->check_gio_access();
1740  return ExternFuncArgument(this->parameters_.at(0));
1741  }
1742 
1744  this->check_gio_access();
1745  this->set_estimate_impl(var, min, extent);
1746  return *this;
1747  }
1748 
1750  this->check_gio_access();
1751  this->set_estimates_impl(estimates);
1752  return *this;
1753  }
1754 
1755  Func in() {
1756  this->check_gio_access();
1757  return Func(*this).in();
1758  }
1759 
1760  Func in(const Func &other) {
1761  this->check_gio_access();
1762  return Func(*this).in(other);
1763  }
1764 
1765  Func in(const std::vector<Func> &others) {
1766  this->check_gio_access();
1767  return Func(*this).in(others);
1768  }
1769 
1770  operator ImageParam() const {
1771  this->check_gio_access();
1772  user_assert(!this->is_array()) << "Cannot convert an Input<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
1773  return ImageParam(this->parameters_.at(0), Func(*this));
1774  }
1775 
1776  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1777  size_t size() const {
1778  this->check_gio_access();
1779  return this->parameters_.size();
1780  }
1781 
1782  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1783  ImageParam operator[](size_t i) const {
1784  this->check_gio_access();
1785  return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1786  }
1787 
1788  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1789  ImageParam at(size_t i) const {
1790  this->check_gio_access();
1791  return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1792  }
1793 
1794  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1795  typename std::vector<ImageParam>::const_iterator begin() const {
1796  user_error << "Input<Buffer<>>::begin() is not supported.";
1797  return {};
1798  }
1799 
1800  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
1801  typename std::vector<ImageParam>::const_iterator end() const {
1802  user_error << "Input<Buffer<>>::end() is not supported.";
1803  return {};
1804  }
1805 
1806  /** Forward methods to the ImageParam. */
1807  // @{
1810  HALIDE_FORWARD_METHOD_CONST(ImageParam, host_alignment)
1811  HALIDE_FORWARD_METHOD(ImageParam, set_host_alignment)
1822  HALIDE_FORWARD_METHOD_CONST(ImageParam, add_trace_tag)
1824  // }@
1825 };
1826 
1827 template<typename T>
1828 class GeneratorInput_Func : public GeneratorInputImpl<T, Func> {
1829 private:
1831 
1832 protected:
1833  using TBase = typename Super::TBase;
1834 
1835  std::string get_c_type() const override {
1836  return "Func";
1837  }
1838 
1839  template<typename T2>
1840  inline T2 as() const {
1841  return (T2) * this;
1842  }
1843 
1844 public:
1845  GeneratorInput_Func(const std::string &name, const Type &t, int d)
1846  : Super(name, ArgInfoKind::Function, {t}, d) {
1847  }
1848 
1849  // unspecified type
1850  GeneratorInput_Func(const std::string &name, int d)
1851  : Super(name, ArgInfoKind::Function, {}, d) {
1852  }
1853 
1854  // unspecified dimension
1855  GeneratorInput_Func(const std::string &name, const Type &t)
1856  : Super(name, ArgInfoKind::Function, {t}, -1) {
1857  }
1858 
1859  // unspecified type & dimension
1860  explicit GeneratorInput_Func(const std::string &name)
1861  : Super(name, ArgInfoKind::Function, {}, -1) {
1862  }
1863 
1864  GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
1865  : Super(array_size, name, ArgInfoKind::Function, {t}, d) {
1866  }
1867 
1868  // unspecified type
1869  GeneratorInput_Func(size_t array_size, const std::string &name, int d)
1870  : Super(array_size, name, ArgInfoKind::Function, {}, d) {
1871  }
1872 
1873  // unspecified dimension
1874  GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
1875  : Super(array_size, name, ArgInfoKind::Function, {t}, -1) {
1876  }
1877 
1878  // unspecified type & dimension
1879  GeneratorInput_Func(size_t array_size, const std::string &name)
1880  : Super(array_size, name, ArgInfoKind::Function, {}, -1) {
1881  }
1882 
1883  template<typename... Args>
1884  Expr operator()(Args &&...args) const {
1885  this->check_gio_access();
1886  return this->funcs().at(0)(std::forward<Args>(args)...);
1887  }
1888 
1889  Expr operator()(const std::vector<Expr> &args) const {
1890  this->check_gio_access();
1891  return this->funcs().at(0)(args);
1892  }
1893 
1894  operator Func() const {
1895  this->check_gio_access();
1896  return this->funcs().at(0);
1897  }
1898 
1899  operator ExternFuncArgument() const {
1900  this->check_gio_access();
1901  return ExternFuncArgument(this->parameters_.at(0));
1902  }
1903 
1905  this->check_gio_access();
1906  this->set_estimate_impl(var, min, extent);
1907  return *this;
1908  }
1909 
1911  this->check_gio_access();
1912  this->set_estimates_impl(estimates);
1913  return *this;
1914  }
1915 
1916  Func in() {
1917  this->check_gio_access();
1918  return Func(*this).in();
1919  }
1920 
1921  Func in(const Func &other) {
1922  this->check_gio_access();
1923  return Func(*this).in(other);
1924  }
1925 
1926  Func in(const std::vector<Func> &others) {
1927  this->check_gio_access();
1928  return Func(*this).in(others);
1929  }
1930 
1931  /** Forward const methods to the underlying Func. (Non-const methods
1932  * aren't available for Input<Func>.) */
1933  // @{
1936  HALIDE_FORWARD_METHOD_CONST(Func, dimensions)
1937  HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
1938  HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
1943  HALIDE_FORWARD_METHOD_CONST(Func, update_args)
1944  HALIDE_FORWARD_METHOD_CONST(Func, update_value)
1945  HALIDE_FORWARD_METHOD_CONST(Func, update_values)
1948  // }@
1949 };
1950 
1951 template<typename T>
1953 private:
1955 
1956  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");
1957 
1958 protected:
1959  std::string get_c_type() const override {
1960  return "Expr";
1961  }
1962 
1963 public:
1964  explicit GeneratorInput_DynamicScalar(const std::string &name)
1965  : Super(name, ArgInfoKind::Scalar, {}, 0) {
1966  user_assert(!std::is_array<T>::value) << "Input<Expr[]> is not allowed";
1967  }
1968 
1969  /** You can use this Input as an expression in a halide
1970  * function definition */
1971  operator Expr() const {
1972  this->check_gio_access();
1973  return this->exprs().at(0);
1974  }
1975 
1976  /** Using an Input as the argument to an external stage treats it
1977  * as an Expr */
1978  operator ExternFuncArgument() const {
1979  this->check_gio_access();
1980  return ExternFuncArgument(this->exprs().at(0));
1981  }
1982 
1983  void set_estimate(const Expr &value) {
1984  this->check_gio_access();
1985  for (Parameter &p : this->parameters_) {
1986  p.set_estimate(value);
1987  }
1988  }
1989 
1990  Type type() const {
1991  return Expr(*this).type();
1992  }
1993 };
1994 
1995 template<typename T>
1997 private:
1999 
2000 protected:
2001  using TBase = typename Super::TBase;
2002 
2003  const TBase def_{TBase()};
2005 
2006  void set_def_min_max() override {
2007  for (Parameter &p : this->parameters_) {
2008  // No: we want to leave the Parameter unset here.
2009  // p.set_scalar<TBase>(def_);
2011  }
2012  }
2013 
2014  std::string get_c_type() const override {
2015  return "Expr";
2016  }
2017 
2018  // Expr() doesn't accept a pointer type in its ctor; add a SFINAE adapter
2019  // so that pointer (aka handle) Inputs will get cast to uint64.
2020  template<typename TBase2 = TBase, typename std::enable_if<!std::is_pointer<TBase2>::value>::type * = nullptr>
2021  static Expr TBaseToExpr(const TBase2 &value) {
2022  return cast<TBase>(Expr(value));
2023  }
2024 
2025  template<typename TBase2 = TBase, typename std::enable_if<std::is_pointer<TBase2>::value>::type * = nullptr>
2026  static Expr TBaseToExpr(const TBase2 &value) {
2027  user_assert(value == 0) << "Zero is the only legal default value for Inputs which are pointer types.\n";
2028  return Expr();
2029  }
2030 
2031 public:
2032  explicit GeneratorInput_Scalar(const std::string &name)
2033  : Super(name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2034  }
2035 
2036  GeneratorInput_Scalar(const std::string &name, const TBase &def)
2037  : Super(name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(def), def_expr_(TBaseToExpr(def)) {
2038  }
2039 
2041  const std::string &name)
2042  : Super(array_size, name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2043  }
2044 
2046  const std::string &name,
2047  const TBase &def)
2048  : Super(array_size, name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(def), def_expr_(TBaseToExpr(def)) {
2049  }
2050 
2051  /** You can use this Input as an expression in a halide
2052  * function definition */
2053  operator Expr() const {
2054  this->check_gio_access();
2055  return this->exprs().at(0);
2056  }
2057 
2058  /** Using an Input as the argument to an external stage treats it
2059  * as an Expr */
2060  operator ExternFuncArgument() const {
2061  this->check_gio_access();
2062  return ExternFuncArgument(this->exprs().at(0));
2063  }
2064 
2065  template<typename T2 = T, typename std::enable_if<std::is_pointer<T2>::value>::type * = nullptr>
2066  void set_estimate(const TBase &value) {
2067  this->check_gio_access();
2068  user_assert(value == nullptr) << "nullptr is the only valid estimate for Input<PointerType>";
2069  Expr e = reinterpret(type_of<T2>(), cast<uint64_t>(0));
2070  for (Parameter &p : this->parameters_) {
2071  p.set_estimate(e);
2072  }
2073  }
2074 
2075  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value && !std::is_pointer<T2>::value>::type * = nullptr>
2076  void set_estimate(const TBase &value) {
2077  this->check_gio_access();
2078  Expr e = Expr(value);
2079  if (std::is_same<T2, bool>::value) {
2080  e = cast<bool>(e);
2081  }
2082  for (Parameter &p : this->parameters_) {
2083  p.set_estimate(e);
2084  }
2085  }
2086 
2087  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2088  void set_estimate(size_t index, const TBase &value) {
2089  this->check_gio_access();
2090  Expr e = Expr(value);
2091  if (std::is_same<T2, bool>::value) {
2092  e = cast<bool>(e);
2093  }
2094  this->parameters_.at(index).set_estimate(e);
2095  }
2096 
2097  Type type() const {
2098  return Expr(*this).type();
2099  }
2100 };
2101 
2102 template<typename T>
2104 private:
2106 
2107 protected:
2108  using TBase = typename Super::TBase;
2109 
2110  const Expr min_, max_;
2111 
2112  void set_def_min_max() override {
2114  // Don't set min/max for bool
2115  if (!std::is_same<TBase, bool>::value) {
2116  for (Parameter &p : this->parameters_) {
2117  if (min_.defined()) {
2118  p.set_min_value(min_);
2119  }
2120  if (max_.defined()) {
2121  p.set_max_value(max_);
2122  }
2123  }
2124  }
2125  }
2126 
2127 public:
2128  explicit GeneratorInput_Arithmetic(const std::string &name)
2129  : Super(name), min_(Expr()), max_(Expr()) {
2130  }
2131 
2132  GeneratorInput_Arithmetic(const std::string &name,
2133  const TBase &def)
2134  : Super(name, def), min_(Expr()), max_(Expr()) {
2135  }
2136 
2138  const std::string &name)
2139  : Super(array_size, name), min_(Expr()), max_(Expr()) {
2140  }
2141 
2143  const std::string &name,
2144  const TBase &def)
2145  : Super(array_size, name, def), min_(Expr()), max_(Expr()) {
2146  }
2147 
2148  GeneratorInput_Arithmetic(const std::string &name,
2149  const TBase &def,
2150  const TBase &min,
2151  const TBase &max)
2152  : Super(name, def), min_(min), max_(max) {
2153  }
2154 
2156  const std::string &name,
2157  const TBase &def,
2158  const TBase &min,
2159  const TBase &max)
2160  : Super(array_size, name, def), min_(min), max_(max) {
2161  }
2162 };
2163 
2164 template<typename>
2165 struct type_sink {
2166  typedef void type;
2167 };
2168 
2169 template<typename T2, typename = void>
2170 struct has_static_halide_type_method : std::false_type {};
2171 
2172 template<typename T2>
2173 struct has_static_halide_type_method<T2, typename type_sink<decltype(T2::static_halide_type())>::type> : std::true_type {};
2174 
2175 template<typename T, typename TBase = typename std::remove_all_extents<T>::type>
2176 using GeneratorInputImplBase =
2177  typename select_type<
2183 
2184 } // namespace Internal
2185 
2186 template<typename T>
2188 private:
2190 
2191 protected:
2192  using TBase = typename Super::TBase;
2193 
2194  // Trick to avoid ambiguous ctor between Func-with-dim and int-with-default-value;
2195  // since we can't use std::enable_if on ctors, define the argument to be one that
2196  // can only be properly resolved for TBase=Func.
2197  struct Unused;
2198  using IntIfNonScalar =
2199  typename Internal::select_type<
2203 
2204 public:
2205  // Mark all of these explicit (not just single-arg versions) so that
2206  // we disallow copy-list-initialization form (i.e., Input foo{"foo"} is ok,
2207  // but Input foo = {"foo"} is not).
2208  explicit GeneratorInput(const std::string &name)
2209  : Super(name) {
2210  }
2211 
2212  explicit GeneratorInput(const std::string &name, const TBase &def)
2213  : Super(name, def) {
2214  }
2215 
2216  explicit GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
2217  : Super(array_size, name, def) {
2218  }
2219 
2220  explicit GeneratorInput(const std::string &name,
2221  const TBase &def, const TBase &min, const TBase &max)
2222  : Super(name, def, min, max) {
2223  }
2224 
2225  explicit GeneratorInput(size_t array_size, const std::string &name,
2226  const TBase &def, const TBase &min, const TBase &max)
2227  : Super(array_size, name, def, min, max) {
2228  }
2229 
2230  explicit GeneratorInput(const std::string &name, const Type &t, int d)
2231  : Super(name, t, d) {
2232  }
2233 
2234  explicit GeneratorInput(const std::string &name, const Type &t)
2235  : Super(name, t) {
2236  }
2237 
2238  // Avoid ambiguity between Func-with-dim and int-with-default
2239  explicit GeneratorInput(const std::string &name, IntIfNonScalar d)
2240  : Super(name, d) {
2241  }
2242 
2243  explicit GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
2244  : Super(array_size, name, t, d) {
2245  }
2246 
2247  explicit GeneratorInput(size_t array_size, const std::string &name, const Type &t)
2248  : Super(array_size, name, t) {
2249  }
2250 
2251  // Avoid ambiguity between Func-with-dim and int-with-default
2252  // template <typename T2 = T, typename std::enable_if<std::is_same<TBase, Func>::value>::type * = nullptr>
2253  explicit GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
2254  : Super(array_size, name, d) {
2255  }
2256 
2257  explicit GeneratorInput(size_t array_size, const std::string &name)
2258  : Super(array_size, name) {
2259  }
2260 };
2261 
2262 namespace Internal {
2263 
2265 protected:
2266  template<typename T2, typename std::enable_if<std::is_same<T2, Func>::value>::type * = nullptr>
2268  static_assert(std::is_same<T2, Func>::value, "Only Func allowed here");
2270  internal_assert(exprs_.empty());
2271  user_assert(!funcs_.empty()) << "No funcs_ are defined yet";
2272  user_assert(funcs_.size() == 1) << "Use [] to access individual Funcs in Output<Func[]>";
2273  return funcs_[0];
2274  }
2275 
2276 public:
2277  /** Forward schedule-related methods to the underlying Func. */
2278  // @{
2279  HALIDE_FORWARD_METHOD(Func, add_trace_tag)
2280  HALIDE_FORWARD_METHOD(Func, align_bounds)
2281  HALIDE_FORWARD_METHOD(Func, align_extent)
2282  HALIDE_FORWARD_METHOD(Func, align_storage)
2283  HALIDE_FORWARD_METHOD(Func, always_partition)
2284  HALIDE_FORWARD_METHOD(Func, always_partition_all)
2286  HALIDE_FORWARD_METHOD(Func, bound)
2287  HALIDE_FORWARD_METHOD(Func, bound_extent)
2288  HALIDE_FORWARD_METHOD(Func, compute_at)
2289  HALIDE_FORWARD_METHOD(Func, compute_inline)
2290  HALIDE_FORWARD_METHOD(Func, compute_root)
2291  HALIDE_FORWARD_METHOD(Func, compute_with)
2292  HALIDE_FORWARD_METHOD(Func, copy_to_device)
2293  HALIDE_FORWARD_METHOD(Func, copy_to_host)
2294  HALIDE_FORWARD_METHOD(Func, define_extern)
2296  HALIDE_FORWARD_METHOD_CONST(Func, dimensions)
2297  HALIDE_FORWARD_METHOD(Func, fold_storage)
2300  HALIDE_FORWARD_METHOD(Func, gpu_blocks)
2301  HALIDE_FORWARD_METHOD(Func, gpu_single_thread)
2302  HALIDE_FORWARD_METHOD(Func, gpu_threads)
2303  HALIDE_FORWARD_METHOD(Func, gpu_tile)
2304  HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
2305  HALIDE_FORWARD_METHOD(Func, hexagon)
2307  HALIDE_FORWARD_METHOD(Func, memoize)
2308  HALIDE_FORWARD_METHOD(Func, never_partition)
2309  HALIDE_FORWARD_METHOD(Func, never_partition_all)
2310  HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
2312  HALIDE_FORWARD_METHOD(Func, parallel)
2313  HALIDE_FORWARD_METHOD(Func, partition)
2314  HALIDE_FORWARD_METHOD(Func, prefetch)
2316  HALIDE_FORWARD_METHOD(Func, rename)
2317  HALIDE_FORWARD_METHOD(Func, reorder)
2318  HALIDE_FORWARD_METHOD(Func, reorder_storage)
2320  HALIDE_FORWARD_METHOD(Func, serial)
2321  HALIDE_FORWARD_METHOD(Func, set_estimate)
2322  HALIDE_FORWARD_METHOD(Func, specialize)
2323  HALIDE_FORWARD_METHOD(Func, specialize_fail)
2324  HALIDE_FORWARD_METHOD(Func, split)
2325  HALIDE_FORWARD_METHOD(Func, store_at)
2326  HALIDE_FORWARD_METHOD(Func, store_root)
2328  HALIDE_FORWARD_METHOD(Func, trace_stores)
2331  HALIDE_FORWARD_METHOD(Func, unroll)
2332  HALIDE_FORWARD_METHOD(Func, update)
2333  HALIDE_FORWARD_METHOD_CONST(Func, update_args)
2334  HALIDE_FORWARD_METHOD_CONST(Func, update_value)
2335  HALIDE_FORWARD_METHOD_CONST(Func, update_values)
2338  HALIDE_FORWARD_METHOD(Func, vectorize)
2339 
2340  // }@
2341 
2342 #undef HALIDE_OUTPUT_FORWARD
2343 #undef HALIDE_OUTPUT_FORWARD_CONST
2344 
2345 protected:
2347  const std::string &name,
2348  ArgInfoKind kind,
2349  const std::vector<Type> &t,
2350  int d);
2351 
2352  GeneratorOutputBase(const std::string &name,
2353  ArgInfoKind kind,
2354  const std::vector<Type> &t,
2355  int d);
2356 
2357  friend class GeneratorBase;
2358  friend class StubEmitter;
2359 
2360  void init_internals();
2361  void resize(size_t size);
2362 
2363  virtual std::string get_c_type() const {
2364  return "Func";
2365  }
2366 
2367  void check_value_writable() const override;
2368 
2369  const char *input_or_output() const override {
2370  return "Output";
2371  }
2372 
2373 public:
2374  ~GeneratorOutputBase() override;
2375 };
2376 
2377 template<typename T>
2379 protected:
2380  using TBase = typename std::remove_all_extents<T>::type;
2381  using ValueType = Func;
2382 
2383  bool is_array() const override {
2384  return std::is_array<T>::value;
2385  }
2386 
2387  template<typename T2 = T, typename std::enable_if<
2388  // Only allow T2 not-an-array
2389  !std::is_array<T2>::value>::type * = nullptr>
2390  GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2391  : GeneratorOutputBase(name, kind, t, d) {
2392  }
2393 
2394  template<typename T2 = T, typename std::enable_if<
2395  // Only allow T2[kSomeConst]
2396  std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
2397  GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2398  : GeneratorOutputBase(std::extent<T2, 0>::value, name, kind, t, d) {
2399  }
2400 
2401  template<typename T2 = T, typename std::enable_if<
2402  // Only allow T2[]
2403  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2404  GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2405  : GeneratorOutputBase(-1, name, kind, t, d) {
2406  }
2407 
2408 public:
2409  template<typename... Args, typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2410  FuncRef operator()(Args &&...args) const {
2411  this->check_gio_access();
2412  return get_values<ValueType>().at(0)(std::forward<Args>(args)...);
2413  }
2414 
2415  template<typename ExprOrVar, typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2416  FuncRef operator()(std::vector<ExprOrVar> args) const {
2417  this->check_gio_access();
2418  return get_values<ValueType>().at(0)(args);
2419  }
2420 
2421  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2422  operator Func() const {
2423  this->check_gio_access();
2424  return get_values<ValueType>().at(0);
2425  }
2426 
2427  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2428  operator Stage() const {
2429  this->check_gio_access();
2430  return get_values<ValueType>().at(0);
2431  }
2432 
2433  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2434  size_t size() const {
2435  this->check_gio_access();
2436  return get_values<ValueType>().size();
2437  }
2438 
2439  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2440  const ValueType &operator[](size_t i) const {
2441  this->check_gio_access();
2442  return get_values<ValueType>()[i];
2443  }
2444 
2445  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2446  const ValueType &at(size_t i) const {
2447  this->check_gio_access();
2448  return get_values<ValueType>().at(i);
2449  }
2450 
2451  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2452  typename std::vector<ValueType>::const_iterator begin() const {
2453  this->check_gio_access();
2454  return get_values<ValueType>().begin();
2455  }
2456 
2457  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2458  typename std::vector<ValueType>::const_iterator end() const {
2459  this->check_gio_access();
2460  return get_values<ValueType>().end();
2461  }
2462 
2463  template<typename T2 = T, typename std::enable_if<
2464  // Only allow T2[]
2465  std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2466  void resize(size_t size) {
2467  this->check_gio_access();
2469  }
2470 };
2471 
2472 template<typename T>
2474 private:
2475  using Super = GeneratorOutputImpl<T>;
2476 
2477  HALIDE_NO_USER_CODE_INLINE void assign_from_func(const Func &f) {
2478  this->check_value_writable();
2479 
2480  internal_assert(f.defined());
2481 
2482  if (this->gio_types_defined()) {
2483  const auto &my_types = this->gio_types();
2484  user_assert(my_types.size() == f.types().size())
2485  << "Cannot assign Func \"" << f.name()
2486  << "\" to Output \"" << this->name() << "\"\n"
2487  << "Output " << this->name()
2488  << " is declared to have " << my_types.size() << " tuple elements"
2489  << " but Func " << f.name()
2490  << " has " << f.types().size() << " tuple elements.\n";
2491  for (size_t i = 0; i < my_types.size(); i++) {
2492  user_assert(my_types[i] == f.types().at(i))
2493  << "Cannot assign Func \"" << f.name()
2494  << "\" to Output \"" << this->name() << "\"\n"
2495  << (my_types.size() > 1 ? "In tuple element " + std::to_string(i) + ", " : "")
2496  << "Output " << this->name()
2497  << " has declared type " << my_types[i]
2498  << " but Func " << f.name()
2499  << " has type " << f.types().at(i) << "\n";
2500  }
2501  }
2502  if (this->dims_defined()) {
2503  user_assert(f.dimensions() == this->dims())
2504  << "Cannot assign Func \"" << f.name()
2505  << "\" to Output \"" << this->name() << "\"\n"
2506  << "Output " << this->name()
2507  << " has declared dimensionality " << this->dims()
2508  << " but Func " << f.name()
2509  << " has dimensionality " << f.dimensions() << "\n";
2510  }
2511 
2512  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2513  user_assert(!this->funcs_.at(0).defined());
2514  this->funcs_[0] = f;
2515  }
2516 
2517 protected:
2518  using TBase = typename Super::TBase;
2519 
2520  explicit GeneratorOutput_Buffer(const std::string &name)
2522  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2523  TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2524  }
2525 
2526  GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t, int d)
2527  : Super(name, ArgInfoKind::Buffer, t, d) {
2528  internal_assert(!t.empty());
2529  internal_assert(d != -1);
2530  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2531  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2532  }
2533 
2534  GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t)
2535  : Super(name, ArgInfoKind::Buffer, t, -1) {
2536  internal_assert(!t.empty());
2537  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2538  }
2539 
2540  GeneratorOutput_Buffer(const std::string &name, int d)
2542  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2543  d) {
2544  internal_assert(d != -1);
2545  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2546  }
2547 
2548  GeneratorOutput_Buffer(size_t array_size, const std::string &name)
2550  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2551  TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2552  }
2553 
2554  GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2555  : Super(array_size, name, ArgInfoKind::Buffer, t, d) {
2556  internal_assert(!t.empty());
2557  internal_assert(d != -1);
2558  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2559  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2560  }
2561 
2562  GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t)
2563  : Super(array_size, name, ArgInfoKind::Buffer, t, -1) {
2564  internal_assert(!t.empty());
2565  static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2566  }
2567 
2568  GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
2570  TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2571  d) {
2572  internal_assert(d != -1);
2573  static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2574  }
2575 
2576  HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override {
2577  if (TBase::has_static_halide_type) {
2578  return "Halide::Internal::StubOutputBuffer<" +
2579  halide_type_to_c_type(TBase::static_halide_type()) +
2580  ">";
2581  } else {
2582  return "Halide::Internal::StubOutputBuffer<>";
2583  }
2584  }
2585 
2586  template<typename T2, typename std::enable_if<!std::is_same<T2, Func>::value>::type * = nullptr>
2588  return (T2) * this;
2589  }
2590 
2591 public:
2592  // Allow assignment from a Buffer<> to an Output<Buffer<>>;
2593  // this allows us to use a statically-compiled buffer inside a Generator
2594  // to assign to an output.
2595  // TODO: This used to take the buffer as a const ref. This no longer works as
2596  // using it in a Pipeline might change the dev field so it is currently
2597  // not considered const. We should consider how this really ought to work.
2598  template<typename T2, int D2>
2600  this->check_gio_access();
2601  this->check_value_writable();
2602 
2603  user_assert(T::can_convert_from(buffer))
2604  << "Cannot assign to the Output \"" << this->name()
2605  << "\": the expression is not convertible to the same Buffer type and/or dimensions.\n";
2606 
2607  if (this->gio_types_defined()) {
2608  user_assert(Type(buffer.type()) == this->gio_type())
2609  << "Output " << this->name() << " should have type=" << this->gio_type() << " but saw type=" << Type(buffer.type()) << "\n";
2610  }
2611  if (this->dims_defined()) {
2612  user_assert(buffer.dimensions() == this->dims())
2613  << "Output " << this->name() << " should have dim=" << this->dims() << " but saw dim=" << buffer.dimensions() << "\n";
2614  }
2615 
2616  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2617  user_assert(!this->funcs_.at(0).defined());
2618  this->funcs_.at(0)(_) = buffer(_);
2619 
2620  return *this;
2621  }
2622 
2623  // Allow assignment from a StubOutputBuffer to an Output<Buffer>;
2624  // this allows us to pipeline the results of a Stub to the results
2625  // of the enclosing Generator.
2626  template<typename T2>
2628  this->check_gio_access();
2629  assign_from_func(stub_output_buffer.f);
2630  return *this;
2631  }
2632 
2633  // Allow assignment from a Func to an Output<Buffer>;
2634  // this allows us to use helper functions that return a plain Func
2635  // to simply set the output(s) without needing a wrapper Func.
2637  this->check_gio_access();
2638  assign_from_func(f);
2639  return *this;
2640  }
2641 
2642  operator OutputImageParam() const {
2643  this->check_gio_access();
2644  user_assert(!this->is_array()) << "Cannot convert an Output<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
2645  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2646  return this->funcs_.at(0).output_buffer();
2647  }
2648 
2649  // Forward set_estimates() to Func (rather than OutputImageParam) so that it can
2650  // handle Tuple-valued outputs correctly.
2652  user_assert(!this->is_array()) << "Cannot call set_estimates() on an array Output; use an explicit subscript operator: " << this->name();
2653  internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2654  this->funcs_.at(0).set_estimates(estimates);
2655  return *this;
2656  }
2657 
2658  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2659  const Func &operator[](size_t i) const {
2660  this->check_gio_access();
2661  return this->template get_values<Func>()[i];
2662  }
2663 
2664  // Allow Output<Buffer[]>.compute_root() (or other scheduling directive that requires nonconst)
2665  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2666  Func operator[](size_t i) {
2667  this->check_gio_access();
2668  return this->template get_values<Func>()[i];
2669  }
2670 
2671  /** Forward methods to the OutputImageParam. */
2672  // @{
2676  HALIDE_FORWARD_METHOD(OutputImageParam, set_host_alignment)
2686  // }@
2687 };
2688 
2689 template<typename T>
2691 private:
2692  using Super = GeneratorOutputImpl<T>;
2693 
2694  HALIDE_NO_USER_CODE_INLINE Func &get_assignable_func_ref(size_t i) {
2695  internal_assert(this->exprs_.empty() && this->funcs_.size() > i);
2696  return this->funcs_.at(i);
2697  }
2698 
2699 protected:
2700  using TBase = typename Super::TBase;
2701 
2702  explicit GeneratorOutput_Func(const std::string &name)
2703  : Super(name, ArgInfoKind::Function, std::vector<Type>{}, -1) {
2704  }
2705 
2706  GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t, int d)
2707  : Super(name, ArgInfoKind::Function, t, d) {
2708  }
2709 
2710  GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t)
2711  : Super(name, ArgInfoKind::Function, t, -1) {
2712  }
2713 
2714  GeneratorOutput_Func(const std::string &name, int d)
2715  : Super(name, ArgInfoKind::Function, {}, d) {
2716  }
2717 
2718  GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2719  : Super(array_size, name, ArgInfoKind::Function, t, d) {
2720  }
2721 
2722 public:
2723  // Allow Output<Func> = Func
2724  template<typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2726  this->check_gio_access();
2727  this->check_value_writable();
2728 
2729  // Don't bother verifying the Func type, dimensions, etc., here:
2730  // That's done later, when we produce the pipeline.
2731  get_assignable_func_ref(0) = f;
2732  return *this;
2733  }
2734 
2735  // Allow Output<Func[]> = Func
2736  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2737  Func &operator[](size_t i) {
2738  this->check_gio_access();
2739  this->check_value_writable();
2740  return get_assignable_func_ref(i);
2741  }
2742 
2743  // Allow Func = Output<Func[]>
2744  template<typename T2 = T, typename std::enable_if<std::is_array<T2>::value>::type * = nullptr>
2745  const Func &operator[](size_t i) const {
2746  this->check_gio_access();
2747  return Super::operator[](i);
2748  }
2749 
2750  GeneratorOutput_Func<T> &set_estimate(const Var &var, const Expr &min, const Expr &extent) {
2751  this->check_gio_access();
2752  internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2753  for (Func &f : this->funcs_) {
2754  f.set_estimate(var, min, extent);
2755  }
2756  return *this;
2757  }
2758 
2760  this->check_gio_access();
2761  internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2762  for (Func &f : this->funcs_) {
2763  f.set_estimates(estimates);
2764  }
2765  return *this;
2766  }
2767 };
2768 
2769 template<typename T>
2771 private:
2772  using Super = GeneratorOutputImpl<T>;
2773 
2774 protected:
2775  using TBase = typename Super::TBase;
2776 
2777  explicit GeneratorOutput_Arithmetic(const std::string &name)
2778  : Super(name, ArgInfoKind::Function, {type_of<TBase>()}, 0) {
2779  }
2780 
2781  GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
2782  : Super(array_size, name, ArgInfoKind::Function, {type_of<TBase>()}, 0) {
2783  }
2784 };
2785 
2786 template<typename T, typename TBase = typename std::remove_all_extents<T>::type>
2788  typename select_type<
2789  cond<has_static_halide_type_method<TBase>::value, GeneratorOutput_Buffer<T>>,
2790  cond<std::is_same<TBase, Func>::value, GeneratorOutput_Func<T>>,
2792 
2793 } // namespace Internal
2794 
2795 template<typename T>
2797 private:
2799 
2800 protected:
2801  using TBase = typename Super::TBase;
2802 
2803 public:
2804  // Mark all of these explicit (not just single-arg versions) so that
2805  // we disallow copy-list-initialization form (i.e., Output foo{"foo"} is ok,
2806  // but Output foo = {"foo"} is not).
2807  explicit GeneratorOutput(const std::string &name)
2808  : Super(name) {
2809  }
2810 
2811  explicit GeneratorOutput(const char *name)
2812  : GeneratorOutput(std::string(name)) {
2813  }
2814 
2815  explicit GeneratorOutput(size_t array_size, const std::string &name)
2816  : Super(array_size, name) {
2817  }
2818 
2819  explicit GeneratorOutput(const std::string &name, int d)
2820  : Super(name, d) {
2821  }
2822 
2823  explicit GeneratorOutput(const std::string &name, const Type &t)
2824  : Super(name, {t}) {
2825  }
2826 
2827  explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t)
2828  : Super(name, t) {
2829  }
2830 
2831  explicit GeneratorOutput(const std::string &name, const Type &t, int d)
2832  : Super(name, {t}, d) {
2833  }
2834 
2835  explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t, int d)
2836  : Super(name, t, d) {
2837  }
2838 
2839  explicit GeneratorOutput(size_t array_size, const std::string &name, int d)
2840  : Super(array_size, name, d) {
2841  }
2842 
2843  explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
2844  : Super(array_size, name, {t}) {
2845  }
2846 
2847  explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t)
2848  : Super(array_size, name, t) {
2849  }
2850 
2851  explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
2852  : Super(array_size, name, {t}, d) {
2853  }
2854 
2855  explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2856  : Super(array_size, name, t, d) {
2857  }
2858 
2859  // TODO: This used to take the buffer as a const ref. This no longer works as
2860  // using it in a Pipeline might change the dev field so it is currently
2861  // not considered const. We should consider how this really ought to work.
2862  template<typename T2, int D2>
2864  Super::operator=(buffer);
2865  return *this;
2866  }
2867 
2868  template<typename T2>
2870  Super::operator=(stub_output_buffer);
2871  return *this;
2872  }
2873 
2875  Super::operator=(f);
2876  return *this;
2877  }
2878 };
2879 
2880 namespace Internal {
2881 
2882 template<typename T>
2883 T parse_scalar(const std::string &value) {
2884  std::istringstream iss(value);
2885  T t;
2886  iss >> t;
2887  user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << value;
2888  return t;
2889 }
2890 
2891 std::vector<Type> parse_halide_type_list(const std::string &types);
2892 
2894  Dim,
2895  ArraySize };
2896 
2897 // This is a type of GeneratorParam used internally to create 'synthetic' params
2898 // (e.g. image.type, image.dim); it is not possible for user code to instantiate it.
2899 template<typename T>
2901 public:
2902  void set_from_string(const std::string &new_value_string) override {
2903  // If error_msg is not empty, this is unsettable:
2904  // display error_msg as a user error.
2905  if (!error_msg.empty()) {
2906  user_error << error_msg;
2907  }
2908  set_from_string_impl<T>(new_value_string);
2909  }
2910 
2911  std::string get_default_value() const override {
2913  return std::string();
2914  }
2915 
2916  std::string call_to_string(const std::string &v) const override {
2918  return std::string();
2919  }
2920 
2921  std::string get_c_type() const override {
2923  return std::string();
2924  }
2925 
2926  bool is_synthetic_param() const override {
2927  return true;
2928  }
2929 
2930 private:
2931  friend class GeneratorParamInfo;
2932 
2933  static std::unique_ptr<Internal::GeneratorParamBase> make(
2934  GeneratorBase *generator,
2935  const std::string &generator_name,
2936  const std::string &gpname,
2937  GIOBase &gio,
2938  SyntheticParamType which,
2939  bool defined) {
2940  std::string error_msg = defined ? "Cannot set the GeneratorParam " + gpname + " for " + generator_name + " because the value is explicitly specified in the C++ source." : "";
2941  return std::unique_ptr<GeneratorParam_Synthetic<T>>(
2942  new GeneratorParam_Synthetic<T>(gpname, gio, which, error_msg));
2943  }
2944 
2945  GeneratorParam_Synthetic(const std::string &name, GIOBase &gio, SyntheticParamType which, const std::string &error_msg = "")
2946  : GeneratorParamImpl<T>(name, T()), gio(gio), which(which), error_msg(error_msg) {
2947  }
2948 
2949  template<typename T2 = T, typename std::enable_if<std::is_same<T2, ::Halide::Type>::value>::type * = nullptr>
2950  void set_from_string_impl(const std::string &new_value_string) {
2952  gio.types_ = parse_halide_type_list(new_value_string);
2953  }
2954 
2955  template<typename T2 = T, typename std::enable_if<std::is_integral<T2>::value>::type * = nullptr>
2956  void set_from_string_impl(const std::string &new_value_string) {
2957  if (which == SyntheticParamType::Dim) {
2958  gio.dims_ = parse_scalar<T2>(new_value_string);
2959  } else if (which == SyntheticParamType::ArraySize) {
2960  gio.array_size_ = parse_scalar<T2>(new_value_string);
2961  } else {
2963  }
2964  }
2965 
2966  GIOBase &gio;
2967  const SyntheticParamType which;
2968  const std::string error_msg;
2969 };
2970 
2971 } // namespace Internal
2972 
2973 /** GeneratorContext is a class that is used when using Generators (or Stubs) directly;
2974  * it is used to allow the outer context (typically, either a Generator or "top-level" code)
2975  * to specify certain information to the inner context to ensure that inner and outer
2976  * Generators are compiled in a compatible way.
2977  *
2978  * If you are using this at "top level" (e.g. with the JIT), you can construct a GeneratorContext
2979  * with a Target:
2980  * \code
2981  * auto my_stub = MyStub(
2982  * GeneratorContext(get_target_from_environment()),
2983  * // inputs
2984  * { ... },
2985  * // generator params
2986  * { ... }
2987  * );
2988  * \endcode
2989  *
2990  * Note that all Generators embed a GeneratorContext, so if you are using a Stub
2991  * from within a Generator, you can just pass 'context()' for the GeneratorContext:
2992  * \code
2993  * struct SomeGen : Generator<SomeGen> {
2994  * void generate() {
2995  * ...
2996  * auto my_stub = MyStub(
2997  * context(), // GeneratorContext
2998  * // inputs
2999  * { ... },
3000  * // generator params
3001  * { ... }
3002  * );
3003  * ...
3004  * }
3005  * };
3006  * \endcode
3007  */
3009 public:
3011 
3012  explicit GeneratorContext(const Target &t);
3013  explicit GeneratorContext(const Target &t,
3015 
3016  GeneratorContext() = default;
3017  GeneratorContext(const GeneratorContext &) = default;
3018  GeneratorContext &operator=(const GeneratorContext &) = default;
3019  GeneratorContext(GeneratorContext &&) = default;
3021 
3022  const Target &target() const {
3023  return target_;
3024  }
3026  return autoscheduler_params_;
3027  }
3028 
3029  // Return a copy of this GeneratorContext that uses the given Target.
3030  // This method is rarely needed; it's really provided as a convenience
3031  // for use with init_from_context().
3032  GeneratorContext with_target(const Target &t) const;
3033 
3034  template<typename T>
3035  inline std::unique_ptr<T> create() const {
3036  return T::create(*this);
3037  }
3038  template<typename T, typename... Args>
3039  inline std::unique_ptr<T> apply(const Args &...args) const {
3040  auto t = this->create<T>();
3041  t->apply(args...);
3042  return t;
3043  }
3044 
3045 private:
3046  Target target_;
3047  AutoschedulerParams autoscheduler_params_;
3048 };
3049 
3051  // Names in this class are only intended for use in derived classes.
3052 protected:
3053  // Import a consistent list of Halide names that can be used in
3054  // Halide generators without qualification.
3073  using Var = Halide::Var;
3074  template<typename T>
3075  static Expr cast(Expr e) {
3076  return Halide::cast<T>(e);
3077  }
3078  static inline Expr cast(Halide::Type t, Expr e) {
3079  return Halide::cast(t, std::move(e));
3080  }
3081  template<typename T>
3083  template<typename T = void, int D = -1>
3085  template<typename T>
3087  static inline Type Bool(int lanes = 1) {
3088  return Halide::Bool(lanes);
3089  }
3090  static inline Type Float(int bits, int lanes = 1) {
3091  return Halide::Float(bits, lanes);
3092  }
3093  static inline Type Int(int bits, int lanes = 1) {
3094  return Halide::Int(bits, lanes);
3095  }
3096  static inline Type UInt(int bits, int lanes = 1) {
3097  return Halide::UInt(bits, lanes);
3098  }
3099 };
3100 
3101 namespace Internal {
3102 
3103 template<typename... Args>
3104 struct NoRealizations : std::false_type {};
3105 
3106 template<>
3107 struct NoRealizations<> : std::true_type {};
3108 
3109 template<typename T, typename... Args>
3110 struct NoRealizations<T, Args...> {
3111  static const bool value = !std::is_convertible<T, Realization>::value && NoRealizations<Args...>::value;
3112 };
3113 
3114 // Note that these functions must never return null:
3115 // if they cannot return a valid Generator, they must assert-fail.
3116 using GeneratorFactory = std::function<AbstractGeneratorPtr(const GeneratorContext &context)>;
3117 
3119  // names used across all params, inputs, and outputs.
3120  std::set<std::string> names;
3121 
3122  // Ordered-list of non-null ptrs to GeneratorParam<> fields.
3123  std::vector<Internal::GeneratorParamBase *> filter_generator_params;
3124 
3125  // Ordered-list of non-null ptrs to Input<> fields.
3126  std::vector<Internal::GeneratorInputBase *> filter_inputs;
3127 
3128  // Ordered-list of non-null ptrs to Output<> fields; empty if old-style Generator.
3129  std::vector<Internal::GeneratorOutputBase *> filter_outputs;
3130 
3131  // list of synthetic GP's that we dynamically created; this list only exists to simplify
3132  // lifetime management, and shouldn't be accessed directly outside of our ctor/dtor,
3133  // regardless of friend access.
3134  std::vector<std::unique_ptr<Internal::GeneratorParamBase>> owned_synthetic_params;
3135 
3136  // list of dynamically-added inputs and outputs, here only for lifetime management.
3137  std::vector<std::unique_ptr<Internal::GIOBase>> owned_extras;
3138 
3139 public:
3140  friend class GeneratorBase;
3141 
3142  GeneratorParamInfo(GeneratorBase *generator, size_t size);
3143 
3144  const std::vector<Internal::GeneratorParamBase *> &generator_params() const {
3145  return filter_generator_params;
3146  }
3147  const std::vector<Internal::GeneratorInputBase *> &inputs() const {
3148  return filter_inputs;
3149  }
3150  const std::vector<Internal::GeneratorOutputBase *> &outputs() const {
3151  return filter_outputs;
3152  }
3153 };
3154 
3156 public:
3157  ~GeneratorBase() override;
3158 
3159  /** Given a data type, return an estimate of the "natural" vector size
3160  * for that data type when compiling for the current target. */
3162  return get_target().natural_vector_size(t);
3163  }
3164 
3165  /** Given a data type, return an estimate of the "natural" vector size
3166  * for that data type when compiling for the current target. */
3167  template<typename data_t>
3168  int natural_vector_size() const {
3169  return get_target().natural_vector_size<data_t>();
3170  }
3171 
3172  /**
3173  * set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler
3174  * in many cases, as it constructs the relevant entries for the vector for you, which
3175  * is often a bit unintuitive at present. The arguments are passed in Input<>-declaration-order,
3176  * and the types must be compatible. Array inputs are passed as std::vector<> of the relevant type.
3177  *
3178  * Note: at present, scalar input types must match *exactly*, i.e., for Input<uint8_t>, you
3179  * must pass an argument that is actually uint8_t; an argument that is int-that-will-fit-in-uint8
3180  * will assert-fail at Halide compile time.
3181  */
3182  template<typename... Args>
3183  void set_inputs(const Args &...args) {
3184  // set_inputs_vector() checks this too, but checking it here allows build_inputs() to avoid out-of-range checks.
3185  GeneratorParamInfo &pi = this->param_info();
3186  user_assert(sizeof...(args) == pi.inputs().size())
3187  << "Expected exactly " << pi.inputs().size()
3188  << " inputs but got " << sizeof...(args) << "\n";
3189  set_inputs_vector(build_inputs(std::forward_as_tuple<const Args &...>(args...), std::make_index_sequence<sizeof...(Args)>{}));
3190  }
3191 
3192  Realization realize(std::vector<int32_t> sizes) {
3193  this->check_scheduled("realize");
3194  return get_pipeline().realize(std::move(sizes), get_target());
3195  }
3196 
3197  // Only enable if none of the args are Realization; otherwise we can incorrectly
3198  // select this method instead of the Realization-as-outparam variant
3199  template<typename... Args, typename std::enable_if<NoRealizations<Args...>::value>::type * = nullptr>
3200  Realization realize(Args &&...args) {
3201  this->check_scheduled("realize");
3202  return get_pipeline().realize(std::forward<Args>(args)..., get_target());
3203  }
3204 
3206  this->check_scheduled("realize");
3208  }
3209 
3210  // Return the Pipeline that has been built by the generate() method.
3211  // This method can only be called from the schedule() method.
3212  // (This may be relaxed in the future to allow calling from generate() as
3213  // long as all Outputs have been defined.)
3215 
3216  // Create Input<Func> with dynamic type & dimensions
3217  template<typename T,
3218  typename std::enable_if<std::is_same<T, Halide::Func>::value>::type * = nullptr>
3219  GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3221  auto *p = new GeneratorInput<T>(name, t, dimensions);
3222  p->generator = this;
3223  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3224  param_info_ptr->filter_inputs.push_back(p);
3225  return p;
3226  }
3227 
3228  // Create Input<Buffer> with dynamic type & dimensions
3229  template<typename T,
3230  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3231  GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3232  static_assert(!T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is void or omitted .");
3233  static_assert(!T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is -1 or omitted.");
3235  auto *p = new GeneratorInput<T>(name, t, dimensions);
3236  p->generator = this;
3237  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3238  param_info_ptr->filter_inputs.push_back(p);
3239  return p;
3240  }
3241 
3242  // Create Input<Buffer> with compile-time type
3243  template<typename T,
3244  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3245  GeneratorInput<T> *add_input(const std::string &name, int dimensions) {
3246  static_assert(T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is not void.");
3247  static_assert(!T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is -1 or omitted.");
3249  auto *p = new GeneratorInput<T>(name, dimensions);
3250  p->generator = this;
3251  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3252  param_info_ptr->filter_inputs.push_back(p);
3253  return p;
3254  }
3255 
3256  // Create Input<Buffer> with compile-time type & dimensions
3257  template<typename T,
3258  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3259  GeneratorInput<T> *add_input(const std::string &name) {
3260  static_assert(T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is not void.");
3261  static_assert(T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is not -1.");
3263  auto *p = new GeneratorInput<T>(name);
3264  p->generator = this;
3265  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3266  param_info_ptr->filter_inputs.push_back(p);
3267  return p;
3268  }
3269  // Create Input<scalar>
3270  template<typename T,
3271  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3272  GeneratorInput<T> *add_input(const std::string &name) {
3274  auto *p = new GeneratorInput<T>(name);
3275  p->generator = this;
3276  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3277  param_info_ptr->filter_inputs.push_back(p);
3278  return p;
3279  }
3280  // Create Input<Expr> with dynamic type
3281  template<typename T,
3282  typename std::enable_if<std::is_same<T, Expr>::value>::type * = nullptr>
3283  GeneratorInput<T> *add_input(const std::string &name, const Type &type) {
3285  auto *p = new GeneratorInput<Expr>(name);
3286  p->generator = this;
3287  p->set_type(type);
3288  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3289  param_info_ptr->filter_inputs.push_back(p);
3290  return p;
3291  }
3292 
3293  // Create Output<Func> with dynamic type & dimensions
3294  template<typename T,
3295  typename std::enable_if<std::is_same<T, Halide::Func>::value>::type * = nullptr>
3296  GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3298  auto *p = new GeneratorOutput<T>(name, t, dimensions);
3299  p->generator = this;
3300  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3301  param_info_ptr->filter_outputs.push_back(p);
3302  return p;
3303  }
3304 
3305  // Create Output<Buffer> with dynamic type & dimensions
3306  template<typename T,
3307  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3308  GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3309  static_assert(!T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is void or omitted .");
3310  static_assert(!T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is -1 or omitted.");
3312  auto *p = new GeneratorOutput<T>(name, t, dimensions);
3313  p->generator = this;
3314  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3315  param_info_ptr->filter_outputs.push_back(p);
3316  return p;
3317  }
3318 
3319  // Create Output<Buffer> with compile-time type
3320  template<typename T,
3321  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3322  GeneratorOutput<T> *add_output(const std::string &name, int dimensions) {
3323  static_assert(T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is not void.");
3324  static_assert(!T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is -1 or omitted.");
3326  auto *p = new GeneratorOutput<T>(name, dimensions);
3327  p->generator = this;
3328  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3329  param_info_ptr->filter_outputs.push_back(p);
3330  return p;
3331  }
3332 
3333  // Create Output<Buffer> with compile-time type & dimensions
3334  template<typename T,
3335  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3336  GeneratorOutput<T> *add_output(const std::string &name) {
3337  static_assert(T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is not void.");
3338  static_assert(T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is not -1.");
3340  auto *p = new GeneratorOutput<T>(name);
3341  p->generator = this;
3342  param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3343  param_info_ptr->filter_outputs.push_back(p);
3344  return p;
3345  }
3346 
3347  void add_requirement(const Expr &condition, const std::vector<Expr> &error_args);
3348 
3349  template<typename... Args,
3350  typename = typename std::enable_if<Internal::all_are_printable_args<Args...>::value>::type>
3351  inline HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args) {
3352  std::vector<Expr> collected_args;
3353  Internal::collect_print_args(collected_args, std::forward<Args>(error_args)...);
3354  add_requirement(condition, collected_args);
3355  }
3356 
3359  }
3360 
3361 protected:
3362  GeneratorBase(size_t size, const void *introspection_helper);
3363  void set_generator_names(const std::string &registered_name, const std::string &stub_name);
3364 
3365  // Note that it is explicitly legal to override init_from_context(), so that you can (say)
3366  // create a modified context with a different Target (eg with features enabled or disabled), but...
3367  //
3368  // *** WARNING ***
3369  //
3370  // Modifying the context here can be fraught with subtle hazards, especially when used
3371  // in conjunction with compiling to multitarget output. Adding or removing Feature
3372  // flags could break your build (if you are lucky), or cause subtle runtime failures (if unlucky)...
3373  //
3374  // e.g. in the latter case, say you decided to enable AVX512_SapphireRapids as an experiment,
3375  // and override init_from_context() to do just that. You'd end up being crashy on pre-AVX512
3376  // hardware, because the code that Halide injects to do runtime CPU feature detection at runtime
3377  // doesn't know it needs to do the runtime detection for this flag.
3378  //
3379  // Even if you are using multitarget output, using this as a 'hook' to enable or disable Features
3380  // can produce hard-to-maintain code in the long term: Halide has dozens of feature flags now,
3381  // many of which are orthogonal to each other and/or specific to a certain architecture
3382  // (or sub-architecture). The interaction between 'orthogonal' flags like this is essentially
3383  // Undefined Behavior (e.g. if I enable the SSE41 Feature on a Target where arch = RISCV, what happens?
3384  // Is it ignored? Does it fail to compile? Something else?). The point here is that adding Features
3385  // here may end up eventually getting added to a Target you didn't anticipate and have adverse consequences.
3386  //
3387  // With all that in mind, here are some guidelines we think will make long-term code maintenance
3388  // less painful for you:
3389  //
3390  // - Override this method *only* for temporary debugging purposes; e.g. if you
3391  // need to add the `profile` feature to a specific Generator, but your build system doesn't easily
3392  // let you specify per-Generator target features, this is the right tool for the job.
3393  //
3394  // - If your build system makes it infeasible to customize the build Target in a reasonable way,
3395  // it may be appropriate to permanently override this method to enable specific Features for
3396  // specific Generators (e.g., enabling `strict_float` is a likely example). In that case,
3397  // we would suggest:
3398  //
3399  // - *NEVER* change the arch/bits/os of the Target.
3400  // - Only add Features; don't remove Features.
3401  // - For Features that are architecture-specific, always check the arch/bits/os
3402  // of the Target to be sure it's what you expect... e.g. if you are enabling
3403  // AVX512, only do so if compiling for an x86-64 Target. Even if your code
3404  // doesn't target any other architecture at the present time, Future You will be
3405  // happier.
3406  // - If you mutate a target conditionally based on the incoming target, try to do so
3407  // so based only on the Target's arch/bits/os, and not at the Features set on the target.
3408  // If examining Features is unavoidable (e.g. enable $FOO only if $BAR is enabled),
3409  // do so as conservatively as possible, and always validate that the rest of the Target
3410  // is sensible for what you are doing.
3411  //
3412  // Furthermore, if you override this, please don't try to directly set the `target` (etc) GeneratorParams
3413  // directly; instead, construct the new GeneratorContext you want and call the superclass
3414  // implementation of init_from_context.
3415  //
3416  // TL;DR: overrides to this method should probably never be checked in to your source control system
3417  // (rather, the override should be temporary and local, for experimentation). If you must check in
3418  // overrides to this method, be paranoid that the Target you get could be something you don't expect.
3419  //
3420  virtual void init_from_context(const Halide::GeneratorContext &context);
3421 
3422  virtual void call_configure() = 0;
3423  virtual void call_generate() = 0;
3424  virtual void call_schedule() = 0;
3425 
3426  void pre_build();
3427  void post_build();
3428  void pre_configure();
3429  void post_configure();
3430  void pre_generate();
3431  void post_generate();
3432  void pre_schedule();
3433  void post_schedule();
3434 
3435  template<typename T>
3437 
3438  template<typename T>
3440 
3441  // A Generator's creation and usage must go in a certain phase to ensure correctness;
3442  // the state machine here is advanced and checked at various points to ensure
3443  // this is the case.
3444  enum Phase {
3445  // Generator has just come into being.
3447 
3448  // Generator has had its configure() method called. (For Generators without
3449  // a configure() method, this phase will be skipped and will advance
3450  // directly to InputsSet.)
3452 
3453  // All Input<>/Param<> fields have been set. (Applicable only in JIT mode;
3454  // in AOT mode, this can be skipped, going Created->GenerateCalled directly.)
3456 
3457  // Generator has had its generate() method called.
3459 
3460  // Generator has had its schedule() method (if any) called.
3462  } phase{Created};
3463 
3464  void check_exact_phase(Phase expected_phase) const;
3465  void check_min_phase(Phase expected_phase) const;
3466  void advance_phase(Phase new_phase);
3467 
3469 
3470  Target get_target() const {
3471  return target;
3472  }
3473  bool using_autoscheduler() const {
3474  return !autoscheduler_.value().name.empty();
3475  }
3476 
3477  // These must remain here for legacy code that access the fields directly.
3480 
3481 private:
3483  friend class GeneratorParamBase;
3484  friend class GIOBase;
3485  friend class GeneratorInputBase;
3486  friend class GeneratorOutputBase;
3487  friend class GeneratorParamInfo;
3488  friend class StubOutputBufferBase;
3489 
3490  const size_t size;
3491 
3492  // Lazily-allocated-and-inited struct with info about our various Params.
3493  // Do not access directly: use the param_info() getter.
3494  std::unique_ptr<GeneratorParamInfo> param_info_ptr;
3495 
3496  std::string generator_registered_name, generator_stub_name;
3497  Pipeline pipeline;
3498 
3499  struct Requirement {
3500  Expr condition;
3501  std::vector<Expr> error_args;
3502  };
3503  std::vector<Requirement> requirements;
3504 
3505  // Return our GeneratorParamInfo.
3506  GeneratorParamInfo &param_info();
3507 
3508  template<typename T>
3509  T *find_by_name(const std::string &name, const std::vector<T *> &v) {
3510  for (T *t : v) {
3511  if (t->name() == name) {
3512  return t;
3513  }
3514  }
3515  return nullptr;
3516  }
3517 
3518  Internal::GeneratorInputBase *find_input_by_name(const std::string &name);
3519  Internal::GeneratorOutputBase *find_output_by_name(const std::string &name);
3520 
3521  void check_scheduled(const char *m) const;
3522 
3523  void build_params(bool force = false);
3524 
3525  // Provide private, unimplemented, wrong-result-type methods here
3526  // so that Generators don't attempt to call the global methods
3527  // of the same name by accident: use the get_target() method instead.
3528  void get_host_target();
3529  void get_jit_target_from_environment();
3530  void get_target_from_environment();
3531 
3532  void set_inputs_vector(const std::vector<std::vector<StubInput>> &inputs);
3533 
3534  static void check_input_is_singular(Internal::GeneratorInputBase *in);
3535  static void check_input_is_array(Internal::GeneratorInputBase *in);
3536  static void check_input_kind(Internal::GeneratorInputBase *in, Internal::ArgInfoKind kind);
3537 
3538  // Allow Buffer<> if:
3539  // -- we are assigning it to an Input<Buffer<>> (with compatible type and dimensions),
3540  // causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
3541  // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Buffer<>.
3542  template<typename T, int Dims>
3543  std::vector<StubInput> build_input(size_t i, const Buffer<T, Dims> &arg) {
3544  auto *in = param_info().inputs().at(i);
3545  check_input_is_singular(in);
3546  const auto k = in->kind();
3547  if (k == Internal::ArgInfoKind::Buffer) {
3548  Halide::Buffer<> b = arg;
3549  StubInputBuffer<> sib(b);
3550  StubInput si(sib);
3551  return {si};
3552  } else if (k == Internal::ArgInfoKind::Function) {
3553  Halide::Func f(arg.name() + "_im");
3554  f(Halide::_) = arg(Halide::_);
3555  StubInput si(f);
3556  return {si};
3557  } else {
3558  check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3559  return {};
3560  }
3561  }
3562 
3563  // Allow Input<Buffer<>> if:
3564  // -- we are assigning it to another Input<Buffer<>> (with compatible type and dimensions),
3565  // allowing us to simply pipe a parameter from an enclosing Generator to the Invoker.
3566  // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Input<Buffer<>>.
3567  template<typename T, int Dims>
3568  std::vector<StubInput> build_input(size_t i, const GeneratorInput<Buffer<T, Dims>> &arg) {
3569  auto *in = param_info().inputs().at(i);
3570  check_input_is_singular(in);
3571  const auto k = in->kind();
3572  if (k == Internal::ArgInfoKind::Buffer) {
3573  StubInputBuffer<> sib = arg;
3574  StubInput si(sib);
3575  return {si};
3576  } else if (k == Internal::ArgInfoKind::Function) {
3577  Halide::Func f = arg.funcs().at(0);
3578  StubInput si(f);
3579  return {si};
3580  } else {
3581  check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3582  return {};
3583  }
3584  }
3585 
3586  // Allow Func iff we are assigning it to an Input<Func> (with compatible type and dimensions).
3587  std::vector<StubInput> build_input(size_t i, const Func &arg) {
3588  auto *in = param_info().inputs().at(i);
3589  check_input_kind(in, Internal::ArgInfoKind::Function);
3590  check_input_is_singular(in);
3591  const Halide::Func &f = arg;
3592  StubInput si(f);
3593  return {si};
3594  }
3595 
3596  // Allow vector<Func> iff we are assigning it to an Input<Func[]> (with compatible type and dimensions).
3597  std::vector<StubInput> build_input(size_t i, const std::vector<Func> &arg) {
3598  auto *in = param_info().inputs().at(i);
3599  check_input_kind(in, Internal::ArgInfoKind::Function);
3600  check_input_is_array(in);
3601  // My kingdom for a list comprehension...
3602  std::vector<StubInput> siv;
3603  siv.reserve(arg.size());
3604  for (const auto &f : arg) {
3605  siv.emplace_back(f);
3606  }
3607  return siv;
3608  }
3609 
3610  // Expr must be Input<Scalar>.
3611  std::vector<StubInput> build_input(size_t i, const Expr &arg) {
3612  auto *in = param_info().inputs().at(i);
3613  check_input_kind(in, Internal::ArgInfoKind::Scalar);
3614  check_input_is_singular(in);
3615  StubInput si(arg);
3616  return {si};
3617  }
3618 
3619  // (Array form)
3620  std::vector<StubInput> build_input(size_t i, const std::vector<Expr> &arg) {
3621  auto *in = param_info().inputs().at(i);
3622  check_input_kind(in, Internal::ArgInfoKind::Scalar);
3623  check_input_is_array(in);
3624  std::vector<StubInput> siv;
3625  siv.reserve(arg.size());
3626  for (const auto &value : arg) {
3627  siv.emplace_back(value);
3628  }
3629  return siv;
3630  }
3631 
3632  // Any other type must be convertible to Expr and must be associated with an Input<Scalar>.
3633  // Use is_arithmetic since some Expr conversions are explicit.
3634  template<typename T,
3635  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3636  std::vector<StubInput> build_input(size_t i, const T &arg) {
3637  auto *in = param_info().inputs().at(i);
3638  check_input_kind(in, Internal::ArgInfoKind::Scalar);
3639  check_input_is_singular(in);
3640  // We must use an explicit Expr() ctor to preserve the type
3641  Expr e(arg);
3642  StubInput si(e);
3643  return {si};
3644  }
3645 
3646  // (Array form)
3647  template<typename T,
3648  typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3649  std::vector<StubInput> build_input(size_t i, const std::vector<T> &arg) {
3650  auto *in = param_info().inputs().at(i);
3651  check_input_kind(in, Internal::ArgInfoKind::Scalar);
3652  check_input_is_array(in);
3653  std::vector<StubInput> siv;
3654  siv.reserve(arg.size());
3655  for (const auto &value : arg) {
3656  // We must use an explicit Expr() ctor to preserve the type;
3657  // otherwise, implicit conversions can downgrade (e.g.) float -> int
3658  Expr e(value);
3659  siv.emplace_back(e);
3660  }
3661  return siv;
3662  }
3663 
3664  template<typename... Args, size_t... Indices>
3665  std::vector<std::vector<StubInput>> build_inputs(const std::tuple<const Args &...> &t, std::index_sequence<Indices...>) {
3666  return {build_input(Indices, std::get<Indices>(t))...};
3667  }
3668 
3669  // Note that this deliberately ignores inputs/outputs with multiple array values
3670  // (ie, one name per input or output, regardless of array_size())
3671  template<typename T>
3672  static void get_arguments(std::vector<AbstractGenerator::ArgInfo> &args, ArgInfoDirection dir, const T &t) {
3673  for (auto *e : t) {
3674  args.push_back({e->name(),
3675  dir,
3676  e->kind(),
3677  e->gio_types_defined() ? e->gio_types() : std::vector<Type>{},
3678  e->dims_defined() ? e->dims() : 0});
3679  }
3680  }
3681 
3682 public:
3683  // AbstractGenerator methods
3684  std::string name() override;
3685  GeneratorContext context() const override;
3686  std::vector<ArgInfo> arginfos() override;
3687  bool allow_out_of_order_inputs_and_outputs() const override;
3688 
3689  void set_generatorparam_value(const std::string &name, const std::string &value) override;
3690  void set_generatorparam_value(const std::string &name, const LoopLevel &loop_level) override;
3691 
3692  std::vector<Parameter> input_parameter(const std::string &name) override;
3693  std::vector<Func> output_func(const std::string &name) override;
3694 
3695  // This is overridden in the concrete Generator<> subclass.
3696  // Pipeline build_pipeline() override;
3697 
3698  void bind_input(const std::string &name, const std::vector<Parameter> &v) override;
3699  void bind_input(const std::string &name, const std::vector<Func> &v) override;
3700  void bind_input(const std::string &name, const std::vector<Expr> &v) override;
3701 
3702  bool emit_cpp_stub(const std::string &stub_file_path) override;
3703  bool emit_hlpipe(const std::string &hlpipe_file_path) override;
3704 
3705  GeneratorBase(const GeneratorBase &) = delete;
3706  GeneratorBase &operator=(const GeneratorBase &) = delete;
3707  GeneratorBase(GeneratorBase &&that) = delete;
3708  GeneratorBase &operator=(GeneratorBase &&that) = delete;
3709 };
3710 
3712 public:
3713  static void register_factory(const std::string &name, GeneratorFactory generator_factory);
3714  static void unregister_factory(const std::string &name);
3715  static std::vector<std::string> enumerate();
3716  // This method returns nullptr if it cannot return a valid Generator;
3717  // the caller is responsible for checking the result.
3718  static AbstractGeneratorPtr create(const std::string &name,
3719  const Halide::GeneratorContext &context);
3720 
3721 private:
3722  using GeneratorFactoryMap = std::map<const std::string, GeneratorFactory>;
3723 
3724  GeneratorFactoryMap factories;
3725  std::mutex mutex;
3726 
3727  static GeneratorRegistry &get_registry();
3728 
3729  GeneratorRegistry() = default;
3730 
3731 public:
3732  GeneratorRegistry(const GeneratorRegistry &) = delete;
3733  GeneratorRegistry &operator=(const GeneratorRegistry &) = delete;
3734  GeneratorRegistry(GeneratorRegistry &&that) = delete;
3735  GeneratorRegistry &operator=(GeneratorRegistry &&that) = delete;
3736 };
3737 
3738 } // namespace Internal
3739 
3740 template<class T>
3742 protected:
3744  : Internal::GeneratorBase(sizeof(T),
3745  Internal::Introspection::get_introspection_helper<T>()) {
3746  }
3747 
3748 public:
3749  static std::unique_ptr<T> create(const Halide::GeneratorContext &context) {
3750  // We must have an object of type T (not merely GeneratorBase) to call a protected method,
3751  // because CRTP is a weird beast.
3752  auto g = std::make_unique<T>();
3753  g->init_from_context(context);
3754  return g;
3755  }
3756 
3757  // This is public but intended only for use by the HALIDE_REGISTER_GENERATOR() macro.
3758  static std::unique_ptr<T> create(const Halide::GeneratorContext &context,
3759  const std::string &registered_name,
3760  const std::string &stub_name) {
3761  auto g = create(context);
3762  g->set_generator_names(registered_name, stub_name);
3763  return g;
3764  }
3765 
3766  template<typename... Args>
3767  void apply(const Args &...args) {
3768  call_configure();
3769  set_inputs(args...);
3770  call_generate();
3771  call_schedule();
3772  }
3773 
3774  template<typename T2>
3775  std::unique_ptr<T2> create() const {
3776  return T2::create(context());
3777  }
3778 
3779  template<typename T2, typename... Args>
3780  inline std::unique_ptr<T2> apply(const Args &...args) const {
3781  auto t = this->create<T2>();
3782  t->apply(args...);
3783  return t;
3784  }
3785 
3786 private:
3787  // std::is_member_function_pointer will fail if there is no member of that name,
3788  // so we use a little SFINAE to detect if there are method-shaped members.
3789  template<typename>
3790  struct type_sink {
3791  typedef void type;
3792  };
3793 
3794  template<typename T2, typename = void>
3795  struct has_configure_method : std::false_type {};
3796 
3797  template<typename T2>
3798  struct has_configure_method<T2, typename type_sink<decltype(std::declval<T2>().configure())>::type> : std::true_type {};
3799 
3800  template<typename T2, typename = void>
3801  struct has_generate_method : std::false_type {};
3802 
3803  template<typename T2>
3804  struct has_generate_method<T2, typename type_sink<decltype(std::declval<T2>().generate())>::type> : std::true_type {};
3805 
3806  template<typename T2, typename = void>
3807  struct has_schedule_method : std::false_type {};
3808 
3809  template<typename T2>
3810  struct has_schedule_method<T2, typename type_sink<decltype(std::declval<T2>().schedule())>::type> : std::true_type {};
3811 
3812  Pipeline build_pipeline_impl() {
3813  T *t = (T *)this;
3814  // No: configure() must be called prior to this
3815  // (and in fact, prior to calling set_inputs).
3816  //
3817  // t->call_configure_impl();
3818 
3819  t->call_generate_impl();
3820  t->call_schedule_impl();
3821  return get_pipeline();
3822  }
3823 
3824  void call_configure_impl() {
3825  pre_configure();
3826  if constexpr (has_configure_method<T>::value) {
3827  T *t = (T *)this;
3828  static_assert(std::is_void<decltype(t->configure())>::value, "configure() must return void");
3829  t->configure();
3830  }
3831  post_configure();
3832  }
3833 
3834  void call_generate_impl() {
3835  pre_generate();
3836  static_assert(has_generate_method<T>::value, "Expected a generate() method here.");
3837  T *t = (T *)this;
3838  static_assert(std::is_void<decltype(t->generate())>::value, "generate() must return void");
3839  t->generate();
3840  post_generate();
3841  }
3842 
3843  void call_schedule_impl() {
3844  pre_schedule();
3845  if constexpr (has_schedule_method<T>::value) {
3846  T *t = (T *)this;
3847  static_assert(std::is_void<decltype(t->schedule())>::value, "schedule() must return void");
3848  t->schedule();
3849  }
3850  post_schedule();
3851  }
3852 
3853 protected:
3856  return this->build_pipeline_impl();
3857  }
3858 
3859  void call_configure() override {
3860  this->call_configure_impl();
3861  }
3862 
3863  void call_generate() override {
3864  this->call_generate_impl();
3865  }
3866 
3867  void call_schedule() override {
3868  this->call_schedule_impl();
3869  }
3870 
3871 private:
3874  friend class ::Halide::GeneratorContext;
3875 
3876 public:
3877  Generator(const Generator &) = delete;
3878  Generator &operator=(const Generator &) = delete;
3879  Generator(Generator &&that) = delete;
3880  Generator &operator=(Generator &&that) = delete;
3881 };
3882 
3883 namespace Internal {
3884 
3886 public:
3887  RegisterGenerator(const char *registered_name, GeneratorFactory generator_factory);
3888 };
3889 
3890 // -----------------------------
3891 
3892 /** ExecuteGeneratorArgs is the set of arguments to execute_generator().
3893  */
3895  // Output directory for all files generated. Must not be empty.
3896  std::string output_dir;
3897 
3898  // Type(s) of outputs to produce. Must not be empty.
3899  std::set<OutputFileType> output_types;
3900 
3901  // Target(s) to use when generating. Must not be empty.
3902  // If list contains multiple entries, a multitarget output will be produced.
3903  std::vector<Target> targets;
3904 
3905  // When generating multitarget output, use these as the suffixes for each Target
3906  // specified by the targets field. If empty, the canonical string form of
3907  // each Target will be used. If nonempty, it must be the same length as the
3908  // targets vector.
3909  std::vector<std::string> suffixes;
3910 
3911  // Name of the generator to execute (or empty if none, e.g. if generating a runtime)
3912  // Must be one recognized by the specified GeneratorFactoryProvider.
3913  std::string generator_name;
3914 
3915  // Name to use for the generated function. May include C++ namespaces,
3916  // e.g. "HalideTest::AnotherNamespace::cxx_mangling". If empty, use `generator_name`.
3917  std::string function_name;
3918 
3919  // Base filename for all outputs (differentated by file extension).
3920  // If empty, use `function_name` (ignoring any C++ namespaces).
3921  std::string file_base_name;
3922 
3923  // The name of a standalone runtime to generate. Only honors EMIT_OPTIONS 'o'
3924  // and 'static_library'. When multiple targets are specified, it picks a
3925  // runtime that is compatible with all of the targets, or fails if it cannot
3926  // find one. Flags across all of the targets that do not affect runtime code
3927  // generation, such as `no_asserts` and `no_runtime`, are ignored.
3928  std::string runtime_name;
3929 
3930  // The mode in which to build the Generator.
3931  enum BuildMode {
3932  // Build it as written.
3934 
3935  // Build a version suitable for using for gradient descent calculation.
3937  } build_mode = Default;
3938 
3939  // The fn that will produce Generator(s) from the name specified.
3940  // (Note that `generator_name` is the only value that will ever be passed
3941  // for name here; it is provided for ease of interoperation with existing code.)
3942  //
3943  // If null, the default global registry of Generators will be used.
3944  using CreateGeneratorFn = std::function<AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)>;
3946 
3947  // Values to substitute for GeneratorParams in the selected Generator.
3948  // Should not contain `target`.
3949  //
3950  // If any of the generator param names specified in this map are unknown
3951  // to the Generator created, an error will occur.
3953 
3954  // Compiler Logger to use, for diagnostic work. If null, don't do any logging.
3956 
3957  // If true, log the path of all output files to stdout.
3958  bool log_outputs = false;
3959 };
3960 
3961 /**
3962  * Execute a Generator for AOT compilation -- this provides the implementation of
3963  * the command-line Generator interface `generate_filter_main()`, but with a structured
3964  * API that is more suitable for calling directly from code (vs command line).
3965  */
3966 void execute_generator(const ExecuteGeneratorArgs &args);
3967 
3968 // -----------------------------
3969 
3970 } // namespace Internal
3971 
3972 /** Create a Generator from the currently-registered Generators, use it to create a Callable.
3973  * Any GeneratorParams specified will be applied to the Generator before compilation.
3974  * If the name isn't registered, assert-fail. */
3975 // @{
3977  const std::string &name,
3978  const GeneratorParamsMap &generator_params = {});
3979 Callable create_callable_from_generator(const Target &target,
3980  const std::string &name,
3981  const GeneratorParamsMap &generator_params = {});
3982 // @}
3983 
3984 } // namespace Halide
3985 
3986 // Define this namespace at global scope so that anonymous namespaces won't
3987 // defeat our static_assert check; define a dummy type inside so we can
3988 // check for type aliasing injected by anonymous namespace usage
3990 struct halide_global_ns;
3991 };
3992 
3993 #define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
3994  namespace halide_register_generator { \
3995  struct halide_global_ns; \
3996  namespace GEN_REGISTRY_NAME##_ns { \
3997  std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
3998  std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
3999  using GenType = std::remove_pointer<decltype(new GEN_CLASS_NAME)>::type; /* NOLINT(bugprone-macro-parentheses) */ \
4000  return GenType::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
4001  } \
4002  } \
4003  namespace { \
4004  auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4005  } \
4006  } \
4007  static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
4008  "HALIDE_REGISTER_GENERATOR must be used at global scope");
4009 
4010 #define _HALIDE_REGISTER_GENERATOR2(GEN_CLASS_NAME, GEN_REGISTRY_NAME) \
4011  _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, GEN_REGISTRY_NAME)
4012 
4013 #define _HALIDE_REGISTER_GENERATOR3(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
4014  _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME)
4015 
4016 // MSVC has a broken implementation of variadic macros: it expands __VA_ARGS__
4017 // as a single token in argument lists (rather than multiple tokens).
4018 // Jump through some hoops to work around this.
4019 #define __HALIDE_REGISTER_ARGCOUNT_IMPL(_1, _2, _3, COUNT, ...) \
4020  COUNT
4021 
4022 #define _HALIDE_REGISTER_ARGCOUNT_IMPL(ARGS) \
4023  __HALIDE_REGISTER_ARGCOUNT_IMPL ARGS
4024 
4025 #define _HALIDE_REGISTER_ARGCOUNT(...) \
4026  _HALIDE_REGISTER_ARGCOUNT_IMPL((__VA_ARGS__, 3, 2, 1, 0))
4027 
4028 #define ___HALIDE_REGISTER_CHOOSER(COUNT) \
4029  _HALIDE_REGISTER_GENERATOR##COUNT
4030 
4031 #define __HALIDE_REGISTER_CHOOSER(COUNT) \
4032  ___HALIDE_REGISTER_CHOOSER(COUNT)
4033 
4034 #define _HALIDE_REGISTER_CHOOSER(COUNT) \
4035  __HALIDE_REGISTER_CHOOSER(COUNT)
4036 
4037 #define _HALIDE_REGISTER_GENERATOR_PASTE(A, B) \
4038  A B
4039 
4040 #define HALIDE_REGISTER_GENERATOR(...) \
4041  _HALIDE_REGISTER_GENERATOR_PASTE(_HALIDE_REGISTER_CHOOSER(_HALIDE_REGISTER_ARGCOUNT(__VA_ARGS__)), (__VA_ARGS__))
4042 
4043 // HALIDE_REGISTER_GENERATOR_ALIAS() can be used to create an an alias-with-a-particular-set-of-param-values
4044 // for a given Generator in the build system. Normally, you wouldn't want to do this;
4045 // however, some existing Halide clients have build systems that make it challenging to
4046 // specify GeneratorParams inside the build system, and this allows a somewhat simpler
4047 // customization route for them. It's highly recommended you don't use this for new code.
4048 //
4049 // The final argument is really an initializer-list of GeneratorParams, in the form
4050 // of an initializer-list for map<string, string>:
4051 //
4052 // { { "gp-name", "gp-value"} [, { "gp2-name", "gp2-value" }] }
4053 //
4054 // It is specified as a variadic template argument to allow for the fact that the embedded commas
4055 // would otherwise confuse the preprocessor; since (in this case) all we're going to do is
4056 // pass it thru as-is, this is fine (and even MSVC's 'broken' __VA_ARGS__ should be OK here).
4057 #define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
4058  namespace halide_register_generator { \
4059  struct halide_global_ns; \
4060  namespace ORIGINAL_REGISTRY_NAME##_ns { \
4061  std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4062  } \
4063  namespace GEN_REGISTRY_NAME##_ns { \
4064  std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
4065  auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
4066  const Halide::GeneratorParamsMap m = __VA_ARGS__; \
4067  g->set_generatorparam_values(m); \
4068  return g; \
4069  } \
4070  } \
4071  namespace { \
4072  auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4073  } \
4074  } \
4075  static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
4076  "HALIDE_REGISTER_GENERATOR_ALIAS must be used at global scope");
4077 
4078 // The HALIDE_GENERATOR_PYSTUB macro is used to produce "PyStubs" -- i.e., CPython wrappers to let a C++ Generator
4079 // be called from Python. It shouldn't be necessary to use by anything but the build system in most cases.
4080 
4081 #define HALIDE_GENERATOR_PYSTUB(GEN_REGISTRY_NAME, MODULE_NAME) \
4082  static_assert(PY_MAJOR_VERSION >= 3, "Python bindings for Halide require Python 3+"); \
4083  extern "C" PyObject *_halide_pystub_impl(const char *module_name, const Halide::Internal::GeneratorFactory &factory); \
4084  namespace halide_register_generator::GEN_REGISTRY_NAME##_ns { \
4085  extern std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4086  } \
4087  extern "C" HALIDE_EXPORT_SYMBOL PyObject *PyInit_##MODULE_NAME() { \
4088  const auto factory = halide_register_generator::GEN_REGISTRY_NAME##_ns::factory; \
4089  return _halide_pystub_impl(#MODULE_NAME, factory); \
4090  }
4091 
4092 #endif // HALIDE_GENERATOR_H_
GeneratorOutput(const std::string &name, const Type &t, int d)
Definition: Generator.h:2831
std::vector< Range > Region
A multi-dimensional box.
Definition: Expr.h:345
GeneratorInput_Buffer< T > & set_estimates(const Region &estimates)
Definition: Generator.h:1749
std::vector< Func > output_func(const std::string &name) override
Given the name of an output, return the Func(s) for that output.
std::vector< Type > parse_halide_type_list(const std::string &types)
enum Halide::Internal::ExecuteGeneratorArgs::BuildMode build_mode
Create a small array of Exprs for defining and calling functions with multiple outputs.
Definition: Tuple.h:18
void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent)
GeneratorOutput_Arithmetic(const std::string &name)
Definition: Generator.h:2777
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:3183
void set_type(const Type &type)
Expr max(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:606
const std::vector< Func > & funcs() const
GeneratorOutput< T > & operator=(Buffer< T2, D2 > &buffer)
Definition: Generator.h:2863
StubInput(const Parameter &p)
Definition: Generator.h:1396
GeneratorOutput(const std::string &name, const std::vector< Type > &t)
Definition: Generator.h:2827
#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE)
Definition: Generator.h:525
std::string get_c_type() const override
Definition: Generator.h:2921
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:1174
A reference to a site in a Halide statement at the top of the body of a particular for loop...
Definition: Schedule.h:203
Func in(const std::vector< Func > &others)
Definition: Generator.h:1765
void set_dimensions(int dims)
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t)
Definition: Generator.h:2710
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:1079
static constexpr bool value
Definition: Generator.h:382
const std::string name_
Definition: Generator.h:1484
GeneratorContext is a class that is used when using Generators (or Stubs) directly; it is used to all...
Definition: Generator.h:3008
A fragment of Halide syntax.
Definition: Expr.h:258
StubOutputBuffer is the placeholder that a Stub uses when it requires a Buffer for an output (rather ...
Definition: Generator.h:1359
std::string get_c_type() const override
Definition: Generator.h:631
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition: Generator.h:2267
const ArgInfoKind kind_
Definition: Generator.h:1485
std::string get_default_value() const override
Definition: Generator.h:909
A class representing a Halide pipeline.
Definition: Pipeline.h:107
GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2718
void set_generatorparam_value(const std::string &name, const std::string &value) override
Set the value for a specific GeneratorParam for an AbstractGenerator instance.
const Func & operator[](size_t i) const
Definition: Generator.h:2659
std::vector< std::string > suffixes
Definition: Generator.h:3909
std::vector< ValueType >::const_iterator begin() const
Definition: Generator.h:1631
virtual bool is_looplevel_param() const
Definition: Generator.h:466
GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
Definition: Generator.h:2568
GeneratorBase * generator
Definition: Generator.h:1498
void set_estimates_impl(const Region &estimates)
void call_schedule() override
Definition: Generator.h:3867
static Expr TBaseToExpr(const TBase2 &value)
Definition: Generator.h:2021
virtual void check_value_writable() const =0
static Type Int(int bits, int lanes=1)
Definition: Generator.h:3093
Generator & operator=(const Generator &)=delete
void set_inputs(const std::vector< StubInput > &inputs)
GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:2851
GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
Definition: Generator.h:2781
std::vector< Expr > exprs_
Definition: Generator.h:1491
GeneratorInput_DynamicScalar(const std::string &name)
Definition: Generator.h:1964
Func in(const Func &f)
Creates and returns a new identity Func that wraps this Func.
StubInput(const StubInputBuffer< T2 > &b)
Definition: Generator.h:1393
HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector< Expr > &args)
Definition: IROperator.h:335
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:603
A Realization is a vector of references to existing Buffer objects.
Definition: Realization.h:19
GeneratorOutput_Func< T > & set_estimate(const Var &var, const Expr &min, const Expr &extent)
Definition: Generator.h:2750
A halide function.
Definition: Func.h:706
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:711
GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:2243
const std::vector< Type > & types() const
Get the type(s) of the outputs of this Func.
auto max(const GeneratorParam< T > &a, const Other &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b))
Compute the maximum value between GeneratorParam<T> and any type that supports max with T...
Definition: Generator.h:1239
int dimensions() const
The dimensionality (number of arguments) of this function.
const std::string & name() const
Partition
Different ways to handle loops with a potentially optimizable boundary conditions.
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!= with T...
Definition: Generator.h:1144
void execute_generator(const ExecuteGeneratorArgs &args)
Execute a Generator for AOT compilation – this provides the implementation of the command-line Gene...
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:808
GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<> instantiations; it is not pa...
Definition: Generator.h:1444
ArgInfoKind kind() const
GeneratorParam_Enum(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition: Generator.h:838
GeneratorInput_Func< T > & set_estimates(const Region &estimates)
Definition: Generator.h:1910
GeneratorContext context() const override
Return the Target and autoscheduler info that this Generator was created with.
GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
Definition: Generator.h:1698
const ValueType & operator[](size_t i) const
Definition: Generator.h:1619
GeneratorInput_Buffer< T > & set_estimate(Var var, Expr min, Expr extent)
Definition: Generator.h:1743
void set(const LoopLevel &other)
Mutate our contents to match the contents of &#39;other&#39;.
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:1040
TailStrategy
Different ways to handle a tail case in a split when the factor does not provably divide the extent...
Definition: Schedule.h:33
GIOBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &types, int dims)
GeneratorInput_Func(const std::string &name, int d)
Definition: Generator.h:1850
GeneratorOutput(const char *name)
Definition: Generator.h:2811
GeneratorContext with_target(const Target &t) const
GeneratorInput(const std::string &name, IntIfNonScalar d)
Definition: Generator.h:2239
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2526
std::string name() override
Return the name of this Generator.
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
Definition: Generator.h:1864
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
GeneratorInput_Func(const std::string &name)
Definition: Generator.h:1860
typename select_type< cond< std::is_same< T, Target >::value, GeneratorParam_Target< 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:950
std::function< AbstractGeneratorPtr(const GeneratorContext &context)> GeneratorFactory
Definition: Generator.h:3116
Halide::LoopLevel LoopLevel
Definition: Generator.h:3061
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:625
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:40
void set_buffer(const Buffer< void > &b)
If the parameter is a buffer parameter, set its current value.
#define internal_assert(c)
Definition: Errors.h:19
virtual bool is_synthetic_param() const
Definition: Generator.h:462
virtual std::string get_default_value() const =0
Defines methods for introspecting in C++.
std::string get_default_value() const override
Definition: Generator.h:864
A Halide variable, to be used when defining functions.
Definition: Var.h:19
GeneratorOutput(const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2835
GeneratorFactoryProvider provides a way to customize the Generators that are visible to generate_filt...
Definition: Generator.h:331
GeneratorInput< T > * add_input(const std::string &name)
Definition: Generator.h:3259
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:1157
static TO2 value(const FROM &from)
Definition: Generator.h:493
bool defined() const
Does this function have at least a pure definition.
void check_value_writable() const override
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2155
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:728
auto min(const GeneratorParam< T > &a, const Other &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b))
Compute minimum between GeneratorParam<T> and any type that supports min with T.
Definition: Generator.h:1226
GeneratorInputBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
signed __INT8_TYPE__ int8_t
void set_estimate(Expr e)
Get and set constraints for scalar parameters.
ImageParam operator[](size_t i) const
Definition: Generator.h:1783
std::string array_name(size_t i) const
AbstractGenerator is an ABC that defines the API a Generator must provide to work with the existing G...
STL namespace.
Callable create_callable_from_generator(const GeneratorContext &context, const std::string &name, const GeneratorParamsMap &generator_params={})
Create a Generator from the currently-registered Generators, use it to create a Callable.
GeneratorOutput(size_t array_size, const std::string &name)
Definition: Generator.h:2815
std::unique_ptr< T > apply(const Args &...args) const
Definition: Generator.h:3039
A fragment of front-end syntax of the form f(x, y, z), where x, y, z are Vars or Exprs.
Definition: Func.h:497
Expr operator()(std::vector< Expr > args) const
Definition: Generator.h:1722
Realization realize(Args &&...args)
Definition: Generator.h:1336
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2855
ImageParam at(size_t i) const
Definition: Generator.h:1789
GeneratorOutput(size_t array_size, const std::string &name, int d)
Definition: Generator.h:2839
#define user_error
Definition: Errors.h:7
const Func & operator[](size_t i) const
Definition: Generator.h:2745
void add_requirement(const Expr &condition, const std::vector< Expr > &error_args)
Expr cast(Expr a)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:364
void set_max_value(const Expr &e)
Get and set constraints for scalar parameters.
std::vector< Parameter > parameters_
Definition: Generator.h:1552
GeneratorInput_Scalar(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2045
GeneratorInput_Func(size_t array_size, const std::string &name)
Definition: Generator.h:1879
GeneratorOutput_Buffer(const std::string &name)
Definition: Generator.h:2520
GeneratorOutput< T > * add_output(const std::string &name)
Definition: Generator.h:3336
virtual std::string call_to_string(const std::string &v) const =0
This file defines the class FunctionDAG, which is our representation of a Halide pipeline, and contains methods to using Halide&#39;s bounds tools to query properties of it.
void set_estimate(size_t index, const TBase &value)
Definition: Generator.h:2088
virtual void verify_internals()
StubInputBuffer(const Buffer< T2, D2 > &b)
Definition: Generator.h:1300
GeneratorInput_Buffer(const std::string &name, const Type &t)
Definition: Generator.h:1704
Parameter parameter() const
Definition: Generator.h:1410
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t)
Definition: Generator.h:2534
virtual void set_impl(const T &new_value)
Definition: Generator.h:555
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&#39;s sc...
constexpr int AnyDims
Definition: Buffer.h:11
std::string get_default_value() const override
Definition: Generator.h:689
T parse_scalar(const std::string &value)
Definition: Generator.h:2883
RegisterGenerator(const char *registered_name, GeneratorFactory generator_factory)
GeneratorInput_Buffer(const std::string &name, int d)
Definition: Generator.h:1709
typename std::remove_all_extents< T >::type TBase
Definition: Generator.h:1584
std::set< OutputFileType > output_types
Definition: Generator.h:3899
StubInput(const Expr &e)
Definition: Generator.h:1402
unsigned __INT8_TYPE__ uint8_t
Defines the structure that describes a Halide target.
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2142
static void register_factory(const std::string &name, GeneratorFactory generator_factory)
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2706
void set_from_string(const std::string &new_value_string) override
GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Definition: Generator.h:1593
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:1105
typename std::remove_all_extents< T >::type TBase
Definition: Generator.h:2380
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:1066
HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override
Forward schedule-related methods to the underlying Func.
Definition: Generator.h:2576
GeneratorOutput_Buffer< T > & set_estimates(const Region &estimates)
Definition: Generator.h:2651
GeneratorInput_Arithmetic(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2148
void check_matching_array_size(size_t size) const
typename Super::TBase TBase
Definition: Generator.h:2192
std::vector< ValueType >::const_iterator end() const
Definition: Generator.h:1637
std::vector< ValueType >::const_iterator begin() const
Definition: Generator.h:2452
Defines Func - the front-end handle on a halide function, and related classes.
std::shared_ptr< AbstractGenerator > generator
Definition: Generator.h:1325
HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map< std::string, T > &enum_map, const T &t)
Definition: Generator.h:298
ArgInfoKind kind() const
Definition: Generator.h:1406
bool is_root() const
virtual std::string get_c_type() const
Forward schedule-related methods to the underlying Func.
Definition: Generator.h:2363
std::string get_c_type() const override
Definition: Generator.h:779
std::string get_c_type() const override
Definition: Generator.h:1959
const void * get_introspection_helper()
Return the address of a global with type T *.
Definition: Introspection.h:50
const std::vector< Internal::GeneratorParamBase * > & generator_params() const
Definition: Generator.h:3144
virtual std::string get_c_type() const =0
void bind_input(const std::string &name, const std::vector< Parameter > &v) override
Rebind a specified Input to refer to the given piece of IR, replacing the default ImageParam / Param ...
const char * input_or_output() const override
Forward schedule-related methods to the underlying Func.
Definition: Generator.h:2369
void resize(size_t size)
Forward schedule-related methods to the underlying Func.
GeneratorOutput< T > * add_output(const std::string &name, const Type &t, int dimensions)
Definition: Generator.h:3296
Func in(const std::vector< Func > &others)
Definition: Generator.h:1926
typename std::conditional< First::value, typename First::type, void >::type type
Definition: Generator.h:391
std::unique_ptr< T2 > apply(const Args &...args) const
Definition: Generator.h:3780
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:850
std::string halide_type_to_c_source(const Type &t)
const std::vector< ElemType > & get_values() const
Realization realize(std::vector< int32_t > sizes)
Definition: Generator.h:3192
const std::vector< Internal::GeneratorOutputBase * > & outputs() const
Definition: Generator.h:3150
GeneratorInput(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2225
const std::string & name() const
Definition: Generator.h:401
void check_gio_access() const
GeneratorParamBase(const std::string &name)
std::string get_default_value() const override
Definition: Generator.h:820
GeneratorParam_LoopLevel(const std::string &name, const LoopLevel &value)
Definition: Generator.h:653
std::map< std::string, std::string > GeneratorParamsMap
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:742
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:1131
HALIDE_ALWAYS_INLINE Type type() const
Get the type of this expression node.
Definition: Expr.h:322
GeneratorBase(size_t size, const void *introspection_helper)
GeneratorOutput_Buffer(const std::string &name, int d)
Definition: Generator.h:2540
GeneratorInput_Scalar(size_t array_size, const std::string &name)
Definition: Generator.h:2040
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:824
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:901
GeneratorParam(const std::string &name, const std::string &value)
Definition: Generator.h:1005
GeneratorOutput_Buffer< T > & operator=(const Func &f)
Definition: Generator.h:2636
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition: Generator.h:2587
std::string get_c_type() const override
Definition: Generator.h:1835
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:2791
static void unregister_factory(const std::string &name)
static LoopLevel root()
Construct a special LoopLevel value which represents the location outside of all for loops...
GeneratorOutput_Func< T > & operator=(const Func &f)
Definition: Generator.h:2725
static Expr cast(Expr e)
Definition: Generator.h:3075
void check_matching_dims(int d) const
Halide::GeneratorContext GeneratorContext
Definition: Generator.h:3059
typename Super::TBase TBase
Definition: Generator.h:2801
friend class StubEmitter
Forward schedule-related methods to the underlying Func.
Definition: Generator.h:2358
std::vector< Func > funcs_
Definition: Generator.h:1490
GeneratorOutput< T > * add_output(const std::string &name, int dimensions)
Definition: Generator.h:3322
Expr make_const(Type t, int64_t val)
Construct an immediate of the given type from any numeric C++ type.
std::unique_ptr< T > create() const
Definition: Generator.h:3035
const ValueType & at(size_t i) const
Definition: Generator.h:2446
void apply(const Args &...args)
Definition: Generator.h:3767
std::vector< ImageParam >::const_iterator begin() const
Definition: Generator.h:1795
GeneratorOutput_Buffer(size_t array_size, const std::string &name)
Definition: Generator.h:2548
GeneratorInput< T > * add_input(const std::string &name, const Type &type)
Definition: Generator.h:3283
std::string get_c_type() const override
Definition: Generator.h:905
friend class GeneratorStub
Definition: Generator.h:1520
A handle on the output buffer of a pipeline.
const ValueType & at(size_t i) const
Definition: Generator.h:1625
GeneratorInput(size_t array_size, const std::string &name)
Definition: Generator.h:2257
bool is_inlined() const
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:2902
GeneratorInput_Scalar(const std::string &name, const TBase &def)
Definition: Generator.h:2036
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:2182
const GeneratorFactoryProvider & get_registered_generators()
Return a GeneratorFactoryProvider that knows about all the currently-registered C++ Generators...
GeneratorInput< T > * add_input(const std::string &name, int dimensions)
Definition: Generator.h:3245
std::string halide_type_to_enum_string(const Type &t)
Definition: Generator.h:316
std::string get_c_type() const override
Definition: Generator.h:2014
void fail_wrong_type(const char *type)
static std::vector< StubOutputBuffer< T > > to_output_buffers(const std::vector< Func > &v, const std::shared_ptr< AbstractGenerator > &gen)
Definition: Generator.h:1369
void set_min_value(const Expr &e)
Get and set constraints for scalar parameters.
unsigned __INT32_TYPE__ uint32_t
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:856
std::string get_default_value() const override
Definition: Generator.h:928
GeneratorParam_String(const std::string &name, const std::string &value)
Definition: Generator.h:921
auto min_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(min(a,(T) b))
Definition: Generator.h:1198
void check_min_phase(Phase expected_phase) const
GeneratorInput_Scalar(const std::string &name)
Definition: Generator.h:2032
const ValueType & operator[](size_t i) const
Definition: Generator.h:2440
virtual ~GIOBase()=default
std::vector< ImageParam >::const_iterator end() const
Definition: Generator.h:1801
virtual std::string get_type_decls() const
Definition: Generator.h:456
GeneratorInput(const std::string &name, const Type &t, int d)
Definition: Generator.h:2230
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:46
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:2916
Not visible externally, similar to &#39;static&#39; linkage in C.
void set_array_size(int size)
Expr reinterpret(Type t, Expr e)
Reinterpret the bits of one value as another type.
int dimensions() const
Get the dimensionality of this parameter.
GeneratorInput(const std::string &name, const Type &t)
Definition: Generator.h:2234
bool allow_out_of_order_inputs_and_outputs() const override
By default, a Generator must declare all Inputs before all Outputs.
Provides a single global registry of Generators, GeneratorParams, and Params indexed by this pointer...
const std::string & name() const
The name of this function, either given during construction, or automatically generated.
void init_internals()
Forward schedule-related methods to the underlying Func.
signed __INT64_TYPE__ int64_t
std::string halide_type_to_c_type(const Type &t)
void advance_phase(Phase new_phase)
virtual AbstractGeneratorPtr create(const std::string &name, const Halide::GeneratorContext &context) const =0
Create an instance of the Generator that is registered under the given name.
static std::unique_ptr< T > create(const Halide::GeneratorContext &context, const std::string &registered_name, const std::string &stub_name)
Definition: Generator.h:3758
Type Bool(int lanes=1)
Construct a boolean type.
Definition: Type.h:550
GeneratorInput_Buffer(const std::string &name)
Definition: Generator.h:1692
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:924
GeneratorParam_Bool(const std::string &name, const T &value)
Definition: Generator.h:804
const std::vector< Internal::GeneratorInputBase * > & inputs() const
Definition: Generator.h:3147
auto operator!(const GeneratorParam< T > &a) -> decltype(!(T) a)
Not operator for GeneratorParam.
Definition: Generator.h:1246
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:1053
GeneratorOutput_Func(const std::string &name, int d)
Definition: Generator.h:2714
auto max_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(max(a,(T) b))
Definition: Generator.h:1207
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 ...
GeneratorParamBase & operator=(const GeneratorParamBase &)=delete
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:3168
Halide::Target Target
Definition: Generator.h:3070
#define internal_error
Definition: Errors.h:23
static std::vector< std::string > enumerate()
bool is_array() const override
Definition: Generator.h:1586
FuncRef operator()(Args &&...args) const
Definition: Generator.h:2410
GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
Definition: Generator.h:2216
bool emit_cpp_stub(const std::string &stub_file_path) override
Emit a Generator Stub (.stub.h) file to the given path.
Special the Autoscheduler to be used (if any), along with arbitrary additional arguments specific to ...
Definition: Pipeline.h:48
Expr operator()(Args &&...args) const
Definition: Generator.h:1717
GeneratorOutput< T > & operator=(const Func &f)
Definition: Generator.h:2874
GeneratorOutput(const std::string &name)
Definition: Generator.h:2807
PrefetchBoundStrategy
Different ways to handle accesses outside the original extents in a prefetch.
ExecuteGeneratorArgs is the set of arguments to execute_generator().
Definition: Generator.h:3894
enum Halide::Internal::GeneratorBase::Phase Created
#define HALIDE_ALWAYS_INLINE
Definition: HalideRuntime.h:49
bool is_array() const override
Definition: Generator.h:2383
std::function< AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)> CreateGeneratorFn
Definition: Generator.h:3944
GeneratorOutput(const std::string &name, int d)
Definition: Generator.h:2819
HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args)
Definition: Generator.h:3351
static std::vector< Parameter > to_parameter_vector(const std::vector< StubInputBuffer< T2 >> &v)
Definition: Generator.h:1310
Expr operator()(Args &&...args) const
Definition: Generator.h:1884
std::unique_ptr< T2 > create() const
Definition: Generator.h:3775
GeneratorInput(const std::string &name, const TBase &def)
Definition: Generator.h:2212
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:3161
static LoopLevel inlined()
Construct a special LoopLevel value that implies that a function should be inlined away...
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target())
See Func::realize.
std::string get_default_value() const override
Definition: Generator.h:759
GeneratorInput_Func(const std::string &name, const Type &t)
Definition: Generator.h:1855
GeneratorOutput(const std::string &name, const Type &t)
Definition: Generator.h:2823
void check_matching_types(const std::vector< Type > &t) const
void call_generate() override
Definition: Generator.h:3863
HALIDE_NO_USER_CODE_INLINE GeneratorOutput_Buffer< T > & operator=(Buffer< T2, D2 > &buffer)
Definition: Generator.h:2599
std::vector< Parameter > input_parameter(const std::string &name) override
Given the name of an input, return the Parameter(s) for that input.
const std::vector< Type > & gio_types() const
void trace_pipeline()
Generate begin_pipeline and end_pipeline tracing calls for this pipeline.
GeneratorInput_Arithmetic(size_t array_size, const std::string &name)
Definition: Generator.h:2137
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition: Generator.h:2554
const char * input_or_output() const override
Definition: Generator.h:1570
const std::map< std::string, Halide::Type > & get_halide_type_enum_map()
void set_default_value(const Expr &e)
Get and set the default values for scalar parameters.
GeneratorInput_Arithmetic(const std::string &name)
Definition: Generator.h:2128
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:773
GeneratorParam_Type(const std::string &name, const T &value)
Definition: Generator.h:897
GeneratorInput_Func(const std::string &name, const Type &t, int d)
Definition: Generator.h:1845
static Type Float(int bits, int lanes=1)
Definition: Generator.h:3090
Type UInt(int bits, int lanes=1)
Constructing an unsigned integer type.
Definition: Type.h:535
A reference-counted handle to Halide&#39;s internal representation of a function.
Definition: Function.h:38
GeneratorParamImpl(const std::string &name, const T &value)
Definition: Generator.h:508
bool is_synthetic_param() const override
Definition: Generator.h:2926
std::vector< Expr > parameter_constraints(const Parameter &p)
Realization realize(std::vector< int32_t > sizes)
An Image parameter to a halide pipeline.
Definition: ImageParam.h:23
unsigned __INT16_TYPE__ uint16_t
An argument to an extern-defined Func.
Types in the halide type system.
Definition: Type.h:276
Pipeline build_pipeline() override
Build and return the Pipeline for this AbstractGenerator.
Definition: Generator.h:3854
Expr operator()(const std::vector< Expr > &args) const
Definition: Generator.h:1889
CompilerLoggerFactory compiler_logger_factory
Definition: Generator.h:3955
GeneratorParam(const std::string &name, const T &value)
Definition: Generator.h:993
#define HALIDE_FORWARD_METHOD(Class, Method)
Definition: Generator.h:1651
std::string get_default_value() const override
Definition: Generator.h:621
void call_configure() override
Definition: Generator.h:3859
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:1027
GeneratorOutput_Func(const std::string &name)
Definition: Generator.h:2702
void set_estimate(const TBase &value)
Definition: Generator.h:2066
GeneratorParam_Target(const std::string &name, const T &value)
Definition: Generator.h:613
NameMangling
An enum to specify calling convention for extern stages.
Definition: Function.h:25
std::string call_to_string(const std::string &v) const override
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:679
std::string call_to_string(const std::string &v) const override
Definition: Generator.h:932
std::string get_default_value() const override
Definition: Generator.h:2911
virtual void set_from_string(const std::string &value_string)=0
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:1014
GeneratorInput_Func(size_t array_size, const std::string &name, int d)
Definition: Generator.h:1869
GeneratorContext & operator=(const GeneratorContext &)=default
void realize(Realization r)
Definition: Generator.h:3205
void check_exact_phase(Phase expected_phase) const
GeneratorOutput_Func< T > & set_estimates(const Region &estimates)
Definition: Generator.h:2759
std::vector< ArgInfo > arginfos() override
Return a list of all the ArgInfos for this generator.
GeneratorOutputBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Forward schedule-related methods to the underlying Func.
GeneratorParam(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition: Generator.h:1001
Halide::Pipeline Pipeline
Definition: Generator.h:3065
Helper class for identifying purpose of an Expr passed to memoize.
Definition: Func.h:691
std::string get_c_type() const override
Definition: Generator.h:716
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:161
GeneratorParam is a templated class that can be used to modify the behavior of the Generator at code-...
Definition: Generator.h:990
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:2202
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
Definition: Generator.h:1874
virtual bool is_array() const
GeneratorBase & operator=(const GeneratorBase &)=delete
GeneratorOutput< T > & operator=(const Internal::StubOutputBuffer< T2 > &stub_output_buffer)
Definition: Generator.h:2869
A single definition of a Func.
Definition: Func.h:69
T enum_from_string(const std::map< std::string, T > &enum_map, const std::string &s)
Definition: Generator.h:309
GeneratorInput(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition: Generator.h:2220
GeneratorParam_AutoSchedulerParams autoscheduler_
Definition: Generator.h:3479
const AutoschedulerParams & autoscheduler_params() const
Definition: Generator.h:3025
GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Definition: Generator.h:2390
A reduction variable represents a single dimension of a reduction domain (RDom).
Definition: RDom.h:29
GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
Definition: Generator.h:2843
A multi-dimensional domain over which to iterate.
Definition: RDom.h:193
std::string get_type_decls() const override
Definition: Generator.h:868
GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
Definition: Generator.h:2253
static std::vector< Parameter > to_parameter_vector(const StubInputBuffer< T2 > &t)
Definition: Generator.h:1305
bool defined() const
Check if this Buffer refers to an existing Buffer.
Definition: Buffer.h:381
A scalar parameter to a halide pipeline.
Definition: Param.h:22
LoopLevel & lock()
int generate_filter_main(int argc, char **argv)
generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() + compile_to_files(); ...
bool emit_hlpipe(const std::string &hlpipe_file_path) override
Emit a Serialized Halide Pipeline (.hlpipe) file to the given path.
GeneratorRegistry & operator=(const GeneratorRegistry &)=delete
StubInputBuffer is the placeholder that a Stub uses when it requires a Buffer for an input (rather th...
Definition: Generator.h:1265
~GeneratorOutputBase() override
Forward schedule-related methods to the underlying Func.
GeneratorInput_Func< T > & set_estimate(Var var, Expr min, Expr extent)
Definition: Generator.h:1904
Realization realize(Args &&...args)
Definition: Generator.h:3200
Classes for declaring image parameters to halide pipelines.
GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
Definition: Generator.h:997
GeneratorOutput_Buffer< T > & operator=(const StubOutputBuffer< T2 > &stub_output_buffer)
Definition: Generator.h:2627
void set_from_string(const std::string &new_value_string) override
Definition: Generator.h:617
static Type Bool(int lanes=1)
Definition: Generator.h:3087
static Expr cast(Halide::Type t, Expr e)
Definition: Generator.h:3078
static Type UInt(int bits, int lanes=1)
Definition: Generator.h:3096
std::string get_c_type() const override
Definition: Generator.h:1676
unsigned __INT64_TYPE__ uint64_t
FuncRef operator()(std::vector< ExprOrVar > args) const
Definition: Generator.h:2416
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition: Generator.h:2847
GeneratorInput(size_t array_size, const std::string &name, const Type &t)
Definition: Generator.h:2247
const std::vector< Expr > & exprs() const
GeneratorInput< T > * add_input(const std::string &name, const Type &t, int dimensions)
Definition: Generator.h:3219
virtual const char * input_or_output() const =0
virtual std::vector< std::string > enumerate() const =0
Return a list of all registered Generators that are available for use with the create() method...
std::string get_c_type() const override
Definition: Generator.h:830
void check_value_writable() const override
Forward schedule-related methods to the underlying Func.
std::function< std::unique_ptr< Internal::CompilerLogger >(const std::string &fn_name, const Target &target)> CompilerLoggerFactory
Definition: Module.h:243
GeneratorInput(const std::string &name)
Definition: Generator.h:2208
GIOBase & operator=(const GIOBase &)=delete
bool gio_types_defined() const
std::string get_c_type() const override
Definition: Generator.h:860
Func in(const Func &other)
Definition: Generator.h:1921
size_t array_size() const
const Target & target() const
Definition: Generator.h:3022
Type type() const
Get the type of this parameter.
static AbstractGeneratorPtr create(const std::string &name, const Halide::GeneratorContext &context)
#define user_assert(c)
Definition: test.h:10
void set_impl(const T &new_value) override
Definition: Generator.h:737
signed __INT32_TYPE__ int32_t
bool is_looplevel_param() const override
Definition: Generator.h:720
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition: Generator.h:2562
GeneratorParam< Target > target
Definition: Generator.h:3478
void set_generator_names(const std::string &registered_name, const std::string &stub_name)
#define HALIDE_FORWARD_METHOD_CONST(Class, Method)
Definition: Generator.h:1657
Type Int(int bits, int lanes=1)
Constructing a signed integer type.
Definition: Type.h:530
virtual void init_from_context(const Halide::GeneratorContext &context)
std::unique_ptr< AbstractGenerator > AbstractGeneratorPtr
Type type() const
Does the same thing as the equivalent Halide::Runtime::Buffer method.
Definition: Buffer.h:533
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target())
Evaluate this function over some rectangular domain and return the resulting buffer or buffers...
GeneratorInput_Arithmetic(const std::string &name, const TBase &def)
Definition: Generator.h:2132
signed __INT16_TYPE__ int16_t
bool array_size_defined() const
virtual std::string get_c_type() const =0
GeneratorParamInfo(GeneratorBase *generator, size_t size)
StubInput(const Func &f)
Definition: Generator.h:1399
std::vector< ValueType >::const_iterator end() const
Definition: Generator.h:2458
std::vector< Type > types_
Definition: Generator.h:1486
std::string get_type_decls() const override
Definition: Generator.h:913
GeneratorFactoryProvider & operator=(const GeneratorFactoryProvider &)=delete
static std::unique_ptr< T > create(const Halide::GeneratorContext &context)
Definition: Generator.h:3749
Type Float(int bits, int lanes=1)
Construct a floating-point type.
Definition: Type.h:540
std::string get_c_type() const override
Definition: Generator.h:936
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:348