Skip to content

Commit 4cee546

Browse files
committed
Emit debug info for global variables without storage.
As with local variables this is done by emitting a constant 0 location and a type of size zero. <rdar://problem/26660971>
1 parent dba926b commit 4cee546

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,11 @@ void IRGenDebugInfo::emitDbgIntrinsic(llvm::BasicBlock *BB,
983983
}
984984

985985

986-
void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::GlobalValue *Var,
986+
void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::Constant *Var,
987987
StringRef Name,
988988
StringRef LinkageName,
989989
DebugTypeInfo DbgTy,
990+
bool IsLocalToUnit,
990991
Optional<SILLocation> Loc) {
991992
if (Opts.DebugInfoKind == IRGenDebugInfoKind::LineTables)
992993
return;
@@ -1004,7 +1005,7 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::GlobalValue *Var,
10041005

10051006
// Emit it as global variable of the current module.
10061007
DBuilder.createGlobalVariable(MainModule, Name, LinkageName, File, L.Line, Ty,
1007-
Var->hasInternalLinkage(), Var, nullptr);
1008+
IsLocalToUnit, Var, nullptr);
10081009
}
10091010

10101011
StringRef IRGenDebugInfo::getMangledName(DebugTypeInfo DbgTy) {

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/nostorage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-swift-frontend %s -emit-ir -g -o %t
22
// RUN: cat %t | FileCheck %s --check-prefix=CHECK1
33
// RUN: cat %t | FileCheck %s --check-prefix=CHECK2
4+
// RUN: cat %t | FileCheck %s --check-prefix=CHECK3
45

56
func used<T>(_ t: T) {}
67

@@ -32,3 +33,8 @@ public func app() {
3233

3334
used(at)
3435
}
36+
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)

0 commit comments

Comments
 (0)