Skip to content

Commit dbd118c

Browse files
mingmingl-llvmgithub-actions[bot]
authored andcommitted
Automerge: [IR] Generalize Function's {set,get}SectionPrefix to GlobalObjects, the base class of {Function, GlobalVariable, IFunc} (#125757)
This is a split of llvm/llvm-project#125756
2 parents 820aa8d + 5399782 commit dbd118c

File tree

12 files changed

+71
-36
lines changed

12 files changed

+71
-36
lines changed

llvm/include/llvm/IR/Function.h

-6
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,6 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
346346
/// sample PGO, to enable the same inlines as the profiled optimized binary.
347347
DenseSet<GlobalValue::GUID> getImportGUIDs() const;
348348

349-
/// Set the section prefix for this function.
350-
void setSectionPrefix(StringRef Prefix);
351-
352-
/// Get the section prefix for this function.
353-
std::optional<StringRef> getSectionPrefix() const;
354-
355349
/// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
356350
/// to use during code generation.
357351
bool hasGC() const {

llvm/include/llvm/IR/GlobalObject.h

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ class GlobalObject : public GlobalValue {
124124
/// appropriate default object file section.
125125
void setSection(StringRef S);
126126

127+
/// Set the section prefix for this global object.
128+
void setSectionPrefix(StringRef Prefix);
129+
130+
/// Get the section prefix for this global object.
131+
std::optional<StringRef> getSectionPrefix() const;
132+
127133
bool hasComdat() const { return getComdat() != nullptr; }
128134
const Comdat *getComdat() const { return ObjComdat; }
129135
Comdat *getComdat() { return ObjComdat; }

llvm/include/llvm/IR/MDBuilder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class MDBuilder {
8989
MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
9090
const DenseSet<GlobalValue::GUID> *Imports);
9191

92-
/// Return metadata containing the section prefix for a function.
93-
MDNode *createFunctionSectionPrefix(StringRef Prefix);
92+
/// Return metadata containing the section prefix for a global object.
93+
MDNode *createGlobalObjectSectionPrefix(StringRef Prefix);
9494

9595
/// Return metadata containing the pseudo probe descriptor for a function.
9696
MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName);

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,11 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
687687
raw_svector_ostream(Name) << '.' << *Prefix;
688688
HasPrefix = true;
689689
}
690+
} else if (const auto *GV = dyn_cast<GlobalVariable>(GO)) {
691+
if (std::optional<StringRef> Prefix = GV->getSectionPrefix()) {
692+
raw_svector_ostream(Name) << '.' << *Prefix;
693+
HasPrefix = true;
694+
}
690695
}
691696

692697
if (UniqueSectionName) {

llvm/lib/IR/Function.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -1164,22 +1164,6 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
11641164
return R;
11651165
}
11661166

1167-
void Function::setSectionPrefix(StringRef Prefix) {
1168-
MDBuilder MDB(getContext());
1169-
setMetadata(LLVMContext::MD_section_prefix,
1170-
MDB.createFunctionSectionPrefix(Prefix));
1171-
}
1172-
1173-
std::optional<StringRef> Function::getSectionPrefix() const {
1174-
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
1175-
assert(cast<MDString>(MD->getOperand(0))->getString() ==
1176-
"function_section_prefix" &&
1177-
"Metadata not match");
1178-
return cast<MDString>(MD->getOperand(1))->getString();
1179-
}
1180-
return std::nullopt;
1181-
}
1182-
11831167
bool Function::nullPointerIsDefined() const {
11841168
return hasFnAttribute(Attribute::NullPointerIsValid);
11851169
}

llvm/lib/IR/Globals.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/IR/GlobalAlias.h"
1919
#include "llvm/IR/GlobalValue.h"
2020
#include "llvm/IR/GlobalVariable.h"
21+
#include "llvm/IR/MDBuilder.h"
2122
#include "llvm/IR/Module.h"
2223
#include "llvm/Support/Error.h"
2324
#include "llvm/Support/ErrorHandling.h"
@@ -286,6 +287,24 @@ void GlobalObject::setSection(StringRef S) {
286287
setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
287288
}
288289

