-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] devirtualize diff witnesses #28480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a18086
[AutoDiff] inline diff witnesses
3c5f041
handle case where one module has a declaration and another has a full…
8108c4d
address comments
5970363
rename inliner => devirtualizer
7d0e396
fix mistake
13a9205
Fix renamed sil-opt flag.
rxwei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lib/SILOptimizer/Transforms/DifferentiabilityWitnessDevirtualizer.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===--- DifferentiabilityWitnessDevirtualizer.cpp ------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2019 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Devirtualized differentiability witnesses whose bodies are availabe, by | ||
// turning "differentiability_witness_function" instructions into "function_ref" | ||
// instructions referencing the appropriate function. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/SIL/SILBuilder.h" | ||
#include "swift/SIL/SILFunction.h" | ||
#include "swift/SIL/SILInstruction.h" | ||
#include "swift/SILOptimizer/PassManager/Transforms.h" | ||
|
||
using namespace swift; | ||
|
||
namespace { | ||
class DifferentiabilityWitnessDevirtualizer : public SILFunctionTransform { | ||
|
||
/// Returns true if and changes were made. | ||
bool devirtualizeDifferentiabilityWitnessesInFunction(SILFunction &f); | ||
|
||
/// The entry point to the transformation. | ||
void run() override { | ||
if (devirtualizeDifferentiabilityWitnessesInFunction(*getFunction())) | ||
invalidateAnalysis(SILAnalysis::InvalidationKind::CallsAndInstructions); | ||
} | ||
}; | ||
} // end anonymous namespace | ||
|
||
bool DifferentiabilityWitnessDevirtualizer:: | ||
devirtualizeDifferentiabilityWitnessesInFunction(SILFunction &f) { | ||
bool changed = false; | ||
llvm::SmallVector<DifferentiabilityWitnessFunctionInst *, 8> insts; | ||
for (auto &bb : f) { | ||
for (auto &inst : bb) { | ||
auto *dfwi = dyn_cast<DifferentiabilityWitnessFunctionInst>(&inst); | ||
if (!dfwi) | ||
continue; | ||
insts.push_back(dfwi); | ||
} | ||
} | ||
for (auto *inst : insts) { | ||
auto *wit = inst->getWitness(); | ||
if (wit->isDeclaration()) | ||
f.getModule().loadDifferentiabilityWitness(wit); | ||
if (wit->isDeclaration()) | ||
continue; | ||
changed = true; | ||
SILBuilderWithScope builder(inst); | ||
auto kind = inst->getWitnessKind().getAsDerivativeFunctionKind(); | ||
assert(kind.hasValue()); | ||
auto *newInst = builder.createFunctionRefFor(inst->getLoc(), | ||
wit->getDerivative(*kind)); | ||
inst->replaceAllUsesWith(newInst); | ||
inst->getParent()->erase(inst); | ||
} | ||
return changed; | ||
} | ||
|
||
SILTransform *swift::createDifferentiabilityWitnessDevirtualizer() { | ||
return new DifferentiabilityWitnessDevirtualizer(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: %target-sil-opt -differentiability-witness-devirtualizer %s -enable-sil-verify-all | %FileCheck %s | ||
|
||
sil_stage raw | ||
|
||
import Swift | ||
import Builtin | ||
|
||
sil_differentiability_witness [parameters 0] [results 0] @witness_defined_in_module : $@convention(thin) (Float) -> Float { | ||
jvp: @witness_defined_in_module_jvp : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) | ||
vjp: @witness_defined_in_module_vjp : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) | ||
} | ||
|
||
sil_differentiability_witness [parameters 0] [results 0] @witness_definition_not_available : $@convention(thin) (Float) -> Float | ||
|
||
// This is an example of a witness that is available (via deserialization) | ||
// even though it is not defined in the current module. | ||
// witness for static Swift.Float.+ infix(Swift.Float, Swift.Float) -> Swift.Float | ||
sil_differentiability_witness [parameters 0 1] [results 0] @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float | ||
|
||
sil @witness_defined_in_module : $@convention(thin) (Float) -> Float | ||
|
||
sil @witness_defined_in_module_jvp : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) | ||
|
||
sil @witness_defined_in_module_vjp : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) | ||
|
||
sil @witness_definition_not_available : $@convention(thin) (Float) -> Float | ||
|
||
sil public_external [transparent] [serialized] @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float | ||
|
||
sil @test : $@convention(thin) (Float) -> () { | ||
bb0(%0 : $Float): | ||
%1 = differentiability_witness_function [vjp] [parameters 0] [results 0] @witness_defined_in_module : $@convention(thin) (Float) -> Float | ||
// CHECK: %1 = function_ref @witness_defined_in_module_vjp : $@convention(thin) (Float) -> (Float, @owned @callee_guaranteed (Float) -> Float) | ||
|
||
%2 = differentiability_witness_function [vjp] [parameters 0] [results 0] @witness_definition_not_available : $@convention(thin) (Float) -> Float | ||
// CHECK: %2 = differentiability_witness_function [vjp] [parameters 0] [results 0] @witness_definition_not_available : $@convention(thin) (Float) -> Float | ||
|
||
%3 = differentiability_witness_function [vjp] [parameters 0 1] [results 0] @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float | ||
// CHECK: %3 = function_ref @AD__$sSf1poiyS2f_SftFZ__vjp_src_0_wrt_0_1 : $@convention(method) (Float, Float, @thin Float.Type) -> (Float, @owned @callee_guaranteed (Float) -> (Float, Float)) | ||
|
||
return undef : $() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain when this condition is true and
convertToDefinition
is called?It doesn't seem wholly obvious, perhaps an explanatory comment would be good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done