Skip to content

Conversation

@perry-ca
Copy link
Contributor

@perry-ca perry-ca commented Oct 3, 2024

  • set the default on z/OS to use the XL pragma semantics
  • add in additional pragma pack values such as twobyte & reset supported by XL on z/OS

@perry-ca perry-ca self-assigned this Oct 3, 2024
@perry-ca perry-ca requested a review from wangpc-pp October 3, 2024 19:52
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:SystemZ clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 3, 2024
@perry-ca perry-ca requested a review from abhina-sree October 3, 2024 19:52
@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2024

@llvm/pr-subscribers-backend-systemz
@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Sean Perry (perry-ca)

Changes
  • set the default on z/OS to use the XL pragma semantics
  • add in additional pragma pack values such as twobyte & reset supported by XL on z/OS

Full diff: https://github.com/llvm/llvm-project/pull/111053.diff

4 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/ZOS.cpp (+4)
  • (modified) clang/lib/Parse/ParsePragma.cpp (+26)
  • (added) clang/test/Driver/zos-pragma-pack.c (+8)
  • (added) clang/test/SemaCXX/pragma-pack-packed-2.cpp (+12)
diff --git a/clang/lib/Driver/ToolChains/ZOS.cpp b/clang/lib/Driver/ToolChains/ZOS.cpp
index 074e0556ecd2ad..c5ad3ef1b00f1d 100644
--- a/clang/lib/Driver/ToolChains/ZOS.cpp
+++ b/clang/lib/Driver/ToolChains/ZOS.cpp
@@ -37,6 +37,10 @@ void ZOS::addClangTargetOptions(const ArgList &DriverArgs,
                                 options::OPT_fno_aligned_allocation))
     CC1Args.push_back("-faligned-alloc-unavailable");
 
+  if (DriverArgs.hasFlag(options::OPT_fxl_pragma_pack,
+                         options::OPT_fno_xl_pragma_pack, true))
+    CC1Args.push_back("-fxl-pragma-pack");
+
   // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
   // or disabled sized deallocations.
   if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index cc6f18b5b319f9..12fed448d477c0 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -2126,6 +2126,7 @@ void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
 //   pack '(' [integer] ')'
 //   pack '(' 'show' ')'
 //   pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
+//   pack '(' 'packed' | 'full' | 'twobyte' | 'reset' ')' with -fzos-extensions
 void PragmaPackHandler::HandlePragma(Preprocessor &PP,
                                      PragmaIntroducer Introducer,
                                      Token &PackTok) {
@@ -2155,10 +2156,35 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
                  ? Sema::PSK_Push_Set
                  : Sema::PSK_Set;
   } else if (Tok.is(tok::identifier)) {
+    // Map pragma pack options to pack (integer).
+    auto MapPack = [&](const char *Literal) {
+      Action = Sema::PSK_Push_Set;
+      Alignment = Tok;
+      Alignment.setKind(tok::numeric_constant);
+      Alignment.setLiteralData(Literal);
+      Alignment.setLength(1);
+    };
+
     const IdentifierInfo *II = Tok.getIdentifierInfo();
     if (II->isStr("show")) {
       Action = Sema::PSK_Show;
       PP.Lex(Tok);
+    } else if (II->isStr("packed") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(packed) is the same as #pragma pack(1)
+      MapPack("1");
+      PP.Lex(Tok);
+    } else if (II->isStr("full") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(full) is the same as #pragma pack(4)
+      MapPack("4");
+      PP.Lex(Tok);
+    } else if (II->isStr("twobyte") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(twobyte) is the same as #pragma pack(2)
+      MapPack("2");
+      PP.Lex(Tok);
+    } else if (II->isStr("reset") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(reset) is the same as #pragma pack(pop) on XL
+      Action = Sema::PSK_Pop;
+      PP.Lex(Tok);
     } else {
       if (II->isStr("push")) {
         Action = Sema::PSK_Push;
diff --git a/clang/test/Driver/zos-pragma-pack.c b/clang/test/Driver/zos-pragma-pack.c
new file mode 100644
index 00000000000000..0e04878daba4c5
--- /dev/null
+++ b/clang/test/Driver/zos-pragma-pack.c
@@ -0,0 +1,8 @@
+// REQUIRES: systemz-registered-target
+
+// RUN: %clang -### -target s390x-ibm-zos -c %s -o /dev/null 2>&1 | FileCheck %s
+// CHECK: "-fxl-pragma-pack"
+
+// RUN: %clang -### -fno-xl-pragma-pack -target s390x-ibm-zos -c %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=NOOPT
+// NOOPT-NOT: "-fxl-pragma-pack"
+
diff --git a/clang/test/SemaCXX/pragma-pack-packed-2.cpp b/clang/test/SemaCXX/pragma-pack-packed-2.cpp
new file mode 100644
index 00000000000000..3639addd6fe5fc
--- /dev/null
+++ b/clang/test/SemaCXX/pragma-pack-packed-2.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple s390x-ibm-zos -fzos-extensions -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -fzos-extensions -fxl-pragma-pack -fsyntax-only -verify %s
+// RUN: %clang -target s390x-ibm-zos -S -emit-llvm -Xclang -verify -fno-xl-pragma-pack %s
+
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 8}}
+#pragma pack(twobyte)
+#pragma pack(packed)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 1}}
+#pragma pack(reset)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 2}}
+#pragma pack(pop)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 8}}

Copy link
Contributor

@abhina-sree abhina-sree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, can we add [SystemZ][z/OS] in the title to make it consistent with other PRs

@perry-ca perry-ca changed the title Use the XL pragma pack semantics on z/OS [SystemZ][z/OS] Use the XL pragma pack semantics on z/OS Oct 4, 2024
@perry-ca perry-ca merged commit 4c26a1e into llvm:main Oct 4, 2024
13 checks passed
@perry-ca perry-ca deleted the perry/xl-packed branch October 7, 2024 15:02
metaflow added a commit that referenced this pull request Oct 8, 2024
test invoked clang that has tried to write in source directory that
might be readonly #111053

for 4c26a1e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:SystemZ clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants