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

>From 4cba74354f3fb59a47e7da142a43701e889d9b4b Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein <vinicius.z...@kpit.com>
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   | 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<GlobalVariable>(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 &NextUniqueID,
+                                              bool Retain, bool ForceUnique) {
+  StringRef SectionName = handlePragmaClangSection(GO, Kind);
+
   // Infer section flags from the section name if we can.
   Kind = getELFKindForNamedSection(SectionName, Kind);
 
@@ -1291,21 +1297,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
 
-  StringRef SectionName = GO->getSection();
-
-  const GlobalVariable *GV = dyn_cast<GlobalVariable>(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();
-    }
-  }
+  StringRef SectionName = handlePragmaClangSection(GO, Kind);
 
   // Parse the section specifier and create it if valid.
   StringRef Segment, Section;
@@ -1674,7 +1666,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
 
 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
-  StringRef Name = GO->getSection();
+  StringRef Name = handlePragmaClangSection(GO, Kind);
   if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
                                       /*AddSegmentInfo=*/false) ||
       Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
diff --git a/llvm/test/CodeGen/X86/clang-section-coff.ll 
b/llvm/test/CodeGen/X86/clang-section-coff.ll
new file mode 100644
index 00000000000000..82ac0910b96e50
--- /dev/null
+++ b/llvm/test/CodeGen/X86/clang-section-coff.ll
@@ -0,0 +1,118 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 5
+;RUN: llc -mtriple=x86_64-windows-msvc %s -o - | FileCheck %s
+;Test that global variables and functions are assigned to correct sections.
+
+@a = global i32 0, align 4 #0
+@b = global i32 1, align 4 #0
+@c = global [4 x i32] zeroinitializer, align 4 #0
+@d = global [5 x i16] zeroinitializer, align 2 #0
+@e = global [6 x i16] [i16 0, i16 0, i16 1, i16 0, i16 0, i16 0], align 2 #0
+@f = constant i32 2, align 4 #0
+@h = global i32 0, align 4 #1
+@i = global i32 0, align 4 #2
+@j = constant i32 4, align 4 #2
+@k = global i32 0, align 4 #2
+@_ZZ3gooE7lstat_h = internal global i32 0, align 4 #2
+@_ZL1g = internal global [2 x i32] zeroinitializer, align 4 #0
+@l = global i32 5, align 4 #3
+@m = constant i32 6, align 4 #3
+@n = global i32 0, align 4
+@o = global i32 6, align 4
+@p = constant i32 7, align 4
+
+declare i32 @zoo(ptr, ptr) #6
+
+; Function Attrs: noinline nounwind
+define i32 @hoo() #7 {
+; CHECK-LABEL: hoo:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    movl b(%rip), %eax
+; CHECK-NEXT:    retq
+entry:
+  %0 = load i32, ptr @b, align 4
+  ret i32 %0
+}
+
+attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" 
"rodata-section"="my_rodata.1" }
+attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
+attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
+attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" 
"rodata-section"="my_rodata.2" }
+attributes #6 = { "correctly-rounded-divide-sqrt-fp-math"="false" 
"denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" 
"less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="true" 
"no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" 
"no-trapping-math"="true" "stack-protector-buffer-size"="8" 
"unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #7 = { noinline nounwind 
"correctly-rounded-divide-sqrt-fp-math"="false" 
"denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" 
"less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="true" 
"no-jump-tables"="false" "no-nans-fp-math"="true" 
"no-signed-zeros-fp-math"="true" "no-trapping-math"="true" 
"stack-protector-buffer-size"="8" "unsafe-fp-math"="false" 
"use-soft-float"="false" }
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 1, !"static_rwdata", i32 1}
+!2 = !{i32 1, !"enumsize_buildattr", i32 2}
+!3 = !{i32 1, !"armlib_unavailable", i32 0}
+
+;CHECK:        .text
+;CHECK:        .type   hoo,%function
+;CHECK: hoo:
+
+;CHECK:        .type   a,%object
+;CHECK:        .section        my_bss.1,"aw",%nobits
+;CHECK: a:
+
+;CHECK:        .type   b,%object
+;CHECK:        .section        my_data.1,"aw",%progbits
+;CHECK: b:
+
+;CHECK:        .type   c,%object
+;CHECK:        .section        my_bss.1,"aw",%nobits
+;CHECK: c:
+
+;CHECK:        .type   d,%object
+;CHECK: d:
+
+;CHECK:        .type   e,%object
+;CHECK:        .section        my_data.1,"aw",%progbits
+;CHECK: e:
+
+;CHECK:        .type   f,%object
+;CHECK:        .section        my_rodata.1,"a",%progbits
+;CHECK: f:
+
+;CHECK:        .type   h,%object
+;CHECK:        .bss
+;CHECK: h:
+
+;CHECK:        .type   i,%object
+;CHECK:        .section        my_bss.2,"aw",%nobits
+;CHECK: i:
+
+;CHECK:        .type   j,%object
+;CHECK:        .section        my_rodata.1,"a",%progbits
+;CHECK: j:
+
+;CHECK:        .type   k,%object
+;CHECK:        .section        my_bss.2,"aw",%nobits
+;CHECK: k:
+
+;CHECK:        .type   _ZZ3gooE7lstat_h,%object @ @_ZZ3gooE7lstat_h
+;CHECK: _ZZ3gooE7lstat_h:
+
+;CHECK:        .type   _ZL1g,%object
+;CHECK:        .section        my_bss.1,"aw",%nobits
+;CHECK: _ZL1g:
+
+;CHECK:        .type   l,%object
+;CHECK:        .section        my_data.2,"aw",%progbits
+;CHECK: l:
+
+;CHECK:        .type   m,%object
+;CHECK:        .section        my_rodata.2,"a",%progbits
+;CHECK: m:
+
+;CHECK:        .type   n,%object
+;CHECK:        .bss
+;CHECK: n:
+
+;CHECK:        .type   o,%object
+;CHECK:        .data
+;CHECK: o:
+
+;CHECK:        .type   p,%object
+;CHECK:        .section        .rodata,"a",%progbits
+;CHECK: p:

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

Reply via email to