[clang] [CIR] Emit globals for declarations that force externally visible defs (PR #198905)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 26 14:58:31 PDT 2026
https://github.com/adams381 updated https://github.com/llvm/llvm-project/pull/198905
>From 48c47dec5689e337762be6892931ddeb54b84c13 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Wed, 20 May 2026 14:03:23 -0700
Subject: [PATCH 1/2] [CIR] Emit globals for declarations that force externally
visible defs
CIRGenModule::emitGlobal hit errorNYI when a forward declaration
forces an externally visible definition under C99 inline rules.
Classic codegen materializes the symbol via GetOrCreateLLVMFunction;
mirror that with getAddrOfFunction.
Unblocks SPEC CPU 2026 berkeley-abc if.h (729.abc_r, 829.abc_s),
where a static inline If_CutCopy precedes an extern declaration at
line 527.
Add inline-extern-force-codegen.c with CIR, LLVM, and OGCG checks.
---
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 8 ++++---
.../CIR/CodeGen/inline-extern-force-codegen.c | 23 +++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/inline-extern-force-codegen.c
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 6d99b9210de84..f14d5b9bb77b8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -536,11 +536,13 @@ void CIRGenModule::emitGlobal(clang::GlobalDecl gd) {
deferredAnnotations[mangledName] = fd;
}
if (!fd->doesThisDeclarationHaveABody()) {
- if (!fd->doesDeclarationForceExternallyVisibleDefinition())
+ if (!fd->doesDeclarationForceExternallyVisibleDefinition() &&
+ (!fd->isMultiVersion() || !getTarget().getTriple().isAArch64()))
return;
- errorNYI(fd->getSourceRange(),
- "function declaration that forces code gen");
+ const CIRGenFunctionInfo &fi = getTypes().arrangeGlobalDeclaration(gd);
+ cir::FuncType ty = getTypes().getFunctionType(fi);
+ getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false);
return;
}
} else {
diff --git a/clang/test/CIR/CodeGen/inline-extern-force-codegen.c b/clang/test/CIR/CodeGen/inline-extern-force-codegen.c
new file mode 100644
index 0000000000000..ff0b29f7b9db8
--- /dev/null
+++ b/clang/test/CIR/CodeGen/inline-extern-force-codegen.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c17 -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c17 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c17 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
+
+// C99 inline definition followed by an extern declaration that forces an
+// externally visible declaration of the same symbol (berkeley-abc if.h shape).
+
+inline int cut_copy(int *dst, int *src) { return *dst = *src; }
+extern int cut_copy(int *dst, int *src);
+
+int driver(void) { return 0; }
+
+// CIR: cir.func{{.*}} @cut_copy(
+// CIR: cir.return
+
+// LLVM: define{{.*}} @cut_copy(
+// LLVM: define{{.*}} @driver(
+
+// OGCG: define{{.*}} @cut_copy(
+// OGCG: define{{.*}} @driver(
>From f30627c9c604cae60ba14edbf1338ff03e9448dc Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Tue, 26 May 2026 14:45:07 -0700
Subject: [PATCH 2/2] [CIR] Address review nits for inline-extern-force-codegen
test
Apply erichkeane's suggestions: collapse OGCG prefix into LLVM
(both RUN lines now check the same FileCheck patterns against their
respective outputs) and drop the berkeley-abc parenthetical from the
comment.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
clang/test/CIR/CodeGen/inline-extern-force-codegen.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/test/CIR/CodeGen/inline-extern-force-codegen.c b/clang/test/CIR/CodeGen/inline-extern-force-codegen.c
index ff0b29f7b9db8..cab98be7d0cb6 100644
--- a/clang/test/CIR/CodeGen/inline-extern-force-codegen.c
+++ b/clang/test/CIR/CodeGen/inline-extern-force-codegen.c
@@ -3,10 +3,10 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c17 -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c17 -emit-llvm %s -o %t.ll
-// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
// C99 inline definition followed by an extern declaration that forces an
-// externally visible declaration of the same symbol (berkeley-abc if.h shape).
+// externally visible declaration of the same symbol.
inline int cut_copy(int *dst, int *src) { return *dst = *src; }
extern int cut_copy(int *dst, int *src);
@@ -18,6 +18,3 @@ int driver(void) { return 0; }
// LLVM: define{{.*}} @cut_copy(
// LLVM: define{{.*}} @driver(
-
-// OGCG: define{{.*}} @cut_copy(
-// OGCG: define{{.*}} @driver(
More information about the cfe-commits
mailing list