mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[AutoDiff] Bump-pointer allocate pullback structs in loops. (#34886)
In derivatives of loops, no longer allocate boxes for indirect case payloads. Instead, use a custom pullback context in the runtime which contains a bump-pointer allocator. When a function contains a differentiated loop, the closure context is a `Builtin.NativeObject`, which contains a `swift::AutoDiffLinearMapContext` and a tail-allocated top-level linear map struct (which represents the linear map struct that was previously directly partial-applied into the pullback). In branching trace enums, the payloads of previously indirect cases will be allocated by `swift::AutoDiffLinearMapContext::allocate` and stored as a `Builtin.RawPointer`.
This commit is contained in:
72
stdlib/public/runtime/AutoDiffSupport.cpp
Normal file
72
stdlib/public/runtime/AutoDiffSupport.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
//===--- AutoDiffSupport.cpp ----------------------------------*- C++ -*---===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "AutoDiffSupport.h"
|
||||
#include "swift/ABI/Metadata.h"
|
||||
#include "swift/Runtime/HeapObject.h"
|
||||
|
||||
using namespace swift;
|
||||
using namespace llvm;
|
||||
|
||||
SWIFT_CC(swift)
|
||||
static void destroyLinearMapContext(SWIFT_CONTEXT HeapObject *obj) {
|
||||
free(obj);
|
||||
}
|
||||
|
||||
/// Heap metadata for a linear map context.
|
||||
static FullMetadata<HeapMetadata> linearMapContextHeapMetadata = {
|
||||
{
|
||||
{
|
||||
&destroyLinearMapContext
|
||||
},
|
||||
{
|
||||
/*value witness table*/ nullptr
|
||||
}
|
||||
},
|
||||
{
|
||||
MetadataKind::Opaque
|
||||
}
|
||||
};
|
||||
|
||||
AutoDiffLinearMapContext::AutoDiffLinearMapContext()
|
||||
: HeapObject(&linearMapContextHeapMetadata) {
|
||||
}
|
||||
|
||||
void *AutoDiffLinearMapContext::projectTopLevelSubcontext() const {
|
||||
auto offset = alignTo(
|
||||
sizeof(AutoDiffLinearMapContext), alignof(AutoDiffLinearMapContext));
|
||||
return const_cast<uint8_t *>(
|
||||
reinterpret_cast<const uint8_t *>(this) + offset);
|
||||
}
|
||||
|
||||
void *AutoDiffLinearMapContext::allocate(size_t size) {
|
||||
return allocator.Allocate(size, alignof(AutoDiffLinearMapContext));
|
||||
}
|
||||
|
||||
AutoDiffLinearMapContext *swift::swift_autoDiffCreateLinearMapContext(
|
||||
size_t topLevelLinearMapStructSize) {
|
||||
auto allocationSize = alignTo(
|
||||
sizeof(AutoDiffLinearMapContext), alignof(AutoDiffLinearMapContext))
|
||||
+ topLevelLinearMapStructSize;
|
||||
auto *buffer = (AutoDiffLinearMapContext *)malloc(allocationSize);
|
||||
return new (buffer) AutoDiffLinearMapContext;
|
||||
}
|
||||
|
||||
void *swift::swift_autoDiffProjectTopLevelSubcontext(
|
||||
AutoDiffLinearMapContext *allocator) {
|
||||
return allocator->projectTopLevelSubcontext();
|
||||
}
|
||||
|
||||
void *swift::swift_autoDiffAllocateSubcontext(
|
||||
AutoDiffLinearMapContext *allocator, size_t size) {
|
||||
return allocator->allocate(size);
|
||||
}
|
||||
Reference in New Issue
Block a user