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

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 17 10:16:55 PDT 2021


yonghong-song added a comment.

I did some debugging on why DeclRefExpr evaluated not to be a Function pointer. Here is what I got.
I made the following clang change to dump out the pointer to the emited value ('&foo'):

diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index a784aade88da..80d19c7bcdf7 100644

- a/clang/lib/CodeGen/CGExpr.cpp

+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -2840,6 +2840,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {

  // Emit debuginfo for the function declaration if the target wants to.
  if (getContext().getTargetInfo().allowDebugInfoForExternalRef()) {
    CGDebugInfo *DI = CGM.getModuleDebugInfo();

+      LV.getPointer(*this)->dump();

  auto *Fn = dyn_cast<llvm::Function>(LV.getPointer(*this));
  if (DI && Fn && !Fn->getSubprogram())
    DI->EmitFunctionDecl(FD, FD->getLocation(), T, Fn);

The C code and the compiler run:

$ cat t1.c
 extern int foo() __attribute__((section("abc")));
 long test() {

  return (long)&foo;

}
 int foo() { return 0; }
 long test2() {

  return (long)&foo;

}
 $ clang -target bpf -O2 -S -g -emit-llvm t1.c
 declare dso_local i32 @foo(...) #0 section "abc"

i32 (...)* bitcast (i32 ()* @foo to i32 (...)*)

You can see, if there is no definition, the emitted insn for '&foo' is a reference
to a declaration (llvm::Function type).
After the function definition, the emitted insn for '&foo' is a reference to
some kind of a function definition (I checked it is a llvm:ConstantExpr).
That is the reason why I hit a NULL pointer for

  auto *Fn = dyn_cast<llvm::Function>(LV.getPointer(*this));

in one of tests.

@dblaikie the code is ready to be reviewed again. Thanks!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D100567



More information about the cfe-commits mailing list