[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-27 Thread Zaara Syeda via cfe-commits

syzaara wrote:

> I'm seeing test failures locally when compiling with 
> `LLVM_REVERSE_ITERATION=ON`. I'm surprised this hasn't failed on buildbot.
> 
> ```
> clang/test/Driver/toc-conf.c:22:18: error: CHECK-EQCONF: expected string not 
> found in input
> // CHECK-EQCONF: "-mtocdata=g3,g4"
>  ^
> :5:385: note: scanning from here
>  "/usr/local/google/home/paulkirth/llvm-fork/build/bin/llvm" "clang" "-cc1" 
> "-triple" "powerpc-unknown-aix" "-emit-obj" "-mrelax-all" "-dumpdir" "a-" 
> "-disable-free" "-clear-ast-before-backend" "-main-file-name" "toc-conf.c" 
> "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=all" 
> "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" 
> "-mconstructor-aliases" "-mno-tocdata" "-mtocdata=g4,g3" "-fxl-pragma-pack" 
> "-target-cpu" "pwr7" "-mfloat-abi" "hard" "-gstrict-dwarf" "-gno-column-info" 
> "-fdebug-compilation-dir=/usr/local/google/home/paulkirth/llvm-fork/build" 
> "-fdata-sections" 
> "-fcoverage-compilation-dir=/usr/local/google/home/paulkirth/llvm-fork/build" 
> "-resource-dir" 
> "/usr/local/google/home/paulkirth/llvm-fork/build/lib/clang/19" 
> "-internal-isystem" 
> "/usr/local/google/home/paulkirth/llvm-fork/build/lib/clang/19/include/ppc_wrappers"
>  "-internal-isystem" 
> "/usr/local/google/home/paulkirth/llvm-fork/build/lib/clang/19/include" 
> "-internal-isystem" "/usr/include" "-ferror-limit" "19" "-fno-signed-char" 
> "-fno-use-cxa-atexit" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-o" 
> "/tmp/toc-conf-f15b3c.o" "-x" "c" 
> "/usr/local/google/home/paulkirth/llvm-fork/clang/test/Driver/toc-conf.c"
> ```
> 
> If you notice, the arg ordering is reversed (`g4,g3`). I think this is the 
> iteration w/ the filter in AIX.cpp around line 475, but it may be the 
> iteration in `buildExceptionList`.

Thanks, this should be fixed with 
https://github.com/llvm/llvm-project/pull/86840

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-14 Thread Zaara Syeda via cfe-commits


@@ -404,6 +404,15 @@ class CodeGenOptions : public CodeGenOptionsBase {
   /// List of pass builder callbacks.
   std::vector> PassBuilderCallbacks;
 
+  /// List of global variables explicitly specified by the user as toc-data.
+  std::vector TocDataVarsUserSpecified;
+
+  /// List of global variables that over-ride the toc-data default.
+  std::vector NoTocDataVars;
+
+  /// Flag for all global variables to be treated as toc-data.
+  bool AllTocData;

syzaara wrote:

Thank you!

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-14 Thread Zaara Syeda via cfe-commits

syzaara wrote:

> This is broken by the patch 
> https://lab.llvm.org/buildbot/#/builders/5/builds/41773/steps/9/logs/stdio

Should be fixed by 
[00ba2a6](https://github.com/llvm/llvm-project/commit/00ba2a6f1e7bbb6fa6f3d50ad87a4a528af87ef5)

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-14 Thread Vitaly Buka via cfe-commits

vitalybuka wrote:

This is broken by the patch 
https://lab.llvm.org/buildbot/#/builders/5/builds/41773/steps/9/logs/stdio

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-13 Thread Fangrui Song via cfe-commits


@@ -404,6 +404,15 @@ class CodeGenOptions : public CodeGenOptionsBase {
   /// List of pass builder callbacks.
   std::vector> PassBuilderCallbacks;
 
+  /// List of global variables explicitly specified by the user as toc-data.
+  std::vector TocDataVarsUserSpecified;
+
+  /// List of global variables that over-ride the toc-data default.
+  std::vector NoTocDataVars;
+
+  /// Flag for all global variables to be treated as toc-data.
+  bool AllTocData;

MaskRay wrote:

The uninitialized `AllTocData` would cause memorysanitizer failures.
booleans and enums should be moved to `CodeGenOptionsBase` using 
`clang/Basic/CodeGenOptions.def`.
I fixed this in 605abe0689dfd28aadc9413306f33a4494cf3fb8

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-13 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara closed 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-03-12 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From d7536fb557b254454b9944c4bef4d552e117190a Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/9] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 7391e4cf3a9aeb..2ce6da677e91e8 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4227,7 +4227,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenOpt

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-20 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From 841345a110db787920ca8acbc08801436f2b36db Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/9] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 7391e4cf3a9aeb..2ce6da677e91e8 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4227,7 +4227,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenOpt

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-15 Thread Amy Kwan via cfe-commits

https://github.com/amy-kwan edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-15 Thread Amy Kwan via cfe-commits

https://github.com/amy-kwan approved this pull request.

Two minor nits but also LGTM.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-15 Thread Amy Kwan via cfe-commits


@@ -265,6 +269,62 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (!isa(GV))
+return;
+
+  auto *GVar = dyn_cast(GV);
+  auto GVId = GV->getName();
+
+  // Is this a global variable specified by the user as toc-data?
+  bool UserSpecifiedTOC =
+  llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+  // Assumes the same variable cannot be in both TocVarsUserSpecified and
+  // NoTocVars.
+  if (UserSpecifiedTOC ||
+  ((M.getCodeGenOpts().AllTocData) &&
+   !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+const unsigned long PointerSize =
+GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+auto *VarD = dyn_cast(D);
+assert(VarD && "Invalid declaration of global variable.");
+
+ASTContext &Context = D->getASTContext();
+unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+const auto *Ty = VarD->getType().getTypePtr();
+const RecordDecl *RDecl =
+Ty->isRecordType() ? Ty->getAs()->getDecl() : nullptr;
+
+bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+auto reportUnsupportedWarning = [&](bool ShouldEmitWarning, StringRef Msg) 
{
+  if (ShouldEmitWarning)
+M.getDiags().Report(D->getLocation(), diag::warn_toc_unsupported_type)
+<< GVId << Msg;
+};
+if (!Ty || Ty->isIncompleteType()) {

amy-kwan wrote:

nit: Since there's only one statement inside these conditions, I think we can 
omit the curly braces.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-15 Thread Amy Kwan via cfe-commits


@@ -82,8 +82,7 @@ void MCSectionXCOFF::printSwitchToSection(const MCAsmInfo 
&MAI, const Triple &T,
   }
 
   if (isCsect() && getMappingClass() == XCOFF::XMC_TD) {
-assert((getKind().isBSSExtern() || getKind().isBSSLocal()) &&
-   "Unexepected section kind for toc-data");
+assert(getKind().isBSS() && "Unexepected section kind for toc-data");

amy-kwan wrote:

```suggestion
assert(getKind().isBSS() && "Unexpected section kind for toc-data");
```

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-12 Thread zhijian lin via cfe-commits


@@ -172,6 +172,30 @@ bool PPCSubtarget::enableSubRegLiveness() const {
   return UseSubRegLiveness;
 }
 
+void PPCSubtarget::tocDataChecks(unsigned PointerSize,
+ const GlobalVariable *GV) const {
+  // TODO: These asserts should be updated as more support for the toc data
+  // transformation is added (struct support, etc.).
+  assert(
+  PointerSize >= GV->getAlign().valueOrOne().value() &&
+  "GlobalVariables with an alignment requirement stricter than TOC entry "
+  "size not supported by the toc data transformation.");
+
+  Type *GVType = GV->getValueType();
+  assert(GVType->isSized() && "A GlobalVariable's size must be known to be "
+  "supported by the toc data transformation.");
+  if (GV->getParent()->getDataLayout().getTypeSizeInBits(GVType) >
+  PointerSize * 8)
+report_fatal_error(
+"A GlobalVariable with size larger than a TOC entry is not currently "
+"supported by the toc data transformation.");
+  if (GV->hasLocalLinkage() || GV->hasPrivateLinkage())
+report_fatal_error("A GlobalVariable with private or local linkage is not "
+   "currently supported by the toc data transformation.");
+  assert(!GV->hasCommonLinkage() &&
+ "Tentative definitions cannot have the mapping class XMC_TD.");
+}

diggerlin wrote:

since the https://github.com/llvm/llvm-project/pull/79530/files merge , please 
remember to remove 
   assert(!GV->hasCommonLinkage() &&
 "Tentative definitions cannot have the mapping class XMC_TD.");

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-02-12 Thread Zaara Syeda via cfe-commits

syzaara wrote:

Ping.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-30 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From dced11bd242aba08158a5a0a399b2b0ea7381a69 Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/8] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 2e658557b0e31..c6c8813d6124f 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4028,7 +4028,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenOptio

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-30 Thread Aaron Liu via cfe-commits


@@ -3520,6 +3520,27 @@ def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">,
   MetaVarName<"">,
   HelpText<"Load pass plugin from a dynamic shared object file (only with new 
pass manager).">,
   MarshallingInfoStringVector>;
+defm tocdata : BoolOption<"m","tocdata",
+  CodeGenOpts<"AllTocData">, DefaultFalse,
+  PosFlag,
+  NegFlag,
+  BothFlags<[], [ClangOption, CLOption]>>, Group;
+def mtocdata_EQ : CommaJoined<["-"], "mtocdata=">,
+  Visibility<[ClangOption, CC1Option]>,
+  Flags<[TargetSpecific]>,

AaronHLiu wrote:

Need to remove all `Flags<[TargetSpecific]>`, otherwise will get errors in link 
step as follows:
```
error: unsupported option '-mtocdata' for target 'powerpc-ibm-aix7.2.0.0'
```

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-22 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From dced11bd242aba08158a5a0a399b2b0ea7381a69 Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/7] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 2e658557b0e310c..c6c8813d6124f7a 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4028,7 +4028,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenO

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-18 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From dced11bd242aba08158a5a0a399b2b0ea7381a69 Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/6] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 2e658557b0e310c..c6c8813d6124f7a 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4028,7 +4028,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenO

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-08 Thread via cfe-commits

https://github.com/diggerlin edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-08 Thread via cfe-commits


@@ -245,6 +245,30 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
   /// True if the GV will be accessed via an indirect symbol.
   bool isGVIndirectSymbol(const GlobalValue *GV) const;
 
+  bool tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) const {

diggerlin wrote:

the function always return true.
suggestion change 
`bool tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) const`

--->
`void tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) const`

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-08 Thread via cfe-commits


@@ -245,6 +245,30 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
   /// True if the GV will be accessed via an indirect symbol.
   bool isGVIndirectSymbol(const GlobalValue *GV) const;
 
+  bool tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) const {

diggerlin wrote:

and it is not a good idea to put a big function in the header file

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2024-01-05 Thread via cfe-commits


@@ -265,6 +269,61 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (!isa(GV))
+return;
+
+  auto *GVar = dyn_cast(GV);
+  auto GVId = GV->getName();
+
+  // Is this a global variable specified by the user as toc-data?
+  bool UserSpecifiedTOC =
+  llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+  // Assumes the same variable cannot be in both TocVarsUserSpecified and
+  // NoTocVars.
+  if (UserSpecifiedTOC ||
+  ((M.getCodeGenOpts().AllTocData) &&
+   !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+const unsigned long PointerSize =
+GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+auto *VarD = dyn_cast(D);
+assert(VarD && "Invalid declaration of global variable.");
+
+ASTContext &Context = D->getASTContext();
+unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+const auto *Ty = VarD->getType().getTypePtr();
+const RecordDecl *RDecl =
+Ty->isRecordType() ? Ty->getAs()->getDecl() : nullptr;
+
+bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+auto reportUnsupportedWarning = [&](bool ShouldEmitWarning, StringRef Msg) 
{
+  if (ShouldEmitWarning)
+M.getDiags().Report(D->getLocation(), diag::warn_toc_unsupported_type)
+<< GVId << Msg;
+};
+if (!Ty || Ty->isIncompleteType()) {
+  reportUnsupportedWarning(EmitDiagnostic, "of incomplete type");
+} else if (RDecl && RDecl->hasFlexibleArrayMember()) {
+  reportUnsupportedWarning(EmitDiagnostic,
+   "it contains a flexible array member");
+} else if (VarD->getTLSKind() != VarDecl::TLS_None) {
+  reportUnsupportedWarning(EmitDiagnostic, "of thread local storage");
+} else if (PointerSize < Context.getTypeInfo(VarD->getType()).Width / 8) {
+  reportUnsupportedWarning(EmitDiagnostic,
+   "variable is larger than a pointer");
+} else if (PointerSize < Alignment) {
+  reportUnsupportedWarning(EmitDiagnostic,
+   "variable is aligned wider than a pointer");
+} else if (D->hasAttr()) {
+  reportUnsupportedWarning(EmitDiagnostic, "of a section attribute");
+} else if ((GV->hasExternalLinkage() || M.getCodeGenOpts().AllTocData) &&
+   !GV->hasLocalLinkage()) {

diggerlin wrote:

 if GV->hasExternalLinkage() is true ,   the !GV->hasLocalLinkage() is true too.

`((GV->hasExternalLinkage() || M.getCodeGenOpts().AllTocData) && 
!GV->hasLocalLinkage())`
look weirds.   suggest changing to 
`(GV->hasExternalLinkage() || (M.getCodeGenOpts().AllTocData) && 
!GV->hasLocalLinkage()))` 
 

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-30 Thread Zaara Syeda via cfe-commits

https://github.com/syzaara updated 
https://github.com/llvm/llvm-project/pull/67999

>From dced11bd242aba08158a5a0a399b2b0ea7381a69 Mon Sep 17 00:00:00 2001
From: Zaara Syeda 
Date: Thu, 28 Sep 2023 15:01:56 -0400
Subject: [PATCH 1/4] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX

This patch enables support that the XL compiler had for AIX under
-qdatalocal/-qdataimported.
---
 clang/docs/UsersManual.rst| 56 
 clang/include/clang/Basic/CodeGenOptions.h|  9 ++
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 +
 .../clang/Basic/DiagnosticFrontendKinds.td|  2 +
 clang/include/clang/Driver/Options.td | 23 +
 clang/lib/CodeGen/CGDecl.cpp  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 28 ++
 clang/lib/CodeGen/Targets/PPC.cpp | 61 +
 clang/lib/Driver/ToolChains/AIX.cpp   | 89 +++
 clang/lib/Frontend/CompilerInstance.cpp   |  5 ++
 .../test/CodeGen/PowerPC/toc-data-attribute.c | 61 +
 .../CodeGen/PowerPC/toc-data-attribute.cpp| 43 +
 .../CodeGen/PowerPC/toc-data-diagnostics.c| 69 ++
 .../PowerPC/toc-data-structs-arrays.cpp   | 64 +
 clang/test/CodeGen/toc-data.c | 31 +++
 clang/test/Driver/toc-conf.c  | 30 +++
 clang/test/Driver/tocdata-cc1.c   | 16 
 llvm/include/llvm/ADT/STLExtras.h | 13 +++
 llvm/lib/MC/MCSectionXCOFF.cpp|  3 +-
 llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp   | 20 ++---
 .../CodeGen/PowerPC/toc-data-large-array.ll   | 16 
 .../CodeGen/PowerPC/toc-data-struct-array.ll  | 29 ++
 22 files changed, 655 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-attribute.cpp
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-diagnostics.c
 create mode 100644 clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp
 create mode 100644 clang/test/CodeGen/toc-data.c
 create mode 100644 clang/test/Driver/toc-conf.c
 create mode 100644 clang/test/Driver/tocdata-cc1.c
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-large-array.ll
 create mode 100644 llvm/test/CodeGen/PowerPC/toc-data-struct-array.ll

diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 2e658557b0e310c..c6c8813d6124f7a 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4028,7 +4028,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage
+-  not have aliases
+-  not have section attributes
+
+The TOC data transformation results in the variable, not its address,
+being placed in the TOC. This eliminates the need to load the address of the
+variable from the TOC.
+
+Note:
+If the TOC data transformation is applied to a variable whose definition
+is imported, the linker will generate fixup code for reading or writing to the
+variable.
+
+Any internal linkage variables specified to any TOC data options will be 
ignored.
+
+**Options:**
+
+.. option:: -mno-tocdata
+
+  This is the default behaviour. Only variables explicitly specified with
+  ``-mtocdata=`` will have the TOC data transformation applied.
+
+.. option:: -mtocdata
+
+  Apply the TOC data transformation to all suitable variables with static
+  storage duration (including static data members of classes and block-scope
+  static variables) that are not explicitly specified with ``-mno-tocdata=``.
+
+.. option:: -mno-tocdata=
+
+  Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
+  list of external linkage variables, specified using their mangled names, as
+  exceptions to ``-mtocdata``.
+
+.. option:: -mtocdata=
+
+  Apply the TOC data transformation to the comma-separated list of external
+  linkage variables, specified using their mangled names, if they are suitable.
+  Emit diagnostics for all unsuitable variables specified.
+
+Default Visibility Export Mapping
+"
 The ``-mdefault-visibility-export-mapping=`` option can be used to control
 mapping of default visibility to an explicit shared object export
 (i.e. XCOFF exported visibility). Three values are provided for the option:
diff --git a/clang/include/clang/Basic/CodeGenO

[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-29 Thread Zaara Syeda via cfe-commits


@@ -0,0 +1,16 @@
+; RUN: not --crash llc -mtriple powerpc-ibm-aix-xcoff < %s 2>&1 | FileCheck %s 
--check-prefix CHECK-ERROR
+; RUN: not --crash llc -mtriple powerpc64-ibm-aix-xcoff < %s 2>&1 | FileCheck 
%s --check-prefix CHECK-ERROR
+
+@a = global [5 x i16] zeroinitializer, align 2 #0
+
+; Function Attrs: noinline
+define i16 @foo() #1 {
+entry:
+  %0 = load i16, ptr @a, align 2
+  ret i16 %0
+}
+
+attributes #0 = { "toc-data" }
+attributes #1 = { noinline }
+

syzaara wrote:

Thanks, I've added a function tocDataChecks which is called from 
emitGlobalVariable to handle this.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-29 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 4c1745207665262380e9bff65201816a966339d0 
f23461ad86202ae37947c5ad1099e301b23b3589 -- 
clang/test/CodeGen/PowerPC/toc-data-attribute.c 
clang/test/CodeGen/PowerPC/toc-data-attribute.cpp 
clang/test/CodeGen/PowerPC/toc-data-diagnostics.c 
clang/test/CodeGen/PowerPC/toc-data-structs-arrays.cpp 
clang/test/Driver/toc-conf.c clang/test/Driver/tocdata-cc1.c 
clang/include/clang/Basic/CodeGenOptions.h clang/lib/CodeGen/CGDecl.cpp 
clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/Targets/PPC.cpp 
clang/lib/Driver/ToolChains/AIX.cpp clang/lib/Frontend/CompilerInstance.cpp 
llvm/include/llvm/ADT/STLExtras.h llvm/lib/MC/MCSectionXCOFF.cpp 
llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp 
llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp 
llvm/lib/Target/PowerPC/PPCSubtarget.h
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index fa0ed80f4c..acb405a594 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -403,7 +403,7 @@ public:
 
   /// List of pass builder callbacks.
   std::vector> PassBuilderCallbacks;
-  
+
   /// List of global variables explicitly specified by the user as toc-data.
   std::vector TocDataVarsUserSpecified;
 

``




https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-29 Thread Zaara Syeda via cfe-commits


@@ -0,0 +1,69 @@
+// REQUIRES: powerpc-registered-target
+// RUN: %clang_cc1 %s -triple=powerpc-ibm-aix-xcoff -S 
-mtocdata=h,g,f,e,d,c,b,a,globalOneWithAlias,globalTwoWithAlias,ll,t3 -verify 
-emit-llvm -o - | FileCheck %s -check-prefix=CHECK --match-full-lines
+// RUN: %clang_cc1 %s -triple=powerpc-ibm-aix-xcoff -S -mtocdata -verify=none 
-emit-llvm -o - | FileCheck %s -check-prefix=CHECK --match-full-lines
+
+// none-no-diagnostics
+
+struct large_struct {
+  int x;
+  short y;
+  short z;
+  char c;
+};
+
+struct large_struct a;  // expected-warning {{-mtocdata 
option is ignored for a because variable is larger than a pointer}}
+long long b = 5;// expected-warning {{-mtocdata 
option is ignored for b because variable is larger than a pointer}}
+int __attribute__((aligned(128))) c = 0;// expected-warning {{-mtocdata 
option is ignored for c because variable is aligned wider than a pointer}}
+double d = 2.5; // expected-warning {{-mtocdata 
option is ignored for d because variable is larger than a pointer}}
+int e __attribute__((section("foo"))) = 10; // expected-warning {{-mtocdata 
option is ignored for e because of a section attribute}}
+__thread int f; // expected-warning {{-mtocdata 
option is ignored for f because of thread local storage}}
+
+struct SomeStruct;
+extern struct SomeStruct g; // expected-warning {{-mtocdata 
option is ignored for g because of incomplete type}}
+
+extern int h[]; // expected-warning {{-mtocdata 
option is ignored for h because of incomplete type}}
+
+struct ty3 {
+  int A;
+  char C[];
+};
+struct ty3 t3 = { 4, "fo" }; // expected-warning {{-mtocdata option is ignored 
for t3 because it contains a flexible array member}}
+
+int globalOneWithAlias = 10;
+__attribute__((__alias__("globalOneWithAlias"))) extern int aliasOne; // 
expected-warning {{-mtocdata option is ignored for globalOneWithAlias because 
the variable has an alias}}
+__attribute__((__alias__("globalTwoWithAlias"))) extern int aliasTwo; // 
expected-warning {{-mtocdata option is ignored for globalTwoWithAlias because 
the variable has an alias}}
+int globalTwoWithAlias = 20;

