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

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 18 23:50:04 PDT 2021


yonghong-song added a comment.

Did some further investigation, the following is the callchain:

  EmitFunctionDeclLValue
     EmitFunctionDeclPointer
         GetAddrOfFunction
             GetOrCreateLLVMFunction

The GetOrCreateLLVMFunction has the following comments:

  /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
  /// module, create and return an llvm Function with the specified type. If there
  /// is something in the module with the specified name, return it potentially
  /// bitcasted to the right type.
  ///
  /// If D is non-null, it specifies a decl that correspond to this.  This is used
  /// to set the attributes on the function when it is first created.
  llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
      StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
      bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
      ForDefinition_t IsForDefinition) {
    const Decl *D = GD.getDecl();
  ...

Basically, GetOrCreateLLVMFunction may return a Function or a Const
depending on whether there is an actual definition.

I still like to generate DebugInfo in the current place. The main reason
is this is the place where '&func' happens and it will ensure the
generated debuginfo will be useful for BPF. We could add some
comments here to explain why sometimes Fn could be nullptr.

Alternatively, we could have the following change,

  diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
  index f719f009ea99..2a6ce6468a8c 100644
  --- a/clang/lib/CodeGen/CodeGenModule.cpp
  +++ b/clang/lib/CodeGen/CodeGenModule.cpp
  @@ -3589,6 +3589,15 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
     // Make sure the result is of the requested type.
     if (!IsIncompleteFunction) {
       assert(F->getFunctionType() == Ty);
  +
  +    // Emit debuginfo for the function declaration if the target wants to.
  +    if (Context.getTargetInfo().allowDebugInfoForExternalRef()) {
  +      CGDebugInfo *DI = getModuleDebugInfo();
  +      const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
  +      if (DI && FD && !FD->isDefined())
  +        DI->EmitFunctionDecl(FD, FD->getLocation(), FD->getType(), F);
  +    }
  +
       return F;
     }

We can emit the debuginfo inside function GetOrCreateLLVMFunction.
We don't need to do dedup (like checking Fn->getSubprogram()) and
F is known to be non-null. The only drawback (maybe only to me) is
that this debuginfo generation is a little further from the use case.
But if you think this is better, I am fine too. 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