[PATCH] D120989: Support debug info for alias variable

2022-04-08 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan added a subscriber: abrachet.
kavitha-natarajan added a comment.

Aliases are not supported on mac. Below commit has made the test to be skipped 
for mac. Thanks @abrachet for the quick fix.

commit 50de659adcc19c4c197ba5e9a7719325af7151ee 

Author: Alex Brachet 
Date:   Thu Apr 7 18:19:54 2022 +

  [clang] Use -triple, not -target for %clang_cc1

commit 3329dae5cb8a8c91c518dd87c09e88c4fad507bd 

Author: Alex Brachet 
Date:   Thu Apr 7 18:17:29 2022 +

  [clang] Fix macos build broken after D120989


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-04-07 Thread Nico Weber via Phabricator via cfe-commits
thakis added a comment.

This breaks tests on mac: 
https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket/8817534773179515649/+/u/package_clang/stdout?format=raw

  Script:
   --
   : 'RUN: at line 1';   
/opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/clang -cc1 
-internal-isystem 
/opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/lib/clang/15.0.0/include
 -nostdsysteminc -emit-llvm -debug-info-kind=limited 
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c
 -o - | /opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/FileCheck 
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c
   --
   Exit Code: 2
   
   Command Output (stderr):
   --
   
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:10:27:
 error: aliases are not supported on darwin
   extern int __attribute__((alias("aliased_global"))) __global_alias;
 ^
   
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:14:27:
 error: aliases are not supported on darwin
   extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
 ^
   
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c:15:27:
 error: aliases are not supported on darwin
   extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
 ^
   3 errors generated.
   FileCheck error: '' is empty.
   FileCheck command line:  
/opt/s/w/ir/cache/builder/src/third_party/llvm-bootstrap/bin/FileCheck 
/opt/s/w/ir/cache/builder/src/third_party/llvm/clang/test/CodeGen/debug-info-alias.c
   
   --
   
   
   Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
   
   Failed Tests (1):
 Clang :: CodeGen/debug-info-alias.c

Probably easiest to pass an explicit triple?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-04-07 Thread Kavitha Natarajan via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb1ea0191a420: [clang][DebugInfo] Support debug info for 
alias variable (authored by kavitha-natarajan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/debug-info-alias.c

Index: clang/test/CodeGen/debug-info-alias.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-alias.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// CHECK-DAG: [[ENTITY1:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global"
+// CHECK-DAG: [[ENTITY2:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global_2"
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias", scope: !2, entity: [[ENTITY1]]
+// CHECK-DAG: [[ENTITY3:![0-9]+]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "global_alias_2", scope: !2, entity: [[ENTITY2]]
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias_2_alias", scope: !2, entity: [[ENTITY3]]
+
+int aliased_global = 1;
+extern int __attribute__((alias("aliased_global"))) __global_alias;
+
+// Recursive alias:
+int aliased_global_2 = 2;
+extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
+extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1222,6 +1222,7 @@
 
   StringRef getMangledName(GlobalDecl GD);
   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
+  const GlobalDecl getMangledNameDecl(StringRef);
 
   void EmitTentativeDefinition(const VarDecl *D);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1502,6 +1502,16 @@
   return Result.first->first();
 }
 
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {
+if (it->second == Name)
+  return it->first;
+it++;
+  }
+  return GlobalDecl();
+}
+
 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
   return getModule().getNamedValue(Name);
 }
@@ -5171,6 +5181,13 @@
   setTLSMode(GA, *VD);
 
   SetCommonAttributes(GD, GA);
+
+  // Emit global alias debug information.
+  if (const auto *VD = dyn_cast(D)) {
+if (CGDebugInfo *DI = getModuleDebugInfo()) {
+  DI->EmitGlobalAlias(cast(GA->getAliasee()), GD);
+}
+  }
 }
 
 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -152,8 +152,10 @@
   llvm::DenseMap DIFileCache;
   llvm::DenseMap SPCache;
   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
-  /// using declarations) that aren't covered by other more specific caches.
+  /// using declarations and global alias variables) that aren't covered
+  /// by other more specific caches.
   llvm::DenseMap DeclCache;
+  llvm::DenseMap ImportedDeclCache;
   llvm::DenseMap NamespaceCache;
   llvm::DenseMap
   NamespaceAliasCache;