syzaara wrote:

I was just testing the cases where the global is defined before the alias 
versus after the alias.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-29 Thread Zaara Syeda via cfe-commits


@@ -622,6 +622,28 @@ static bool checkAliasedGlobal(
   return true;
 }
 
+// Emit a warning if toc-data attribute is requested for global variables that
+// have aliases and remove the toc-data attribute.
+static void checkAliasForTocData(llvm::GlobalVariable *GVar,
+ const CodeGenOptions &CodeGenOpts,
+ DiagnosticsEngine &Diags,
+ SourceLocation Location) {
+  if (GVar->hasAttribute("toc-data")) {
+auto GVId = GVar->getGlobalIdentifier();

syzaara wrote:

When we support the local linkage variables for toc-data, it will be for the 
option -mtocdata which enables toc-data across the board. The list option 
-mtocdata= is only for variables with external linkage specified using their 
mangled name. So there shouldn't be an issue with searching for the name of 
variables with local linkage.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-03 Thread via cfe-commits

https://github.com/diggerlin edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-03 Thread via cfe-commits


@@ -0,0 +1,29 @@
+; RUN: llc -mtriple powerpc-ibm-aix-xcoff < %s | FileCheck %s --check-prefix 
CHECK
+; RUN: llc -mtriple powerpc64-ibm-aix-xcoff < %s | FileCheck %s --check-prefix 
CHECK
+

diggerlin wrote:

also need to add test scenario `--filetype=obj` 

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits

stephenpeckham wrote:

> What are the ABI implications here? Do all translation units have to agree on 
> whether a variable is "tocdata"? The manual change hints there's some sort of 
> linker fixup, but it's not clear to me how that works.

There are no ABI implications. Using -mtocdata removes a level of indirection, 
but you can mix tocdata and non-tocdata references to the same variable.

The -mtocdata option is intended for variables that are part of the current 
loadable object and not imported. If you specify --mtocdata for a symbol that 
is actually imported, the linker will generate extra code (called "fixup" code) 
to reference the variable.  A warning is printed because fixup code is not 
great for performance


https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits

https://github.com/diggerlin edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits


@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -triple powerpc-unknown-aix -S -mtocdata=g1,g2,g3 
-emit-llvm -o - | FileCheck %s -check-prefix=CHECK-MIX --match-full-lines

diggerlin wrote:

I am not sure the purpose of the test case, I think all the test scenario have 
be covered by previous test cases.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits


@@ -0,0 +1,16 @@
+; RUN: not --crash llc -mtriple powerpc-ibm-aix-xcoff < %s 2>&1 | FileCheck %s 
--check-prefix CHECK-ERROR
+; RUN: not --crash llc -mtriple powerpc64-ibm-aix-xcoff < %s 2>&1 | FileCheck 
%s --check-prefix CHECK-ERROR
+
+@a = global [5 x i16] zeroinitializer, align 2 #0
+
+; Function Attrs: noinline
+define i16 @foo() #1 {
+entry:
+  %0 = load i16, ptr @a, align 2
+  ret i16 %0
+}
+
+attributes #0 = { "toc-data" }
+attributes #1 = { noinline }
+

diggerlin wrote:

If I deleted the 
`; Function Attrs: noinline
define i16 @foo() #1 {
entry:
  %0 = load i16, ptr @a, align 2
  ret i16 %0
}`
 
and  
`attributes #1 = { noinline }`

it do not crash (I should be crash)

and it generate following Asm


```
.csect [PR],5
.file   "","IBM Open XL C/C++ for Linux on Power 17.1.2 
(5725-C72, 5765-J20), version 17.1.2.0, LLVM version 18.0.0git"
.toc
.csect a[TD],2
.globl  a[TD]   # @a
.align  1
.space  10 

```
 if I understand correctly. It  should not generate a[TD] which is 10 bytes 
large than  8 bytes pointer size.




https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits


@@ -0,0 +1,69 @@
+// REQUIRES: powerpc-registered-target
+// RUN: %clang_cc1 %s -triple=powerpc-ibm-aix-xcoff -S 
-mtocdata=h,g,f,e,d,c,b,a,globalOneWithAlias,globalTwoWithAlias,ll,t3 -verify 
-emit-llvm -o - | FileCheck %s -check-prefix=CHECK --match-full-lines
+// RUN: %clang_cc1 %s -triple=powerpc-ibm-aix-xcoff -S -mtocdata -verify=none 
-emit-llvm -o - | FileCheck %s -check-prefix=CHECK --match-full-lines
+
+// none-no-diagnostics
+
+struct large_struct {
+  int x;
+  short y;
+  short z;
+  char c;
+};
+
+struct large_struct a;  // expected-warning {{-mtocdata 
option is ignored for a because variable is larger than a pointer}}
+long long b = 5;// expected-warning {{-mtocdata 
option is ignored for b because variable is larger than a pointer}}
+int __attribute__((aligned(128))) c = 0;// expected-warning {{-mtocdata 
option is ignored for c because variable is aligned wider than a pointer}}
+double d = 2.5; // expected-warning {{-mtocdata 
option is ignored for d because variable is larger than a pointer}}
+int e __attribute__((section("foo"))) = 10; // expected-warning {{-mtocdata 
option is ignored for e because of a section attribute}}
+__thread int f; // expected-warning {{-mtocdata 
option is ignored for f because of thread local storage}}
+
+struct SomeStruct;
+extern struct SomeStruct g; // expected-warning {{-mtocdata 
option is ignored for g because of incomplete type}}
+
+extern int h[]; // expected-warning {{-mtocdata 
option is ignored for h because of incomplete type}}
+
+struct ty3 {
+  int A;
+  char C[];
+};
+struct ty3 t3 = { 4, "fo" }; // expected-warning {{-mtocdata option is ignored 
for t3 because it contains a flexible array member}}
+
+int globalOneWithAlias = 10;
+__attribute__((__alias__("globalOneWithAlias"))) extern int aliasOne; // 
expected-warning {{-mtocdata option is ignored for globalOneWithAlias because 
the variable has an alias}}
+__attribute__((__alias__("globalTwoWithAlias"))) extern int aliasTwo; // 
expected-warning {{-mtocdata option is ignored for globalTwoWithAlias because 
the variable has an alias}}
+int globalTwoWithAlias = 20;

