Skip to content

Commit 0a9fc92

Browse files
author
Sjoerd Meijer
committed
[Driver] Default to -fno-common for all targets
This makes -fno-common the default for all targets because this has performance and code-size benefits and is more language conforming for C code. Additionally, GCC10 also defaults to -fno-common and so we get consistent behaviour with GCC. With this change, C code that uses tentative definitions as definitions of a variable in multiple translation units will trigger multiple-definition linker errors. Generally, this occurs when the use of the extern keyword is neglected in the declaration of a variable in a header file. In some cases, no specific translation unit provides a definition of the variable. The previous behavior can be restored by specifying -fcommon. As GCC has switched already, we benefit from applications already being ported and existing documentation how to do this. For example: - https://gcc.gnu.org/gcc-10/porting_to.html - https://wiki.gentoo.org/wiki/Gcc_10_porting_notes/fno_common Differential revision: https://reviews.llvm.org/D75056
1 parent d58e383 commit 0a9fc92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+157
-146
lines changed

clang/docs/ClangCommandLineReference.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,10 @@ Use colors in diagnostics
13071307

13081308
.. option:: -fcommon, -fno-common
13091309

1310+
Place definitions of variables with no storage class and no initializer
1311+
(tentative definitions) in a common block, instead of generating individual
1312+
zero-initialized definitions (default -fno-common).
1313+
13101314
.. option:: -fcompile-resource=<arg>, --resource <arg>, --resource=<arg>
13111315

13121316
.. option:: -fconstant-cfstrings, -fno-constant-cfstrings

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ future versions of Clang.
8484
Modified Compiler Flags
8585
-----------------------
8686

87+
- -fno-common has been enabled as the default for all targets. Therefore, C
88+
code that uses tentative definitions as definitions of a variable in multiple
89+
translation units will trigger multiple-definition linker errors. Generally,
90+
this occurs when the use of the ``extern`` keyword is neglected in the declaration
91+
of a variable in a header file. In some cases, no specific translation unit
92+
provides a definition of the variable. The previous behavior can be restored by
93+
specifying ``-fcommon``.
8794

8895
New Pragmas in Clang
8996
--------------------

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ def fno_record_command_line : Flag<["-"], "fno-record-command-line">,
848848
Group<f_clang_Group>;
849849
def : Flag<["-"], "frecord-gcc-switches">, Alias<frecord_command_line>;
850850
def : Flag<["-"], "fno-record-gcc-switches">, Alias<fno_record_command_line>;
851-
def fcommon : Flag<["-"], "fcommon">, Group<f_Group>;
851+
def fcommon : Flag<["-"], "fcommon">, Group<f_Group>,
852+
Flags<[CoreOption, CC1Option]>, HelpText<"Place uninitialized global variables in a common block">;
852853
def fcompile_resource_EQ : Joined<["-"], "fcompile-resource=">, Group<f_Group>;
853854
def fcomplete_member_pointers : Flag<["-"], "fcomplete-member-pointers">, Group<f_clang_Group>,
854855
Flags<[CoreOption, CC1Option]>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,20 +1408,6 @@ static bool isSignedCharDefault(const llvm::Triple &Triple) {
14081408
}
14091409
}
14101410

1411-
static bool isNoCommonDefault(const llvm::Triple &Triple) {
1412-
switch (Triple.getArch()) {
1413-
default:
1414-
if (Triple.isOSFuchsia())
1415-
return true;
1416-
return false;
1417-
1418-
case llvm::Triple::xcore:
1419-
case llvm::Triple::wasm32:
1420-
case llvm::Triple::wasm64:
1421-
return true;
1422-
}
1423-
}
1424-
14251411
static bool hasMultipleInvocations(const llvm::Triple &Triple,
14261412
const ArgList &Args) {
14271413
// Supported only on Darwin where we invoke the compiler multiple times
@@ -5675,11 +5661,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56755661
if (!Args.hasFlag(options::OPT_Qy, options::OPT_Qn, true))
56765662
CmdArgs.push_back("-Qn");
56775663

5678-
// -fcommon is the default unless compiling kernel code or the target says so
5679-
bool NoCommonDefault = KernelOrKext || isNoCommonDefault(RawTriple);
5680-
if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common,
5681-
!NoCommonDefault))
5682-
CmdArgs.push_back("-fno-common");
5664+
// -fno-common is the default, set -fcommon only when that flag is set.
5665+
if (Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common, false))
5666+
CmdArgs.push_back("-fcommon");
56835667

56845668
// -fsigned-bitfields is default, and clang doesn't yet support
56855669
// -funsigned-bitfields.

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
809809
Opts.RecordCommandLine =
810810
std::string(Args.getLastArgValue(OPT_record_command_line));
811811
Opts.MergeAllConstants = Args.hasArg(OPT_fmerge_all_constants);
812-
Opts.NoCommon = Args.hasArg(OPT_fno_common);
812+
Opts.NoCommon = !Args.hasArg(OPT_fcommon);
813813
Opts.NoInlineLineTables = Args.hasArg(OPT_gno_inline_line_tables);
814814
Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float);
815815
Opts.OptimizeSize = getOptimizationLevelSize(Args);

clang/test/CodeGen/2008-07-21-mixed-var-fn-decl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
int g0, f0();
44
int f1(), g1;
55

6-
// CHECK: @g0 = common {{(dso_local )?}}global i32 0, align 4
7-
// CHECK: @g1 = common {{(dso_local )?}}global i32 0, align 4
6+
// CHECK: @g0 = {{(dso_local )?}}global i32 0, align 4
7+
// CHECK: @g1 = {{(dso_local )?}}global i32 0, align 4
88

clang/test/CodeGen/2009-10-20-GlobalDebug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %clang -target i386-apple-darwin10 -flto -S -g %s -o - | FileCheck %s
33

44
// CHECK: @main.localstatic = internal global i32 0, align 4, !dbg [[L:![0-9]+]]
5-
// CHECK: @global = common global i32 0, align 4, !dbg [[G:![0-9]+]]
5+
// CHECK: @global = global i32 0, align 4, !dbg [[G:![0-9]+]]
66

77
int global;
8-
int main() {
8+
int main() {
99
static int localstatic;
1010
return 0;
1111
}

clang/test/CodeGen/aarch64-sve.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// CHECK-DEBUG: cannot yet generate debug info for SVE type '__SVFloat64_t'
1717
// CHECK-DEBUG: cannot yet generate debug info for SVE type '__SVBool_t'
1818

19-
// CHECK: @ptr = common global <vscale x 16 x i8>* null, align 8
19+
// CHECK: @ptr = global <vscale x 16 x i8>* null, align 8
2020
// CHECK: %s8 = alloca <vscale x 16 x i8>, align 16
2121
// CHECK: %s16 = alloca <vscale x 8 x i16>, align 16
2222
// CHECK: %s32 = alloca <vscale x 4 x i32>, align 16

clang/test/CodeGen/address-space.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,X86 %s
22
// RUN: %clang_cc1 -triple amdgcn -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,AMDGCN %s
33

4-
// CHECK: @foo = common addrspace(1) global
4+
// CHECK: @foo = addrspace(1) global
55
int foo __attribute__((address_space(1)));
66

7-
// CHECK: @ban = common addrspace(1) global
7+
// CHECK: @ban = addrspace(1) global
88
int ban[10] __attribute__((address_space(1)));
99

10-
// CHECK: @a = common global
10+
// CHECK: @a = global
1111
int a __attribute__((address_space(0)));
1212

1313
// CHECK-LABEL: define i32 @test1()

clang/test/CodeGen/alias.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm -o - %s | FileCheck -check-prefix=CHECKGLOBALS %s
66

77
int g0;
8-
// CHECKBASIC-DAG: @g0 = common global i32 0
9-
// CHECKASM-DAG: .comm g0,4,4
8+
// CHECKBASIC-DAG: @g0 = global i32 0
9+
// CHECKASM-DAG: .bss
10+
// CHECKASM-DAG: .globl g0
11+
// CHECKASM-DAG: .p2align 2
12+
// CHECKASM-DAG: g0:
13+
// CHECKASM-DAG: .long 0
14+
// CHECKASM-DAG: .size g0, 4
1015
__thread int TL_WITH_ALIAS;
1116
// CHECKBASIC-DAG: @TL_WITH_ALIAS = thread_local global i32 0, align 4
1217
// CHECKASM-DAG: .globl TL_WITH_ALIAS

0 commit comments

Comments
 (0)