@@ -507,6 +509,9 @@
   /// Emit information about an external variable.
   void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
 
+  /// Emit information about global variable alias.
+  void EmitGlobalAlias(const llvm::GlobalValue *GV, const GlobalDecl Decl);
+
   /// Emit C++ using directive.
   void EmitUsingDirective(const UsingDirectiveDecl );
 
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3845,6 +3845,17 @@
 auto N = I->second;
 if (auto *GVE = dyn_cast_or_null(N))
   return GVE->getVariable();
+return cast(N);
+  }
+
+  // Search imported declaration cache if it is already defined
+  // as imported declaration.
+  auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
+
+  if (IE != ImportedDeclCache.end()) {
+auto N = IE->second;
+if (auto *GVE = dyn_cast_or_null(N))
+  return cast(GVE);
 return dyn_cast_or_null(N);
   }
 
@@ -5392,6 +5403,47 @@
   Var->addDebugInfo(GVE);
 }
 
+void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
+  const GlobalDecl GD) {
+
+  assert(GV);
+
+  if 

[PATCH] D120989: Support debug info for alias variable

2022-04-06 Thread David Blaikie via Phabricator via cfe-commits
dblaikie accepted this revision.
dblaikie added a comment.
This revision is now accepted and ready to land.

In D120989#3411553 , 
@kavitha-natarajan wrote:

>> Thanks for the details - can you ping this thread once the gdb thread has 
>> progressed/seems like it's moving in this direction?
>
> @dblaikie, received comments for the gdb patch 
> https://sourceware.org/pipermail/gdb-patches/2022-March/186960.html and the 
> next message with my response. Can you please look through?

Sure, sounds like things are moving in the right direction there - so from my 
perspective, this sounds good. Please wait for @aprantl's sign off as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-28 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan added a comment.

> Thanks for the details - can you ping this thread once the gdb thread has 
> progressed/seems like it's moving in this direction?

@dblaikie, received comments for the gdb patch 
https://sourceware.org/pipermail/gdb-patches/2022-March/186960.html and the 
next message with my response. Can you please look through?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-24 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan updated this revision to Diff 417878.
kavitha-natarajan added a comment.

@aprantl, thanks for your comments. Updated the patch with the fixes.

@dblaikie, haven't got any response from gdb reviewers about the gdb patch yet.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/debug-info-alias.c

Index: clang/test/CodeGen/debug-info-alias.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-alias.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// CHECK-DAG: [[ENTITY1:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global"
+// CHECK-DAG: [[ENTITY2:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global_2"
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias", scope: !2, entity: [[ENTITY1]]
+// CHECK-DAG: [[ENTITY3:![0-9]+]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "global_alias_2", scope: !2, entity: [[ENTITY2]]
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias_2_alias", scope: !2, entity: [[ENTITY3]]
+
+int aliased_global = 1;
+extern int __attribute__((alias("aliased_global"))) __global_alias;
+
+// Recursive alias:
+int aliased_global_2 = 2;
+extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
+extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1213,6 +1213,7 @@
 
   StringRef getMangledName(GlobalDecl GD);
   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
+  const GlobalDecl getMangledNameDecl(StringRef);
 
   void EmitTentativeDefinition(const VarDecl *D);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1498,6 +1498,16 @@
   return Result.first->first();
 }
 
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {
+if (it->second == Name)
+  return it->first;
+it++;
+  }
+  return GlobalDecl();
+}
+
 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
   return getModule().getNamedValue(Name);
 }
@@ -5164,6 +5174,13 @@
   setTLSMode(GA, *VD);
 
   SetCommonAttributes(GD, GA);
+
+  // Emit global alias debug information.
+  if (const auto *VD = dyn_cast(D)) {
+if (CGDebugInfo *DI = getModuleDebugInfo()) {
+  DI->EmitGlobalAlias(cast(GA->getAliasee()), GD);
+}
+  }
 }
 
 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -152,8 +152,10 @@
   llvm::DenseMap DIFileCache;
   llvm::DenseMap SPCache;
   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
-  /// using declarations) that aren't covered by other more specific caches.
+  /// using declarations and global alias variables) that aren't covered
+  /// by other more specific caches.
   llvm::DenseMap DeclCache;
+  llvm::DenseMap ImportedDeclCache;
   llvm::DenseMap NamespaceCache;
   llvm::DenseMap
   NamespaceAliasCache;
