Skip to content

Commit 3565bc0

Browse files
add test coverage
1 parent dcb9f00 commit 3565bc0

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

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

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: 19 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"}
@@ -80,6 +87,9 @@ DataAccessProfiles:
8087
AccessCount: 145
8188
KnownColdSymbols:
8289
- foo
90+
# The llvm.fake_var entry is added to test optimizer won't annotate it with section prefix.
91+
# In real profiles, llvm.* variables may not have entries in the first place.
92+
- llvm.fake_var
8393
KnownColdStrHashes: [ 999, 1001 ]
8494
...
8595
;--- memprof-no-dap.yaml
@@ -112,11 +122,14 @@ target triple = "x86_64-unknown-linux-gnu"
112122
@foo = global i8 2
113123
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
114124
@var4 = constant [1 x i64][i64 98765] #0
125+
@llvm.fake_var = global i32 123
126+
@qux = external global i64
115127

116128
define i32 @func() {
117129
%a = load i32, ptr @var1
118130
%b = load i32, ptr @var2.llvm.125
119-
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %a, i32 %b)
131+
%c = load i32, ptr @llvm.fake_var
132+
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %a, i32 %b, i32 %c)
120133
ret i32 %ret
121134
}
122135

@@ -136,5 +149,8 @@ target triple = "x86_64-unknown-linux-gnu"
136149
@foo = global i8 2
137150
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
138151
@var4 = constant [1 x i64][i64 98765] #0
152+
@llvm.fake_var = global i32 123
153+
@qux = external global i64
154+
139155

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

0 commit comments

Comments
 (0)