forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.h
More file actions
211 lines (165 loc) · 7 KB
/
Function.h
File metadata and controls
211 lines (165 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef HALIDE_FUNCTION_H
#define HALIDE_FUNCTION_H
/** \file
* Defines the internal representation of a halide function and related classes
*/
#include "Expr.h"
#include "IntrusivePtr.h"
#include "Parameter.h"
#include "Schedule.h"
#include "Reduction.h"
namespace Halide {
namespace Internal {
struct FunctionContents;
}
/** An argument to an extern-defined Func. May be a Function, Buffer,
* ImageParam or Expr. */
struct ExternFuncArgument {
enum ArgType {UndefinedArg = 0, FuncArg, BufferArg, ExprArg, ImageParamArg};
ArgType arg_type;
Internal::IntrusivePtr<Internal::FunctionContents> func;
Buffer buffer;
Expr expr;
Internal::Parameter image_param;
ExternFuncArgument(Internal::IntrusivePtr<Internal::FunctionContents> f): arg_type(FuncArg), func(f) {}
ExternFuncArgument(Buffer b): arg_type(BufferArg), buffer(b) {}
ExternFuncArgument(Expr e): arg_type(ExprArg), expr(e) {}
ExternFuncArgument(int e): arg_type(ExprArg), expr(e) {}
ExternFuncArgument(float e): arg_type(ExprArg), expr(e) {}
ExternFuncArgument(Internal::Parameter p) : arg_type(ImageParamArg), image_param(p) {
// Scalar params come in via the Expr constructor.
internal_assert(p.is_buffer());
}
ExternFuncArgument() : arg_type(UndefinedArg) {}
bool is_func() const {return arg_type == FuncArg;}
bool is_expr() const {return arg_type == ExprArg;}
bool is_buffer() const {return arg_type == BufferArg;}
bool is_image_param() const {return arg_type == ImageParamArg;}
bool defined() const {return arg_type != UndefinedArg;}
};
namespace Internal {
struct UpdateDefinition {
std::vector<Expr> values, args;
Schedule schedule;
ReductionDomain domain;
};
/** A reference-counted handle to Halide's internal representation of
* a function. Similar to a front-end Func object, but with no
* syntactic sugar to help with definitions. */
class Function {
private:
IntrusivePtr<FunctionContents> contents;
public:
/** Construct a new function with no definitions and no name. This
* constructor only exists so that you can make vectors of
* functions, etc.
*/
EXPORT Function();
/** Reconstruct a Function from a FunctionContents pointer. */
EXPORT Function(const IntrusivePtr<FunctionContents> &c) : contents(c) {}
/** Construct a new function with the given name */
EXPORT Function(const std::string &n);
/** Add a pure definition to this function. It may not already
* have a definition. All the free variables in 'value' must
* appear in the args list. 'value' must not depend on any
* reduction domain */
EXPORT void define(const std::vector<std::string> &args, std::vector<Expr> values);
/** Add an update definition to this function. It must already
* have a pure definition but not an update definition, and the
* length of args must match the length of args used in the pure
* definition. 'value' must depend on some reduction domain, and
* may contain variables from that domain as well as pure
* variables. Any pure variables must also appear as Variables in
* the args array, and they must have the same name as the pure
* definition's argument in the same index. */
EXPORT void define_update(const std::vector<Expr> &args, std::vector<Expr> values);
/** Accept a visitor to visit all of the definitions and arguments
* of this function. */
EXPORT void accept(IRVisitor *visitor) const;
/** Get the name of the function */
EXPORT const std::string &name() const;
/** Get the pure arguments */
EXPORT const std::vector<std::string> &args() const;
/** Get the dimensionality */
int dimensions() const {
return (int)args().size();
}
/** Get the number of outputs */
int outputs() const {
return (int)output_types().size();
}
/** Get the types of the outputs */
EXPORT const std::vector<Type> &output_types() const;
/** Get the right-hand-side of the pure definition */
EXPORT const std::vector<Expr> &values() const;
/** Does this function have a pure definition */
bool has_pure_definition() const {
return !values().empty();
}
/** Does this function *only* have a pure definition */
bool is_pure() const {
return (has_pure_definition() &&
!has_update_definition() &&
!has_extern_definition());
}
/** Get a handle to the schedule for the purpose of modifying
* it */
EXPORT Schedule &schedule();
/** Get a const handle to the schedule for inspecting it */
EXPORT const Schedule &schedule() const;
/** Get a handle on the output buffer used for setting constraints
* on it. */
EXPORT const std::vector<Parameter> &output_buffers() const;
/** Get a mutable handle to the schedule for the update
* stage */
EXPORT Schedule &update_schedule(int idx = 0);
/** Get a const reference to this function's update definitions. */
EXPORT const std::vector<UpdateDefinition> &updates() const;
/** Does this function have an update definition */
EXPORT bool has_update_definition() const;
/** Check if the function has an extern definition */
EXPORT bool has_extern_definition() const;
/** Check if the function has an extern definition */
EXPORT bool extern_definition_is_c_plus_plus() const;
/** Add an external definition of this Func */
EXPORT void define_extern(const std::string &function_name,
const std::vector<ExternFuncArgument> &args,
const std::vector<Type> &types,
int dimensionality,
bool is_c_plus_plus);
/** Retrive the arguments of the extern definition */
EXPORT const std::vector<ExternFuncArgument> &extern_arguments() const;
/** Get the name of the extern function called for an extern
* definition. */
EXPORT const std::string &extern_function_name() const;
/** Equality of identity */
bool same_as(const Function &other) const {
return contents.same_as(other.contents);
}
/** Get a const handle to the debug filename */
EXPORT const std::string &debug_file() const;
/** Get a handle to the debug filename */
EXPORT std::string &debug_file();
/** Use an an extern argument to another function. */
operator ExternFuncArgument() const {
return ExternFuncArgument(contents);
}
/** Tracing calls and accessors, passed down from the Func
* equivalents. */
// @{
EXPORT void trace_loads();
EXPORT void trace_stores();
EXPORT void trace_realizations();
EXPORT bool is_tracing_loads() const;
EXPORT bool is_tracing_stores() const;
EXPORT bool is_tracing_realizations() const;
// @}
/** Mark function as frozen, which means it cannot accept new
* definitions. */
EXPORT void freeze();
/** Check if a function has been frozen. If so, it is an error to
* add new definitions. */
EXPORT bool frozen() const;
};
}}
#endif