Halide
Parameter.h
Go to the documentation of this file.
1 #ifndef HALIDE_PARAMETER_H
2 #define HALIDE_PARAMETER_H
3 
4 /** \file
5  * Defines the internal representation of parameters to halide piplines
6  */
7 
8 #include "Expr.h"
9 #include "Buffer.h"
10 
11 namespace Halide {
12 
13 class OutputImageParam;
14 
15 namespace Internal {
16 
17 struct ParameterContents;
18 
19 /** A reference-counted handle to a parameter to a halide
20  * pipeline. May be a scalar parameter or a buffer */
21 class Parameter {
23 
24  void check_defined() const;
25  void check_is_buffer() const;
26  void check_is_scalar() const;
27  void check_dim_ok(int dim) const;
28 
29 public:
30  /** Construct a new undefined handle */
31  EXPORT Parameter();
32 
33  /** Construct a new parameter of the given type. If the second
34  * argument is true, this is a buffer parameter of the given
35  * dimensionality, otherwise, it is a scalar parameter (and the
36  * dimensionality should be zero). The parameter will be given a
37  * unique auto-generated name. */
39 
40  /** Construct a new parameter of the given type with name given by
41  * the third argument. If the second argument is true, this is a
42  * buffer parameter, otherwise, it is a scalar parameter. The
43  * third argument gives the dimensionality of the buffer
44  * parameter. It should be zero for scalar parameters. If the
45  * fifth argument is true, the the name being passed in was
46  * explicitly specified (as opposed to autogenerated). If the
47  * sixth argument is true, the Parameter is registered in the global
48  * ObjectInstanceRegistry. */
49  EXPORT Parameter(Type t, bool is_buffer, int dimensions,
50  const std::string &name, bool is_explicit_name = false,
51  bool register_instance = true, bool is_bound_before_lowering = false);
52 
53  /** Copy ctor, operator=, and dtor, needed for ObjectRegistry accounting. */
54  EXPORT Parameter(const Parameter&);
55  EXPORT Parameter& operator=(const Parameter&);
57 
58  /** Get the type of this parameter */
59  EXPORT Type type() const;
60 
61  /** Get the dimensionality of this parameter. Zero for scalars. */
62  EXPORT int dimensions() const;
63 
64  /** Get the name of this parameter */
65  EXPORT const std::string &name() const;
66 
67  /** Return true iff the name was explicitly specified */
68  EXPORT bool is_explicit_name() const;
69 
70  /** Return true iff this Parameter is expected to be replaced with a
71  * constant at the start of lowering, and thus should not be used to
72  * infer arguments */
73  EXPORT bool is_bound_before_lowering() const;
74 
75  /** Does this parameter refer to a buffer/image? */
76  EXPORT bool is_buffer() const;
77 
78  /** If the parameter is a scalar parameter, get its currently
79  * bound value. Only relevant when jitting */
80  template<typename T>
81  NO_INLINE T scalar() const {
82  // Allow scalar<uint64_t>() for all Handle types
83  user_assert(type() == type_of<T>() || (type().is_handle() && type_of<T>() == UInt(64)))
84  << "Can't get Param<" << type()
85  << "> as scalar of type " << type_of<T>() << "\n";
86  return *((const T *)(scalar_address()));
87  }
88 
89  /** This returns the current value of scalar<type()>()
90  * as an Expr. */
91  EXPORT Expr scalar_expr() const;
92 
93  /** If the parameter is a scalar parameter, set its current
94  * value. Only relevant when jitting */
95  template<typename T>
96  NO_INLINE void set_scalar(T val) {
97  // Allow set_scalar<uint64_t>() for all Handle types
98  user_assert(type() == type_of<T>() || (type().is_handle() && type_of<T>() == UInt(64)))
99  << "Can't set Param<" << type()
100  << "> to scalar of type " << type_of<T>() << "\n";
101  *((T *)(scalar_address())) = val;
102  }
103 
104  /** If the parameter is a buffer parameter, get its currently
105  * bound buffer. Only relevant when jitting */
106  EXPORT Buffer<> buffer() const;
107 
108  /** If the parameter is a buffer parameter, set its current
109  * value. Only relevant when jitting */
110  EXPORT void set_buffer(Buffer<> b);
111 
112  /** Get the pointer to the current value of the scalar
113  * parameter. For a given parameter, this address will never
114  * change. Only relevant when jitting. */
115 
116  EXPORT void *scalar_address() const;
117 
118  /** Tests if this handle is the same as another handle */
119  EXPORT bool same_as(const Parameter &other) const;
120 
121  /** Tests if this handle is non-nullptr */
122  EXPORT bool defined() const;
123 
124  /** Get and set constraints for the min, extent, stride, and estimates on
125  * the min/extent. */
126  //@{
127  EXPORT void set_min_constraint(int dim, Expr e);
128  EXPORT void set_extent_constraint(int dim, Expr e);
129  EXPORT void set_stride_constraint(int dim, Expr e);
131  EXPORT void set_extent_constraint_estimate(int dim, Expr extent);
132  EXPORT void set_host_alignment(int bytes);
133  EXPORT Expr min_constraint(int dim) const;
134  EXPORT Expr extent_constraint(int dim) const;
135  EXPORT Expr stride_constraint(int dim) const;
136  EXPORT Expr min_constraint_estimate(int dim) const;
137  EXPORT Expr extent_constraint_estimate(int dim) const;
138  EXPORT int host_alignment() const;
139  //@}
140 
141  /** Get and set constraints for scalar parameters. These are used
142  * directly by Param, so they must be exported. */
143  // @{
144  EXPORT void set_min_value(Expr e);
145  EXPORT Expr min_value() const;
146  EXPORT void set_max_value(Expr e);
147  EXPORT Expr max_value() const;
148  EXPORT void set_estimate(Expr e);
149  EXPORT Expr estimate() const;
150  // @}
151 };
152 
153 /** Validate arguments to a call to a func, image or imageparam. */
154 void check_call_arg_types(const std::string &name, std::vector<Expr> *args, int dims);
155 
156 }
157 }
158 
159 #endif
A fragment of Halide syntax.
Definition: Expr.h:276
EXPORT bool same_as(const Parameter &other) const
Tests if this handle is the same as another handle.
EXPORT int dimensions() const
Get the dimensionality of this parameter.
EXPORT Parameter()
Construct a new undefined handle.
EXPORT bool defined() const
Tests if this handle is non-nullptr.
EXPORT Expr stride_constraint(int dim) const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT Parameter & operator=(const Parameter &)
EXPORT int host_alignment() const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
NO_INLINE T scalar() const
If the parameter is a scalar parameter, get its currently bound value.
Definition: Parameter.h:81
EXPORT void * scalar_address() const
Get the pointer to the current value of the scalar parameter.
Defines methods for manipulating and analyzing boolean expressions.
EXPORT Expr estimate() const
Get and set constraints for scalar parameters.
EXPORT Expr max_value() const
Get and set constraints for scalar parameters.
EXPORT void set_min_value(Expr e)
Get and set constraints for scalar parameters.
EXPORT Expr extent_constraint_estimate(int dim) const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
Expr min(FuncRef a, FuncRef b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:418
NO_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:96
EXPORT bool is_explicit_name() const
Return true iff the name was explicitly specified.
EXPORT Expr min_constraint_estimate(int dim) const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT bool is_bound_before_lowering() const
Return true iff this Parameter is expected to be replaced with a constant at the start of lowering...
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt) ...
#define NO_INLINE
Definition: Util.h:41
EXPORT void set_host_alignment(int bytes)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT Expr min_constraint(int dim) const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT void set_min_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT void set_extent_constraint_estimate(int dim, Expr extent)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT void set_estimate(Expr e)
Get and set constraints for scalar parameters.
EXPORT void set_min_constraint_estimate(int dim, Expr min)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT Expr scalar_expr() const
This returns the current value of scalar<type()>() as an Expr.
EXPORT void set_buffer(Buffer<> b)
If the parameter is a buffer parameter, set its current value.
EXPORT Type type() const
Get the type of this parameter.
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:21
EXPORT const std::string & name() const
Get the name of this parameter.
Type UInt(int bits, int lanes=1)
Constructing an unsigned integer type.
Definition: Type.h:442
#define user_assert(c)
Definition: Error.h:141
Types in the halide type system.
Definition: Type.h:285
EXPORT void set_extent_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT void set_max_value(Expr e)
Get and set constraints for scalar parameters.
EXPORT Expr min_value() const
Get and set constraints for scalar parameters.
#define EXPORT
Definition: Util.h:30
EXPORT Expr extent_constraint(int dim) const
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT void set_stride_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
EXPORT Buffer buffer() const
If the parameter is a buffer parameter, get its currently bound buffer.
EXPORT bool is_buffer() const
Does this parameter refer to a buffer/image?
void check_call_arg_types(const std::string &name, std::vector< Expr > *args, int dims)
Validate arguments to a call to a func, image or imageparam.