Skip to content

Commit 70c87d2

Browse files
andykaylorPriyanshu3820
authored andcommitted
[CIR] Add support for common linkage (llvm#168613)
Add support for marking global variables with common linkage.
1 parent f1b9bd1 commit 70c87d2

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,17 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
883883
// FIXME(cir): setLinkage should likely set MLIR's visibility automatically.
884884
gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
885885
assert(!cir::MissingFeatures::opGlobalDLLImportExport());
886-
if (linkage == cir::GlobalLinkageKind::CommonLinkage)
887-
errorNYI(initExpr->getSourceRange(), "common linkage");
886+
if (linkage == cir::GlobalLinkageKind::CommonLinkage) {
887+
// common vars aren't constant even if declared const.
888+
gv.setConstant(false);
889+
// Tentative definition of global variables may be initialized with
890+
// non-zero null pointers. In this case they should have weak linkage
891+
// since common linkage must have zero initializer and must not have
892+
// explicit section therefore cannot have non-zero initial value.
893+
std::optional<mlir::Attribute> initializer = gv.getInitialValue();
894+
if (initializer && !getBuilder().isNullValue(*initializer))
895+
gv.setLinkage(cir::GlobalLinkageKind::WeakAnyLinkage);
896+
}
888897

889898
setNonAliasAttributes(vd, gv);
890899

@@ -1238,10 +1247,8 @@ cir::GlobalLinkageKind CIRGenModule::getCIRLinkageForDeclarator(
12381247
// linkage.
12391248
if (!getLangOpts().CPlusPlus && isa<VarDecl>(dd) &&
12401249
!isVarDeclStrongDefinition(astContext, *this, cast<VarDecl>(dd),
1241-
getCodeGenOpts().NoCommon)) {
1242-
errorNYI(dd->getBeginLoc(), "common linkage", dd->getDeclKindName());
1250+
getCodeGenOpts().NoCommon))
12431251
return cir::GlobalLinkageKind::CommonLinkage;
1244-
}
12451252

12461253
// selectany symbols are externally visible, so use weak instead of
12471254
// linkonce. MSVC optimizes away references to const selectany globals, so

