Skip to content

[clang] Implement pragma clang section on COFF targets #112714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

vtz
Copy link
Contributor

@vtz vtz commented Oct 17, 2024

This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-arm

@llvm/pr-subscribers-clang

Author: Vinicius Tadeu Zein (vtz)

Changes

This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.


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

2 Files Affected:

  • (added) clang/test/Sema/pragma-clang-section-coff.c (+7)
  • (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+32-1)
diff --git a/clang/test/Sema/pragma-clang-section-coff.c b/clang/test/Sema/pragma-clang-section-coff.c
new file mode 100644
index 00000000000000..573a629505a0cf
--- /dev/null
+++ b/clang/test/Sema/pragma-clang-section-coff.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
+// expected-no-diagnostics
+#pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" text = "mytext.1"
+#pragma clang section bss="" data="" rodata="" text=""
+#pragma clang section
+
+int a;
\ No newline at end of file
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index ce50a3c19ffe04..e7cc8d32d97ad0 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1677,6 +1677,22 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
       Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
                                       /*AddSegmentInfo=*/false))
     Kind = SectionKind::getMetadata();
+
+  const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
+  if (GV && GV->hasImplicitSection()) {
+    auto Attrs = GV->getAttributes();
+    if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
+      Name = Attrs.getAttribute("bss-section").getValueAsString();
+    } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
+      Name = Attrs.getAttribute("rodata-section").getValueAsString();
+    } else if (Attrs.hasAttribute("relro-section") &&
+               Kind.isReadOnlyWithRel()) {
+      Name = Attrs.getAttribute("relro-section").getValueAsString();
+    } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
+      Name = Attrs.getAttribute("data-section").getValueAsString();
+    }
+  }
+
   int Selection = 0;
   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
   StringRef COMDATSymName = "";
@@ -2378,13 +2394,28 @@ MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
   StringRef SectionName = GO->getSection();
 
   // Handle the XCOFF::TD case first, then deal with the rest.
-  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
+  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) {
     if (GVar->hasAttribute("toc-data"))
       return getContext().getXCOFFSection(
           SectionName, Kind,
           XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD),
           /* MultiSymbolsAllowed*/ true);
 
+    if (GVar->hasImplicitSection()) {
+      auto Attrs = GVar->getAttributes();
+      if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
+        SectionName = Attrs.getAttribute("bss-section").getValueAsString();
+      } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
+        SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
+      } else if (Attrs.hasAttribute("relro-section") &&
+                 Kind.isReadOnlyWithRel()) {
+        SectionName = Attrs.getAttribute("relro-section").getValueAsString();
+      } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
+        SectionName = Attrs.getAttribute("data-section").getValueAsString();
+      }
+    }
+  }
+
   XCOFF::StorageMappingClass MappingClass;
   if (Kind.isText())
     MappingClass = XCOFF::XMC_PR;

@vtz
Copy link
Contributor Author

vtz commented Oct 23, 2024

Dear @rnk, I believe you're the right one to review this PR. If not, could you please share the appropriate reviewer?
Thank you very much!

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

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

I'm not an expert here but added some generic comments for now. FYI with the llvm conference last week, many people are still away so review pace is slower right now.

Since this is a Clang change it should have a release note, I think it can go in https://clang.llvm.org/docs/ReleaseNotes.html#windows-support.

The source of that file is clang/docs/ReleaseNotes.rst. You can use the #GH syntax to link back to this pull request. You'll see other examples in the doc.

(RST can be a bit strange if you are used to Markdown so if in doubt, emulate what's already there)

@DavidSpickett DavidSpickett changed the title [PATCH] [COFF] Implement pragma clang section on COFF targets [clang] Implement pragma clang section on COFF targets Nov 1, 2024
@DavidSpickett
Copy link
Collaborator

Also I edited the title tags. They aren't an exact science but clang seems the most appropriate here.

@efriedma-quic
Copy link
Collaborator

(Not sure who the right reviewers are for XCOFF; I tried to guess.)

Copy link
Collaborator

@hubert-reinterpretcast hubert-reinterpretcast left a comment

Choose a reason for hiding this comment

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

The documentation (see https://clang.llvm.org/docs/LanguageExtensions.html) needs to be updated. It currently says that "This feature is only defined to work sensibly for ELF and Mach-O targets".

Copy link
Collaborator

@hubert-reinterpretcast hubert-reinterpretcast left a comment

Choose a reason for hiding this comment

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

There doesn't seem to be changes to handle the text section cases?

@@ -2378,13 +2394,28 @@ MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
StringRef SectionName = GO->getSection();
Copy link
Collaborator

Choose a reason for hiding this comment

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

See report_fatal_error above.
I think the new code should be moved up so that SectionName is correct by the time we reach the next line.

I also believe that this pragma should suppress the application of -mtocdata.
For explicit section attributes and -mtocdata=<var>, there is a warning implemented:

-mtocdata option is ignored for <var> because variable has a section attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The focus was from the beginning on COFF format. We misinterpreted and thought the XCOFF would be the same way. It doesn't look to be so, so we decided to drop the changes for XCOFF from this PR. This also makes this PR more isolated.

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

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

This is an LLVM code change, not a clang code change. It's an important principle that we test LLVM at the smallest reasonable granularity. Can you replace the clang test with an IR test? I'm sure we already have existing IR carrying existing sections, we just need to test it with a new target.

@vtz
Copy link
Contributor Author

vtz commented Nov 22, 2024

This is an LLVM code change, not a clang code change. It's an important principle that we test LLVM at the smallest reasonable granularity. Can you replace the clang test with an IR test? I'm sure we already have existing IR carrying existing sections, we just need to test it with a new target.

@rnk this is also addressed with our new commit.

Please refer to my comment above and quoted below:

I tried making a new test based on llvm/test/CodeGen/ARM/clang-section.ll, but it ended up equal so I added an extra RUN command to this test as well

@DavidSpickett
Copy link
Collaborator

My comments have been addressed.

Copy link

github-actions bot commented Dec 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@vtz vtz force-pushed the coff-pragma-section branch from e01f6fb to 6048939 Compare January 13, 2025 21:19
@@ -1,4 +1,5 @@
;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s
;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

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

"armv7-msvc" doesn't actually trigger COFF output. Try "armv7-windows-msvc".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to x86. I hope this is fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's fine. But the CHECK lines in the new test don't look right (nobits etc. are ELF-specific).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. Improved it.

@@ -1684,6 +1676,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
/*AddSegmentInfo=*/false))
Kind = SectionKind::getMetadata();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@vtz vtz closed this Jan 14, 2025
@vtz vtz force-pushed the coff-pragma-section branch from 4cba743 to c6c864d Compare January 14, 2025 19:37
@efriedma-quic
Copy link
Collaborator

(The PR was automatically closed because vtz:coff-pragma-section doesn't have any changes anymore. It should be possible to reopen if you push the correct commit to the branch.)

@vtz vtz reopened this Jan 14, 2025
This patch implements the directive pragma clang section on
    COFF targets with the exact same features available on ELF
    and Mach-O.
@vtz vtz force-pushed the coff-pragma-section branch from e186a67 to 0e36be9 Compare January 20, 2025 12:16
@vtz
Copy link
Contributor Author

vtz commented Jan 20, 2025

@efriedma-quic , I guess all your comments were addressed.

@vtz vtz requested a review from efriedma-quic January 21, 2025 18:46
Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

@efriedma-quic efriedma-quic merged commit 6ab9daf into llvm:main Jan 22, 2025
9 checks passed
Copy link

@vtz Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:ARM backend:X86 clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants