Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
FunctionPtr.h
Go to the documentation of this file.
1#ifndef HALIDE_FUNCTION_PTR_H
2#define HALIDE_FUNCTION_PTR_H
3
4#include "IntrusivePtr.h"
5
6namespace Halide {
7namespace Internal {
8
9/** Functions are allocated in groups for memory management. Each
10 * group has a ref count associated with it. All within-group
11 * references must be weak. If there are any references from outside
12 * the group, at least one must be strong. Within-group references
13 * may form cycles, but there may not be reference cycles that span
14 * multiple groups. These rules are not enforced automatically. */
15struct FunctionGroup;
16
17/** The opaque struct describing a Halide function. Wrap it in a
18 * Function object to access it. */
19struct FunctionContents;
20
21/** A possibly-weak pointer to a Halide function. Take care to follow
22 * the rules mentioned above. Preserves weakness/strength on copy.
23 *
24 * Note that Function objects are always strong pointers to Halide
25 * functions.
26 */
28 /** A strong and weak pointer to the group. Only one of these
29 * should be non-zero. */
30 // @{
32 FunctionGroup *weak = nullptr;
33 // @}
34
35 /** The index of the function within the group. */
36 int idx = 0;
37
38 /** Get a pointer to the group this Function belongs to. */
39 FunctionGroup *group() const {
40 return weak ? weak : strong.get();
41 }
42
43 /** Get the opaque FunctionContents object this pointer refers
44 * to. Wrap it in a Function to do anything interesting with it. */
45 // @{
46 FunctionContents *get() const;
47
48 FunctionContents &operator*() const {
49 return *get();
50 }
51
52 FunctionContents *operator->() const {
53 return get();
54 }
55 // @}
56
57 /** Convert from a strong reference to a weak reference. Does
58 * nothing if the pointer is undefined, or if the reference is
59 * already weak. */
60 void weaken() {
61 weak = group();
62 strong = nullptr;
63 }
64
65 /** Convert from a weak reference to a strong reference. Does
66 * nothing if the pointer is undefined, or if the reference is
67 * already strong. */
68 void strengthen() {
69 strong = group();
70 weak = nullptr;
71 }
72
73 /** Check if the reference is defined. */
74 bool defined() const {
75 return weak || strong.defined();
76 }
77
78 /** Check if two FunctionPtrs refer to the same Function. */
79 bool same_as(const FunctionPtr &other) const {
80 return idx == other.idx && group() == other.group();
81 }
82
83 /** Pointer comparison, for using FunctionPtrs as keys in maps and
84 * sets. */
85 bool operator<(const FunctionPtr &other) const {
86 return get() < other.get();
87 }
88};
89
90} // namespace Internal
91} // namespace Halide
92
93#endif
Support classes for reference-counting via intrusive shared pointers.
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
A possibly-weak pointer to a Halide function.
Definition FunctionPtr.h:27
void weaken()
Convert from a strong reference to a weak reference.
Definition FunctionPtr.h:60
FunctionContents * get() const
Get the opaque FunctionContents object this pointer refers to.
IntrusivePtr< FunctionGroup > strong
A strong and weak pointer to the group.
Definition FunctionPtr.h:31
bool operator<(const FunctionPtr &other) const
Pointer comparison, for using FunctionPtrs as keys in maps and sets.
Definition FunctionPtr.h:85
bool defined() const
Check if the reference is defined.
Definition FunctionPtr.h:74
FunctionContents & operator*() const
Definition FunctionPtr.h:48
void strengthen()
Convert from a weak reference to a strong reference.
Definition FunctionPtr.h:68
FunctionGroup * group() const
Get a pointer to the group this Function belongs to.
Definition FunctionPtr.h:39
bool same_as(const FunctionPtr &other) const
Check if two FunctionPtrs refer to the same Function.
Definition FunctionPtr.h:79
FunctionContents * operator->() const
Definition FunctionPtr.h:52
int idx
The index of the function within the group.
Definition FunctionPtr.h:36
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
HALIDE_ALWAYS_INLINE bool defined() const
T * get() const
Access the raw pointer in a variety of ways.