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

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 17 18:07:09 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");
+    }
----------------
alexfh wrote:

On another look it seems that using cast<> is not any worse than using llvm_unreachable() along with dyn_cast. Reverted this part and left just a single check for GlobalAlias.

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


More information about the cfe-commits mailing list