diggerlin wrote:

the test aliasTwo (globalTwoWithAlias) has the same purpose of 
aliasOne(globalOneWithAlias) , can we delete one of them ?

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits


@@ -0,0 +1,64 @@
+// RUN: %clang_cc1 %s -triple powerpc-ibm-aix-xcoff -S 
-mtocdata=a4,a5,a8,a9,b,c,d,e,v -emit-llvm -o - 2>&1 \
+// RUN:  | FileCheck %s -check-prefixes=CHECK32 --match-full-lines
+// RUN: %clang_cc1  %s -triple powerpc-ibm-aix-xcoff -S -mtocdata -emit-llvm 
-o - 2>&1 \
+// RUN:  | FileCheck %s -check-prefixes=CHECK32 --match-full-lines
+
+// RUN: %clang_cc1  %s -triple powerpc64-ibm-aix-xcoff -S 
-mtocdata=a4,a5,a8,a9,b,c,d,e,v -emit-llvm -o - 2>&1 \
+// RUN:  | FileCheck %s -check-prefixes=CHECK64 --match-full-lines
+// RUN: %clang_cc1  %s -triple powerpc64-ibm-aix-xcoff -S -mtocdata -emit-llvm 
-o - 2>&1 \
+// RUN:  | FileCheck %s -check-prefixes=CHECK64 --match-full-lines
+
+struct size4_struct {
+  int x;
+};
+
+struct size5_struct {
+  int x;
+  char c;
+};
+
+struct size8_struct {
+  int x;
+  short y;
+  short z;
+};
+
+struct size9_struct {
+  int x;
+  short y;
+  short z;
+  char c;
+};
+
+struct size4_struct a4;
+struct size5_struct a5;
+struct size8_struct a8;
+struct size9_struct a9;
+
+short b[2];
+short c[3];
+short d[4];
+short e[5];
+
+int func_a() {
+  return a4.x+a5.x+a8.x+a9.x+b[0]+c[0]+d[0]+e[0];
+}
+
+// CHECK32: @a4 = global %struct.size4_struct zeroinitializer, align 4 #0
+// CHECK32: @a5 = global %struct.size5_struct zeroinitializer, align 4
+// CHECK32: @a8 = global %struct.size8_struct zeroinitializer, align 4
+// CHECK32: @a9 = global %struct.size9_struct zeroinitializer, align 4
+// CHECK32: @b = global [2 x i16] zeroinitializer, align 2 #0
+// CHECK32: @c = global [3 x i16] zeroinitializer, align 2
+// CHECK32: @d = global [4 x i16] zeroinitializer, align 2
+// CHECK32: @e = global [5 x i16] zeroinitializer, align 2
+// CHECK32: attributes #0 = { "toc-data" }
+
+// CHECK64: @a4 = global %struct.size4_struct zeroinitializer, align 4 #0
+// CHECK64: @a5 = global %struct.size5_struct zeroinitializer, align 4 #0
+// CHECK64: @a8 = global %struct.size8_struct zeroinitializer, align 4 #0
+// CHECK64: @a9 = global %struct.size9_struct zeroinitializer, align 4
+// CHECK64: @b = global [2 x i16] zeroinitializer, align 2 #0
+// CHECK64: @c = global [3 x i16] zeroinitializer, align 2 #0
+// CHECK64: @d = global [4 x i16] zeroinitializer, align 2 #0

