Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions llvm/include/llvm/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,6 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
/// sample PGO, to enable the same inlines as the profiled optimized binary.
DenseSet<GlobalValue::GUID> getImportGUIDs() const;

/// Set the section prefix for this function.
void setSectionPrefix(StringRef Prefix);

/// Get the section prefix for this function.
std::optional<StringRef> getSectionPrefix() const;

/// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
/// to use during code generation.
bool hasGC() const {
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class GlobalObject : public GlobalValue {
/// appropriate default object file section.
void setSection(StringRef S);

/// Set the section prefix for this global object.
void setSectionPrefix(StringRef Prefix);

/// Get the section prefix for this global object.
std::optional<StringRef> getSectionPrefix() const;

bool hasComdat() const { return getComdat() != nullptr; }
const Comdat *getComdat() const { return ObjComdat; }
Comdat *getComdat() { return ObjComdat; }
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/MDBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class MDBuilder {
MDNode *createFunctionEntryCount(uint64_t Count, bool Synthetic,
const DenseSet<GlobalValue::GUID> *Imports);

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

/// Return metadata containing the pseudo probe descriptor for a function.
MDNode *createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName);
Expand Down
127 changes: 58 additions & 69 deletions llvm/lib/CodeGen/StaticDataSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,17 @@ STATISTIC(NumUnknownJumpTables,
"Number of jump tables with unknown hotness. Option "
"-static-data-default-hotness specifies the hotness.");

static cl::opt<MachineFunctionDataHotness> StaticDataDefaultHotness(
"static-data-default-hotness", cl::Hidden,
cl::desc("This option specifies the hotness of static data when profile "
"information is unavailable"),
cl::init(MachineFunctionDataHotness::Hot),
cl::values(clEnumValN(MachineFunctionDataHotness::Hot, "hot", "Hot"),
clEnumValN(MachineFunctionDataHotness::Cold, "cold", "Cold")));

class StaticDataSplitter : public MachineFunctionPass {
const MachineBranchProbabilityInfo *MBPI = nullptr;
const MachineBlockFrequencyInfo *MBFI = nullptr;
const ProfileSummaryInfo *PSI = nullptr;

// Returns true iff any jump table is hot-cold categorized.
bool splitJumpTables(MachineFunction &MF);
void updateStats(bool ProfileAvailable, const MachineJumpTableInfo *MJTI);
void updateJumpTableStats(bool ProfileAvailable,
const MachineJumpTableInfo &MJTI);

// Same as above but works on functions with profile information.
bool splitJumpTablesWithProfiles(const MachineFunction &MF,
MachineJumpTableInfo &MJTI);
// Use profiles to partition static data.
bool partitionStaticDataWithProfiles(MachineFunction &MF);

public:
static char ID;
Expand All @@ -85,13 +77,22 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();

return splitJumpTables(MF);
const bool ProfileAvailable = PSI && PSI->hasProfileSummary() && MBFI &&
MF.getFunction().hasProfileData();
bool Changed = false;

if (ProfileAvailable)
Changed |= partitionStaticDataWithProfiles(MF);

updateStats(ProfileAvailable, MF.getJumpTableInfo());
return Changed;
}

bool StaticDataSplitter::splitJumpTablesWithProfiles(
const MachineFunction &MF, MachineJumpTableInfo &MJTI) {
bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
int NumChangedJumpTables = 0;

MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();

// Jump table could be used by either terminating instructions or
// non-terminating ones, so we walk all instructions and use
// `MachineOperand::isJTI()` to identify jump table operands.
Expand All @@ -100,70 +101,58 @@ bool StaticDataSplitter::splitJumpTablesWithProfiles(
for (const auto &MBB : MF) {
for (const MachineInstr &I : MBB) {
for (const MachineOperand &Op : I.operands()) {
if (!Op.isJTI())
continue;
const int JTI = Op.getIndex();
// This is not a source block of jump table.
if (JTI == -1)
continue;

auto Hotness = MachineFunctionDataHotness::Hot;

// Hotness is based on source basic block hotness.
// TODO: PSI APIs are about instruction hotness. Introduce API for data
// access hotness.
if (PSI->isColdBlock(&MBB, MBFI))
Hotness = MachineFunctionDataHotness::Cold;

if (MJTI.updateJumpTableEntryHotness(JTI, Hotness))
++NumChangedJumpTables;
if (Op.isJTI()) {
assert(MJTI != nullptr && "Jump table info is not available.");
const int JTI = Op.getIndex();
// This is not a source block of jump table.
if (JTI == -1)
continue;

auto Hotness = MachineFunctionDataHotness::Hot;

// Hotness is based on source basic block hotness.
// TODO: PSI APIs are about instruction hotness. Introduce API for
// data access hotness.
if (PSI->isColdBlock(&MBB, MBFI))
Hotness = MachineFunctionDataHotness::Cold;

if (MJTI->updateJumpTableEntryHotness(JTI, Hotness))
++NumChangedJumpTables;
}
}
}
}
return NumChangedJumpTables > 0;
}

bool StaticDataSplitter::splitJumpTables(MachineFunction &MF) {
MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
if (!MJTI || MJTI->getJumpTables().empty())
return false;
void StaticDataSplitter::updateJumpTableStats(
bool ProfileAvailable, const MachineJumpTableInfo &MJTI) {

const bool ProfileAvailable = PSI && PSI->hasProfileSummary() && MBFI &&
MF.getFunction().hasProfileData();
auto statOnExit = llvm::make_scope_exit([&] {
if (!AreStatisticsEnabled())
return;

if (!ProfileAvailable) {
NumUnknownJumpTables += MJTI->getJumpTables().size();
return;
}
if (!ProfileAvailable) {
NumUnknownJumpTables += MJTI.getJumpTables().size();
return;
}

for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++) {
auto Hotness = MJTI->getJumpTables()[JTI].Hotness;
if (Hotness == MachineFunctionDataHotness::Hot) {
++NumHotJumpTables;
} else {
assert(Hotness == MachineFunctionDataHotness::Cold &&
"A jump table is either hot or cold when profile information is "
"available.");
++NumColdJumpTables;
}
for (size_t JTI = 0; JTI < MJTI.getJumpTables().size(); JTI++) {
auto Hotness = MJTI.getJumpTables()[JTI].Hotness;
if (Hotness == MachineFunctionDataHotness::Hot) {
++NumHotJumpTables;
} else {
assert(Hotness == MachineFunctionDataHotness::Cold &&
"A jump table is either hot or cold when profile information is "
"available.");
++NumColdJumpTables;
}
});

// Place jump tables according to block hotness if function has profile data.
if (ProfileAvailable)
return splitJumpTablesWithProfiles(MF, *MJTI);
}
}

// If function profile is unavailable (e.g., module not instrumented, or new
// code paths lacking samples), -static-data-default-hotness specifies the
// hotness.
for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++)
MF.getJumpTableInfo()->updateJumpTableEntryHotness(
JTI, StaticDataDefaultHotness);
void StaticDataSplitter::updateStats(bool ProfileAvailable,
const MachineJumpTableInfo *MJTI) {
if (!AreStatisticsEnabled())
return;

return true;
if (MJTI)
updateJumpTableStats(ProfileAvailable, *MJTI);
}

char StaticDataSplitter::ID = 0;
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,10 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
}

bool HasPrefix = false;
if (const auto *F = dyn_cast<Function>(GO)) {
if (std::optional<StringRef> Prefix = F->getSectionPrefix()) {
raw_svector_ostream(Name) << '.' << *Prefix;
HasPrefix = true;
}

if (std::optional<StringRef> Prefix = GO->getSectionPrefix()) {
raw_svector_ostream(Name) << '.' << *Prefix;
HasPrefix = true;
}

if (UniqueSectionName) {
Expand Down
16 changes: 0 additions & 16 deletions llvm/lib/IR/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,22 +1164,6 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
return R;
}

void Function::setSectionPrefix(StringRef Prefix) {
MDBuilder MDB(getContext());
setMetadata(LLVMContext::MD_section_prefix,
MDB.createFunctionSectionPrefix(Prefix));
}

std::optional<StringRef> Function::getSectionPrefix() const {
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
assert(cast<MDString>(MD->getOperand(0))->getString() ==
"function_section_prefix" &&
"Metadata not match");
return cast<MDString>(MD->getOperand(1))->getString();
}
return std::nullopt;
}

bool Function::nullPointerIsDefined() const {
return hasFnAttribute(Attribute::NullPointerIsValid);
}
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/IR/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -286,6 +287,24 @@ void GlobalObject::setSection(StringRef S) {
setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
}

void GlobalObject::setSectionPrefix(StringRef Prefix) {
MDBuilder MDB(getContext());
setMetadata(LLVMContext::MD_section_prefix,
MDB.createGlobalObjectSectionPrefix(Prefix));
}

std::optional<StringRef> GlobalObject::getSectionPrefix() const {
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
[[maybe_unused]] StringRef MDName =
cast<MDString>(MD->getOperand(0))->getString();
assert((MDName == "section_prefix" ||
(isa<Function>(this) && MDName == "function_section_prefix")) &&
"Metadata not match");
return cast<MDString>(MD->getOperand(1))->getString();
}
return std::nullopt;
}

bool GlobalValue::isNobuiltinFnDef() const {
const Function *F = dyn_cast<Function>(this);
if (!F || F->empty())
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/IR/MDBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ MDNode *MDBuilder::createFunctionEntryCount(
return MDNode::get(Context, Ops);
}

MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
return MDNode::get(
Context, {createString("function_section_prefix"), createString(Prefix)});
MDNode *MDBuilder::createGlobalObjectSectionPrefix(StringRef Prefix) {
return MDNode::get(Context,
{createString("section_prefix"), createString(Prefix)});
}

MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/X86/data-section-prefix.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: llc -mtriple x86_64-linux-gnu -data-sections %s -o - | FileCheck %s --check-prefix=ELF
; RUN: llc -mtriple x86_64-linux-gnu -unique-section-names=0 -data-sections %s -o - | FileCheck %s --check-prefix=ELF-NOUNIQ

; RUN: llc -mtriple x86_64-windows-msvc -data-sections %s -o - | FileCheck %s --check-prefix=COFF-MSVC

; ELF: .section .data.hot.foo,
; ELF: .section .data.bar,
; ELF: .section .bss.unlikely.baz,
; ELF: .section .bss.quz,

; ELF-NOUNIQ: .section .data.hot.,"aw",@progbits,unique,1
; ELF-NOUNIQ: .section .data,"aw",@progbits,unique,2
; ELF-NOUNIQ: .section .bss.unlikely.,"aw",@nobits,unique,3
; ELF-NOUNIQ: .section .bss,"aw",@nobits,unique,4

; COFF-MSVC: .section .data,"dw",one_only,foo
; COFF-MSVC: .section .data,"dw",one_only,bar
; COFF-MSVC: .section .bss,"bw",one_only,baz
; COFF-MSVC: .section .bss,"bw",one_only,quz

@foo = global i32 1, !section_prefix !0
@bar = global i32 2
@baz = global i32 0, !section_prefix !1
@quz = global i32 0

!0 = !{!"section_prefix", !"hot"}
!1 = !{!"section_prefix", !"unlikely"}
4 changes: 2 additions & 2 deletions llvm/test/Transforms/CodeGenPrepare/X86/section-samplepgo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ define void @cold_func() !prof !16 {
ret void
}

; CHECK: ![[HOT_ID]] = !{!"function_section_prefix", !"hot"}
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
; CHECK: ![[HOT_ID]] = !{!"section_prefix", !"hot"}
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
!llvm.module.flags = !{!1}
!1 = !{i32 1, !"ProfileSummary", !2}
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/CodeGenPrepare/X86/section.ll
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ define void @cold_func3() !prof !16 {
ret void
}

; CHECK: ![[HOT_ID]] = !{!"function_section_prefix", !"hot"}
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
; CHECK: ![[HOT_ID]] = !{!"section_prefix", !"hot"}
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
!llvm.module.flags = !{!1}
!1 = !{i32 1, !"ProfileSummary", !2}
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/HotColdSplit/coldentrycount.ll
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ declare void @sink() cold
; CHECK: define {{.*}} @fun.cold.1{{.*}} ![[PROF:[0-9]+]] {{.*}}section_prefix ![[UNLIKELY:[0-9]+]]

; CHECK: ![[HOTPROF]] = !{!"function_entry_count", i64 100}
; CHECK: ![[LIKELY]] = !{!"function_section_prefix", !"hot"}
; CHECK: ![[LIKELY]] = !{!"section_prefix", !"hot"}
; CHECK: ![[PROF]] = !{!"function_entry_count", i64 0}
; CHECK: ![[UNLIKELY]] = !{!"function_section_prefix", !"unlikely"}
; CHECK: ![[UNLIKELY]] = !{!"section_prefix", !"unlikely"}

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"ProfileSummary", !1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ attributes #1 = { "use-sample-profile" }

; CHECK: ![[NOPROFILE_ID]] = !{!"function_entry_count", i64 -1}
; CHECK: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
; CHECK: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
; CHECK: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
; UNKNOWN: ![[NOPROFILE_ID]] = !{!"function_entry_count", i64 -1}
; UNKNOWN: ![[UNKNOWN_ID]] = !{!"function_section_prefix", !"unknown"}
; UNKNOWN: ![[UNKNOWN_ID]] = !{!"section_prefix", !"unknown"}
; ACCURATE: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
; ACCURATE: ![[COLD_ID]] = !{!"function_section_prefix", !"unlikely"}
; ACCURATE: ![[COLD_ID]] = !{!"section_prefix", !"unlikely"}
!llvm.module.flags = !{!1}
!1 = !{i32 1, !"ProfileSummary", !2}
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
Expand Down