13
13
#include " AutoDiffSupport.h"
14
14
#include " swift/ABI/Metadata.h"
15
15
#include " swift/Runtime/HeapObject.h"
16
-
16
+ # include " llvm/ADT/SmallVector.h "
17
17
#include < new>
18
18
19
19
using namespace swift ;
20
20
using namespace llvm ;
21
21
22
22
SWIFT_CC (swift)
23
23
static void destroyLinearMapContext(SWIFT_CONTEXT HeapObject *obj) {
24
- static_cast <AutoDiffLinearMapContext *>(obj)->~AutoDiffLinearMapContext ();
24
+ auto *linearMapContext = static_cast <AutoDiffLinearMapContext *>(obj);
25
+
26
+ for (auto *heapObjectPtr : linearMapContext->getAllocatedHeapObjects ()) {
27
+ swift_release (heapObjectPtr);
28
+ }
29
+
30
+ linearMapContext->~AutoDiffLinearMapContext ();
25
31
free (obj);
26
32
}
27
33
@@ -43,36 +49,54 @@ static FullMetadata<HeapMetadata> linearMapContextHeapMetadata = {
43
49
}
44
50
};
45
51
46
- AutoDiffLinearMapContext::AutoDiffLinearMapContext ()
52
+ AutoDiffLinearMapContext::AutoDiffLinearMapContext (
53
+ OpaqueValue *const topLevelLinearMapContextProjection)
47
54
: HeapObject(&linearMapContextHeapMetadata) {
55
+ this ->topLevelLinearMapContextProjection = topLevelLinearMapContextProjection;
48
56
}
49
57
50
- void *AutoDiffLinearMapContext::projectTopLevelSubcontext () const {
51
- auto offset = alignTo (
52
- sizeof (AutoDiffLinearMapContext), alignof (AutoDiffLinearMapContext));
53
- return const_cast <uint8_t *>(
54
- reinterpret_cast <const uint8_t *>(this ) + offset);
55
- }
58
+ AutoDiffLinearMapContext *swift::swift_autoDiffCreateLinearMapContext (
59
+ const Metadata *topLevelLinearMapContextMetadata) {
60
+ // Linear map context metadata must have non-null value witnesses
61
+ assert (topLevelLinearMapContextMetadata->getValueWitnesses ());
56
62
57
- void * AutoDiffLinearMapContext::allocate ( size_t size) {
58
- return allocator. Allocate (size, alignof (AutoDiffLinearMapContext));
59
- }
63
+ // Allocate a box for the top-level linear map context
64
+ auto [topLevelContextHeapObjectPtr, toplevelContextProjection] =
65
+ swift_allocBox (topLevelLinearMapContextMetadata);
60
66
61
- AutoDiffLinearMapContext *swift::swift_autoDiffCreateLinearMapContext (
62
- size_t topLevelLinearMapStructSize) {
63
- auto allocationSize = alignTo (
64
- sizeof (AutoDiffLinearMapContext), alignof (AutoDiffLinearMapContext))
65
- + topLevelLinearMapStructSize;
66
- auto *buffer = (AutoDiffLinearMapContext *)malloc (allocationSize);
67
- return ::new (buffer) AutoDiffLinearMapContext;
67
+ // Create a linear map context object that stores the projection
68
+ // for the top level context
69
+ auto linearMapContext =
70
+ new AutoDiffLinearMapContext (toplevelContextProjection);
71
+
72
+ // Stash away the `HeapObject` pointer for the allocated context
73
+ // for proper "release" during clean up.
74
+ linearMapContext->storeAllocatedHeapObjectPtr (topLevelContextHeapObjectPtr);
75
+
76
+ // Return the newly created linear map context object
77
+ return linearMapContext;
68
78
}
69
79
70
80
void *swift::swift_autoDiffProjectTopLevelSubcontext (
71
- AutoDiffLinearMapContext *allocator) {
72
- return allocator->projectTopLevelSubcontext ();
81
+ AutoDiffLinearMapContext *linearMapContext) {
82
+ return static_cast <void *>(
83
+ linearMapContext->getTopLevelLinearMapContextProjection ());
73
84
}
74
85
75
86
void *swift::swift_autoDiffAllocateSubcontext (
76
- AutoDiffLinearMapContext *allocator, size_t size) {
77
- return allocator->allocate (size);
87
+ AutoDiffLinearMapContext *linearMapContext,
88
+ const Metadata *linearMapSubcontextMetadata) {
89
+ // Linear map context metadata must have non-null value witnesses
90
+ assert (linearMapSubcontextMetadata->getValueWitnesses ());
91
+
92
+ // Allocate a box for the linear map subcontext
93
+ auto [subcontextHeapObjectPtr, subcontextProjection] =
94
+ swift_allocBox (linearMapSubcontextMetadata);
95
+
96
+ // Stash away the `HeapObject` pointer for the allocated context
97
+ // for proper "release" during clean up.
98
+ linearMapContext->storeAllocatedHeapObjectPtr (subcontextHeapObjectPtr);
99
+
100
+ // Return the subcontext projection
101
+ return static_cast <void *>(subcontextProjection);
78
102
}
0 commit comments