Skip to content

Commit 78c1870

Browse files
committed
Merge pull request #2926 from adrian-prantl/master
2 parents 1325a28 + 4cee546 commit 78c1870

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ llvm::GlobalVariable *LinkInfo::createVariable(IRGenModule &IGM,
13951395
if (IGM.DebugInfo && !DbgTy.isNull() && ForDefinition)
13961396
IGM.DebugInfo->emitGlobalVariableDeclaration(
13971397
var, DebugName.empty() ? getName() : DebugName, getName(), DbgTy,
1398-
DebugLoc);
1398+
var->hasInternalLinkage(), DebugLoc);
13991399

14001400
return var;
14011401
}

lib/IRGen/GenInit.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
#include "swift/SIL/SILGlobalVariable.h"
2222
#include "llvm/IR/GlobalVariable.h"
2323

24+
#include "DebugTypeInfo.h"
2425
#include "Explosion.h"
2526
#include "GenHeap.h"
2627
#include "GenTuple.h"
28+
#include "IRGenDebugInfo.h"
2729
#include "IRGenFunction.h"
2830
#include "IRGenModule.h"
2931
#include "FixedTypeInfo.h"
@@ -37,8 +39,17 @@ Address IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
3739

3840
// If the variable is empty in all resilience domains, don't actually emit it;
3941
// just return undef.
40-
if (ti.isKnownEmpty(ResilienceExpansion::Minimal))
42+
if (ti.isKnownEmpty(ResilienceExpansion::Minimal)) {
43+
if (DebugInfo && var->getDecl()) {
44+
auto Zero = llvm::ConstantInt::get(Int64Ty, 0);
45+
DebugTypeInfo DbgTy(var->getDecl(), var->getLoweredType().getSwiftType(),
46+
Int8Ty, Size(0), Alignment(1));
47+
DebugInfo->emitGlobalVariableDeclaration(
48+
Zero, var->getDecl()->getName().str(), "", DbgTy,
49+
var->getLinkage() != SILLinkage::Public, SILLocation(var->getDecl()));
50+
}
4151
return ti.getUndefAddress();
52+
}
4253

4354
/// Get the global variable.
4455
Address addr = getAddrOfSILGlobalVariable(var, ti,

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,9 @@ void IRGenDebugInfo::emitVariableDeclaration(
949949

950950
// Emit locationless intrinsic for variables that were optimized away.
951951
if (Storage.size() == 0) {
952-
auto *undef = llvm::UndefValue::get(DbgTy.StorageType);
953-
emitDbgIntrinsic(BB, undef, Var, DBuilder.createExpression(), Line,
952+
auto Zero =
953+
llvm::ConstantInt::get(llvm::Type::getInt64Ty(M.getContext()), 0);
954+
emitDbgIntrinsic(BB, Zero, Var, DBuilder.createExpression(), Line,
954955
Loc.Column, Scope, DS);
955956
}
956957
}
@@ -982,10 +983,11 @@ void IRGenDebugInfo::emitDbgIntrinsic(llvm::BasicBlock *BB,
982983
}
983984

984985

985-
void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::GlobalValue *Var,
986+
void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::Constant *Var,
986987
StringRef Name,
987988
StringRef LinkageName,
988989
DebugTypeInfo DbgTy,
990+
bool IsLocalToUnit,
989991
Optional<SILLocation> Loc) {
990992
if (Opts.DebugInfoKind == IRGenDebugInfoKind::LineTables)
991993
return;
@@ -1003,7 +1005,7 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::GlobalValue *Var,
10031005

10041006
// Emit it as global variable of the current module.
10051007
DBuilder.createGlobalVariable(MainModule, Name, LinkageName, File, L.Line, Ty,
1006-
Var->hasInternalLinkage(), Var, nullptr);
1008+
IsLocalToUnit, Var, nullptr);
10071009
}
10081010

