[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.
This commit is contained in:
Arthur Eubanks 2024-04-11 13:29:29 -06:00 committed by GitHub
parent 41e8a9b1b2
commit 5d6d8dcd29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 31 additions and 104 deletions

View File

@ -2635,7 +2635,7 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
addUsedGlobal(F);
if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
if (!D->getAttr<SectionAttr>())
F->addFnAttr("implicit-section-name", SA->getName());
F->setSection(SA->getName());
llvm::AttrBuilder Attrs(F->getContext());
if (GetCPUAndFeaturesAttributes(GD, Attrs)) {

View File

@ -69,8 +69,5 @@ static int int_zvar;
// CHECK: define internal void @int_fun() #0 section ".int_fun_attr"
// CHECK: define internal void @int_fun2() #0 section ".int_fun2_attr"
//
// Function attributes should not include implicit-section-name.
// CHECK-NOT: attributes #0 = {{.*}}implicit-section-name
//
// No other attribute group should be present in the file.
// CHECK-NOT: attributes #1

View File

@ -81,24 +81,22 @@ int hoo(void) {
//CHECK: @p ={{.*}} constant i32 7, align 4
//CHECK: @_ZL5fptrs = internal constant [2 x ptr] [ptr @foo, ptr @goo], align {{4|8}} #3
//CHECK: define{{.*}} i32 @foo() #5 {
//CHECK: define{{.*}} i32 @goo() #6 {
//CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #7
//CHECK: define{{.*}} i32 @hoo() #8 {
//ELF: define{{.*}} i32 @foo(){{.*}} section "my_text.1" {
//ELF: define{{.*}} i32 @goo(){{.*}} section "my_text.2" {
//MACHO: define{{.*}} i32 @foo(){{.*}} section "__TEXT,__mytext1" {
//MACHO: define{{.*}} i32 @goo(){{.*}} section "__TEXT,__mytext2" {
// ensure zoo/hoo don't have a section
//CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #6{{$}}
//CHECK: define{{.*}} i32 @hoo() #5 {
//ELF: attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
//ELF: attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
//ELF: attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
//ELF: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "relro-section"="my_relro.2" "rodata-section"="my_rodata.2" }
//ELF: attributes #4 = { "relro-section"="my_relro.2" }
//ELF: attributes #5 = { {{.*"implicit-section-name"="my_text.1".*}} }
//ELF: attributes #6 = { {{.*"implicit-section-name"="my_text.2".*}} }
//MACHO: attributes #0 = { "bss-section"="__BSS,__mybss1" "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
//MACHO: attributes #1 = { "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
//MACHO: attributes #2 = { "bss-section"="__BSS,__mybss2" "rodata-section"="__RODATA,__myrodata1" }
//MACHO: attributes #3 = { "bss-section"="__BSS,__mybss2" "data-section"="__DATA,__mydata2" "relro-section"="__RELRO,__myrelro2" "rodata-section"="__RODATA,__myrodata2" }
//MACHO: attributes #4 = { "relro-section"="__RELRO,__myrelro2" }
//MACHO: attributes #5 = { {{.*"implicit-section-name"="__TEXT,__mytext1".*}} }
//MACHO: attributes #6 = { {{.*"implicit-section-name"="__TEXT,__mytext2".*}} }
//CHECK-NOT: attributes #7 = { {{.*"implicit-section-name".*}} }
//CHECK-NOT: attributes #8 = { {{.*"implicit-section-name".*}} }

View File

@ -1473,8 +1473,7 @@ bool TargetInstrInfo::isFunctionSafeToSplit(const MachineFunction &MF) const {
// since the split part may not be placed in a contiguous region. It may also
// be more beneficial to augment the linker to ensure contiguous layout of
// split functions within the same section as specified by the attribute.
if (MF.getFunction().hasSection() ||
MF.getFunction().hasFnAttribute("implicit-section-name"))
if (MF.getFunction().hasSection())
return false;
// We don't want to proceed further for cold functions

View File

@ -797,10 +797,6 @@ static MCSection *selectExplicitSectionGlobal(
SectionName = Attrs.getAttribute("data-section").getValueAsString();
}
}
const Function *F = dyn_cast<Function>(GO);
if (F && F->hasFnAttribute("implicit-section-name")) {
SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
}
// Infer section flags from the section name if we can.
Kind = getELFKindForNamedSection(SectionName, Kind);
@ -934,7 +930,7 @@ MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
unsigned Flags = getELFSectionFlags(Kind);
// If the function's section names is pre-determined via pragma or a
// section attribute, call selectExplicitSectionGlobal.
if (F.hasSection() || F.hasFnAttribute("implicit-section-name"))
if (F.hasSection())
return selectExplicitSectionGlobal(
&F, Kind, TM, getContext(), getMangler(), NextUniqueID,
Used.count(&F), /* ForceUnique = */true);
@ -1298,11 +1294,6 @@ MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
}
}
const Function *F = dyn_cast<Function>(GO);
if (F && F->hasFnAttribute("implicit-section-name")) {
SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
}
// Parse the section specifier and create it if valid.
StringRef Segment, Section;
unsigned TAA = 0, StubSize = 0;

View File

@ -5267,6 +5267,14 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
F.removeRetAttrs(AttributeFuncs::typeIncompatible(F.getReturnType()));
for (auto &Arg : F.args())
Arg.removeAttrs(AttributeFuncs::typeIncompatible(Arg.getType()));
// Older versions of LLVM treated an "implicit-section-name" attribute
// similarly to directly setting the section on a Function.
if (Attribute A = F.getFnAttribute("implicit-section-name");
A.isValid() && A.isStringAttribute()) {
F.setSection(A.getValueAsString());
F.removeFnAttr("implicit-section-name");
}
}
static bool isOldLoopArgument(Metadata *MD) {

View File

@ -334,11 +334,6 @@ MCSection *TargetLoweringObjectFile::SectionForGlobal(
}
}
if (auto *F = dyn_cast<Function>(GO)) {
if (F->hasFnAttribute("implicit-section-name"))
return getExplicitSectionGlobal(GO, Kind, TM);
}
// Use default section depending on the 'type' of global
return SelectSectionForGlobal(GO, Kind, TM);
}

