]> granicus.if.org Git - clang/commitdiff
Add -fapply-global-visibility-to-externs for -cc1
authorScott Linder <scott@scottlinder.com>
Mon, 28 Jan 2019 17:12:19 +0000 (17:12 +0000)
committerScott Linder <scott@scottlinder.com>
Mon, 28 Jan 2019 17:12:19 +0000 (17:12 +0000)
Introduce an option to request global visibility settings be applied to
declarations without a definition or an explicit visibility, rather than
the existing behavior of giving these default visibility. When the
visibility of all or most extern definitions are known this allows for
the same optimisations -fvisibility permits without updating source code
to annotate all declarations.

Differential Revision: https://reviews.llvm.org/D56868

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352391 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/LangOptions.def
include/clang/Driver/CC1Options.td
lib/CodeGen/CodeGenModule.cpp
lib/Driver/ToolChains/AMDGPU.cpp
lib/Driver/ToolChains/HIP.cpp
lib/Frontend/CompilerInvocation.cpp
test/CodeGen/set-visibility-for-decls.c [new file with mode: 0644]
test/Driver/amdgpu-visibility.cl
test/Driver/hip-toolchain-no-rdc.hip
test/Driver/hip-toolchain-rdc.hip

index 36d3764c74dabd27a5b200d16f3b5df6dbc833b2..4e391d2562c476bf7712c958e303851b125abefd 100644 (file)
@@ -261,6 +261,8 @@ ENUM_LANGOPT(ValueVisibilityMode, Visibility, 3, DefaultVisibility,
              "default visibility for functions and variables [-fvisibility]")
 ENUM_LANGOPT(TypeVisibilityMode, Visibility, 3, DefaultVisibility,
              "default visibility for types [-ftype-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 4cce9e63c31fafc09443d147224481a29551b195..cead87201eee743b06c05179e18171b53a955ece 100644 (file)
@@ -702,6 +702,8 @@ def fvisibility : Separate<["-"], "fvisibility">,
   HelpText<"Default type and symbol visibility">;
 def ftype_visibility : Separate<["-"], "ftype-visibility">,
   HelpText<"Default type visibility">;
+def fapply_global_visibility_to_externs : Flag<["-"], "fapply-global-visibility-to-externs">,
+  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 0be4bad7ee7bd98e60e5dafb67df8c75d25fad69..5a1b7cfe4fa1dab580602d3c00dff7584710b19d 100644 (file)
@@ -731,9 +731,11 @@ void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
   }
   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 a03a607330340995b453178d594977297055e526..b9300bcc42e778b49fdd213ef8fd7b85446b0d93 100644 (file)
@@ -108,5 +108,6 @@ void AMDGPUToolChain::addClangTargetOptions(
                          options::OPT_fvisibility_ms_compat)) {
     CC1Args.push_back("-fvisibility");
     CC1Args.push_back("hidden");
+    CC1Args.push_back("-fapply-global-visibility-to-externs");
   }
 }
index f55dd2fc04f026df0fc461468515bb2af45fc52a..cd8ed6118fe0acd006fec33a2e79d8423af243ff 100644 (file)
@@ -293,8 +293,10 @@ void HIPToolChain::addClangTargetOptions(
   // 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("-fapply-global-visibility-to-externs");
+  }
 }
 
 llvm::opt::DerivedArgList *
index 4d72924fad21d1cb9ad676b7e0a8871e2d028ece..dbc29e24aee36294d93179d6c58f0a12107ae438 100644 (file)
@@ -2501,6 +2501,9 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
   if (Args.hasArg(OPT_fvisibility_global_new_delete_hidden))
     Opts.GlobalAllocationFunctionVisibilityHidden = 1;
 
+  if (Args.hasArg(OPT_fapply_global_visibility_to_externs))
+    Opts.SetVisibilityForExternDecls = 1;
+
   if (Args.hasArg(OPT_ftrapv)) {
     Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
     // Set the handler, if one is specified.
diff --git a/test/CodeGen/set-visibility-for-decls.c b/test/CodeGen/set-visibility-for-decls.c
new file mode 100644 (file)
index 0000000..04232f8
--- /dev/null
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility hidden -fapply-global-visibility-to-externs -emit-llvm -o - | FileCheck --check-prefix=CHECK-HIDDEN %s
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility protected -fapply-global-visibility-to-externs -emit-llvm -o - | FileCheck --check-prefix=CHECK-PROTECTED %s
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility default -fapply-global-visibility-to-externs -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 35969db0340ed2de1d9076e665283da4d4fd2138..19756d4744e00c699d19a7417501f0f95faef83b 100644 (file)
@@ -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: "-fapply-global-visibility-to-externs"
+
+// OVERRIDE-PROTECTED-NOT: "-fapply-global-visibility-to-externs"
 // OVERRIDE-PROTECTED: "-fvisibility" "protected"
-// OVERRIDE-MS:  "-fvisibility" "hidden" "-ftype-visibility" "default"
+// OVERRIDE-PROTECTED-NOT: "-fapply-global-visibility-to-externs"
+
+// OVERRIDE-MS-NOT: "-fapply-global-visibility-to-externs"
+// OVERRIDE-MS-DAG: "-fvisibility" "hidden"
+// OVERRIDE-MS-DAG: "-ftype-visibility" "default"
+// OVERRIDE-MS-NOT: "-fapply-global-visibility-to-externs"
index 4f31a39a72b98db8bff79fda1e827636a5f1d3f2..e8e491858f7c2857225003b99cd283c3f240fa0a 100644 (file)
@@ -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: "-fapply-global-visibility-to-externs"
 // 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: "-fapply-global-visibility-to-externs"
 // 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: "-fapply-global-visibility-to-externs"
 // CHECK-SAME: {{.*}} "-o" [[B_BC_803:".*bc"]] "-x" "hip"
 // CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]
 
 // 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: "-fapply-global-visibility-to-externs"
 // CHECK-SAME: {{.*}} "-o" [[B_BC_900:".*bc"]] "-x" "hip"
 // CHECK-SAME: {{.*}} [[B_SRC]]
 
index 5f3fd9250b6c534337e930c5ccc7ab675d8aae2c..dc6fc6f6518dce53646e702f682d4b383798e0e3 100644 (file)
 // 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: "-fapply-global-visibility-to-externs"
 // 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: "-fapply-global-visibility-to-externs"
 // CHECK-SAME: {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip"
 // CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]