@@ -512,6 +514,9 @@
   /// Emit information about an external variable.
   void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
 
+  /// Emit information about global variable alias.
+  void EmitGlobalAlias(const llvm::GlobalValue *GV, const GlobalDecl Decl);
+
   /// Emit C++ using directive.
   void EmitUsingDirective(const UsingDirectiveDecl );
 
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3908,6 +3908,17 @@
 auto N = I->second;
 if (auto *GVE = dyn_cast_or_null(N))
   return GVE->getVariable();
+return cast(N);
+  }
+
+  // Search imported declaration cache if it is already defined
+  // as imported declaration.
+  auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
+
+  if (IE != ImportedDeclCache.end()) {
+auto N = IE->second;
+if (auto *GVE = dyn_cast_or_null(N))
+  return cast(GVE);
 return dyn_cast_or_null(N);
   }
 
@@ -5467,6 +5478,47 @@
   Var->addDebugInfo(GVE);
 }
 
+void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
+  const GlobalDecl GD) {
+
+  

[PATCH] D120989: Support debug info for alias variable

2022-03-18 Thread Adrian Prantl via Phabricator via cfe-commits
aprantl added inline comments.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:3922
+  return cast(GVE);
+return dyn_cast_or_null(N);
+  }

if we don't expect anything but non-null DINodes to be in the cache, then this 
whole condition should be
```
return cast(N);
```
That will assert if N is null.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:5508
+return;
+  else if (!(DI = getDeclarationOrDefinition(
+ AliaseeDecl.getCanonicalDecl().getDecl(

LLVM coding style says to remove the redundant `else` after a `return`.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:5518
+
+  // Record this DIE in the cache for nested declaration reference
+  ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI);

`.`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-10 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan added inline comments.



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:3921
+  return cast(GVE);
+return dyn_cast_or_null(N);
+  }

aprantl wrote:
> When would we enter a nullptr into the cache?
In this change, only llvm::DIImportedEntity entries are pushed to 
ImportedDeclCache. So it will never return nullptr. It is only a guard to not 
to fall into the case of no definition in case of non llvm::DIImportedEntity in 
the cache (undesired and to be handled by the caller).



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1502
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {

aprantl wrote:
> std::find()?
MangledDeclNames is a llvm::MapVector implementation, so can't use std::find(). 
The key for MangledDeclNames MapVector is GlobalDecl and StringRef is the 
value. llvm::Mapvector::find() is search by key. Here we are doing search by 
value.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-10 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan updated this revision to Diff 414577.
kavitha-natarajan added a comment.

@aprantl Updated the comments following LLVM coding style.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/debug-info-alias.c

Index: clang/test/CodeGen/debug-info-alias.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-alias.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// CHECK-DAG: [[ENTITY1:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global"
+// CHECK-DAG: [[ENTITY2:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global_2"
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias", scope: !2, entity: [[ENTITY1]]
+// CHECK-DAG: [[ENTITY3:![0-9]+]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "global_alias_2", scope: !2, entity: [[ENTITY2]]
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias_2_alias", scope: !2, entity: [[ENTITY3]]
+
+int aliased_global = 1;
+extern int __attribute__((alias("aliased_global"))) __global_alias;
+
+// Recursive alias:
+int aliased_global_2 = 2;
+extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
+extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1213,6 +1213,7 @@
 
   StringRef getMangledName(GlobalDecl GD);
   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
+  const GlobalDecl getMangledNameDecl(StringRef);
 
   void EmitTentativeDefinition(const VarDecl *D);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1498,6 +1498,16 @@
   return Result.first->first();
 }
 
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {
+if (it->second == Name)
+  return it->first;
+it++;
+  }
+  return GlobalDecl();
+}
+
 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
   return getModule().getNamedValue(Name);
 }
@@ -5164,6 +5174,13 @@
   setTLSMode(GA, *VD);
 
   SetCommonAttributes(GD, GA);
+
+  // Emit global alias debug information.
+  if (const auto *VD = dyn_cast(D)) {
+if (CGDebugInfo *DI = getModuleDebugInfo()) {
+  DI->EmitGlobalAlias(cast(GA->getAliasee()), GD);
+}
+  }
 }
 
 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -152,8 +152,10 @@
   llvm::DenseMap DIFileCache;
   llvm::DenseMap SPCache;
   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