diggerlin wrote:

add check  for 64bit ?
// CHECK64: @e = global [5 x i16] zeroinitializer, align 2 

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-02 Thread via cfe-commits


@@ -265,6 +269,61 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (!isa(GV))
+return;
+
+  auto *GVar = dyn_cast(GV);
+  auto GVId = M.getMangledName(dyn_cast(D));
+
+  // Is this a global variable specified by the user as toc-data?
+  bool UserSpecifiedTOC =
+  llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+  // Assumes the same variable cannot be in both TocVarsUserSpecified and
+  // NoTocVars.
+  if (UserSpecifiedTOC ||
+  ((M.getCodeGenOpts().AllTocData) &&
+   !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+const unsigned long PointerSize =
+GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+ASTContext &Context = D->getASTContext();

diggerlin wrote:

nit; move the line to close proximity to its first usage.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-01 Thread via cfe-commits


@@ -421,13 +421,102 @@ void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList 
&Args,
   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
 }
 
+// This function processes all the mtocdata options to build the final
+// simplified toc data options to pass to CC1.
+static void addTocDataOptions(const llvm::opt::ArgList &Args,
+  llvm::opt::ArgStringList &CC1Args,
+  const Driver &D) {
+
+  // Check the global toc-data setting. The default is -mno-tocdata.
+  // To enable toc-data globally, -mtocdata must be specified.
+  // Additionally, it must be last to take effect.
+  const bool TOCDataGloballyinEffect = [&Args]() {
+if (!Args.hasArg(options::OPT_mtocdata))
+  return false;
+
+const Arg *LastArg =
+Args.getLastArg(options::OPT_mtocdata, options::OPT_mno_tocdata);
+return LastArg->getOption().matches(options::OPT_mtocdata);
+  }();
+
+  // Currently only supported for small code model.
+  if (TOCDataGloballyinEffect &&
+  (Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("large") ||
+   Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("medium"))) {
+D.Diag(clang::diag::warn_drv_unsupported_tocdata);
+return;
+  }
+
+  enum TOCDataSetting {
+AddressInTOC = 0, // Address of the symbol stored in the TOC.
+DataInTOC = 1 // Symbol defined in the TOC.
+  };
+
+  const TOCDataSetting DefaultTocDataSetting =
+  TOCDataGloballyinEffect ? DataInTOC : AddressInTOC;
+
+  // Process the list of variables in the explicitly specified options
+  // -mtocdata= and -mno-tocdata= to see which variables are opposite to
+  // the global setting of tocdata in TOCDataGloballyinEffect.
+  // Those that have the opposite setting to TOCDataGloballyinEffect, are added
+  // to ExplicitlySpecifiedGlobals.
+  llvm::StringSet<> ExplicitlySpecifiedGlobals;
+  for (const auto Arg :
+   Args.filtered(options::OPT_mtocdata_EQ, options::OPT_mno_tocdata_EQ)) {
+TOCDataSetting ArgTocDataSetting =
+Arg->getOption().matches(options::OPT_mtocdata_EQ) ? DataInTOC
+   : AddressInTOC;
+
+if (ArgTocDataSetting != DefaultTocDataSetting)
+  for (const char *Val : Arg->getValues())
+ExplicitlySpecifiedGlobals.insert(Val);
+else
+  for (const char *Val : Arg->getValues())
+ExplicitlySpecifiedGlobals.erase(Val);
+  }
+
+  const bool HasExplicitValues = !ExplicitlySpecifiedGlobals.empty();
+
+  auto buildExceptionList = [](const llvm::StringSet<> &ExplicitValues,
+   const char *OptionSpelling) -> std::string {

diggerlin wrote:

we has only one return `Option` ,the `Option` is std::string, we do not need 
`-> std::string` here

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-11-01 Thread via cfe-commits


@@ -421,13 +421,102 @@ void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList 
&Args,
   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
 }
 
+// This function processes all the mtocdata options to build the final
+// simplified toc data options to pass to CC1.
+static void addTocDataOptions(const llvm::opt::ArgList &Args,
+  llvm::opt::ArgStringList &CC1Args,
+  const Driver &D) {
+
+  // Check the global toc-data setting. The default is -mno-tocdata.
+  // To enable toc-data globally, -mtocdata must be specified.
+  // Additionally, it must be last to take effect.
+  const bool TOCDataGloballyinEffect = [&Args]() {
+if (!Args.hasArg(options::OPT_mtocdata))
+  return false;
+
+const Arg *LastArg =
+Args.getLastArg(options::OPT_mtocdata, options::OPT_mno_tocdata);
+return LastArg->getOption().matches(options::OPT_mtocdata);
+  }();
+
+  // Currently only supported for small code model.
+  if (TOCDataGloballyinEffect &&
+  (Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("large") ||
+   Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("medium"))) {
+D.Diag(clang::diag::warn_drv_unsupported_tocdata);
+return;
+  }
+
+  enum TOCDataSetting {
+AddressInTOC = 0, // Address of the symbol stored in the TOC.
+DataInTOC = 1 // Symbol defined in the TOC.
+  };
+
+  const TOCDataSetting DefaultTocDataSetting =
+  TOCDataGloballyinEffect ? DataInTOC : AddressInTOC;
+
+  // Process the list of variables in the explicitly specified options
+  // -mtocdata= and -mno-tocdata= to see which variables are opposite to
+  // the global setting of tocdata in TOCDataGloballyinEffect.
+  // Those that have the opposite setting to TOCDataGloballyinEffect, are added
+  // to ExplicitlySpecifiedGlobals.
+  llvm::StringSet<> ExplicitlySpecifiedGlobals;
+  for (const auto Arg :
+   Args.filtered(options::OPT_mtocdata_EQ, options::OPT_mno_tocdata_EQ)) {
+TOCDataSetting ArgTocDataSetting =
+Arg->getOption().matches(options::OPT_mtocdata_EQ) ? DataInTOC
+   : AddressInTOC;
+
+if (ArgTocDataSetting != DefaultTocDataSetting)
+  for (const char *Val : Arg->getValues())
+ExplicitlySpecifiedGlobals.insert(Val);
+else
+  for (const char *Val : Arg->getValues())
+ExplicitlySpecifiedGlobals.erase(Val);
+  }

diggerlin wrote:

"The code above defines a rule for handling multiple  `-mno-tocdata=` and 
`-mtocdata=`, and how they interact with `-mno-tocdata` and `-mtocdata` . I 
believe users may not fully understand how these multiple options interact 
without reading the code. Therefore, I think it would be beneficial to add 
descriptions and some examples to the UsersManual.rst file.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits

https://github.com/diggerlin edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread Fangrui Song via cfe-commits


@@ -265,6 +269,63 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (auto *GVar = dyn_cast(GV)) {

MaskRay wrote:

We prefer early-return pattern for this `if` use.

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits

https://github.com/diggerlin edited 
https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits


@@ -265,6 +269,63 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (auto *GVar = dyn_cast(GV)) {
+auto GVId = M.getMangledName(dyn_cast(D));
+
+// Is this a global variable specified by the user as toc-data?
+bool UserSpecifiedTOC =
+llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+// Assumes the same variable cannot be in both TocVarsUserSpecified and
+// NoTocVars.
+if (UserSpecifiedTOC ||
+((M.getCodeGenOpts().AllTocData) &&
+ !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+  const unsigned long PointerSize =
+  GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+  ASTContext &Context = D->getASTContext();
+  auto *VarD = dyn_cast(D);
+  assert(VarD && "Invalid declaration of global variable.");
+
+  unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;

diggerlin wrote:

we can delete  the code 
`ASTContext &Context = D->getASTContext();`

and change the code to
`unsigned Alignment = D->getASTContext().toBits(Context.getDeclAlign(D)) / 8;`



https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits


@@ -265,6 +269,63 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (auto *GVar = dyn_cast(GV)) {
+auto GVId = M.getMangledName(dyn_cast(D));
+
+// Is this a global variable specified by the user as toc-data?
+bool UserSpecifiedTOC =
+llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+// Assumes the same variable cannot be in both TocVarsUserSpecified and
+// NoTocVars.
+if (UserSpecifiedTOC ||
+((M.getCodeGenOpts().AllTocData) &&
+ !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+  const unsigned long PointerSize =
+  GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+  ASTContext &Context = D->getASTContext();
+  auto *VarD = dyn_cast(D);
+  assert(VarD && "Invalid declaration of global variable.");
+
+  unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+  const auto *Ty = VarD->getType().getTypePtr();
+  const RecordDecl *RDecl =
+  Ty->isRecordType() ? Ty->getAs()->getDecl() : nullptr;
+
+  bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+  if (!Ty || Ty->isIncompleteType()) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "of incomplete type";
+  } else if (RDecl && RDecl->hasFlexibleArrayMember()) {
+if (UserSpecifiedTOC)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "it contains a flexible array member";
+  } else if (VarD->getTLSKind() != VarDecl::TLS_None) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "of thread local storage model";

diggerlin wrote:

change to `of thread local storage`

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits


@@ -265,6 +269,63 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (auto *GVar = dyn_cast(GV)) {
+auto GVId = M.getMangledName(dyn_cast(D));
+
+// Is this a global variable specified by the user as toc-data?
+bool UserSpecifiedTOC =
+llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+// Assumes the same variable cannot be in both TocVarsUserSpecified and
+// NoTocVars.
+if (UserSpecifiedTOC ||
+((M.getCodeGenOpts().AllTocData) &&
+ !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+  const unsigned long PointerSize =
+  GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+  ASTContext &Context = D->getASTContext();
+  auto *VarD = dyn_cast(D);
+  assert(VarD && "Invalid declaration of global variable.");
+
+  unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+  const auto *Ty = VarD->getType().getTypePtr();
+  const RecordDecl *RDecl =
+  Ty->isRecordType() ? Ty->getAs()->getDecl() : nullptr;
+
+  bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+  if (!Ty || Ty->isIncompleteType()) {

diggerlin wrote:

suggest that add lambda function

```
auto reportWarningUnsupportType = [&](bool ShouldEmitWarning, char * Msgsg) {
if  (ShouldEmitWarning) 
   M.getDiags().Report(D->getLocation(), diag::warn_toc_unsupported_type)
  << GVId << Msg;
  }
```

and all the following report warnning can write as something like

`reportWarningUnsupportType ( EmitDiagnostic, "of incomplete type");`


https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread Eli Friedman via cfe-commits


@@ -265,6 +269,63 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (auto *GVar = dyn_cast(GV)) {
+auto GVId = M.getMangledName(dyn_cast(D));
+
+// Is this a global variable specified by the user as toc-data?
+bool UserSpecifiedTOC =
+llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+// Assumes the same variable cannot be in both TocVarsUserSpecified and
+// NoTocVars.
+if (UserSpecifiedTOC ||
+((M.getCodeGenOpts().AllTocData) &&
+ !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+  const unsigned long PointerSize =
+  GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+  ASTContext &Context = D->getASTContext();
+  auto *VarD = dyn_cast(D);
+  assert(VarD && "Invalid declaration of global variable.");
+
+  unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+  const auto *Ty = VarD->getType().getTypePtr();
+  const RecordDecl *RDecl =
+  Ty->isRecordType() ? Ty->getAs()->getDecl() : nullptr;
+
+  bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+  if (!Ty || Ty->isIncompleteType()) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "of incomplete type";
+  } else if (RDecl && RDecl->hasFlexibleArrayMember()) {
+if (UserSpecifiedTOC)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "it contains a flexible array member";
+  } else if (VarD->getTLSKind() != VarDecl::TLS_None) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "of thread local storage model";
+  } else if (PointerSize < Context.getTypeInfo(VarD->getType()).Width / 8) 
{
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "variable is larger than a pointer";
+  } else if (PointerSize < Alignment) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "variable is aligned wider than a pointer";
+  } else if (D->hasAttr()) {
+if (EmitDiagnostic)
+  M.getDiags().Report(D->getLocation(), 
diag::warn_toc_unsupported_type)
+  << GVId << "of a section attribute";
+  } else if ((GV->hasExternalLinkage() || M.getCodeGenOpts().AllTocData) &&
+ !GV->hasInternalLinkage()) {

efriedma-quic wrote:

Maybe should be `hasLocalLinkage()`?

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

2023-10-31 Thread via cfe-commits


@@ -4016,7 +4016,63 @@ Clang expects the GCC executable "gcc.exe" compiled for
 
 AIX
 ^^^
+TOC Data Transformation
+"""
+TOC data transformation is off by default (``-mno-tocdata``).
+When ``-mtocdata`` is specified, the TOC data transformation will be applied to
+all suitable variables with static storage duration, including static data
+members of classes and block-scope static variables (if not marked as 
exceptions,
+see further below).
 
+Suitable variables must:
+
+-  have complete types
+-  be independently generated (i.e., not placed in a pool)
+-  be at most as large as a pointer
+-  not be aligned more strictly than a pointer
+-  not be structs containing flexible array members
+-  not have internal linkage

diggerlin wrote:

just curiously, why not  `not have internal linkage` ?
and  we need to add the 
`not be thread local storage` 

https://github.com/llvm/llvm-project/pull/67999
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits