Index: include/clang/Basic/LangOptions.def =================================================================== --- include/clang/Basic/LangOptions.def +++ include/clang/Basic/LangOptions.def @@ -262,6 +262,8 @@ "value symbol visibility") ENUM_LANGOPT(TypeVisibilityMode, Visibility, 3, DefaultVisibility, "type symbol visibility") +LANGOPT(SetVisibilityForExternDecls, 1, 0, + "apply global symbol visibility to external declarations without an explicit visibility") ENUM_LANGOPT(StackProtector, StackProtectorMode, 2, SSPOff, "stack protector mode") ENUM_LANGOPT(TrivialAutoVarInit, TrivialAutoVarInitKind, 2, TrivialAutoVarInitKind::Uninitialized, Index: include/clang/Driver/CC1Options.td =================================================================== --- include/clang/Driver/CC1Options.td +++ include/clang/Driver/CC1Options.td @@ -703,6 +703,8 @@ HelpText<"Default type and symbol visibility">; def ftype_visibility : Separate<["-"], "ftype-visibility">, HelpText<"Default type visibility">; +def fextern_visibility : Flag<["-"], "fextern-visibility">, + HelpText<"Apply global symbol visibility to external declarations without an explicit visibility">; def ftemplate_depth : Separate<["-"], "ftemplate-depth">, HelpText<"Maximum depth of recursive template instantiation">; def foperator_arrow_depth : Separate<["-"], "foperator-arrow-depth">, Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -731,9 +731,11 @@ } if (!D) return; - // Set visibility for definitions. + // Set visibility for definitions, and for declarations if requested globally + // or set explicitly. LinkageInfo LV = D->getLinkageAndVisibility(); - if (LV.isVisibilityExplicit() || !GV->isDeclarationForLinker()) + if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls || + !GV->isDeclarationForLinker()) GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); } Index: lib/Driver/ToolChains/AMDGPU.cpp =================================================================== --- lib/Driver/ToolChains/AMDGPU.cpp +++ lib/Driver/ToolChains/AMDGPU.cpp @@ -109,5 +109,6 @@ options::OPT_fvisibility_ms_compat)) { CC1Args.push_back("-fvisibility"); CC1Args.push_back("hidden"); + CC1Args.push_back("-fextern-visibility"); } } Index: lib/Driver/ToolChains/HIP.cpp =================================================================== --- lib/Driver/ToolChains/HIP.cpp +++ lib/Driver/ToolChains/HIP.cpp @@ -294,8 +294,10 @@ // Default to "hidden" visibility, as object level linking will not be // supported for the foreseeable future. if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, - options::OPT_fvisibility_ms_compat)) + options::OPT_fvisibility_ms_compat)) { CC1Args.append({"-fvisibility", "hidden"}); + CC1Args.push_back("-fextern-visibility"); + } } llvm::opt::DerivedArgList * Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -2502,6 +2502,9 @@ if (Args.hasArg(OPT_fvisibility_global_new_delete_hidden)) Opts.GlobalAllocationFunctionVisibilityHidden = 1; + if (Args.hasArg(OPT_fextern_visibility)) + Opts.SetVisibilityForExternDecls = 1; + if (Args.hasArg(OPT_ftrapv)) { Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping); // Set the handler, if one is specified. Index: test/CodeGen/set-visibility-for-decls.c =================================================================== --- /dev/null +++ test/CodeGen/set-visibility-for-decls.c @@ -0,0 +1,42 @@ +// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility hidden -fextern-visibility -emit-llvm -o - | FileCheck --check-prefix=CHECK-HIDDEN %s +// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility protected -fextern-visibility -emit-llvm -o - | FileCheck --check-prefix=CHECK-PROTECTED %s +// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility default -fextern-visibility -emit-llvm -o - | FileCheck --check-prefix=CHECK-DEFAULT %s + +// CHECK-HIDDEN: @var_hidden = external hidden global +// CHECK-PROTECTED: @var_hidden = external hidden global +// CHECK-DEFAULT: @var_hidden = external hidden global +__attribute__((visibility("hidden"))) extern int var_hidden; +// CHECK-HIDDEN: @var_protected = external protected global +// CHECK-PROTECTED: @var_protected = external protected global +// CHECK-DEFAULT: @var_protected = external protected global +__attribute__((visibility("protected"))) extern int var_protected; +// CHECK-HIDDEN: @var_default = external global +// CHECK-PROTECTED: @var_default = external global +// CHECK-DEFAULT: @var_default = external global +__attribute__((visibility("default"))) extern int var_default; +// CHECK-HIDDEN: @var = external hidden global +// CHECK-PROTECTED: @var = external protected global +// CHECK-DEFAULT: @var = external global +extern int var; + +// CHECK-HIDDEN: declare hidden i32 @func_hidden() +// CHECK-PROTECTED: declare hidden i32 @func_hidden() +// CHECK-DEFAULT: declare hidden i32 @func_hidden() +__attribute__((visibility("hidden"))) int func_hidden(void); +// CHECK-HIDDEN: declare protected i32 @func_protected() +// CHECK-PROTECTED: declare protected i32 @func_protected() +// CHECK-DEFAULT: declare protected i32 @func_protected() +__attribute__((visibility("protected"))) int func_protected(void); +// CHECK-HIDDEN: declare i32 @func_default() +// CHECK-PROTECTED: declare i32 @func_default() +// CHECK-DEFAULT: declare i32 @func_default() +__attribute__((visibility("default"))) int func_default(void); +// CHECK-HIDDEN: declare hidden i32 @func() +// CHECK-PROTECTED: declare protected i32 @func() +// CHECK-DEFAULT: declare i32 @func() +int func(void); + +int use() { + return var_hidden + var_protected + var_default + var + + func_hidden() + func_protected() + func_default() + func(); +} Index: test/Driver/amdgpu-visibility.cl =================================================================== --- test/Driver/amdgpu-visibility.cl +++ test/Driver/amdgpu-visibility.cl @@ -2,6 +2,14 @@ // RUN: %clang -### -target amdgcn-amd-amdhsa -x cl -c -emit-llvm -fvisibility=protected %s 2>&1 | FileCheck -check-prefix=OVERRIDE-PROTECTED %s // RUN: %clang -### -target amdgcn-amd-amdhsa -x cl -c -emit-llvm -fvisibility-ms-compat %s 2>&1 | FileCheck -check-prefix=OVERRIDE-MS %s -// DEFAULT: "-fvisibility" "hidden" +// DEFAULT-DAG: "-fvisibility" "hidden" +// DEFAULT-DAG: "-fextern-visibility" + +// OVERRIDE-PROTECTED-NOT: "-fextern-visibility" // OVERRIDE-PROTECTED: "-fvisibility" "protected" -// OVERRIDE-MS: "-fvisibility" "hidden" "-ftype-visibility" "default" +// OVERRIDE-PROTECTED-NOT: "-fextern-visibility" + +// OVERRIDE-MS-NOT: "-fextern-visibility" +// OVERRIDE-MS-DAG: "-fvisibility" "hidden" +// OVERRIDE-MS-DAG: "-ftype-visibility" "default" +// OVERRIDE-MS-NOT: "-fextern-visibility" Index: test/Driver/hip-toolchain-no-rdc.hip =================================================================== --- test/Driver/hip-toolchain-no-rdc.hip +++ test/Driver/hip-toolchain-no-rdc.hip @@ -20,6 +20,7 @@ // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803" // CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[A_BC_803:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]] @@ -47,6 +48,7 @@ // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx900" // CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[A_BC_900:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[A_SRC]] @@ -89,6 +91,7 @@ // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803" // CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[B_BC_803:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]] @@ -116,6 +119,7 @@ // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx900" // CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[B_BC_900:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[B_SRC]] Index: test/Driver/hip-toolchain-rdc.hip =================================================================== --- test/Driver/hip-toolchain-rdc.hip +++ test/Driver/hip-toolchain-rdc.hip @@ -12,10 +12,11 @@ // RUN: %S/Inputs/hip_multiple_inputs/b.hip \ // RUN: 2>&1 | FileCheck %s -// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "amdgcn-amd-amdhsa" +// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "amdgcn-amd-amdhsa" // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803" // CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[A_BC:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]] @@ -23,6 +24,7 @@ // CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" // CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803" // CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden" +// CHECK-SAME: "-fextern-visibility" // CHECK-SAME: {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip" // CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]