Skip to content
Open
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 @@ -180,6 +180,12 @@ private func tryEliminate(copy: CopyLikeInstruction, _ context: FunctionPassCont
use.set(to: copy.destinationAddress, context)
}
}

// Salvage the debug variable attribute, if present.
if let debugVariable = allocStack.debugVariable {
let builder = Builder(before: firstUseOfAllocStack, location: allocStack.location, context)
builder.createDebugValue(value: copy.destinationAddress, debugVariable: debugVariable)
}
context.erase(instruction: allocStack)
context.erase(instructionIncludingAllUsers: copy.loadingInstruction)
}
Expand Down
6 changes: 6 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,17 @@ extension MutatingContext {

public func erase(instructionIncludingDebugUses inst: Instruction) {
precondition(inst.results.allSatisfy { $0.uses.ignoreDebugUses.isEmpty })
salvageDebugInfo(of: inst)
erase(instructionIncludingAllUsers: inst)
}

public func erase(block: BasicBlock) {
_bridged.eraseBlock(block.bridged)
}

/// Transfer debug info associated with (the result of) `instruction` to a
/// new `debug_value` instruction before `instruction` is deleted.
public func salvageDebugInfo(of instruction: Instruction) {
BridgedContext.salvageDebugInfo(instruction.bridged)
}
}
1 change: 1 addition & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,7 @@ struct BridgedContext {
BRIDGED_INLINE void eraseBlock(BridgedBasicBlock block) const;
static BRIDGED_INLINE void moveInstructionBefore(BridgedInstruction inst, BridgedInstruction beforeInst);
static BRIDGED_INLINE void copyInstructionBefore(BridgedInstruction inst, BridgedInstruction beforeInst);
static BRIDGED_INLINE void salvageDebugInfo(BridgedInstruction inst);

// SSAUpdater

Expand Down
5 changes: 5 additions & 0 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "swift/SIL/SILVTable.h"
#include "swift/SIL/SILWitnessTable.h"
#include "swift/SILOptimizer/Utils/ConstExpr.h"
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
#include "swift/SIL/SILConstants.h"
#include <stdbool.h>
#include <stddef.h>
Expand Down Expand Up @@ -3217,6 +3218,10 @@ void BridgedContext::copyInstructionBefore(BridgedInstruction inst, BridgedInstr
inst.unbridged()->clone(beforeInst.unbridged());
}

void BridgedContext::salvageDebugInfo(BridgedInstruction inst) {
swift::salvageDebugInfo(inst.unbridged());
}

OptionalBridgedFunction BridgedContext::lookupStdlibFunction(BridgedStringRef name) const {
return {context->lookupStdlibFunction(name.unbridged())};
}
Expand Down
9 changes: 7 additions & 2 deletions lib/SILOptimizer/Transforms/SILMem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,11 +1968,16 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
if (!runningVals) {
// Loading from uninitialized memory is only acceptable if the type is
// empty--an aggregate of types without storage.
const auto initialValue =
createEmptyAndUndefValue(asi->getElementType(), inst, ctx);
runningVals = {
LiveValues::toReplace(asi,
/*replacement=*/createEmptyAndUndefValue(
asi->getElementType(), inst, ctx)),
/*replacement=*/initialValue),
/*isStorageValid=*/!doesLoadInvalidateStorage(inst)};
if (auto varInfo = asi->getVarInfo()) {
SILBuilderWithScope(inst, ctx).createDebugValue(
inst->getLoc(), initialValue, *varInfo);
}
}
auto *loadInst = dyn_cast<LoadInst>(inst);
if (loadInst &&
Expand Down
5 changes: 2 additions & 3 deletions lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,8 @@ splitAggregateLoad(LoadOperation loadInst, CanonicalizeInstruction &pass) {
}

// Preserve the original load's debug information.
if (pass.preserveDebugInfo) {
swift::salvageLoadDebugInfo(loadInst);
}
swift::salvageLoadDebugInfo(loadInst);

// Remove the now unused borrows.
for (auto *borrow : borrows)
nextII = killInstAndIncidentalUses(borrow, nextII, pass);
Expand Down
2 changes: 1 addition & 1 deletion test/DebugInfo/self-nostorage.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g -o - | %FileCheck %s
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g -O -o - | %FileCheck %s

public struct S {
func f() {
Expand Down
23 changes: 23 additions & 0 deletions test/DebugInfo/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ bb3:


// CHECK-IR: ![[DBG_VAR]] = !DILocalVariable(name: "hello"

struct T {
@_hasStorage let x: Builtin.Int32 { get }
init(x: Builtin.Int32)
}

// This test verifies that splitAggregateLoad in CanonicalizeInstruction.cpp
// salvages debug info attached to the loaded aggregate in optimized builds.
//
// CHECK-LABEL: sil @split_aggregate_load : $@convention(thin) (@in T) -> Builtin.Int32 {
// CHECK: bb0([[ARG:%[0-9]+]] : $*T):
// CHECK-NEXT: [[ELEM_ADDR:%[0-9]+]] = struct_element_addr [[ARG]] : $*T, #T.x
// CHECK-NEXT: [[ELEM:%[0-9]+]] = load [[ELEM_ADDR]]
// CHECK-NEXT: debug_value [[ARG]] : $*T, let, name "var", expr op_deref
// CHECK-NEXT: return [[ELEM]] : $Builtin.Int32
// CHECK-LABEL: } // end sil function 'split_aggregate_load'
sil @split_aggregate_load : $@convention(thin) (@in T) -> Builtin.Int32 {
bb0(%0 : $*T):
%1 = load %0
debug_value %1, let, name "var"
%2 = struct_extract %1, #T.x
return %2
}
43 changes: 43 additions & 0 deletions test/DebugInfo/simplify_builtin.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -simplification -simplify-instruction=builtin -sil-print-debuginfo | %FileCheck %s

// REQUIRES: swift_in_compiler

import Swift
import Builtin

// This verifies that debug information attached to the arguments of builtin
// instructions is preserved when those instructions are constant-folded.
//
// CHECK-LABEL: sil @preserve_bitwise_operands : $@convention(thin) () -> Builtin.Int32 {
// CHECK: bb0:
// CHECK-NEXT: debug_value undef : $Builtin.Int32, let, name "x", expr op_constu:1
// CHECK-NEXT: debug_value undef : $Builtin.Int32, let, name "y", expr op_constu:2
// CHECK-NEXT: [[THREE:%[0-9]+]] = integer_literal $Builtin.Int32, 3
// CHECK-NEXT: return [[THREE]] : $Builtin.Int32
// CHECK-LABEL: } // end sil function 'preserve_bitwise_operands'
sil @preserve_bitwise_operands : $@convention(thin) () -> Builtin.Int32 {
bb0:
%0 = integer_literal $Builtin.Int32, 1
debug_value %0, let, name "x"
%1 = integer_literal $Builtin.Int32, 2
debug_value %1, let, name "y"
%2 = builtin "or_Int32"(%0, %1) : $Builtin.Int32
return %2
}

// CHECK-LABEL: sil @preserve_add_operands : $@convention(thin) () -> Builtin.Int32 {
// CHECK: bb0:
// CHECK-NEXT: debug_value undef : $Builtin.Int32, let, name "x", expr op_consts:4294967295
// CHECK-NEXT: debug_value undef : $Builtin.Int32, let, name "y", expr op_constu:2
// CHECK-NEXT: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int32, 1
// CHECK-NEXT: return [[ONE]] : $Builtin.Int32
// CHECK-LABEL: } // end sil function 'preserve_add_operands'
sil @preserve_add_operands : $@convention(thin) () -> Builtin.Int32 {
bb0:
%0 = integer_literal $Builtin.Int32, -1
debug_value %0, let, name "x"
%1 = integer_literal $Builtin.Int32, 2
debug_value %1, let, name "y"
%2 = builtin "add_Int32"(%0, %1) : $Builtin.Int32
return %2
}
27 changes: 27 additions & 0 deletions test/DebugInfo/simplify_struct_extract.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -simplification -simplify-instruction=struct_extract -sil-print-debuginfo | %FileCheck %s

// REQUIRES: swift_in_compiler

import Builtin
import Swift

struct T {
var x: Builtin.Int32
}

// This verifies that tryReplaceRedundantInstructionPair in OptUtils.swift
// salvages debug info attached to the first instruction of the pair,
// in this case the struct.
//
// CHECK-LABEL: sil @redundant_pair : $@convention(thin) (Builtin.Int32) -> Builtin.Int32 {
// CHECK: bb0([[VAR:%[0-9]+]] : $Builtin.Int32):
// CHECK-NEXT: debug_value [[VAR]] : $Builtin.Int32, let, name "var", type $T, expr op_fragment:#T.x
// CHECK-NEXT: return [[VAR]]
// CHECK-LABEL: } // end sil function 'redundant_pair'
sil @redundant_pair : $@convention(thin) (Builtin.Int32) -> Builtin.Int32 {
bb0(%0 : $Builtin.Int32):
%1 = struct $T(%0)
debug_value %1, let, name "var"
%2 = struct_extract %1, #T.x
return %2
}
26 changes: 26 additions & 0 deletions test/DebugInfo/temp_lvalue_elimination.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %target-sil-opt -sil-print-types -sil-verify-all -temp-lvalue-elimination %s | %FileCheck %s
sil_stage canonical

// REQUIRES: swift_in_compiler

import Builtin

// This verifies that the TempLValueElimination pass correctly salvages the
// debug variable attribute of alloc_stack instructions.
//
// CHECK-LABEL: sil @stack_var_elimination : $@convention(thin) (Builtin.Int32) -> @out Builtin.Int32 {
// CHECK: bb0([[X:%[0-9]+]] : $*Builtin.Int32, [[VAL:%[0-9]+]] : $Builtin.Int32):
// CHECK-NEXT: debug_value [[X]] : $*Builtin.Int32, var, name "x"
// CHECK-NEXT: store [[VAL]] to [[X]] : $*Builtin.Int32
// CHECK-NEXT: [[E:%[0-9]+]] = tuple ()
// CHECK-NEXT: return [[E]] : $()
// CHECK-LABEL: } // end sil function 'stack_var_elimination'
sil @stack_var_elimination : $@convention(thin) (Builtin.Int32) -> @out Builtin.Int32 {
bb0(%0 : $*Builtin.Int32, %1 : $Builtin.Int32):
%2 = alloc_stack [var_decl] $Builtin.Int32, var, name "x"
store %1 to %2
copy_addr %2 to %0
dealloc_stack %2
%3 = tuple ()
return %3
}