[clang] [llvm] [BPF] Fix linking issues in static map initializers (PR #91310)

Nick Zavaritsky via cfe-commits cfe-commits at lists.llvm.org
Fri May 17 06:29:39 PDT 2024


================
@@ -1950,8 +1950,22 @@ ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
     if (D->hasAttr<WeakRefAttr>())
       return CGM.GetWeakRefReference(D).getPointer();
 
-    if (auto FD = dyn_cast<FunctionDecl>(D))
-      return CGM.GetAddrOfFunction(FD);
+    if (auto FD = dyn_cast<FunctionDecl>(D)) {
+      auto *C = CGM.GetAddrOfFunction(FD);
+
+      // we don't normally emit debug info for extern fns referenced via
+      // variable initialisers; BPF needs it since it generates BTF from
+      // debug info and bpftool demands BTF for every symbol linked
+      if (CGM.getTarget().getTriple().isBPF() && FD->getStorageClass() == SC_Extern) {
----------------
mejedi wrote:

@efriedma-quic @yonghong-song I ended up generalising `ExternalDeclarations` to include both `VarDecl`s and `FunctionDecl`s.
Since `GetAddrOfFunction` is invoked in so many different contexts, I found it difficult to emit debug info only for the subset of calls we care about.
Personally, I find it cleaner to collect external declarations on a list and handle them separately rather than plugging directly into codegen. Please let me know if I am missing something.
Concerning referenced/all debacle -
 * `Sema::ActOnEndOfTranslationUnit` checks whether a declaration is used (does it mean referenced?) before calling `Consumer.CompleteExternalDeclaration`;
 * plugging into codegen ensures that we notice any reference that made it into llvm bitcode BUT they could be removed by the optimiser later;
 * BPF backend ensures that only referenced entities make it to BTF.

It looks like both approaches are quite similar?

https://github.com/llvm/llvm-project/pull/91310


More information about the cfe-commits mailing list