-  /// using declarations) that aren't covered by other more specific caches.
+  /// using declarations and global alias variables) that aren't covered
+  /// by other more specific caches.
   llvm::DenseMap DeclCache;
+  llvm::DenseMap ImportedDeclCache;
   llvm::DenseMap NamespaceCache;
   llvm::DenseMap
   NamespaceAliasCache;
@@ -512,6 +514,9 @@
   /// Emit information about an external variable.
   void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
 
+  /// Emit information about global variable alias.
+  void EmitGlobalAlias(const llvm::GlobalValue *GV, const GlobalDecl Decl);
+
   /// Emit C++ using directive.
   void EmitUsingDirective(const UsingDirectiveDecl );
 
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3911,6 +3911,17 @@
 return dyn_cast_or_null(N);
   }
 
+  // Search imported declaration cache if it is already defined
+  // as imported declaration.
+  auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
+
+  if (IE != ImportedDeclCache.end()) {
+auto N = IE->second;
+if (auto *GVE = dyn_cast_or_null(N))
+  return cast(GVE);
+return dyn_cast_or_null(N);
+  }
+
   // No definition for now. Emit a forward definition that might be
   // merged with a potential upcoming definition.
   if (const auto *FD = dyn_cast(D))
@@ -5467,6 +5478,47 @@
   Var->addDebugInfo(GVE);
 }
 
+void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
+  const GlobalDecl GD) {
+
+  assert(GV);
+
+  if 

[PATCH] D120989: Support debug info for alias variable

2022-03-10 Thread Adrian Prantl via Phabricator via cfe-commits
aprantl added a comment.

This looks mostly fine to me, I have a couple of superficial comments inline.




Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:3914
 
+  // imported declaration
+  auto IE = ImportedDeclCache.find(D->getCanonicalDecl());

Nit: The LLVM coding style wants all comments to be full sentences, including a 
trailing `.`



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:3921
+  return cast(GVE);
+return dyn_cast_or_null(N);
+  }

