Skip to content

[IRGen] Zero-init stack shadow copies of values at -Onone. #16486

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
May 11, 2018
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
56 changes: 31 additions & 25 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,32 @@ class IRGenSILFunction :
}
}

/// To make it unambiguous whether a `var` binding has been initialized,
/// zero-initialize the first pointer-sized field. LLDB uses this to
/// recognize to detect uninitizialized variables. This can be removed once
/// swiftc switches to @llvm.dbg.addr() intrinsics.
void zeroInit(llvm::AllocaInst *AI) {
if (!AI)
return;

// Only do this at -Onone.
if (IGM.IRGen.Opts.shouldOptimize())
return;

auto &DL = IGM.DataLayout;
if (DL.getTypeSizeInBits(AI->getAllocatedType()) <
DL.getPointerSizeInBits())
return;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// No debug location is how LLVM marks prologue instructions.

llvm::IRBuilder<> ZeroInitBuilder(AI->getNextNode());
ZeroInitBuilder.SetCurrentDebugLocation(nullptr);
auto *BC =
ZeroInitBuilder.CreateBitCast(AI, IGM.OpaquePtrTy->getPointerTo());
ZeroInitBuilder.CreateAlignedStore(
llvm::ConstantPointerNull::get(IGM.OpaquePtrTy), BC,
IGM.getPointerAlignment().getValue());
}

/// Account for bugs in LLVM.
///
/// - The LLVM type legalizer currently doesn't update debug
Expand All @@ -742,6 +768,7 @@ class IRGenSILFunction :
auto &Alloca = ShadowStackSlots[{ArgNo, {Scope, Name}}];
if (!Alloca.isValid())
Alloca = createAlloca(Storage->getType(), Align, Name+".addr");
zeroInit(dyn_cast<llvm::AllocaInst>(Alloca.getAddress()));

ArtificialLocation AutoRestore(Scope, IGM.DebugInfo, Builder);
Builder.CreateStore(Storage, Alloca.getAddress(), Align);
Expand Down Expand Up @@ -4000,31 +4027,10 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
if (!Decl)
return;

// To make it unambiguous whether a `var` binding has been initialized,
// zero-initialize the first pointer-sized field. LLDB uses this to
// recognize to detect uninitizialized variables. This can be removed once
// swiftc switches to @llvm.dbg.addr() intrinsics.
auto zeroInit = [&]() {
if (IGM.IRGen.Opts.shouldOptimize())
return;
Type Desugared = Decl->getType()->getDesugaredType();
if (!Desugared->getClassOrBoundGenericClass() &&
!Desugared->getStructOrBoundGenericStruct())
return;

auto *AI = dyn_cast<llvm::AllocaInst>(addr.getAddress().getAddress());
if (!AI)
return;

auto &DL = IGM.DataLayout;
if (DL.getTypeSizeInBits(AI->getAllocatedType()) < DL.getPointerSizeInBits())
return;

auto *BC = Builder.CreateBitCast(AI, IGM.OpaquePtrTy->getPointerTo());
Builder.CreateStore(llvm::ConstantPointerNull::get(IGM.OpaquePtrTy), BC,
IGM.getPointerAlignment());
};
zeroInit();
Type Desugared = Decl->getType()->getDesugaredType();
if (Desugared->getClassOrBoundGenericClass() ||
Desugared->getStructOrBoundGenericStruct())
zeroInit(dyn_cast<llvm::AllocaInst>(addr.getAddress().getAddress()));
emitDebugInfoForAllocStack(i, type, addr.getAddress().getAddress());
}

Expand Down
3 changes: 1 addition & 2 deletions test/DebugInfo/returnlocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import Foundation
// CHECK_NONE: define{{( protected)?}} {{.*}}void {{.*}}none
public func none(_ a: inout Int64) {
// CHECK_NONE: call void @llvm.dbg{{.*}}, !dbg
// CHECK_NONE: store{{.*}}, !dbg
// CHECK_NONE: !dbg ![[NONE_INIT:.*]]
// CHECK_NONE: store i64{{.*}}, !dbg ![[NONE_INIT:.*]]
a -= 2
// CHECK_NONE: ret {{.*}}, !dbg ![[NONE_RET:.*]]
// CHECK_NONE: ![[NONE_INIT]] = !DILocation(line: [[@LINE-2]], column:
Expand Down
2 changes: 2 additions & 0 deletions test/DebugInfo/shadowcopy-linetable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ func foo(_ x: inout Int64) {
// not.
// CHECK: %[[X:.*]] = alloca %Ts5Int64V*, align {{(4|8)}}
// CHECK-NEXT: call void @llvm.dbg.declare
// CHECK-NEXT: [[ZEROED:%[0-9]+]] = bitcast %Ts5Int64V** %[[X]] to %swift.opaque**
// CHECK-NEXT: store %swift.opaque* null, %swift.opaque** [[ZEROED]], align {{(4|8)}}
// CHECK: store %Ts5Int64V* %0, %Ts5Int64V** %[[X]], align {{(4|8)}}
// CHECK-SAME: !dbg ![[LOC0:.*]]
// CHECK-NEXT: getelementptr inbounds %Ts5Int64V, %Ts5Int64V* %0, i32 0, i32 0,
Expand Down
8 changes: 4 additions & 4 deletions test/DebugInfo/uninitialized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public func f() {
var object: MyClass
// CHECK: %[[OBJ:.*]] = alloca %[[T1:.*]]*, align
// CHECK: call void @llvm.dbg.declare(metadata %[[T1]]** %[[OBJ]],
// CHECK: %[[BC1:.*]] = bitcast %[[T1]]** %[[OBJ]] to %swift.opaque**, !dbg
// CHECK: store %swift.opaque* null, %swift.opaque** %[[BC1]], align {{.*}}, !dbg
// CHECK: %[[BC1:.*]] = bitcast %[[T1]]** %[[OBJ]] to %swift.opaque**
// CHECK: store %swift.opaque* null, %swift.opaque** %[[BC1]], align {{.*}}
// OPT-NOT: store
// OPT: ret
}
Expand All @@ -20,8 +20,8 @@ public func g() {
var dict: Dictionary<Int64, Int64>
// CHECK: %[[DICT:.*]] = alloca %[[T2:.*]], align
// CHECK: call void @llvm.dbg.declare(metadata %[[T2]]* %[[DICT]],
// CHECK: %[[BC2:.*]] = bitcast %[[T2]]* %[[DICT]] to %swift.opaque**, !dbg
// CHECK: store %swift.opaque* null, %swift.opaque** %[[BC2]], align {{.*}}, !dbg
// CHECK: %[[BC2:.*]] = bitcast %[[T2]]* %[[DICT]] to %swift.opaque**
// CHECK: store %swift.opaque* null, %swift.opaque** %[[BC2]], align {{.*}}
// OPT-NOT: store
// OPT: ret
}