290+
void GlobalObject::setSectionPrefix(StringRef Prefix) {
291+
MDBuilder MDB(getContext());
292+
setMetadata(LLVMContext::MD_section_prefix,
293+
MDB.createGlobalObjectSectionPrefix(Prefix));
294+
}
295+
296+
std::optional<StringRef> GlobalObject::getSectionPrefix() const {
297+
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
298+
[[maybe_unused]] StringRef MDName =
299+
cast<MDString>(MD->getOperand(0))->getString();
300+
assert((MDName == "section_prefix" ||
301+
(isa<Function>(this) && MDName == "function_section_prefix")) &&
302+
"Metadata not match");
303+
return cast<MDString>(MD->getOperand(1))->getString();
304+
}
305+
return std::nullopt;
306+
}
307+
289308
bool GlobalValue::isNobuiltinFnDef() const {
290309
const Function *F = dyn_cast<Function>(this);
291310
if (!F || F->empty())

llvm/lib/IR/MDBuilder.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ MDNode *MDBuilder::createFunctionEntryCount(
8787
return MDNode::get(Context, Ops);
8888
}
8989

90-
MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
91-
return MDNode::get(
92-
Context, {createString("function_section_prefix"), createString(Prefix)});
90+
MDNode *MDBuilder::createGlobalObjectSectionPrefix(StringRef Prefix) {
91+
return MDNode::get(Context,
92+
{createString("section_prefix"), createString(Prefix)});
9393
}
9494

9595
MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: llc -mtriple x86_64-linux-gnu -data-sections %s -o - | FileCheck %s --check-prefix=ELF
2+
; RUN: llc -mtriple x86_64-linux-gnu -unique-section-names=0 -data-sections %s -o - | FileCheck %s --check-prefix=ELF-NOUNIQ
3+
4+
; RUN: llc -mtriple x86_64-windows-msvc -data-sections %s -o - | FileCheck %s --check-prefix=COFF-MSVC
5+
6+
; ELF: .section .data.hot.foo,
7+
; ELF: .section .data.bar,
8+
; ELF: .section .bss.unlikely.baz,
9+
; ELF: .section .bss.quz,
10+
11+
; ELF-NOUNIQ: .section .data.hot.,"aw",@progbits,unique,1
12+
; ELF-NOUNIQ: .section .data,"aw",@progbits,unique,2
13+
; ELF-NOUNIQ: .section .bss.unlikely.,"aw",@nobits,unique,3
14+
; ELF-NOUNIQ: .section .bss,"aw",@nobits,unique,4
15+
16+
; COFF-MSVC: .section .data,"dw",one_only,foo
17+
; COFF-MSVC: .section .data,"dw",one_only,bar
18+
; COFF-MSVC: .section .bss,"bw",one_only,baz
19+
; COFF-MSVC: .section .bss,"bw",one_only,quz
20+
21+
@foo = global i32 1, !section_prefix !0
22+
@bar = global i32 2
23+
@baz = global i32 0, !section_prefix !1
24+
@quz = global i32 0
25+
26+
!0 = !{!"section_prefix", !"hot"}
27+
!1 = !{!"section_prefix", !"unlikely"}

llvm/test/Transforms/CodeGenPrepare/X86/section-samplepgo.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ define void @cold_func() !prof !16 {
3434
ret void
3535
}
3636

37-
; CHECK: ![[HOT_ID]] = !{!"function_section_prefix", !"hot"}
38-
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
37+
; CHECK: ![[HOT_ID]] = !{!"section_prefix", !"hot"}
38+
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
3939
!llvm.module.flags = !{!1}
4040
!1 = !{i32 1, !"ProfileSummary", !2}
4141
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}

llvm/test/Transforms/CodeGenPrepare/X86/section.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ define void @cold_func3() !prof !16 {
6666
ret void
6767
}
6868

69-
; CHECK: ![[HOT_ID]] = !{!"function_section_prefix", !"hot"}
70-
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
69+
; CHECK: ![[HOT_ID]] = !{!"section_prefix", !"hot"}
70+
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
7171
!llvm.module.flags = !{!1}
7272
!1 = !{i32 1, !"ProfileSummary", !2}
7373
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}

llvm/test/Transforms/HotColdSplit/coldentrycount.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ declare void @sink() cold
2727
; CHECK: define {{.*}} @fun.cold.1{{.*}} ![[PROF:[0-9]+]] {{.*}}section_prefix ![[UNLIKELY:[0-9]+]]
2828

2929
; CHECK: ![[HOTPROF]] = !{!"function_entry_count", i64 100}
30-
; CHECK: ![[LIKELY]] = !{!"function_section_prefix", !"hot"}
30+
; CHECK: ![[LIKELY]] = !{!"section_prefix", !"hot"}
3131
; CHECK: ![[PROF]] = !{!"function_entry_count", i64 0}
32-
; CHECK: ![[UNLIKELY]] = !{!"function_section_prefix", !"unlikely"}
32+
; CHECK: ![[UNLIKELY]] = !{!"section_prefix", !"unlikely"}
3333

3434
!llvm.module.flags = !{!0}
3535
!0 = !{i32 1, !"ProfileSummary", !1}

llvm/test/Transforms/SampleProfile/section-accurate-samplepgo.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ attributes #1 = { "use-sample-profile" }
3636

3737
; CHECK: ![[NOPROFILE_ID]] = !{!"function_entry_count", i64 -1}
3838
; CHECK: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
39-
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
39+
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
4040
; UNKNOWN: ![[NOPROFILE_ID]] = !{!"function_entry_count", i64 -1}
41-
; UNKNOWN: ![[UNKNOWN_ID]] = !{!"function_section_prefix", !"unknown"}
41+
; UNKNOWN: ![[UNKNOWN_ID]] = !{!"section_prefix", !"unknown"}
4242
; ACCURATE: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
43-
; ACCURATE: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
43+
; ACCURATE: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
4444
!llvm.module.flags = !{!1}
4545
!1 = !{i32 1, !"ProfileSummary", !2}
4646
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}

0 commit comments

Comments
 (0)