-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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 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. |
@llvm/pr-subscribers-backend-x86 @llvm/pr-subscribers-clang Author: Vinicius Tadeu Zein (vtz) ChangesThis 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:
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;
|
Dear @rnk, I believe you're the right one to review this PR. If not, could you please share the appropriate reviewer? |
There was a problem hiding this 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)
Also I edited the title tags. They aren't an exact science but clang seems the most appropriate here. |
(Not sure who the right reviewers are for XCOFF; I tried to guess.) |
There was a problem hiding this 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".
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
0222326
to
e01f6fb
Compare
@rnk this is also addressed with our new commit. Please refer to my comment above and quoted below:
|
My comments have been addressed. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
e01f6fb
to
6048939
Compare
@@ -1,4 +1,5 @@ | |||
;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s | |||
;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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(); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
6048939
to
4cba743
Compare
4cba743
to
c6c864d
Compare
(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.) |
This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.
e186a67
to
0e36be9
Compare
@efriedma-quic , I guess all your comments were addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@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! |
This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.