Skip to content

Commit 5d6d8dc

Browse files
authored
[clang][llvm] Remove "implicit-section-name" attribute (#87906)
D33412/D33413 introduced this to support a clang pragma to set section names for a symbol depending on if it would be placed in bss/data/rodata/text, which may not be known until the backend. However, for text we know that only functions will go there, so just directly set the section in clang instead of going through a completely separate attribute. Autoupgrade the "implicit-section-name" attribute to directly setting the section on a Fuction.
1 parent 41e8a9b commit 5d6d8dc

12 files changed

+31
-104
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
26352635
addUsedGlobal(F);
26362636
if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
26372637
if (!D->getAttr<SectionAttr>())
2638-
F->addFnAttr("implicit-section-name", SA->getName());
2638+
F->setSection(SA->getName());
26392639

26402640
llvm::AttrBuilder Attrs(F->getContext());
26412641
if (GetCPUAndFeaturesAttributes(GD, Attrs)) {

clang/test/CodeGen/clang-sections-attribute.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,5 @@ static int int_zvar;
6969
// CHECK: define internal void @int_fun() #0 section ".int_fun_attr"
7070
// CHECK: define internal void @int_fun2() #0 section ".int_fun2_attr"
7171
//
72-
// Function attributes should not include implicit-section-name.
73-
// CHECK-NOT: attributes #0 = {{.*}}implicit-section-name
74-
//
7572
// No other attribute group should be present in the file.
7673
// CHECK-NOT: attributes #1

clang/test/CodeGenCXX/clang-sections.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,22 @@ int hoo(void) {
8181
//CHECK: @p ={{.*}} constant i32 7, align 4
8282
//CHECK: @_ZL5fptrs = internal constant [2 x ptr] [ptr @foo, ptr @goo], align {{4|8}} #3
8383

84-
//CHECK: define{{.*}} i32 @foo() #5 {
85-
//CHECK: define{{.*}} i32 @goo() #6 {
86-
//CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #7
87-
//CHECK: define{{.*}} i32 @hoo() #8 {
84+
//ELF: define{{.*}} i32 @foo(){{.*}} section "my_text.1" {
85+
//ELF: define{{.*}} i32 @goo(){{.*}} section "my_text.2" {
86+
//MACHO: define{{.*}} i32 @foo(){{.*}} section "__TEXT,__mytext1" {
87+
//MACHO: define{{.*}} i32 @goo(){{.*}} section "__TEXT,__mytext2" {
88+
89+
// ensure zoo/hoo don't have a section
90+
//CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #6{{$}}
91+
//CHECK: define{{.*}} i32 @hoo() #5 {
8892

8993
//ELF: attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
9094
//ELF: attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
9195
//ELF: attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
9296
//ELF: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "relro-section"="my_relro.2" "rodata-section"="my_rodata.2" }
9397
//ELF: attributes #4 = { "relro-section"="my_relro.2" }
94-
//ELF: attributes #5 = { {{.*"implicit-section-name"="my_text.1".*}} }
95-
//ELF: attributes #6 = { {{.*"implicit-section-name"="my_text.2".*}} }
9698
//MACHO: attributes #0 = { "bss-section"="__BSS,__mybss1" "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
9799
//MACHO: attributes #1 = { "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
98100
//MACHO: attributes #2 = { "bss-section"="__BSS,__mybss2" "rodata-section"="__RODATA,__myrodata1" }
99101
//MACHO: attributes #3 = { "bss-section"="__BSS,__mybss2" "data-section"="__DATA,__mydata2" "relro-section"="__RELRO,__myrelro2" "rodata-section"="__RODATA,__myrodata2" }
100102
//MACHO: attributes #4 = { "relro-section"="__RELRO,__myrelro2" }
101-
//MACHO: attributes #5 = { {{.*"implicit-section-name"="__TEXT,__mytext1".*}} }
102-
//MACHO: attributes #6 = { {{.*"implicit-section-name"="__TEXT,__mytext2".*}} }
103-
//CHECK-NOT: attributes #7 = { {{.*"implicit-section-name".*}} }
104-
//CHECK-NOT: attributes #8 = { {{.*"implicit-section-name".*}} }

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,8 +1473,7 @@ bool TargetInstrInfo::isFunctionSafeToSplit(const MachineFunction &MF) const {
14731473
// since the split part may not be placed in a contiguous region. It may also
14741474
// be more beneficial to augment the linker to ensure contiguous layout of
14751475
// split functions within the same section as specified by the attribute.
1476-
if (MF.getFunction().hasSection() ||
1477-
MF.getFunction().hasFnAttribute("implicit-section-name"))
1476+
if (MF.getFunction().hasSection())
14781477
return false;
14791478

14801479
// We don't want to proceed further for cold functions

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,6 @@ static MCSection *selectExplicitSectionGlobal(
797797
SectionName = Attrs.getAttribute("data-section").getValueAsString();
798798
}
799799
}
800-
const Function *F = dyn_cast<Function>(GO);
801-
if (F && F->hasFnAttribute("implicit-section-name")) {
802-
SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
803-
}
804800

805801
// Infer section flags from the section name if we can.
806802
Kind = getELFKindForNamedSection(SectionName, Kind);
@@ -934,7 +930,7 @@ MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
934930
unsigned Flags = getELFSectionFlags(Kind);
935931
// If the function's section names is pre-determined via pragma or a
936932
// section attribute, call selectExplicitSectionGlobal.
937-
if (F.hasSection() || F.hasFnAttribute("implicit-section-name"))
933+
if (F.hasSection())
938934
return selectExplicitSectionGlobal(
939935
&F, Kind, TM, getContext(), getMangler(), NextUniqueID,
940936
Used.count(&F), /* ForceUnique = */true);
@@ -1298,11 +1294,6 @@ MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
12981294
}
12991295
}
13001296

1301-
const Function *F = dyn_cast<Function>(GO);
1302-
if (F && F->hasFnAttribute("implicit-section-name")) {
1303-
SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
1304-
}
1305-
13061297
// Parse the section specifier and create it if valid.
13071298
StringRef Segment, Section;
13081299
unsigned TAA = 0, StubSize = 0;

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5267,6 +5267,14 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
52675267
F.removeRetAttrs(AttributeFuncs::typeIncompatible(F.getReturnType()));
52685268
for (auto &Arg : F.args())
52695269
Arg.removeAttrs(AttributeFuncs::typeIncompatible(Arg.getType()));
5270+
5271+
// Older versions of LLVM treated an "implicit-section-name" attribute
5272+
// similarly to directly setting the section on a Function.
5273+
if (Attribute A = F.getFnAttribute("implicit-section-name");
5274+
A.isValid() && A.isStringAttribute()) {
5275+
F.setSection(A.getValueAsString());
5276+
F.removeFnAttr("implicit-section-name");
5277+
}
52705278
}
52715279

