[PATCH] D100567: BPF: emit debuginfo for Function of DeclRefExpr if requested

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 26 12:05:37 PDT 2021


yonghong-song updated this revision to Diff 340606.
yonghong-song added a comment.

- somehow previous test case didn't trigger nullptr for Fn. Replaced it with a working test case.

With the following change,

  diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
  index a784aade88da..fc86c1a72056 100644
  --- a/clang/lib/CodeGen/CGExpr.cpp
  +++ b/clang/lib/CodeGen/CGExpr.cpp
  @@ -2841,6 +2841,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
       if (getContext().getTargetInfo().allowDebugInfoForExternalRef()) {
         CGDebugInfo *DI = CGM.getModuleDebugInfo();
         auto *Fn = dyn_cast<llvm::Function>(LV.getPointer(*this));
  +      fprintf(stderr, "Fn = %p\n", (void *)Fn);
         if (DI && Fn && !Fn->getSubprogram())
           DI->EmitFunctionDecl(FD, FD->getLocation(), T, Fn);
       }

I have the following for the added test case,

  $ clang -cc1 -x c -debug-info-kind=limited -triple bpf-linux-gnu -emit-llvm debug-info-extern-callback.c
  Fn = 0x7afcf28
  Fn = 0x7afce98
  Fn = (nil)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D100567

Files:
  clang/include/clang/Basic/TargetInfo.h
  clang/lib/Basic/Targets/BPF.h
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/test/CodeGen/debug-info-extern-callback.c


Index: clang/test/CodeGen/debug-info-extern-callback.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/debug-info-extern-callback.c
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -x c -debug-info-kind=limited -triple bpf-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+extern int do_work1(int);
+long bpf_helper1(void *callback_fn);
+long prog1() {
+  return bpf_helper1(&do_work1);
+}
+
+extern int do_work2();
+long prog2_1() {
+  return (long)&do_work2;
+}
+int do_work2() { return 0; }
+long prog2_2() {
+  return (long)&do_work2;
+}
+
+// CHECK: declare !dbg ![[FUNC1:[0-9]+]] i32 @do_work1
+// CHECK: define dso_local i32 @do_work2() #{{[0-9]+}} !dbg ![[FUNC2:[0-9]+]]
+
+// CHECK: ![[FUNC1]] = !DISubprogram(name: "do_work1"
+// CHECK: ![[FUNC2]] = distinct !DISubprogram(name: "do_work2"
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -12667,7 +12667,7 @@
         Diag(Var->getLocation(), diag::note_private_extern);
       }
 
-      if (Context.getTargetInfo().allowDebugInfoForExternalVar() &&
+      if (Context.getTargetInfo().allowDebugInfoForExternalRef() &&
           !Var->isInvalidDecl() && !getLangOpts().CPlusPlus)
         ExternalDeclarations.push_back(Var);
 
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -2834,8 +2834,19 @@
     return LV;
   }
 
-  if (const auto *FD = dyn_cast<FunctionDecl>(ND))
-    return EmitFunctionDeclLValue(*this, E, FD);
+  if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
+    LValue LV = EmitFunctionDeclLValue(*this, E, FD);
+
+    // Emit debuginfo for the function declaration if the target wants to.
+    if (getContext().getTargetInfo().allowDebugInfoForExternalRef()) {
+      CGDebugInfo *DI = CGM.getModuleDebugInfo();
+      auto *Fn = dyn_cast<llvm::Function>(LV.getPointer(*this));
+      if (DI && Fn && !Fn->getSubprogram())
+        DI->EmitFunctionDecl(FD, FD->getLocation(), T, Fn);
+    }
+
+    return LV;
+  }
 
   // FIXME: While we're emitting a binding from an enclosing scope, all other
   // DeclRefExprs we see should be implicitly treated as if they also refer to
Index: clang/lib/Basic/Targets/BPF.h
===================================================================
--- clang/lib/Basic/Targets/BPF.h
+++ clang/lib/Basic/Targets/BPF.h
@@ -76,7 +76,7 @@
     return None;
   }
 
-  bool allowDebugInfoForExternalVar() const override { return true; }
+  bool allowDebugInfoForExternalRef() const override { return true; }
 
   CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
     switch (CC) {
Index: clang/include/clang/Basic/TargetInfo.h
===================================================================
--- clang/include/clang/Basic/TargetInfo.h
+++ clang/include/clang/Basic/TargetInfo.h
@@ -1538,8 +1538,8 @@
 
   virtual void setAuxTarget(const TargetInfo *Aux) {}
 
-  /// Whether target allows debuginfo types for decl only variables.
-  virtual bool allowDebugInfoForExternalVar() const { return false; }
+  /// Whether target allows debuginfo types for decl only variables/functions.
+  virtual bool allowDebugInfoForExternalRef() const { return false; }
 
 protected:
   /// Copy type and layout related info.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100567.340606.patch
Type: text/x-patch
Size: 3435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210426/369db35f/attachment.bin>


More information about the cfe-commits mailing list