[clang] [BPF] Handle aliases in CodeGenModule::EmitExternalDeclaration. Fixes #192365 (PR #192374)

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 17 17:10:24 PDT 2026


================
@@ -5990,12 +5990,21 @@ void CodeGenModule::EmitExternalDeclaration(const DeclaratorDecl *D) {
 
   llvm::Constant *Addr = GetAddrOfGlobal(GD)->stripPointerCasts();
   if (const auto *VD = dyn_cast<VarDecl>(D)) {
-    DI->EmitExternalVariable(
-        cast<llvm::GlobalVariable>(Addr->stripPointerCasts()), VD);
+    if (auto *GV = dyn_cast<llvm::GlobalVariable>(Addr))
+      DI->EmitExternalVariable(GV, VD);
+    else if (auto *GA = dyn_cast<llvm::GlobalAlias>(Addr))
+      DI->EmitGlobalAlias(GA, GD);
+    else
+      llvm_unreachable("Unexpected address for external variable");
   } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
-    llvm::Function *Fn = cast<llvm::Function>(Addr);
-    if (!Fn->getSubprogram())
-      DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
+    if (auto *Fn = dyn_cast<llvm::Function>(Addr)) {
+      if (!Fn->getSubprogram())
+        DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
+    } else if (auto *GA = dyn_cast<llvm::GlobalAlias>(Addr)) {
+      DI->EmitGlobalAlias(GA, GD);
+    } else {
+      llvm_unreachable("Unexpected address for external function");
+    }
----------------
dwblaikie wrote:

Are both these codepaths tested by this test coverage? Looks like there's only functions in the test, and no global variables.

& could this code be refactored to be shared in some way between the two cases? Little lambda declared before the VarDecl dyn_cast, perhaps? That just takes the result of the dyn_cast GlobalVariable or Function as a parameter, and either returns it if non-null (& then the caller in each branch does the thing with it) otherwise the lambda does the GlobalAlias fallback?

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


More information about the cfe-commits mailing list