52725280
static bool isOldLoopArgument(Metadata *MD) {

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,6 @@ MCSection *TargetLoweringObjectFile::SectionForGlobal(
334334
}
335335
}
336336

337-
if (auto *F = dyn_cast<Function>(GO)) {
338-
if (F->hasFnAttribute("implicit-section-name"))
339-
return getExplicitSectionGlobal(GO, Kind, TM);
340-
}
341-
342337
// Use default section depending on the 'type' of global
343338
return SelectSectionForGlobal(GO, Kind, TM);
344339
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: llvm-as < %s | llvm-dis - | FileCheck %s
2+
3+
; CHECK: define void @f() section "foo_section"
4+
; CHECK-NOT: "implicit-section-name"
5+
6+
define void @f() "implicit-section-name"="foo_section" {
7+
ret void
8+
}

llvm/test/CodeGen/AArch64/clang-section-macho.ll

Lines changed: 0 additions & 22 deletions
This file was deleted.

llvm/test/CodeGen/ARM/clang-section.ll

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ target triple = "armv7-arm-none-eabi"
2222
@o = global i32 6, align 4
2323
@p = constant i32 7, align 4
2424

25-
; Function Attrs: noinline nounwind
26-
define i32 @foo() #4 {
27-
entry:
28-
%0 = load i32, ptr @b, align 4
29-
ret i32 %0
30-
}
31-
32-
; Function Attrs: noinline
33-
define i32 @goo() #5 {
34-
entry:
35-
%call = call i32 @zoo(ptr @_ZL1g, ptr @_ZZ3gooE7lstat_h)
36-
ret i32 %call
37-
}
38-
3925
declare i32 @zoo(ptr, ptr) #6
4026

4127
; Function Attrs: noinline nounwind
@@ -49,8 +35,6 @@ attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-se
4935
attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
5036
attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
5137
attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "rodata-section"="my_rodata.2" }
52-
attributes #4 = { noinline nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" "implicit-section-name"="my_text.1" "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" "target-cpu"="cortex-a9" "target-features"="+dsp,+fp16,+neon,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false" }
53-
attributes #5 = { noinline "correctly-rounded-divide-sqrt-fp-math"="false" "denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" "implicit-section-name"="my_text.2" "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" "target-cpu"="cortex-a9" "target-features"="+dsp,+fp16,+neon,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false" }
5438
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" "target-cpu"="cortex-a9" "target-features"="+dsp,+fp16,+neon,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false" }
5539
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" "target-cpu"="cortex-a9" "target-features"="+dsp,+fp16,+neon,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false" }
5640

@@ -61,14 +45,6 @@ attributes #7 = { noinline nounwind "correctly-rounded-divide-sqrt-fp-math"="fal
6145
!2 = !{i32 1, !"enumsize_buildattr", i32 2}
6246
!3 = !{i32 1, !"armlib_unavailable", i32 0}
6347

64-
;CHECK: .section my_text.1,"ax",%progbits
65-
;CHECK: .type foo,%function
66-
;CHECK: foo:
67-
68-
;CHECK: .section my_text.2,"ax",%progbits
69-
;CHECK: .type goo,%function
70-
;CHECK: goo:
71-
7248
;CHECK: .text
7349
;CHECK: .type hoo,%function
7450
;CHECK: hoo:

llvm/test/CodeGen/Generic/machine-function-splitter.ll

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,6 @@ exit:
260260
ret i32 %5
261261
}
262262

263-
define void @foo9(i1 zeroext %0) nounwind #0 !prof !14 {
264-
;; Check that function with section attribute is not split.
265-
; MFS-DEFAULTS-LABEL: foo9
266-
; MFS-DEFAULTS-NOT: foo9.cold:
267-
br i1 %0, label %2, label %4, !prof !17
268-
269-
2: ; preds = %1
270-
%3 = call i32 @bar()
271-
br label %6
272-
273-
4: ; preds = %1
274-
%5 = call i32 @baz()
275-
br label %6
276-
277-
6: ; preds = %4, %2
278-
%7 = tail call i32 @qux()
279-
ret void
280-
}
281-
282263
define i32 @foo10(i1 zeroext %0) personality ptr @__gxx_personality_v0 !prof !14 {
283264
;; Check that nop is inserted just before the EH pad if it's beginning a section.
284265
; MFS-DEFAULTS-LABEL: foo10
@@ -564,10 +545,10 @@ define i32 @foo19(i32 %in) !prof !14 !section_prefix !15 {
564545
; MFS-DEFAULTS-LABEL: foo19
565546
; MFS-DEFAULTS: .section .text.split.foo19
566547
; MFS-DEFAULTS-NEXT: foo19.cold:
567-
; MFS-DEFAULTS-X86: .LJTI18_0
568-
; MFS-DEFAULTS-AARCH64-NOT: .LJTI18_0
548+
; MFS-DEFAULTS-X86: .LJTI17_0
549+
; MFS-DEFAULTS-AARCH64-NOT: .LJTI17_0
569550
; MFS-DEFAULTS: .section .rodata
570-
; MFS-DEFAULTS: .LJTI18_0
551+
; MFS-DEFAULTS: .LJTI17_0
571552
%cmp = icmp sgt i32 %in, 3
572553
br i1 %cmp, label %hot, label %cold_switch, !prof !17
573554

@@ -638,8 +619,6 @@ declare i32 @__gxx_personality_v0(...)
638619

639620
@_ZTIi = external constant ptr
640621

641-
attributes #0 = { "implicit-section-name"="nosplit" }
642-
643622
!llvm.module.flags = !{!0}
644623
!0 = !{i32 1, !"ProfileSummary", !1}
645624
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}

llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
;; return 0;
3939
;; }
4040

41-
define dso_local i32 @_Z3fooi(i32 %n) local_unnamed_addr #0 {
41+
define dso_local i32 @_Z3fooi(i32 %n) local_unnamed_addr section "foo_section" {
4242
entry:
4343
%cmp = icmp slt i32 %n, 0
4444
br i1 %cmp, label %if.then, label %if.end
@@ -52,5 +52,3 @@ if.end: ; preds = %entry
5252
}
5353

5454
declare dso_local void @exit(i32) local_unnamed_addr
55-
56-
attributes #0 = {"implicit-section-name"="foo_section" }

0 commit comments

Comments
 (0)