View File

@ -0,0 +1,8 @@
; RUN: llvm-as < %s | llvm-dis - | FileCheck %s
; CHECK: define void @f() section "foo_section"
; CHECK-NOT: "implicit-section-name"
define void @f() "implicit-section-name"="foo_section" {
ret void
}

View File

@ -1,22 +0,0 @@
;RUN: llc -mtriple=arm64-apple-ios %s -o - | FileCheck %s
define dso_local void @foo() #0 {
entry:
ret void
}
attributes #0 = { "implicit-section-name"="__TEXT,__mytext" }
; CHECK: .section __TEXT,__mytext
; CHECK-NEXT: .globl _foo
define dso_local void @bar() #1 {
entry:
ret void
}
attributes #1 = { "implicit-section-name"="__EDATA,zerofill" }
; CHECK: .section __EDATA,zerofill
; CHECK-NEXT: .globl _bar

View File

@ -22,20 +22,6 @@ target triple = "armv7-arm-none-eabi"
@o = global i32 6, align 4
@p = constant i32 7, align 4
; Function Attrs: noinline nounwind
define i32 @foo() #4 {
entry:
%0 = load i32, ptr @b, align 4
ret i32 %0
}
; Function Attrs: noinline
define i32 @goo() #5 {
entry:
%call = call i32 @zoo(ptr @_ZL1g, ptr @_ZZ3gooE7lstat_h)
ret i32 %call
}
declare i32 @zoo(ptr, ptr) #6
; Function Attrs: noinline nounwind
@ -49,8 +35,6 @@ attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-se
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 #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" }
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" }
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" }
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" }
@ -61,14 +45,6 @@ attributes #7 = { noinline nounwind "correctly-rounded-divide-sqrt-fp-math"="fal
!2 = !{i32 1, !"enumsize_buildattr", i32 2}
!3 = !{i32 1, !"armlib_unavailable", i32 0}
;CHECK: .section my_text.1,"ax",%progbits
;CHECK: .type foo,%function
;CHECK: foo:
;CHECK: .section my_text.2,"ax",%progbits
;CHECK: .type goo,%function
;CHECK: goo:
;CHECK: .text
;CHECK: .type hoo,%function
;CHECK: hoo:

View File

@ -260,25 +260,6 @@ exit:
ret i32 %5
}
define void @foo9(i1 zeroext %0) nounwind #0 !prof !14 {
;; Check that function with section attribute is not split.
; MFS-DEFAULTS-LABEL: foo9
; MFS-DEFAULTS-NOT: foo9.cold:
br i1 %0, label %2, label %4, !prof !17
2: ; preds = %1
%3 = call i32 @bar()
br label %6
4: ; preds = %1
%5 = call i32 @baz()
br label %6
6: ; preds = %4, %2
%7 = tail call i32 @qux()
ret void
}
define i32 @foo10(i1 zeroext %0) personality ptr @__gxx_personality_v0 !prof !14 {
;; Check that nop is inserted just before the EH pad if it's beginning a section.
; MFS-DEFAULTS-LABEL: foo10
@ -564,10 +545,10 @@ define i32 @foo19(i32 %in) !prof !14 !section_prefix !15 {
; MFS-DEFAULTS-LABEL: foo19
; MFS-DEFAULTS: .section .text.split.foo19
; MFS-DEFAULTS-NEXT: foo19.cold:
; MFS-DEFAULTS-X86: .LJTI18_0
; MFS-DEFAULTS-AARCH64-NOT: .LJTI18_0
; MFS-DEFAULTS-X86: .LJTI17_0
; MFS-DEFAULTS-AARCH64-NOT: .LJTI17_0
; MFS-DEFAULTS: .section .rodata
; MFS-DEFAULTS: .LJTI18_0
; MFS-DEFAULTS: .LJTI17_0
%cmp = icmp sgt i32 %in, 3
br i1 %cmp, label %hot, label %cold_switch, !prof !17
@ -638,8 +619,6 @@ declare i32 @__gxx_personality_v0(...)
@_ZTIi = external constant ptr
attributes #0 = { "implicit-section-name"="nosplit" }
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"ProfileSummary", !1}
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}

View File

@ -38,7 +38,7 @@
;; return 0;
;; }
define dso_local i32 @_Z3fooi(i32 %n) local_unnamed_addr #0 {
define dso_local i32 @_Z3fooi(i32 %n) local_unnamed_addr section "foo_section" {
entry:
%cmp = icmp slt i32 %n, 0
br i1 %cmp, label %if.then, label %if.end
@ -52,5 +52,3 @@ if.end: ; preds = %entry
}
declare dso_local void @exit(i32) local_unnamed_addr
attributes #0 = {"implicit-section-name"="foo_section" }