Skip to content

Swift Optimizer: some reformatting in the optimization passes #67717

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 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

import SIL

let assumeSingleThreadedPass = FunctionPass(
name: "sil-assume-single-threaded", { function, context in
for inst in function.instructions {
guard let rcInst = inst as? RefCountingInst else { continue }
let assumeSingleThreadedPass = FunctionPass(name: "sil-assume-single-threaded") {
(function: Function, context: FunctionPassContext) in

rcInst.setAtomicity(isAtomic: false, context)
}
for inst in function.instructions {
guard let rcInst = inst as? RefCountingInst else { continue }

rcInst.setAtomicity(isAtomic: false, context)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import SIL
/// ```
/// The pass does not try to change or re-compute _defined_ effects.
///
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects") {
(function: Function, context: FunctionPassContext) in

var newEffects = function.effects.escapeEffects.arguments.filter {!$0.isDerived }
Expand Down Expand Up @@ -73,8 +73,7 @@ let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
effects.escapeEffects.arguments = newEffects
}
})

}

/// Returns true if an argument effect was added.
private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import SIL
/// ```
/// are computed.
///
let computeSideEffects = FunctionPass(name: "compute-side-effects", {
let computeSideEffects = FunctionPass(name: "compute-side-effects") {
(function: Function, context: FunctionPassContext) in

if function.isAvailableExternally {
Expand Down Expand Up @@ -71,7 +71,7 @@ let computeSideEffects = FunctionPass(name: "compute-side-effects", {
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
effects.sideEffects = SideEffects(arguments: collectedEffects.argumentEffects, global: collectedEffects.globalEffects)
}
})
}

/// The collected argument and global side effects of the function.
private struct CollectedEffects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import SIL
/// br continue_bb(%5)
/// continue_bb(%bridgedOptionalSwiftValue):
/// ```
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt") {
(function: Function, context: FunctionPassContext) in

if !function.hasOwnership { return }
Expand All @@ -54,7 +54,7 @@ let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
}
}
}
})
}