clang/test/CIR/CodeGen/no-common.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -emit-cir -o %t-default.cir
2+
// RUN: FileCheck --input-file=%t-default.cir %s -check-prefix=CIR-DEFAULT
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -fno-common -emit-cir -o %t-no-common.cir
4+
// RUN: FileCheck --input-file=%t-no-common.cir %s -check-prefix=CIR-DEFAULT
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -fcommon -emit-cir -o %t-common.cir
6+
// RUN: FileCheck --input-file=%t-common.cir %s -check-prefix=CIR-COMMON
7+
8+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -emit-llvm -o %t-default-cir.ll
9+
// RUN: FileCheck --input-file=%t-default-cir.ll %s -check-prefix=LLVM-DEFAULT
10+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -fno-common -emit-llvm -o %t-no-common-cir.ll
11+
// RUN: FileCheck --input-file=%t-no-common-cir.ll %s -check-prefix=LLVM-DEFAULT
12+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir %s -fcommon -emit-llvm -o %t-common-cir.ll
13+
// RUN: FileCheck --input-file=%t-common-cir.ll %s -check-prefix=LLVM-COMMON
14+
15+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o %t-default.ll
16+
// RUN: FileCheck --input-file=%t-default.ll %s -check-prefix=OGCG-DEFAULT
17+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fno-common -emit-llvm -o %t-no-common.ll
18+
// RUN: FileCheck --input-file=%t-no-common.ll %s -check-prefix=OGCG-DEFAULT
19+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fcommon -emit-llvm -o %t-common.ll
20+
// RUN: FileCheck --input-file=%t-common.ll %s -check-prefix=OGCG-COMMON
21+
22+
const int a = 42;
23+
// CIR-DEFAULT: cir.global constant external @a = #cir.int<42>
24+
// LLVM-DEFAULT: @a = constant i32 42
25+
// OGCG-DEFAULT: @a = constant i32 42
26+
27+
// CIR-COMMON: cir.global constant external @a
28+
// LLVM-COMMON: @a = constant i32 42
29+
// OGCG-COMMON: @a = constant i32 42
30+
31+
const int b __attribute__((common)) = 42;
32+
// CIR-DEFAULT: cir.global constant external @b = #cir.int<42>
33+
// LLVM-DEFAULT: @b = constant i32 42
34+
// OGCG-DEFAULT: @b = constant i32 42
35+
36+
// CIR-COMMON: cir.global constant external @b = #cir.int<42>
37+
// LLVM-COMMON: @b = constant i32 42
38+
// OGCG-COMMON: @b = constant i32 42
39+
40+
const int c __attribute__((nocommon)) = 42;
41+
// CIR-DEFAULT: cir.global constant external @c = #cir.int<42>
42+
// LLVM-DEFAULT: @c = constant i32 42
43+
// OGCG-DEFAULT: @c = constant i32 42
44+
45+
// CIR-COMMON: cir.global constant external @c = #cir.int<42>
46+
// LLVM-COMMON: @c = constant i32 42
47+
// OGCG-COMMON: @c = constant i32 42
48+
49+
int d = 11;
50+
// CIR-DEFAULT: cir.global external @d = #cir.int<11>
51+
// LLVM-DEFAULT: @d = global i32 11
52+
// OGCG-DEFAULT: @d = global i32 11
53+
54+
// CIR-COMMON: cir.global external @d = #cir.int<11>
55+
// LLVM-COMMON: @d = global i32 11
56+
// OGCG-COMMON: @d = global i32 11
57+
58+
int e;
59+
// CIR-DEFAULT: cir.global external @e = #cir.int<0>
60+
// LLVM-DEFAULT: @e = global i32 0
61+
// OGCG-DEFAULT: @e = global i32 0
62+
63+
// CIR-COMMON: cir.global common @e = #cir.int<0>
64+
// LLVM-COMMON: @e = common global i32 0
65+
// OGCG-COMMON: @e = common global i32 0
66+
67+
68+
int f __attribute__((common));
69+
// CIR-DEFAULT: cir.global common @f = #cir.int<0>
70+
// LLVM-DEFAULT: @f = common global i32 0
71+
// OGCG-DEFAULT: @f = common global i32 0
72+
73+
// CIR-COMMON: cir.global common @f
74+
// LLVM-COMMON: @f = common global i32 0
75+
// OGCG-COMMON: @f = common global i32 0
76+
77+
int g __attribute__((nocommon));
78+
// CIR-DEFAULT: cir.global external @g = #cir.int<0>
79+
// LLVM-DEFAULT: @g = global i32
80+
// OGCG-DEFAULT: @g = global i32 0
81+
82+
// CIR-COMMON: cir.global external @g = #cir.int<0>
83+
// LLVM-COMMON: @g = global i32 0
84+
// OGCG-COMMON: @g = global i32 0
85+
86+
const int h;
87+
// CIR-DEFAULT: cir.global constant external @h = #cir.int<0>
88+
// LLVM-DEFAULT: @h = constant i32
89+
// OGCG-DEFAULT: @h = constant i32 0
90+
91+
// CIR-COMMON: cir.global common @h = #cir.int<0>
92+
// LLVM-COMMON: @h = common global i32 0
93+
// OGCG-COMMON: @h = common global i32 0
94+
95+
typedef void* (*fn_t)(long a, long b, char *f, int c);
96+
fn_t ABC __attribute__ ((nocommon));
97+
// CIR-DEFAULT: cir.global external @ABC = #cir.ptr<null>
98+
// LLVM-DEFAULT: @ABC = global ptr null
99+
// OGCG-DEFAULT: @ABC = global ptr null
100+
101+
// CIR-COMMON: cir.global external @ABC = #cir.ptr<null>
102+
// LLVM-COMMON: @ABC = global ptr null
103+
// OGCG-COMMON: @ABC = global ptr null

0 commit comments

Comments
 (0)