DiggerLin created this revision.
DiggerLin added reviewers: hubert.reinterpretcast, jasonliu, daltenty.
Herald added subscribers: cfe-commits, dang, steven.zhang.
Herald added a project: clang.
DiggerLin requested review of this revision.

In IBM compiler Xlclang , there is option -fnovisibiilty. The option is 
description as

  
https://www.ibm.com/support/knowledgecenter/SSGH3R_16.1.0/com.ibm.xlcpp161.aix.doc/compiler_ref/opt_visibility.html
 
   
  we need to add the option -fnovisibiilty for clang in the IBM AIX OS(and the 
option is enabled by default in AIX OS).  
  I will implement the option in the other OS platform.(but the option is 
disabled by default in other OS).
   
  For example, the file test.c
   

bash-4.2$ test.c
 __attribute__((visibility ("protected"))) int b;

1 In AIX OS:

1.1 Compiled with

  clang -fnovisibility    -target powerpc-unknown-aix  -emit-llvm  -S test.c

or

  clang -target powerpc-unknown-aix  -emit-llvm  -S test.c  ( the 
-fnovisibility is enabled by default in AIX OS)

Generate IR as :

  @b = global i32 0, align 4
   

1.2 Compiled with

  (If have "-fnovisibility  -fvisibility=*" at the same time.  the compile will 
ignore the -fnovisibility). 
   
  clang -fnovisibility  -fvisibility=default   -target powerpc-unknown-aix   
-emit-llvm  -S test.c

Or

  clang -fvisibility=default   -target powerpc-unknown-aix   -emit-llvm  -S 
test.c
   
  Generate IR as :
  @b = protected global i32 0, align 4
   

2. In Other OS(not AIX) 2.1 clang -fnovisibility    -target 
powerpc-unknown-linux  -emit-llvm  -S test.c

  Generate IR as : @b = global i32 0, align 4

  2.2 clang -target powerpc-unknown-linux  -emit-llvm  -S test.c ( the 
-fnovisibility is disabled by default in not AIX OS) Or (if have 
"-fnovisibility  -fvisibility=*" at the same time.  the compile will ignore the 
-fnovisibility). clang -fnovisibility  -fvisibility=default   -target 
powerpc-unknown-linux   -emit-llvm  -S test.c Or clang -fvisibility=default   
-target powerpc-unknown-linux   -emit-llvm  -S test.c

  Generate IR as : @b = protected global i32 0, align 4


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87451

Files:
  clang/docs/ClangCommandLineReference.rst
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/AST/Decl.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/Driver/fnovisibility.cpp

Index: clang/test/Driver/fnovisibility.cpp
===================================================================
--- /dev/null
+++ clang/test/Driver/fnovisibility.cpp
@@ -0,0 +1,69 @@
+// RUN: %clang    -target powerpc-unknown-aix  -emit-llvm  -o - -S  %s  |\
+// RUN:   FileCheck --check-prefixes=NOVISIBILITY,AIX-NOVISIBILITY %s
+
+// RUN: %clang -fnovisibility -target powerpc-unknown-aix  -emit-llvm -o -  -S %s  | \
+// RUN: FileCheck -check-prefixes=NOVISIBILITY,AIX-NOVISIBILITY %s
+
+// RUN: %clang -fnovisibility -target powerpc-unknown-linux  -emit-llvm -o - -S %s  | \
+// RUN: FileCheck -check-prefixes=NOVISIBILITY,LINUX-NOVISIBILITY %s
+
+// RUN: %clang -fnovisibility -fvisibility=default -target powerpc-unknown-aix  -emit-llvm  -o - -S %s  | \
+// RUN: FileCheck -check-prefix=VISIBILITY %s
+
+// RUN: %clang -target powerpc-unknown-linux  -emit-llvm  -o - -S %s  | \
+// RUN: FileCheck -check-prefix=VISIBILITY %s
+
+// RUN: %clang -fnovisibility -fvisibility=default -target powerpc-unknown-linux  -emit-llvm -o - -S %s  | \
+// RUN: FileCheck -check-prefix=VISIBILITY %s
+
+__attribute__((visibility("hidden"))) void foo_h(int *p) {
+  (*p)++;
+}
+
+__attribute__((visibility("protected"))) int b;
+
+extern __attribute__((visibility("hidden"))) void zoo_extern_h(void);
+
+void (*foo_p)(void) = zoo_extern_h;
+
+__attribute__((visibility("protected"))) void bar() {
+  foo_h(&b);
+  foo_p();
+}
+
+class TestClass {
+public:
+  __attribute__((__visibility__("hidden"))) int value() const noexcept { return 0; }
+};
+
+int main() {
+  TestClass TC;
+  return TC.value();
+}
+
+template <class T>
+class basic {
+public:
+  __attribute__((__visibility__("protected"))) int getdata() { return 1; }
+};
+
+template class basic<int>;
+
+// AIX-NOVISIBILITY:    @b = global i32 0
+// LINUX-NOVISIBILITY:  @b = dso_local global i32 0
+// AIX-NOVISIBILITY:    define void @_Z5foo_hPi(i32* %p)
+// LINUX-NOVISIBILITY:  define dso_local void @_Z5foo_hPi(i32* %p)
+// NOVISIBILITY:        declare void @_Z12zoo_extern_hv()
+// AIX-NOVISIBILITY:    define void @_Z3barv()
+// LINUX-NOVISIBILITY:  define dso_local void @_Z3barv()
+// AIX-NOVISIBILITY:    define linkonce_odr i32 @_ZNK9TestClass5valueEv(%class.TestClass* %this)
+// LINUX-NOVISIBILITY:  define linkonce_odr dso_local i32 @_ZNK9TestClass5valueEv(%class.TestClass* %this)
+// AIX-NOVISIBILITY:    define weak_odr i32 @_ZN5basicIiE7getdataEv(%class.basic* %this)
+// LINUX-NOVISIBILITY:  define weak_odr dso_local i32 @_ZN5basicIiE7getdataEv(%class.basic* %this)
+
+// VISIBILITY:    @b = protected global i32 0
+// VISIBILITY:    define hidden void @_Z5foo_hPi(i32* %p)
+// VISIBILITY:    declare hidden void @_Z12zoo_extern_hv()
+// VISIBILITY:    define protected void @_Z3barv()
+// VISIBILITY:    define linkonce_odr hidden i32 @_ZNK9TestClass5valueEv(%class.TestClass* %this)
+// VISIBILITY:    define weak_odr protected i32 @_ZN5basicIiE7getdataEv(%class.basic* %this)
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2752,6 +2752,9 @@
     Opts.setValueVisibilityMode(parseVisibility(visOpt, Args, Diags));
   } else {
     Opts.setValueVisibilityMode(DefaultVisibility);
+    // In AIX oS, the -fnovisibility is enable by default.
+    if (T.isOSAIX() || Args.hasArg(OPT_fnovisibility))
+      Opts.NoVisibility = 1;
   }
 
   // The type-visibility mode defaults to the value-visibility mode.
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -5198,6 +5198,9 @@
     }
   }
 