//===----------------------------------------------------------------------===//
// Top-level optimization functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,42 @@ import SIL
/// The optimization is only done for stack promoted objects because they are
/// known to have no associated objects (which are not explicitly released
/// in the deinit method).
let releaseDevirtualizerPass = FunctionPass(
name: "release-devirtualizer", { function, context in
for block in function.blocks {
// The last `release_value`` or `strong_release`` instruction before the
// deallocation.
var lastRelease: RefCountingInst?

for instruction in block.instructions {
if let release = lastRelease {
// We only do the optimization for stack promoted object, because for
// these we know that they don't have associated objects, which are
// _not_ released by the deinit method.
if let deallocStackRef = instruction as? DeallocStackRefInst {
if !context.continueWithNextSubpassRun(for: release) {
return
}
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
lastRelease = nil
continue
let releaseDevirtualizerPass = FunctionPass(name: "release-devirtualizer") {
(function: Function, context: FunctionPassContext) in

for block in function.blocks {
// The last `release_value`` or `strong_release`` instruction before the
// deallocation.
var lastRelease: RefCountingInst?

for instruction in block.instructions {
if let release = lastRelease {
// We only do the optimization for stack promoted object, because for
// these we know that they don't have associated objects, which are
// _not_ released by the deinit method.
if let deallocStackRef = instruction as? DeallocStackRefInst {
if !context.continueWithNextSubpassRun(for: release) {
return
}
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
lastRelease = nil
continue
}
}

switch instruction {
case is ReleaseValueInst, is StrongReleaseInst:
lastRelease = instruction as? RefCountingInst
case is DeallocRefInst, is SetDeallocatingInst:
switch instruction {
case is ReleaseValueInst, is StrongReleaseInst:
lastRelease = instruction as? RefCountingInst
case is DeallocRefInst, is SetDeallocatingInst:
lastRelease = nil
default:
if instruction.mayRelease {
lastRelease = nil
default:
if instruction.mayRelease {
lastRelease = nil
}
}
}
}
}
}
)
}

/// Tries to de-virtualize the final release of a stack-promoted object.
private func tryDevirtualizeReleaseOfObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ private func log(_ message: @autoclosure () -> String) {
/// inter-procedural analysis. If this is not possible and the `enableMoveInoutStackProtection`
/// option is set, the fallback is to move the argument into a temporary `alloc_stack`
/// and do the unsafe pointer operations on the temporary.
let stackProtection = ModulePass(name: "stack-protection", {
(context: ModulePassContext) in
let stackProtection = ModulePass(name: "stack-protection") {
(context: ModulePassContext) in

if !context.options.enableStackProtection {
return
}

var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
optimization.processModule(context)
})
}

/// The stack-protection optimization on function-level.
///
/// In contrast to the `stack-protection` pass, this pass doesn't do any inter-procedural
/// analysis. It runs at Onone.
let functionStackProtection = FunctionPass(name: "function-stack-protection", {
let functionStackProtection = FunctionPass(name: "function-stack-protection") {
(function: Function, context: FunctionPassContext) in

if !context.options.enableStackProtection {
Expand All @@ -62,7 +62,7 @@ let functionStackProtection = FunctionPass(name: "function-stack-protection", {

var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
optimization.process(function: function, context)
})
}

/// The optimization algorithm.
private struct StackProtectionOptimization {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SIL
/// dumps anything, but aborts if the result is wrong.
///
/// This pass is used for testing `AccessUtils`.
let accessDumper = FunctionPass(name: "dump-access", {
let accessDumper = FunctionPass(name: "dump-access") {
(function: Function, context: FunctionPassContext) in
print("Accesses for \(function.name)")

Expand All @@ -46,7 +46,7 @@ let accessDumper = FunctionPass(name: "dump-access", {
}

print("End accesses for \(function.name)")
})
}

private struct AccessStoragePathVisitor : ValueUseDefWalker {
var walkUpCache = WalkerCache<Path>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

import SIL

let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks", {
let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks") {
(function: Function, context: FunctionPassContext) in


print("Function \(function.name)")

var deadEndBlocks = DeadEndBlocks(function: function, context)
print(deadEndBlocks)
defer { deadEndBlocks.deinitialize() }

print("end function \(function.name)")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import SIL
/// Dumps the EscapeInfo query results for all `alloc_stack` instructions in a function.
///
/// This pass is used for testing EscapeInfo.
let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
let escapeInfoDumper = FunctionPass(name: "dump-escape-info") {
(function: Function, context: FunctionPassContext) in

print("Escape information for \(function.name):")
Expand Down Expand Up @@ -60,16 +60,15 @@ let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
}
}
print("End function \(function.name)\n")
})

}

/// Dumps the results of address-related escape analysis.
///
/// Dumps the EscapeInfo query results for addresses escaping to function calls.
/// The `fix_lifetime` instruction is used as marker for addresses and values to query.
///
/// This pass is used for testing EscapeInfo.
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info") {
(function: Function, context: FunctionPassContext) in

print("Address escape information for \(function.name):")
Expand Down Expand Up @@ -159,4 +158,4 @@ let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
}

print("End function \(function.name)\n")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import SIL

let functionUsesDumper = ModulePass(name: "dump-function-uses", {
let functionUsesDumper = ModulePass(name: "dump-function-uses") {
(context: ModulePassContext) in

var functionUses = FunctionUses()
Expand All @@ -25,4 +25,4 @@ let functionUsesDumper = ModulePass(name: "dump-function-uses", {
print(uses)
print("End function \(function.name)\n")
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import SIL

let rangeDumper = FunctionPass(name: "dump-ranges", {
let rangeDumper = FunctionPass(name: "dump-ranges") {
(function: Function, context: FunctionPassContext) in

var begin: Instruction?
Expand Down Expand Up @@ -73,7 +73,7 @@ let rangeDumper = FunctionPass(name: "dump-ranges", {
assert(!instRange.contains(o))
assert(!instRange.inclusiveRangeContains(o))
}
})
}

private func verify(_ blockRange: BasicBlockRange, _ context: FunctionPassContext) {
var inRange = BasicBlockSet(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import SIL

/// This pass should only be used by sil-opt to run all the unit tests.
///
let runUnitTests = ModulePass(name: "run-unit-tests", {
let runUnitTests = ModulePass(name: "run-unit-tests") {
(context: ModulePassContext) in

print("--- Run unit tests ---")

print("test ProjectionPath")
SmallProjectionPath.runUnitTests()
})
}