When would we enter a nullptr into the cache?



Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:5496
+  if (!AliaseeDecl)
+/* FIXME: Aliasee not declared yet - possibly declared later
+ * For example,

See LLVM coding style



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1502
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {

std::find()?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-07 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added subscribers: JDevlieghere, aprantl.
dblaikie added a comment.

Maybe APple folks would want to move forward with this only when targeting 
lldb, though - which might unblock this patch. @aprantl @JDevlieghere ? What do 
you folks think?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-07 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added a comment.

In D120989#3362912 , 
@kavitha-natarajan wrote:

> In D120989#3362491 , @dblaikie 
> wrote:
>
>> In D120989#3362490 , @dblaikie 
>> wrote:
>>
>>> Broad question about debug info for aliases: How's this proposed solution 
>>> compare/contrast to GCC's behavior?
>>
>> Aaand, I see that was described in thhe patch description. Sorry for not 
>> reading...
>>
>> Right, paging in all the context - this review hinges on whether gdb/lldb 
>> can handle/be made to handle this sort of debug info correctly.
>
> I have posted a gdb patch to handle this change for review:
> https://sourceware.org/pipermail/gdb-patches/2022-March/186329.html
>
> lldb can handle this debug info correctly.

Thanks for the details - can you ping this thread once the gdb thread has 
progressed/seems like it's moving in this direction?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-06 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan added a comment.

In D120989#3362491 , @dblaikie wrote:

> In D120989#3362490 , @dblaikie 
> wrote:
>
>> Broad question about debug info for aliases: How's this proposed solution 
>> compare/contrast to GCC's behavior?
>
> Aaand, I see that was described in thhe patch description. Sorry for not 
> reading...
>
> Right, paging in all the context - this review hinges on whether gdb/lldb can 
> handle/be made to handle this sort of debug info correctly.

I have posted a gdb patch to handle this change for review:
https://sourceware.org/pipermail/gdb-patches/2022-March/186329.html

lldb can handle this debug info correctly.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-06 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan updated this revision to Diff 413347.
kavitha-natarajan added a comment.

Rebased and applied pre-merge check formatting.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/debug-info-alias.c

Index: clang/test/CodeGen/debug-info-alias.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-alias.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// CHECK-DAG: [[ENTITY1:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global"
+// CHECK-DAG: [[ENTITY2:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global_2"
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias", scope: !2, entity: [[ENTITY1]]
+// CHECK-DAG: [[ENTITY3:![0-9]+]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "global_alias_2", scope: !2, entity: [[ENTITY2]]
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias_2_alias", scope: !2, entity: [[ENTITY3]]
+
+int aliased_global = 1;
+extern int __attribute__((alias("aliased_global"))) __global_alias;
+
+// Recursive alias:
+int aliased_global_2 = 2;
+extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
+extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1213,6 +1213,7 @@
 
   StringRef getMangledName(GlobalDecl GD);
   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
+  const GlobalDecl getMangledNameDecl(StringRef);
 
   void EmitTentativeDefinition(const VarDecl *D);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1498,6 +1498,16 @@
   return Result.first->first();
 }
 
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {
+if (it->second == Name)
+  return it->first;
+it++;
+  }
+  return GlobalDecl();
+}
+
 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
   return getModule().getNamedValue(Name);
 }
@@ -5164,6 +5174,13 @@
   setTLSMode(GA, *VD);
 
   SetCommonAttributes(GD, GA);
+
+  // Emit global alias debug information.
+  if (const auto *VD = dyn_cast(D)) {
+if (CGDebugInfo *DI = getModuleDebugInfo()) {
+  DI->EmitGlobalAlias(cast(GA->getAliasee()), GD);
+}
+  }
 }
 
 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
Index: clang/lib/CodeGen/CGDebugInfo.h
===
--- clang/lib/CodeGen/CGDebugInfo.h
+++ clang/lib/CodeGen/CGDebugInfo.h
@@ -152,8 +152,10 @@
   llvm::DenseMap DIFileCache;
   llvm::DenseMap SPCache;
   /// Cache declarations relevant to DW_TAG_imported_declarations (C++
-  /// using declarations) that aren't covered by other more specific caches.
+  /// using declarations and global alias variables) that aren't covered
+  /// by other more specific caches.
   llvm::DenseMap DeclCache;
+  llvm::DenseMap ImportedDeclCache;
   llvm::DenseMap NamespaceCache;
   llvm::DenseMap
   NamespaceAliasCache;
@@ -512,6 +514,9 @@
   /// Emit information about an external variable.
   void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
 
+  /// Emit information about global variable alias.
+  void EmitGlobalAlias(const llvm::GlobalValue *GV, const GlobalDecl Decl);
+
   /// Emit C++ using directive.
   void EmitUsingDirective(const UsingDirectiveDecl );
 
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3911,6 +3911,16 @@
 return dyn_cast_or_null(N);
   }
 
+  // imported declaration
+  auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
+
+  if (IE != ImportedDeclCache.end()) {
+auto N = IE->second;
+if (auto *GVE = dyn_cast_or_null(N))
+  return cast(GVE);
+return dyn_cast_or_null(N);
+  }
+
   // No definition for now. Emit a forward definition that might be
   // merged with a potential upcoming definition.
   if (const auto *FD = dyn_cast(D))
@@ -5467,6 +5477,48 @@
   Var->addDebugInfo(GVE);
 }
 
+void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
+  const GlobalDecl GD) {
+
+  assert(GV);
+
+  if (!CGM.getCodeGenOpts().hasReducedDebugInfo())
+return;
+
+  const auto *D = 

[PATCH] D120989: Support debug info for alias variable

2022-03-06 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added a comment.

In D120989#3362490 , @dblaikie wrote:

> Broad question about debug info for aliases: How's this proposed solution 
> compare/contrast to GCC's behavior?

Aaand, I see that was described in thhe patch description. Sorry for not 
reading...

Right, paging in all the context - this review hinges on whether gdb/lldb can 
handle/be made to handle this sort of debug info correctly.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-06 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added a comment.

Broad question about debug info for aliases: How's this proposed solution 
compare/contrast to GCC's behavior?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-04 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added a comment.

I think it'd potentially go somewhere in 
`DWARFDebugLine::LineTable::getFileLineInfoForAddress` for instance - which 
could inspect the candidate row in the line table, and if it's line zero, it 
could go back one row in the table. This would avoid probing addresses that are 
defined by the same row and will have the same result. (& could keep going back 
rows until it finds a non-zero row) & document that it's a fuzzy match. Or 
always produce the precise result and a separate non-zero fuzzy result if the 
main result is zero. "Nearest preceeding non-zero" or something.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120989/new/

https://reviews.llvm.org/D120989

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D120989: Support debug info for alias variable

2022-03-04 Thread Kavitha Natarajan via Phabricator via cfe-commits
kavitha-natarajan created this revision.
kavitha-natarajan added reviewers: kamleshbhalui, umesh.kalappa0, probinson, 
dblaikie.
kavitha-natarajan added a project: debug-info.
Herald added a subscriber: jeroen.dobbelaere.
Herald added a project: All.
kavitha-natarajan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is in continuation with the patch posted earlier for bug-50052 
:

https://reviews.llvm.org/D103131

For the below testcase, when compiled with clang compiler, debugger is not able 
to print alias variable type or value. 
$ cat test.c
int oldname = 1;
extern int newname attribute((alias("oldname")));

int main ()
{

  return 0;

}

$ clang -g -O0 test.c

$ gdb a.out
(gdb) ptype oldname
type = int
(gdb) ptype newname
type = 
(gdb) p oldname
$1 = 1
(gdb) p newname
'newname' has unknown type; cast it to its declared type

This is because clang is not emitting dwarf information for alias variable. The 
above mentioned patch supports clang to emit debug info for alias variable as 
imported entity (DW_TAG_imported_declaration). However, gdb cannot handle the 
imported declaration for alias variables. GCC emits debug info for alias 
variables as DW_TAG_variable which gdb can handle. The discussions in the above 
bug report and patch review links talk about why it is appropriate to emit 
alias variable as DW_TAG_imported_declaration rather than DW_TAG_variable. 
Refer section "3.2.3 Imported (or Renamed) Declaration Entries" in DWARF 5 
specification.

In the clang patch, CGDebugInfo.cpp:EmitGlobalAlias() function is rewritten to 
handle nested (recursive) imported declaration and developed a testcase as 
well. A corresponding gdb patch that can handle DW_TAG_imported_declaration as 
alias variables is also developed and will be posted to gdb community for 
review in parallel.

After clang and gdb fixes:

$ gdb a.out
(gdb) ptype oldname
type = int
(gdb) ptype newname
type = int
(gdb) p oldname
$1 = 1
(gdb) p newname
$2 = 1
(gdb)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120989

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDebugInfo.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/debug-info-alias.c

Index: clang/test/CodeGen/debug-info-alias.c
===
--- /dev/null
+++ clang/test/CodeGen/debug-info-alias.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// CHECK-DAG: [[ENTITY1:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global"
+// CHECK-DAG: [[ENTITY2:![0-9]+]] = distinct !DIGlobalVariable(name: "aliased_global_2"
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias", scope: !2, entity: [[ENTITY1]]
+// CHECK-DAG: [[ENTITY3:![0-9]+]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "global_alias_2", scope: !2, entity: [[ENTITY2]]
+// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "__global_alias_2_alias", scope: !2, entity: [[ENTITY3]]
+
+int aliased_global = 1;
+extern int __attribute__((alias("aliased_global"))) __global_alias;
+
+// Recursive alias:
+int aliased_global_2 = 2;
+extern int __attribute__((alias("aliased_global_2"))) global_alias_2;
+extern int __attribute__((alias("global_alias_2"))) __global_alias_2_alias;
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1213,6 +1213,7 @@
 
   StringRef getMangledName(GlobalDecl GD);
   StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
+  const GlobalDecl getMangledNameDecl(StringRef);
 
   void EmitTentativeDefinition(const VarDecl *D);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1495,6 +1495,16 @@
   return Result.first->first();
 }
 
+const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) {
+  auto it = MangledDeclNames.begin();
+  while (it != MangledDeclNames.end()) {
+if (it->second == Name)
+  return it->first;
+it++;
+  }
+  return GlobalDecl();
+}
+
 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
   return getModule().getNamedValue(Name);
 }
@@ -5139,6 +5149,13 @@
   setTLSMode(GA, *VD);
 
   SetCommonAttributes(GD, GA);
+
+  // Emit global alias debug information.
+  if (const auto *VD = dyn_cast(D)) {
+if (CGDebugInfo *DI = getModuleDebugInfo()) {
+  DI->EmitGlobalAlias(cast(GA->getAliasee()), GD);
+}
+  }
 }
 
 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
Index: clang/lib/CodeGen/CGDebugInfo.h
===
---