mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Swift Optimizer: add a pass to cleanup debug_step instructions
If a `debug_step` has the same debug location as a previous or succeeding instruction it is removed. It's just important that there is at least one instruction for a certain debug location so that single stepping on that location will work.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
swift_compiler_sources(Optimizer
|
||||
AssumeSingleThreaded.swift
|
||||
CleanupDebugSteps.swift
|
||||
ComputeEscapeEffects.swift
|
||||
ComputeSideEffects.swift
|
||||
ObjCBridgingOptimization.swift
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//===--- SimplificationPasses.swift ----------------------------------------==//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2023 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SIL
|
||||
|
||||
/// Removes redundant `debug_step` instructions.
|
||||
/// If a `debug_step` has the same debug location as a previous or succeeding instruction
|
||||
/// it is removed. It's just important that there is at least one instruction for a
|
||||
/// certain debug location so that single stepping on that location will work.
|
||||
let cleanupDebugStepsPass = FunctionPass(name: "cleanup-debug-steps") {
|
||||
(function: Function, context: FunctionPassContext) in
|
||||
|
||||
for block in function.blocks {
|
||||
cleanupDebugSteps(in: block, context)
|
||||
}
|
||||
}
|
||||
|
||||
private func cleanupDebugSteps(in block: BasicBlock, _ context: FunctionPassContext) {
|
||||
var lastInstWithSameLocation: Instruction?
|
||||
|
||||
for inst in block.instructions {
|
||||
if !inst.location.isDebugSteppable {
|
||||
if inst is DebugStepInst && !inst.location.isDebugSteppable {
|
||||
// First case: the instruction which is replaced by the debug_step didn't have a valid
|
||||
// location itself. Then we don't need the debug_step either.
|
||||
context.erase(instruction: inst)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if let li = lastInstWithSameLocation,
|
||||
!inst.location.hasSameSourceLocation(as: li.location) {
|
||||
lastInstWithSameLocation = nil
|
||||
}
|
||||
|
||||
// Only instructions which are really compiled down to some machine instructions can be
|
||||
// single stepped on.
|
||||
if !inst.producesMachineCode {
|
||||
continue
|
||||
}
|
||||
|
||||
if let li = lastInstWithSameLocation {
|
||||
if inst is DebugStepInst {
|
||||
|
||||
// Second case:
|
||||
// %li = some_instruction, loc "l"
|
||||
// debug_step, loc "l" // current inst -> erase
|
||||
context.erase(instruction: inst)
|
||||
continue
|
||||
} else if li is DebugStepInst {
|
||||
|
||||
// Third case:
|
||||
// debug_step, loc "l" // li -> erase
|
||||
// %inst = some_instruction, loc "l" // current inst
|
||||
context.erase(instruction: li)
|
||||
}
|
||||
}
|
||||
lastInstWithSameLocation = inst
|
||||
}
|
||||
}
|
||||
|
||||
private extension Instruction {
|
||||
var producesMachineCode: Bool {
|
||||
switch self {
|
||||
// We could include more instructions here.
|
||||
// In worst case a debug_step instruction remains in the code although it's not needed.
|
||||
// This is harmless.
|
||||
case is DebugStepInst, is ApplySite, is LoadInst, is StoreInst, is TermInst:
|
||||
return location.isDebugSteppable
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,7 @@ private func registerSwiftPasses() {
|
||||
registerPass(simplificationPass, { simplificationPass.run($0) })
|
||||
registerPass(ononeSimplificationPass, { ononeSimplificationPass.run($0) })
|
||||
registerPass(lateOnoneSimplificationPass, { lateOnoneSimplificationPass.run($0) })
|
||||
registerPass(cleanupDebugStepsPass, { cleanupDebugStepsPass.run($0) })
|
||||
|
||||
// Instruction passes
|
||||
registerForSILCombine(BeginCOWMutationInst.self, { run(BeginCOWMutationInst.self, $0) })
|
||||
|
||||
@@ -395,6 +395,8 @@ SWIFT_FUNCTION_PASS(OnoneSimplification, "onone-simplification",
|
||||
"Peephole simplifications which runs at -Onone")
|
||||
SWIFT_FUNCTION_PASS(LateOnoneSimplification, "late-onone-simplification",
|
||||
"Peephole simplifications which can only run late in the -Onone pipeline")
|
||||
SWIFT_FUNCTION_PASS(CleanupDebugSteps, "cleanup-debug-steps",
|
||||
"Cleanup debug_step instructions for Onone")
|
||||
PASS(SimplifyBBArgs, "simplify-bb-args",
|
||||
"SIL Block Argument Simplification")
|
||||
PASS(SimplifyCFG, "simplify-cfg",
|
||||
|
||||
154
test/SILOptimizer/cleanup_debugstep.sil
Normal file
154
test/SILOptimizer/cleanup_debugstep.sil
Normal file
@@ -0,0 +1,154 @@
|
||||
// RUN: %target-sil-opt -enable-sil-verify-all %s -cleanup-debug-steps -sil-print-debuginfo | %FileCheck %s
|
||||
|
||||
// REQUIRES: swift_in_compiler
|
||||
|
||||
import Swift
|
||||
import Builtin
|
||||
|
||||
sil @f : $@convention(thin) () -> ()
|
||||
|
||||
// CHECK-LABEL: sil @remove_after_previous_location
|
||||
// CHECK-NOT: debug_step
|
||||
// CHECK: } // end sil function 'remove_after_previous_location'
|
||||
sil @remove_after_previous_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> (), loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @dont_remove_after_previous_location
|
||||
// CHECK: debug_step
|
||||
// CHECK: } // end sil function 'dont_remove_after_previous_location'
|
||||
sil @dont_remove_after_previous_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> (), loc "a.swift":1:2
|
||||
%r = tuple (), loc "a.swift":27:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @remove_two_after_previous_location
|
||||
// CHECK-NOT: debug_step
|
||||
// CHECK: } // end sil function 'remove_two_after_previous_location'
|
||||
sil @remove_two_after_previous_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> (), loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @remove_before_next_location
|
||||
// CHECK-NOT: debug_step
|
||||
// CHECK: } // end sil function 'remove_before_next_location'
|
||||
sil @remove_before_next_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple (), loc "<compiler-generated>":0:0
|
||||
return %r : $(), loc "a.swift":1:2
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @dont_remove_before_next_location
|
||||
// CHECK: debug_step
|
||||
// CHECK: } // end sil function 'dont_remove_before_next_location'
|
||||
sil @dont_remove_before_next_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple (), loc "a.swift":27:2
|
||||
return %r : $(), loc "a.swift":1:2
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @remove_two_before_next_location
|
||||
// CHECK-NOT: debug_step
|
||||
// CHECK: } // end sil function 'remove_two_before_next_location'
|
||||
sil @remove_two_before_next_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple (), loc "<compiler-generated>":0:0
|
||||
return %r : $(), loc "a.swift":1:2
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @remove_null_location
|
||||
// CHECK-NOT: debug_step
|
||||
// CHECK: } // end sil function 'remove_null_location'
|
||||
sil @remove_null_location : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "<compiler-generated>":0:0
|
||||
%r = tuple ()
|
||||
return %r : $(), loc "a.swift":1:2
|
||||
}
|
||||
// CHECK-LABEL: sil @dont_remove_one
|
||||
// CHECK: debug_step
|
||||
// CHECK: } // end sil function 'dont_remove_one'
|
||||
sil @dont_remove_one : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @replace_three_with_one
|
||||
// CHECK: apply
|
||||
// CHECK-NEXT: debug_step , loc "a.swift":1:2
|
||||
// CHECK-NEXT: tuple
|
||||
// CHECK: } // end sil function 'replace_three_with_one'
|
||||
sil @replace_three_with_one : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":1:2
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
// CHECK-LABEL: sil @replace_three_with_two
|
||||
// CHECK: apply
|
||||
// CHECK-NEXT: debug_step , loc "a.swift":1:2
|
||||
// CHECK-NEXT: debug_step , loc "a.swift":2:3
|
||||
// CHECK-NEXT: tuple
|
||||
// CHECK: } // end sil function 'replace_three_with_two'
|
||||
sil @replace_three_with_two : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":2:3
|
||||
debug_step, loc "a.swift":2:3
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
|
||||
// CHECK-LABEL: sil @dont_remove_two
|
||||
// CHECK: debug_step
|
||||
// CHECK-NEXT: debug_step
|
||||
// CHECK: } // end sil function 'dont_remove_two'
|
||||
sil @dont_remove_two : $@convention(thin) () -> () {
|
||||
bb0:
|
||||
%f = function_ref @f : $@convention(thin) () -> ()
|
||||
%a = apply %f() : $@convention(thin) () -> ()
|
||||
debug_step, loc "a.swift":1:2
|
||||
debug_step, loc "a.swift":2:3
|
||||
%r = tuple ()
|
||||
return %r : $()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user