+  if (Args.getLastArg(options::OPT_fnovisibility))
+    CmdArgs.push_back("-fnovisibility");
+
   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
 
Index: clang/lib/AST/Decl.cpp
===================================================================
--- clang/lib/AST/Decl.cpp
+++ clang/lib/AST/Decl.cpp
@@ -1477,10 +1477,16 @@
 }
 
 LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) {
-  return getLVForDecl(D,
-                      LVComputationKind(usesTypeVisibility(D)
-                                            ? NamedDecl::VisibilityForType
-                                            : NamedDecl::VisibilityForValue));
+  NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D)
+                                             ? NamedDecl::VisibilityForType
+                                             : NamedDecl::VisibilityForValue;
+  LVComputationKind CK(EK);
+
+  // In AIX OS, if there is -fnovisibility and -fvisibility is default
+  // visibility, the clang will ignore all visibility.
+  return getLVForDecl(D, D->getASTContext().getLangOpts().NoVisibility
+                             ? CK.forLinkageOnly()
+                             : CK);
 }
 
 Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const {
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1936,6 +1936,9 @@
 def fverbose_asm : Flag<["-"], "fverbose-asm">, Group<f_Group>,
   HelpText<"Generate verbose assembly output">;
 def dA : Flag<["-"], "dA">, Alias<fverbose_asm>;
+def fnovisibility : Flag<["-"], "fnovisibility">, Group<f_Group>,
+  HelpText<"Ignore all the visibility attributes in source code">,
+  Flags<[CC1Option]>;
 def fvisibility_EQ : Joined<["-"], "fvisibility=">, Group<f_Group>,
   HelpText<"Set the default symbol visibility for all global declarations">, Values<"hidden,default">;
 def fvisibility_inlines_hidden : Flag<["-"], "fvisibility-inlines-hidden">, Group<f_Group>,
Index: clang/include/clang/Basic/LangOptions.def
===================================================================
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -297,6 +297,7 @@
     "type's inheritance model would be determined under the Microsoft ABI")
 
 ENUM_LANGOPT(GC, GCMode, 2, NonGC, "Objective-C Garbage Collection mode")
+BENIGN_LANGOPT(NoVisibility, 1, 0, "All the visibility attributes that are specified in the source are ignored.")
 ENUM_LANGOPT(ValueVisibilityMode, Visibility, 3, DefaultVisibility,
              "default visibility for functions and variables [-fvisibility]")
 ENUM_LANGOPT(TypeVisibilityMode, Visibility, 3, DefaultVisibility,
Index: clang/docs/ClangCommandLineReference.rst
===================================================================
--- clang/docs/ClangCommandLineReference.rst
+++ clang/docs/ClangCommandLineReference.rst
@@ -2309,6 +2309,10 @@
 
 .. option:: -fvisibility-ms-compat
 
+Ignore all the visibility attributes in source code
+
+.. option:: -fnovisibility
+
 Give global types 'default' visibility and global functions and variables 'hidden' visibility by default
 
 .. option:: -fvisibility=<arg>
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to