Halide
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 
6 namespace Halide {
7 namespace 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. */
15 struct FunctionGroup;
16 
17 /** The opaque struct describing a Halide function. Wrap it in a
18  * Function object to access it. */
19 struct 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  */
27 struct FunctionPtr {
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
Halide::Internal::FunctionPtr::operator*
FunctionContents & operator*() const
Definition: FunctionPtr.h:48
Halide::Internal::FunctionPtr::strengthen
void strengthen()
Convert from a weak reference to a strong reference.
Definition: FunctionPtr.h:68
Halide::Internal::IntrusivePtr< FunctionGroup >
Halide::Internal::FunctionPtr::same_as
bool same_as(const FunctionPtr &other) const
Check if two FunctionPtrs refer to the same Function.
Definition: FunctionPtr.h:79
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::FunctionPtr::operator->
FunctionContents * operator->() const
Definition: FunctionPtr.h:52
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::FunctionPtr::weak
FunctionGroup * weak
Definition: FunctionPtr.h:32
Halide::Internal::FunctionPtr
A possibly-weak pointer to a Halide function.
Definition: FunctionPtr.h:27
Halide::Internal::FunctionPtr::strong
IntrusivePtr< FunctionGroup > strong
A strong and weak pointer to the group.
Definition: FunctionPtr.h:31
Halide::Internal::IntrusivePtr::get
T * get() const
Access the raw pointer in a variety of ways.
Definition: IntrusivePtr.h:99
Halide::Internal::FunctionPtr::idx
int idx
The index of the function within the group.
Definition: FunctionPtr.h:36
Halide::Internal::FunctionPtr::weaken
void weaken()
Convert from a strong reference to a weak reference.
Definition: FunctionPtr.h:60
Halide::Internal::FunctionPtr::defined
bool defined() const
Check if the reference is defined.
Definition: FunctionPtr.h:74
Halide::Internal::FunctionPtr::group
FunctionGroup * group() const
Get a pointer to the group this Function belongs to.
Definition: FunctionPtr.h:39
Halide::Internal::FunctionPtr::operator<
bool operator<(const FunctionPtr &other) const
Pointer comparison, for using FunctionPtrs as keys in maps and sets.
Definition: FunctionPtr.h:85
Halide::Internal::IntrusivePtr::defined
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:156
Halide::Internal::FunctionPtr::get
FunctionContents * get() const
Get the opaque FunctionContents object this pointer refers to.
IntrusivePtr.h