Skip to content

Commit 3b2f494

Browse files
Merge branch 'users/mingmingl-llvm/sdpi' into users/mingmingl-llvm/setsectionprefix
2 parents c56b792 + 4a90c77 commit 3b2f494

File tree

6 files changed

+102
-16
lines changed

6 files changed

+102
-16
lines changed

llvm/lib/Analysis/StaticDataProfileInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ static bool hasExplicitSectionName(const GlobalVariable &GVar) {
2929
AnnotationKind getAnnotationKind(const GlobalVariable &GV) {
3030
if (GV.isDeclarationForLinker())
3131
return AnnotationKind::DeclForLinker;
32+
// Skip 'llvm.'-prefixed global variables conservatively because they are
33+
// often handled specially,
3234
StringRef Name = GV.getName();
3335
if (Name.starts_with("llvm."))
3436
return AnnotationKind::ReservedName;
37+
// Respect user-specified custom data sections.
3538
if (hasExplicitSectionName(GV))
3639
return AnnotationKind::ExplicitSection;
3740
return AnnotationKind::AnnotationOK;

llvm/lib/CodeGen/StaticDataSplitter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,8 @@ StaticDataSplitter::getConstant(const MachineOperand &Op,
130130
if (Op.isGlobal()) {
131131
// Find global variables with local linkage.
132132
const GlobalVariable *GV = getLocalLinkageGlobalVariable(Op.getGlobal());
133-
// Skip 'llvm.'-prefixed global variables conservatively because they are
134-
// often handled specially, and skip those not in static data
135-
// sections.
136-
if (!GV || GV->getName().starts_with("llvm.") ||
133+
// Skip those not eligible for annotation or not in static data sections.
134+
if (!GV || !llvm::memprof::IsAnnotationOK(*GV) ||
137135
!inStaticDataSection(*GV, TM))
138136
return nullptr;
139137
return GV;

llvm/lib/Transforms/Instrumentation/MemProfUse.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ static bool isAllocationWithHotColdVariant(const Function *Callee,
195195
}
196196
}
197197

198+
static void HandleUnsupportedAnnotationKinds(GlobalVariable &GVar,
199+
AnnotationKind Kind) {
200+
assert(Kind != llvm::memprof::AnnotationKind::AnnotationOK &&
201+
"Should not handle AnnotationOK here");
202+
SmallString<32> Reason;
203+
switch (Kind) {
204+
case llvm::memprof::AnnotationKind::ExplicitSection:
205+
++NumOfMemProfExplicitSectionGlobalVars;
206+
Reason.append("explicit section name");
207+
break;
208+
case llvm::memprof::AnnotationKind::DeclForLinker:
209+
Reason.append("linker declaration");
210+
break;
211+
case llvm::memprof::AnnotationKind::ReservedName:
212+
Reason.append("name starts with `llvm.`");
213+
break;
214+
default:
215+
llvm_unreachable("Unexpected annotation kind");
216+
}
217+
LLVM_DEBUG(dbgs() << "Skip annotation for " << GVar.getName() << " due to "
218+
<< Reason << ".\n");
219+
return;
220+
}
221+
198222
struct AllocMatchInfo {
199223
uint64_t TotalSize = 0;
200224
AllocationType AllocType = AllocationType::None;
@@ -805,12 +829,8 @@ bool MemProfUsePass::annotateGlobalVariables(
805829
switch (Kind) {
806830
case llvm::memprof::AnnotationKind::AnnotationOK:
807831
break;
808-
case llvm::memprof::AnnotationKind::ExplicitSection:
809-
++NumOfMemProfExplicitSectionGlobalVars;
810-
LLVM_DEBUG(dbgs() << "Global variable " << GVar.getName()
811-
<< " has explicit section name. Skip annotating.\n");
812-
[[fallthrough]];
813832
default:
833+
HandleUnsupportedAnnotationKinds(GVar, Kind);
814834
continue;
815835
}
816836

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
;; A minimal test case. llc will crash if global variables already has a section
5+
;; prefix. Subsequent PRs will expand on this test case to test the hotness
6+
;; reconcillation implementation.
7+
; RUN: not llc -mtriple=x86_64-unknown-linux-gnu -relocation-model=pic \
8+
; RUN: -partition-static-data-sections=true \
9+
; RUN: -data-sections=true -unique-section-names=false \
10+
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefix=ERR
11+
12+
ERR: Global variable hot_bss already has a section prefix hot
13+
14+
@hot_bss = internal global i32 0, !section_prefix !17
15+
16+
define void @hot_func() !prof !14 {
17+
%9 = load i32, ptr @hot_bss
18+
%11 = call i32 (...) @func_taking_arbitrary_param(i32 %9)
19+
ret void
20+
}
21+
22+
declare i32 @func_taking_arbitrary_param(...)
23+
24+
!llvm.module.flags = !{!1}
25+
26+
!1 = !{i32 1, !"ProfileSummary", !2}
27+
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
28+
!3 = !{!"ProfileFormat", !"InstrProf"}
29+
!4 = !{!"TotalCount", i64 1460183}
30+
!5 = !{!"MaxCount", i64 849024}
31+
!6 = !{!"MaxInternalCount", i64 32769}
32+
!7 = !{!"MaxFunctionCount", i64 849024}
33+
!8 = !{!"NumCounts", i64 23627}
34+
!9 = !{!"NumFunctions", i64 3271}
35+
!10 = !{!"DetailedSummary", !11}
36+
!11 = !{!12, !13}
37+
!12 = !{i32 990000, i64 166, i32 73}
38+
!13 = !{i32 999999, i64 3, i32 1443}
39+
!14 = !{!"function_entry_count", i64 100000}
40+
!15 = !{!"function_entry_count", i64 1}
41+
!16 = !{!"branch_weights", i32 1, i32 99999}
42+
!17 = !{!"section_prefix", !"hot"}

llvm/test/CodeGen/X86/global-variable-partition.ll

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,31 @@ target triple = "x86_64-unknown-linux-gnu"
106106
; UNIQ-NEXT: .section .data.unlikely.,"aw",@progbits,unique,8
107107
; AGG-NEXT: .section .data.unlikely.,"aw",@progbits
108108

109+
;; The `.section` directive is omitted for .data with -unique-section-names=false.
110+
; See MCSectionELF::shouldOmitSectionDirective for the implementation details.
111+
109112
; For @data_with_unknown_hotness
110113
; SYM: .type .Ldata_with_unknown_hotness,@object # @data_with_unknown_hotness
111114
; SYM: .section .data..Ldata_with_unknown_hotness,"aw",@progbits
112115
; UNIQ: .section .data,"aw",@progbits,unique,9
113-
; The `.section` directive is omitted for .data with -unique-section-names=false.
114-
; See MCSectionELF::shouldOmitSectionDirective for the implementation details.
116+
115117
; AGG: .data
116118
; COMMON: .Ldata_with_unknown_hotness:
117119

118-
; For @hot_data_custom_bar_section
119-
; It has an explicit section attribute 'var' and shouldn't have hot or unlikely suffix.
120+
; For variables that are not eligible for section prefix annotation
120121
; COMMON: .type hot_data_custom_bar_section,@object
121122
; SYM-NEXT: .section bar,"aw",@progbits
122123
; SYM: hot_data_custom_bar_section
123124
; UNIQ: .section bar,"aw",@progbits
124125
; AGG: .section bar,"aw",@progbits
125126

127+
; SYM: .section .data.llvm.fake_var,"aw"
128+
; UNIQ: .section .data,"aw"
129+
; AGG: .data
130+
131+
;; No section for linker declaration
132+
; COMMON-NOT: qux
133+
126134
@.str = private unnamed_addr constant [5 x i8] c"hot\09\00", align 1
127135
@.str.1 = private unnamed_addr constant [10 x i8] c"%d\09%d\09%d\0A\00", align 1
128136
@hot_relro_array = internal constant [2 x ptr] [ptr @bss2, ptr @data3]
@@ -137,6 +145,8 @@ target triple = "x86_64-unknown-linux-gnu"
137145
@data3 = internal global i32 3
138146
@data_with_unknown_hotness = private global i32 5
139147
@hot_data_custom_bar_section = internal global i32 101 #0
148+
@llvm.fake_var = internal global i32 123
149+
@qux = external global i64
140150

141151
define void @cold_func(i32 %0) !prof !15 {
142152
%2 = load i32, ptr @cold_bss

llvm/test/Transforms/PGOProfile/data-access-profile.ll

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
; LOG: Global variable var2.llvm.125 is annotated as hot
3232
; LOG: Global variable bar is not annotated
3333
; LOG: Global variable foo is annotated as unlikely
34-
; LOG: Global variable var3 has explicit section name. Skip annotating.
35-
; LOG: Global variable var4 has explicit section name. Skip annotating.
34+
; LOG: Skip annotation for var3 due to explicit section name.
35+
; LOG: Skip annotation for var4 due to explicit section name.
36+
; LOG: Skip annotation for llvm.fake_var due to name starts with `llvm.`.
37+
; LOG: Skip annotation for qux due to linker declaration.
3638

3739
;; String literals are not annotated.
3840
; IR: @.str = unnamed_addr constant [5 x i8] c"abcde"
@@ -54,6 +56,11 @@
5456
; IR-NEXT: @var3 = constant [2 x i32] [i32 12345, i32 6789], section "sec1"
5557
; IR-NEXT: @var4 = constant [1 x i64] [i64 98765] #0
5658

59+
; IR: @llvm.fake_var = global i32 123
60+
; IR-NOT: !section_prefix
61+
; IR: @qux = external global i64
62+
; IR-NOT: !section_prefix
63+
5764
; IR: attributes #0 = { "rodata-section"="sec2" }
5865

5966
; IR: !0 = !{!"section_prefix", !"hot"}
@@ -112,11 +119,14 @@ target triple = "x86_64-unknown-linux-gnu"
112119
@foo = global i8 2
113120
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
114121
@var4 = constant [1 x i64][i64 98765] #0
122+
@llvm.fake_var = global i32 123
123+
@qux = external global i64
115124

116125
define i32 @func() {
117126
%a = load i32, ptr @var1
118127
%b = load i32, ptr @var2.llvm.125
119-
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %a, i32 %b)
128+
%c = load i32, ptr @llvm.fake_var
129+
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %a, i32 %b, i32 %c)
120130
ret i32 %ret
121131
}
122132

@@ -136,5 +146,8 @@ target triple = "x86_64-unknown-linux-gnu"
136146
@foo = global i8 2
137147
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
138148
@var4 = constant [1 x i64][i64 98765] #0
149+
@llvm.fake_var = global i32 123
150+
@qux = external global i64
151+
139152

140153
attributes #0 = { "rodata-section"="sec2" }

0 commit comments

Comments
 (0)