[clang] [llvm] [PATCH] [COFF] Implement pragma clang section on COFF targets (PR #112714)

2024-10-17 Thread Vinicius Tadeu Zein via cfe-commits

https://github.com/vtz created https://github.com/llvm/llvm-project/pull/112714

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

>From 02223262c678f02b024b3ce83bb89f08d94815bf Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein 
Date: Wed, 16 Oct 2024 13:22:48 -0400
Subject: [PATCH] [PATCH] [COFF] Implement pragma clang section on COFF targets

This patch implements the directive pragma clang section on
COFF targets with the exact same features available on ELF
and Mach-O.
---
 clang/test/Sema/pragma-clang-section-coff.c   |  7 
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  | 33 ++-
 2 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/pragma-clang-section-coff.c

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 00..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(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(GO))
+  if (const GlobalVariable *GVar = dyn_cast(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;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PATCH] [COFF] Implement pragma clang section on COFF targets (PR #112714)

2024-10-23 Thread Vinicius Tadeu Zein via cfe-commits

vtz wrote:

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! 

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1677,6 +1677,22 @@ MCSection 
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
   Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
   /*AddSegmentInfo=*/false))
 Kind = SectionKind::getMetadata();
+
+  const GlobalVariable *GV = dyn_cast(GO);
+  if (GV && GV->hasImplicitSection()) {
+auto Attrs = GV->getAttributes();
+if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {

vtz wrote:

Done

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1677,6 +1677,22 @@ MCSection 
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
   Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
   /*AddSegmentInfo=*/false))
 Kind = SectionKind::getMetadata();
+
+  const GlobalVariable *GV = dyn_cast(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();
+}
+  }
+

vtz wrote:

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

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


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

vtz wrote:

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.

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits

https://github.com/vtz updated https://github.com/llvm/llvm-project/pull/112714

>From e01f6fb36734b214e23cbbe66818b3269d21a591 Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein 
Date: Fri, 22 Nov 2024 10:39:08 -0500
Subject: [PATCH] [llvm] Implement pragma clang section on COFF targets

This patch implements the directive pragma clang section on
COFF targets with the exact same features available on ELF
and Mach-O.
---
 clang/docs/LanguageExtensions.rst |  2 +-
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/test/Sema/pragma-clang-section.c|  1 +
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  | 52 ---
 llvm/test/CodeGen/ARM/clang-section.ll|  1 +
 5 files changed, 26 insertions(+), 32 deletions(-)

diff --git a/clang/docs/LanguageExtensions.rst 
b/clang/docs/LanguageExtensions.rst
index 3c9078bcdf8118..caeebb39ce31ed 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5531,7 +5531,7 @@ The ``#pragma clang section`` directive obeys the 
following rules:
 
 * The pragma clang section is enabled automatically, without need of any flags.
 
-* This feature is only defined to work sensibly for ELF and Mach-O targets.
+* This feature is only defined to work sensibly for ELF, Mach-O and COFF 
targets.
 
 * If section name is specified through _attribute_((section("myname"))), then
   the attribute name gains precedence.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 54145b28154eb4..4d9401c34e2f17 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -845,6 +845,8 @@ Windows Support
   When `-fms-compatibility-version=18.00` or prior is set on the command line 
this Microsoft extension is still
   allowed as VS2013 and prior allow it.
 
+- Clang now supports the ``#pragma clang section`` directive for COFF targets.
+
 LoongArch Support
 ^
 
diff --git a/clang/test/Sema/pragma-clang-section.c 
b/clang/test/Sema/pragma-clang-section.c
index 458c91c2cf31cd..e33e1dfe8cbef7 100644
--- a/clang/test/Sema/pragma-clang-section.c
+++ b/clang/test/Sema/pragma-clang-section.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
 #pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" 
text = "mytext.1" // expected-note 2 {{#pragma entered here}}
 #pragma clang section bss="" data="" rodata="" text=""
 #pragma clang section
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp 
b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index ce50a3c19ffe04..28875f91ae7202 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -781,29 +781,32 @@ getGlobalObjectInfo(const GlobalObject *GO, const 
TargetMachine &TM) {
   return {Group, IsComdat, Flags};
 }
 
-static MCSection *selectExplicitSectionGlobal(
-const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
-MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
-bool Retain, bool ForceUnique) {
-  StringRef SectionName = GO->getSection();
-
+static StringRef handlePragmaClangSection(const GlobalObject *GO, SectionKind 
Kind) {
   // Check if '#pragma clang section' name is applicable.
   // Note that pragma directive overrides -ffunction-section, -fdata-section
   // and so section name is exactly as user specified and not uniqued.
   const GlobalVariable *GV = dyn_cast(GO);
   if (GV && GV->hasImplicitSection()) {
 auto Attrs = GV->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();
-}
+if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
+  return Attrs.getAttribute("bss-section").getValueAsString();
+else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
+  return Attrs.getAttribute("rodata-section").getValueAsString();
+else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
+  return Attrs.getAttribute("relro-section").getValueAsString();
+else if (Attrs.hasAttribute("data-section") && Kind.isData())
+  return Attrs.getAttribute("data-section").getValueAsString();
   }
 
+  return GO->getSection();
+}
+
+static MCSection *selectExplicitSectionGlobal(
+const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
+MCContext &Ctx, Mangler &Mang, unsigned &N

[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1677,6 +1677,22 @@ MCSection 
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
   Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
   /*AddSegmentInfo=*/false))
 Kind = SectionKind::getMetadata();
+
+  const GlobalVariable *GV = dyn_cast(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();
+}

vtz wrote:

"would falling through without setting name be ok?" yes, in this case we would 
keep the section's original name, I don't see the need for an explicit else 
here.

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1677,6 +1677,22 @@ MCSection 
*TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
   Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
   /*AddSegmentInfo=*/false))
 Kind = SectionKind::getMetadata();
+
+  const GlobalVariable *GV = dyn_cast(GO);
+  if (GV && GV->hasImplicitSection()) {
+auto Attrs = GV->getAttributes();

vtz wrote:

Done

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits


@@ -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

vtz wrote:

Solved with an aditional RUN command in the existing test

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2024-11-22 Thread Vinicius Tadeu Zein via cfe-commits

vtz wrote:

> 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

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-20 Thread Vinicius Tadeu Zein via cfe-commits

https://github.com/vtz updated https://github.com/llvm/llvm-project/pull/112714

>From 0e36be9b9017c2a258c05a212d353c908c8a5c01 Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein 
Date: Mon, 13 Jan 2025 16:15:33 -0500
Subject: [PATCH] [llvm] Implement pragma clang section on COFF targets

This patch implements the directive pragma clang section on
COFF targets with the exact same features available on ELF
and Mach-O.
---
 clang/docs/LanguageExtensions.rst |   2 +-
 clang/docs/ReleaseNotes.rst   |   2 +
 clang/test/Sema/pragma-clang-section.c|   1 +
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  |  54 +++
 llvm/test/CodeGen/X86/clang-section-coff.ll   | 146 ++
 5 files changed, 173 insertions(+), 32 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/clang-section-coff.ll

diff --git a/clang/docs/LanguageExtensions.rst 
b/clang/docs/LanguageExtensions.rst
index 2eb0777dbdc6c8..24f436ff207ecf 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5548,7 +5548,7 @@ The ``#pragma clang section`` directive obeys the 
following rules:
 
 * The pragma clang section is enabled automatically, without need of any flags.
 
-* This feature is only defined to work sensibly for ELF and Mach-O targets.
+* This feature is only defined to work sensibly for ELF, Mach-O and COFF 
targets.
 
 * If section name is specified through _attribute_((section("myname"))), then
   the attribute name gains precedence.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9eeb872aa57d79..26a7bf78f91f8c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1093,6 +1093,8 @@ Windows Support
   When `-fms-compatibility-version=18.00` or prior is set on the command line 
this Microsoft extension is still
   allowed as VS2013 and prior allow it.
 
+- Clang now supports the ``#pragma clang section`` directive for COFF targets.
+
 LoongArch Support
 ^
 
diff --git a/clang/test/Sema/pragma-clang-section.c 
b/clang/test/Sema/pragma-clang-section.c
index 458c91c2cf31cd..e33e1dfe8cbef7 100644
--- a/clang/test/Sema/pragma-clang-section.c
+++ b/clang/test/Sema/pragma-clang-section.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
 #pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" 
text = "mytext.1" // expected-note 2 {{#pragma entered here}}
 #pragma clang section bss="" data="" rodata="" text=""
 #pragma clang section
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp 
b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index be243c0e74e9db..7db949ffde7883 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -788,29 +788,35 @@ getGlobalObjectInfo(const GlobalObject *GO, const 
TargetMachine &TM) {
   return {Group, IsComdat, Flags};
 }
 
-static MCSection *selectExplicitSectionGlobal(
-const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
-MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
-bool Retain, bool ForceUnique) {
-  StringRef SectionName = GO->getSection();
-
+static StringRef handlePragmaClangSection(const GlobalObject *GO,
+  SectionKind Kind) {
   // Check if '#pragma clang section' name is applicable.
   // Note that pragma directive overrides -ffunction-section, -fdata-section
   // and so section name is exactly as user specified and not uniqued.
   const GlobalVariable *GV = dyn_cast(GO);
   if (GV && GV->hasImplicitSection()) {
 auto Attrs = GV->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();
-}
+if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
+  return Attrs.getAttribute("bss-section").getValueAsString();
+else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
+  return Attrs.getAttribute("rodata-section").getValueAsString();
+else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
+  return Attrs.getAttribute("relro-section").getValueAsString();
+else if (Attrs.hasAttribute("data-section") && Kind.isData())
+  return Attrs.getAttribute("data-section").getValueAsString();
   }
 
+  return GO->getSection();
+}
+
+static MCSection *selectExplicitSection

[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-20 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1,4 +1,5 @@
 ;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s
+;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s

vtz wrote:

You're right. Improved it.

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-20 Thread Vinicius Tadeu Zein via cfe-commits

vtz wrote:

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

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-14 Thread Vinicius Tadeu Zein via cfe-commits


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

vtz wrote:

Removed.

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-14 Thread Vinicius Tadeu Zein via cfe-commits


@@ -1,4 +1,5 @@
 ;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s
+;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s

vtz wrote:

Changed to x86. I hope this is fine.

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-14 Thread Vinicius Tadeu Zein via cfe-commits

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


[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)

2025-01-14 Thread Vinicius Tadeu Zein via cfe-commits

https://github.com/vtz updated https://github.com/llvm/llvm-project/pull/112714

>From e186a67261ba51dd59652103d4644a2115063379 Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein 
Date: Tue, 14 Jan 2025 14:46:03 -0500
Subject: [PATCH] [llvm] Implement pragma clang section on COFF targets

This patch implements the directive pragma clang section on
COFF targets with the exact same features available on ELF
and Mach-O.
---
 clang/docs/LanguageExtensions.rst |   2 +-
 clang/docs/ReleaseNotes.rst   |   2 +
 clang/test/Sema/pragma-clang-section.c|   1 +
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  |  54 
 llvm/test/CodeGen/X86/clang-section-coff.ll   | 118 ++
 5 files changed, 145 insertions(+), 32 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/clang-section-coff.ll

diff --git a/clang/docs/LanguageExtensions.rst 
b/clang/docs/LanguageExtensions.rst
index 2eb0777dbdc6c8..24f436ff207ecf 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5548,7 +5548,7 @@ The ``#pragma clang section`` directive obeys the 
following rules:
 
 * The pragma clang section is enabled automatically, without need of any flags.
 
-* This feature is only defined to work sensibly for ELF and Mach-O targets.
+* This feature is only defined to work sensibly for ELF, Mach-O and COFF 
targets.
 
 * If section name is specified through _attribute_((section("myname"))), then
   the attribute name gains precedence.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9eeb872aa57d79..26a7bf78f91f8c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1093,6 +1093,8 @@ Windows Support
   When `-fms-compatibility-version=18.00` or prior is set on the command line 
this Microsoft extension is still
   allowed as VS2013 and prior allow it.
 
+- Clang now supports the ``#pragma clang section`` directive for COFF targets.
+
 LoongArch Support
 ^
 
diff --git a/clang/test/Sema/pragma-clang-section.c 
b/clang/test/Sema/pragma-clang-section.c
index 458c91c2cf31cd..e33e1dfe8cbef7 100644
--- a/clang/test/Sema/pragma-clang-section.c
+++ b/clang/test/Sema/pragma-clang-section.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
 #pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" 
text = "mytext.1" // expected-note 2 {{#pragma entered here}}
 #pragma clang section bss="" data="" rodata="" text=""
 #pragma clang section
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp 
b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index be243c0e74e9db..7db949ffde7883 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -788,29 +788,35 @@ getGlobalObjectInfo(const GlobalObject *GO, const 
TargetMachine &TM) {
   return {Group, IsComdat, Flags};
 }
 
-static MCSection *selectExplicitSectionGlobal(
-const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
-MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
-bool Retain, bool ForceUnique) {
-  StringRef SectionName = GO->getSection();
-
+static StringRef handlePragmaClangSection(const GlobalObject *GO,
+  SectionKind Kind) {
   // Check if '#pragma clang section' name is applicable.
   // Note that pragma directive overrides -ffunction-section, -fdata-section
   // and so section name is exactly as user specified and not uniqued.
   const GlobalVariable *GV = dyn_cast(GO);
   if (GV && GV->hasImplicitSection()) {
 auto Attrs = GV->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();
-}
+if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
+  return Attrs.getAttribute("bss-section").getValueAsString();
+else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
+  return Attrs.getAttribute("rodata-section").getValueAsString();
+else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
+  return Attrs.getAttribute("relro-section").getValueAsString();
+else if (Attrs.hasAttribute("data-section") && Kind.isData())
+  return Attrs.getAttribute("data-section").getValueAsString();
   }
 
+  return GO->getSection();
+}
+
+static MCSection *sele