10091011
StringRef IRGenDebugInfo::getMangledName(DebugTypeInfo DbgTy) {
@@ -1259,12 +1261,6 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
12591261
// to emit the (target!) size of the underlying basic type.
12601262
uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
12611263
uint64_t SizeInBits = DbgTy.size.getValue() * SizeOfByte;
1262-
// Prefer the actual storage size over the DbgTy.
1263-
if (DbgTy.StorageType && DbgTy.StorageType->isSized()) {
1264-
uint64_t Storage = IGM.DataLayout.getTypeSizeInBits(DbgTy.StorageType);
1265-
if (Storage)
1266-
SizeInBits = Storage;
1267-
}
12681264
uint64_t AlignInBits = DbgTy.align.getValue() * SizeOfByte;
12691265
unsigned Encoding = 0;
12701266
unsigned Flags = 0;

lib/IRGen/IRGenDebugInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ class IRGenDebugInfo {
181181
const SILDebugScope *DS);
182182

183183
/// Create debug metadata for a global variable.
184-
void emitGlobalVariableDeclaration(llvm::GlobalValue *Storage, StringRef Name,
184+
void emitGlobalVariableDeclaration(llvm::Constant *Storage, StringRef Name,
185185
StringRef LinkageName,
186186
DebugTypeInfo DebugType,
187+
bool IsLocalToUnit,
187188
Optional<SILLocation> Loc);
188189

189190
/// Emit debug metadata for type metadata (for generic types). So meta.

test/DebugInfo/letclause.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ func peek() -> Symbol? { return Symbol() }
66

77
func foo() {
88
// CHECK: define {{.*}}foo
9-
// CHECK: call void @llvm.dbg.declare(metadata %V9letclause6Symbol undef, metadata ![[S:.*]], metadata !{{[0-9]+}})
9+
// CHECK: call void @llvm.dbg.value(metadata i{{.*}} 0,
10+
// CHECK-SAME: metadata ![[S:.*]], metadata !{{[0-9]+}})
1011
// CHECK: ![[S]] = !DILocalVariable(name: "s"
1112
// CHECK-SAME: line: [[@LINE+1]],
1213
while let s = peek() {

test/DebugInfo/nostorage.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
// RUN: %target-swift-frontend %s -emit-ir -g -o - | FileCheck %s
1+
// RUN: %target-swift-frontend %s -emit-ir -g -o %t
2+
// RUN: cat %t | FileCheck %s --check-prefix=CHECK1
3+
// RUN: cat %t | FileCheck %s --check-prefix=CHECK2
4+
// RUN: cat %t | FileCheck %s --check-prefix=CHECK3
25

3-
func markUsed<T>(_ t: T) {}
6+
func used<T>(_ t: T) {}
47

5-
class AClass {
6-
func f () -> Int64 { return 1 }
8+
public class Foo {
9+
func foo() {
10+
{ [weak self] in
11+
// CHECK1: call void @llvm.dbg.value(metadata i{{.*}} 0,
12+
// CHECK1-SAME: metadata ![[TYPE:.*]], metadata
13+
// CHECK1: ![[TYPE]] = !DILocalVariable(name: "type",
14+
// CHECK1-SAME: line: [[@LINE+4]],
15+
// CHECK1-SAME: type: ![[FOO:[0-9]+]]
16+
// CHECK1: ![[FOO]] = !DICompositeType(tag: DW_TAG_structure_type,
17+
// CHECK1-SAME: line: [[@LINE+1]], align: 8, flags:
18+
let type = self.dynamicType
19+
used(type)
20+
}()
21+
}
722
}
823

9-
class AnotherClass : AClass {
10-
override func f() -> Int64 { return 2 }
11-
}
12-
13-
struct AStruct {
14-
func f() -> Int64 { return 3 }
15-
}
24+
struct AStruct {}
1625

17-
// CHECK: define hidden void @_TF9nostorage3appFT_T_()
18-
func app() {
19-
var ac: AClass = AnotherClass()
26+
// CHECK2: define{{.*}}app
27+
public func app() {
2028
// No members? No storage! Emitted as a constant 0, because.
21-
// CHECK: call void @llvm.dbg.value(metadata {{.*}}, i64 0, metadata ![[AT:.*]], metadata !{{[0-9]+}}), !dbg
22-
// CHECK: ![[AT]] = !DILocalVariable(name: "at",
23-
// CHECK-SAME: line: [[@LINE+1]]
29+
// CHECK2: call void @llvm.dbg.value(metadata i{{.*}} 0,
30+
// CHECK2-SAME: metadata ![[AT:.*]], metadata
31+
// CHECK2: ![[AT]] = !DILocalVariable(name: "at",{{.*}}line: [[@LINE+1]]
2432
var at = AStruct()
25-
markUsed("\(ac.f()) \(at.f())")
33+
34+
used(at)
2635
}
2736

28-
app()
37+
public enum empty { case exists }
38+
public let globalvar = empty.exists
39+
// CHECK3: !DIGlobalVariable(name: "globalvar", {{.*}}line: [[@LINE-1]],
40+
// CHECK3-SAME: isLocal: false, isDefinition: true, variable: i64 0)

test/DebugInfo/self-nostorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public struct S {
44
func f() {
55
// CHECK: define {{.*}}_TFV4main1S1ffT_T_
6-
// CHECK: call void @llvm.dbg.declare(metadata %V4main1S undef,
7-
// CHECK-SAME: metadata ![[SELF:[0-9]+]]
6+
// CHECK: call void @llvm.dbg.value(metadata i{{.*}} 0,
7+
// CHECK-SAME: metadata ![[SELF:[0-9]+]]
88
// CHECK: ![[SELF]] = !DILocalVariable(name: "self", arg: 1,
99
}
1010
}

0 commit comments

Comments
 (0)