Files
swift-mirror/stdlib/public/runtime/AutoDiffSupport.cpp
Richard Wei de2dbe57ed [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`.
2020-11-30 15:49:38 -08:00

73 lines
2.1 KiB
C++

//===--- 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);
}