forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new simple utility optimization pass for serialization of SILMo…
…dules
- Loading branch information
Showing
11 changed files
with
173 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,53 @@ | ||
//===--- SerializeSILPass.cpp ---------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#define DEBUG_TYPE "serialize-sil" | ||
#include "swift/Strings.h" | ||
#include "swift/SILOptimizer/PassManager/Passes.h" | ||
#include "swift/SILOptimizer/PassManager/Transforms.h" | ||
|
||
using namespace swift; | ||
|
||
/// A utility pass to serialize a SILModule at any place inside the optimization | ||
/// pipeline. | ||
class SerializeSILPass : public SILModuleTransform { | ||
/// Removes [serialized] from all functions. This allows for more | ||
/// optimizations and for a better dead function elimination. | ||
void removeSerializedFlagFromAllFunctions(SILModule &M) { | ||
for (auto &F : M) { | ||
F.setSerialized(IsSerialized_t::IsNotSerialized); | ||
} | ||
} | ||
|
||
public: | ||
SerializeSILPass() {} | ||
void run() override { | ||
auto &M = *getModule(); | ||
// Nothing to do if the module was serialized already. | ||
if (M.isSerialized()) | ||
return; | ||
|
||
// Mark all reachable functions as "anchors" so that they are not | ||
// removed later by the dead function elimination pass. This | ||
// is required, because clients may reference any of the | ||
// serialized functions or anything referenced from them. Therefore, | ||
// to avoid linker errors, the object file of the current module should | ||
// contain all the symbols which were alive at the time of serialization. | ||
DEBUG(llvm::dbgs() << "Serializing SILModule in SerializeSILPass\n"); | ||
getModule()->serialize(); | ||
removeSerializedFlagFromAllFunctions(M); | ||
} | ||
}; | ||
|
||
SILTransform *swift::createSerializeSILPass() { | ||
return new SerializeSILPass(); | ||
} |
This file contains 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,60 @@ | ||
// RUN: %target-sil-opt %s -verify -closure-specialize -assume-parsing-unqualified-ownership-sil -o - | %FileCheck %s | ||
|
||
// Make sure we do not specialize resilientCallee. | ||
|
||
sil_stage canonical | ||
|
||
import Builtin | ||
import Swift | ||
import SwiftShims | ||
|
||
@_semantics("optimize.sil.never") public func action() | ||
|
||
@inline(__always) public func fragileCaller() | ||
|
||
public func resilientCallee(fn: () -> ()) | ||
|
||
// action() | ||
sil [_semantics "optimize.sil.never"] @_T026closure_specialize_fragile6actionyyF : $@convention(thin) () -> () { | ||
bb0: | ||
%0 = tuple () | ||
return %0 : $() | ||
} // end sil function '_T026closure_specialize_fragile6actionyyF' | ||
|
||
// CHECK-LABEL: sil [serialized] [always_inline] @_T026closure_specialize_fragile0C6CalleryyF : $@convention(thin) () -> () | ||
// CHECK: function_ref @_T026closure_specialize_fragile15resilientCalleeyyyc2fn_tF : $@convention(thin) (@owned @callee_owned () -> ()) -> () | ||
// CHECK: return | ||
// fragileCaller() | ||
sil [serialized] [always_inline] @_T026closure_specialize_fragile0C6CalleryyF : $@convention(thin) () -> () { | ||
bb0: | ||
// function_ref resilientCallee(fn:) | ||
%0 = function_ref @_T026closure_specialize_fragile15resilientCalleeyyyc2fn_tF : $@convention(thin) (@owned @callee_owned () -> ()) -> () | ||
// function_ref closure #1 in fragileCaller() | ||
%1 = function_ref @_T026closure_specialize_fragile0C6CalleryyFyycfU_ : $@convention(thin) () -> () | ||
%2 = thin_to_thick_function %1 : $@convention(thin) () -> () to $@callee_owned () -> () | ||
%3 = apply %0(%2) : $@convention(thin) (@owned @callee_owned () -> ()) -> () | ||
%4 = tuple () | ||
return %4 : $() | ||
} // end sil function '_T026closure_specialize_fragile0C6CalleryyF' | ||
|
||
// CHECK-LABEL: sil @_T026closure_specialize_fragile15resilientCalleeyyyc2fn_tF : $@convention(thin) (@owned @callee_owned () -> ()) -> () | ||
|
||
// resilientCallee(fn:) | ||
sil @_T026closure_specialize_fragile15resilientCalleeyyyc2fn_tF : $@convention(thin) (@owned @callee_owned () -> ()) -> () { | ||
bb0(%0 : $@callee_owned () -> ()): | ||
strong_retain %0 : $@callee_owned () -> () | ||
%3 = apply %0() : $@callee_owned () -> () | ||
strong_release %0 : $@callee_owned () -> () | ||
%5 = tuple () | ||
return %5 : $() | ||
} // end sil function '_T026closure_specialize_fragile15resilientCalleeyyyc2fn_tF' | ||
|
||
// closure #1 in fragileCaller() | ||
sil shared [serialized] @_T026closure_specialize_fragile0C6CalleryyFyycfU_ : $@convention(thin) () -> () { | ||
bb0: | ||
// function_ref action() | ||
%0 = function_ref @_T026closure_specialize_fragile6actionyyF : $@convention(thin) () -> () | ||
%1 = apply %0() : $@convention(thin) () -> () | ||
%2 = tuple () | ||
return %2 : $() | ||
} // end sil function '_T026closure_specialize_fragile0C6CalleryyFyycfU_' |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,37 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -O -module-name Swift -module-link-name swiftCore -parse-as-library -parse-stdlib -emit-module -sil-serialize-witness-tables -sil-serialize-vtables %s -o %t/Swift.swiftmodule | ||
// RUN: %target-sil-opt -enable-sil-verify-all %t/Swift.swiftmodule -o - | %FileCheck %s | ||
|
||
// Test that early serialization works as expected: | ||
// - it happens before the performance inlining and thus preserves @_semantics functions | ||
// - it happens after generic specialization | ||
|
||
public struct Int { | ||
@_inlineable | ||
public init() {} | ||
} | ||
|
||
public struct Array<T> { | ||
@_inlineable | ||
public init() {} | ||
|
||
// Check that the generic version of a @_semantics function is preserved. | ||
// CHECK: sil [serialized] [_semantics "array.get_capacity"] @_T0Sa12_getCapacitySiyF : $@convention(method) <T> (Array<T>) -> Int | ||
// Check that a specialized version of a function is produced | ||
// CHECK: sil shared [serializable] [_semantics "array.get_capacity"] @_T0Sa12_getCapacitySiyFSi_Tgq5 : $@convention(method) (Array<Int>) -> Int | ||
@_inlineable | ||
@_versioned | ||
@_semantics("array.get_capacity") | ||
internal func _getCapacity() -> Int { | ||
return Int() | ||
} | ||
} | ||
|
||
// Check that a call of a @_semantics function was not inlined if early-serialization is enabled. | ||
// CHECK: sil [serialized] @_T0s28userOfSemanticsAnnotatedFuncSiSaySiGF | ||
// CHECK: function_ref | ||
// CHECK: apply | ||
@_inlineable | ||
public func userOfSemanticsAnnotatedFunc(_ a: Array<Int>) -> Int { | ||
return a._getCapacity() | ||
} |
This file contains 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