@@ -21,13 +21,7 @@ using namespace llvm;
2121
2222SWIFT_CC (swift)
2323static void destroyLinearMapContext(SWIFT_CONTEXT HeapObject *obj) {
24- auto *linearMapContext = static_cast <AutoDiffLinearMapContext *>(obj);
25-
26- for (auto *heapObjectPtr : linearMapContext->getAllocatedHeapObjects ()) {
27- swift_release (heapObjectPtr);
28- }
29-
30- linearMapContext->~AutoDiffLinearMapContext ();
24+ static_cast <AutoDiffLinearMapContext *>(obj)->~AutoDiffLinearMapContext ();
3125 free (obj);
3226}
3327
@@ -50,53 +44,48 @@ static FullMetadata<HeapMetadata> linearMapContextHeapMetadata = {
5044};
5145
5246AutoDiffLinearMapContext::AutoDiffLinearMapContext (
53- OpaqueValue * const topLevelLinearMapContextProjection )
47+ const Metadata *topLevelLinearMapContextMetadata )
5448 : HeapObject(&linearMapContextHeapMetadata) {
55- this ->topLevelLinearMapContextProjection = topLevelLinearMapContextProjection;
49+ allocatedContextObjects.push_back (AllocatedContextObjectRecord{
50+ topLevelLinearMapContextMetadata, projectTopLevelSubcontext ()});
5651}
5752
58- AutoDiffLinearMapContext *swift::swift_autoDiffCreateLinearMapContext (
59- const Metadata *topLevelLinearMapContextMetadata) {
60- // Linear map context metadata must have non-null value witnesses
61- assert (topLevelLinearMapContextMetadata->getValueWitnesses ());
62-
63- // Allocate a box for the top-level linear map context
64- auto [topLevelContextHeapObjectPtr, toplevelContextProjection] =
65- swift_allocBox (topLevelLinearMapContextMetadata);
66-
67- // Create a linear map context object that stores the projection
68- // for the top level context
69- auto linearMapContext =
70- new AutoDiffLinearMapContext (toplevelContextProjection);
53+ void *AutoDiffLinearMapContext::projectTopLevelSubcontext () const {
54+ auto offset = alignTo (sizeof (AutoDiffLinearMapContext),
55+ alignof (AutoDiffLinearMapContext));
56+ return const_cast <uint8_t *>(reinterpret_cast <const uint8_t *>(this ) +
57+ offset);
58+ }
7159
72- // Stash away the `HeapObject` pointer for the allocated context
73- // for proper "release" during clean up.
74- linearMapContext->storeAllocatedHeapObjectPtr (topLevelContextHeapObjectPtr);
60+ void *AutoDiffLinearMapContext::allocateSubcontext (
61+ const Metadata *contextObjectMetadata) {
62+ auto size = contextObjectMetadata->vw_size ();
63+ auto align = contextObjectMetadata->vw_alignment ();
64+ auto *contextObjectPtr = allocator.Allocate (size, align);
65+ allocatedContextObjects.push_back (
66+ AllocatedContextObjectRecord{contextObjectMetadata, contextObjectPtr});
67+ return contextObjectPtr;
68+ }
7569
76- // Return the newly created linear map context object
77- return linearMapContext;
70+ AutoDiffLinearMapContext *swift::swift_autoDiffCreateLinearMapContext (
71+ const Metadata *topLevelLinearMapContextMetadata) {
72+ auto topLevelLinearMapContextSize =
73+ topLevelLinearMapContextMetadata->vw_size ();
74+ auto allocationSize = alignTo (sizeof (AutoDiffLinearMapContext),
75+ alignof (AutoDiffLinearMapContext)) +
76+ topLevelLinearMapContextSize;
77+ auto *buffer = (AutoDiffLinearMapContext *)malloc (allocationSize);
78+ return ::new (buffer)
79+ AutoDiffLinearMapContext (topLevelLinearMapContextMetadata);
7880}
7981
8082void *swift::swift_autoDiffProjectTopLevelSubcontext (
8183 AutoDiffLinearMapContext *linearMapContext) {
82- return static_cast <void *>(
83- linearMapContext->getTopLevelLinearMapContextProjection ());
84+ return static_cast <void *>(linearMapContext->projectTopLevelSubcontext ());
8485}
8586
8687void *swift::swift_autoDiffAllocateSubcontext (
8788 AutoDiffLinearMapContext *linearMapContext,
8889 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);
90+ return linearMapContext->allocateSubcontext (linearMapSubcontextMetadata);
10291}
0 commit comments