Skip to content

Commit 4ead381

Browse files
simplify test. The next step is to add debug log
1 parent bc8e67a commit 4ead381

File tree

4 files changed

+63
-95
lines changed

4 files changed

+63
-95
lines changed

llvm/include/llvm/Analysis/StaticDataProfileInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace llvm {
1212

13+
bool IsReservedGlobalVariable(const GlobalVariable &GV);
14+
1315
/// A class that holds the constants that represent static data and their
1416
/// profile information and provides methods to operate on them.
1517
class StaticDataProfileInfo {

llvm/lib/Analysis/StaticDataProfileInfo.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ using namespace llvm;
1414

1515
extern cl::opt<bool> AnnotateStaticDataSectionPrefix;
1616

17+
bool llvm::IsReservedGlobalVariable(const GlobalVariable &GV) {
18+
return GV.getName().starts_with("llvm.");
19+
}
20+
1721
void StaticDataProfileInfo::addConstantProfileCount(
1822
const Constant *C, std::optional<uint64_t> Count) {
1923
if (!Count) {
@@ -87,7 +91,8 @@ StringRef StaticDataProfileInfo::getConstantSectionPrefix(
8791
// Module flag `HasDataAccessProf` is 1 -> empty section prefix means
8892
// unknown hotness except for string literals.
8993
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
90-
GV && !GV->getName().starts_with(".str")) {
94+
GV && !IsReservedGlobalVariable(*GV) &&
95+
!GV->getName().starts_with(".str")) {
9196
auto HotnessFromDAP = getSectionHotnessUsingDAP(GV->getSectionPrefix());
9297

9398
if (!Count) {

llvm/lib/CodeGen/StaticDataSplitter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ StaticDataSplitter::getConstant(const MachineOperand &Op,
133133
// Skip 'llvm.'-prefixed global variables conservatively because they are
134134
// often handled specially, and skip those not in static data
135135
// sections.
136-
if (!GV || GV->getName().starts_with("llvm.") ||
137-
!inStaticDataSection(*GV, TM))
136+
if (!GV || llvm::IsReservedGlobalVariable(*GV) || !inStaticDataSection(*GV, TM))
138137
return nullptr;
139138
return GV;
140139
}

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

Lines changed: 54 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,99 @@ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:
22
target triple = "x86_64-unknown-linux-gnu"
33

44
;; Comments for RUN command options
5-
; 1. `-relocation-model=pic` -> `hot_relro_array` and `cold_relro_array` are
5+
; 1. `-relocation-model=pic` -> `relro_var` is
66
; placed in the .data.rel.ro-prefixed section.
77
; 2. `-data-sections=true -unique-section-names=false` -> data sections are
88
; uniqufied by variable names.
99
;
1010
; RUN: llc -mtriple=x86_64-unknown-linux-gnu -relocation-model=pic \
1111
; RUN: -partition-static-data-sections=true \
1212
; RUN: -data-sections=true -unique-section-names=false \
13-
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefixes=UNIQ,COMMON --dump-input=always
13+
; RUN: %s -o - 2>&1 | FileCheck %s --dump-input=always
1414

1515
; For @.str and @.str.1
16-
; COMMON: .type .L.str,@object
17-
; UNIQ-NEXT: .section .rodata.str1.1.hot.,"aMS",@progbits,1
18-
; COMMON-NEXT: .L.str:
19-
; COMMON-NEXT: "hot\t"
20-
; COMMON: .L.str.1:
21-
; COMMON-NEXT: "%d\t%d\t%d\n"
16+
; CHECK: .type .L.str,@object
17+
; CHECK-NEXT: .section .rodata.str1.1.hot.,"aMS",@progbits,1
18+
; CHECK-NEXT: .L.str:
19+
; CHECK-NEXT: "1234"
20+
; CHECK: .type .str.1,@object
21+
; CHECK: .str.1:
22+
; CHECK-NEXT: "abcde"
2223

23-
; For @hot_relro_array
24-
; COMMON: .type hot_relro_array,@object
25-
; UNIQ-NEXT: .section .data.rel.ro.hot.,"aw",@progbits,unique,1
24+
; For @.str.2
25+
; CHECK: .type .str.2,@object
26+
; CHECK-NEXT: .section .rodata.str1.1,"aMS",@progbits
27+
; CHECK-NEXT: .globl .str.2
28+
; CHECK-NEXT: .str.2:
29+
; CHECK-NEXT: "beef"
30+
@.str = private unnamed_addr constant [5 x i8] c"1234\00", align 1
31+
@.str.1 = internal unnamed_addr constant [6 x i8] c"abcde\00"
32+
@.str.2 = unnamed_addr constant [5 x i8] c"beef\00", align 1
2633

27-
; For @external_hot_data, which is accessed by {cold_func, unprofiled_func, hot_func}.
28-
; COMMON: .type external_hot_data,@object
29-
; UNIQ-NEXT: .section .data.hot.,"aw",@progbits,unique,2
34+
; CHECK: .type relro_var,@object
35+
; CHECK-NEXT: .section .data.rel.ro,"aw",@progbits,unique,1
3036

31-
; For @hot_bss, which is accessed by {unprofiled_func, hot_func}.
32-
; COMMON: .type hot_bss,@object
33-
; UNIQ-NEXT: .section .bss.hot.,"aw",@nobits,unique,3
37+
; CHECK: .type external_hot_data,@object
38+
; CHECK-NEXT: .section .data.hot.,"aw",@progbits,unique,2
3439

35-
; For @.str.2
36-
; COMMON: .type .L.str.2,@object
37-
; UNIQ-NEXT: .section .rodata.str1.1.unlikely.,"aMS",@progbits,1
38-
; COMMON-NEXT: .L.str.2:
39-
; COMMON-NEXT: "cold%d\t%d\t%d\n"
40-
41-
; For @cold_bss
42-
; COMMON: .type cold_bss,@object
43-
; UNIQ-NEXT: .section .bss.unlikely.,"aw",@nobits,unique,4
44-
45-
; For @cold_data
46-
; COMMON: .type cold_data,@object
47-
; UNIQ-NEXT: .section .data.unlikely.,"aw",@progbits,unique,5
48-
49-
; For @cold_data_custom_foo_section
50-
; It has an explicit section 'foo' and shouldn't have hot or unlikely suffix.
51-
; COMMON: .type cold_data_custom_foo_section,@object
52-
; UNIQ-NEXT: .section foo,"aw",@progbits
53-
54-
; For @cold_relro_array
55-
; COMMON: .type cold_relro_array,@object
56-
; UNIQ-NEXT: .section .data.rel.ro.unlikely.,"aw",@progbits,unique,6
57-
58-
59-
; @bss2 and @data3 are indirectly accessed via @hot_relro_array and
60-
; @cold_relro_array, and actually hot due to accesses via @hot_relro_array.
61-
; Under the hood, the static data splitter pass analyzes accesses from code but
62-
; won't aggressively propgate the hotness of @hot_relro_array into the array
63-
; elements -- instead, this pass reconciles the hotness information from both
64-
; global variable section prefix and PGO counters.
65-
66-
; @bss2 has a section prefix 'hot' in the IR. StaticDataProfileInfo reconciles
67-
; it into a hot prefix.
68-
; COMMON: .type bss2,@object
69-
; UNIQ-NEXT: .section .bss.hot.,"aw",@nobits,unique,7
70-
71-
; @data3 doesn't have data access profile coverage and thereby doesn't have a
72-
; section prefix. PGO counter analysis categorizes it as cold, so it will have
73-
; section name `.data.unlikely`.
74-
; COMMON: .type data3,@object
75-
; UNIQ-NEXT: .section .data,"aw",@progbits,unique,8
76-
77-
; For @data_with_unknown_hotness
78-
; COMMON: .type .Ldata_with_unknown_hotness,@object # @data_with_unknown_hotness
79-
; UNIQ: .section .data,"aw",@progbits,unique,9
80-
81-
; For @hot_data_custom_bar_section
82-
; It has an explicit section attribute 'var' and shouldn't have hot or unlikely suffix.
83-
; COMMON: .type hot_data_custom_bar_section,@object
84-
; UNIQ: .section bar,"aw",@progbits
85-
86-
; For @external_cold_bss
87-
; COMMON: .type external_cold_bss,@object
88-
; UNIQ-NEXT: .section .bss,"aw",@nobits,unique,
89-
90-
@.str = private unnamed_addr constant [5 x i8] c"hot\09\00", align 1
91-
@.str.1 = private unnamed_addr constant [10 x i8] c"%d\09%d\09%d\0A\00", align 1
92-
@hot_relro_array = internal constant [2 x ptr] [ptr @bss2, ptr @data3]
40+
; CHECK: .type hot_bss,@object
41+
; CHECK-NEXT: .section .bss.hot.,"aw",@nobits,unique,3
42+
43+
@relro_var = constant [2 x ptr] [ptr @bss2, ptr @data3]
9344
@external_hot_data = global i32 5, !section_prefix !17
9445
@hot_bss = internal global i32 0
95-
@.str.2 = private unnamed_addr constant [14 x i8] c"cold%d\09%d\09%d\0A\00", align 1
46+
47+
;; Both section prefix and PGO counters indicate @cold_bss and @cold_data are
48+
;; rarely accesed.
49+
; CHECK: .type cold_bss,@object
50+
; CHECK-NEXT: .section .bss.unlikely.,"aw",@nobits,unique,4
51+
; CHECK: .type cold_data,@object
52+
; CHECK-NEXT: .section .data.unlikely.,"aw",@progbits,unique,5
9653
@cold_bss = internal global i32 0, !section_prefix !18
9754
@cold_data = internal global i32 4, !section_prefix !18
98-
@cold_data_custom_foo_section = internal global i32 100, section "foo"
99-
@cold_relro_array = internal constant [2 x ptr] [ptr @data3, ptr @bss2], !section_prefix !18
55+
56+
;; @bss2 has a section prefix 'hot' in the IR. StaticDataProfileInfo reconciles
57+
;; it into a hot prefix.
58+
; CHECK: .type bss2,@object
59+
; CHECK-NEXT: .section .bss.hot.,"aw",@nobits,unique,6
10060
@bss2 = internal global i32 0, !section_prefix !17
61+
62+
;; Since `HasDataAccessProf` is true, data without a section prefix is
63+
;; conservatively categorized as unknown (e.g., from incremental source code)
64+
;; rather than cold.
65+
; CHECK: .type data3,@object
66+
; CHECK-NEXT: .section .data,"aw",@progbits,unique,7
10167
@data3 = internal global i32 3
102-
@data_with_unknown_hotness = private global i32 5
68+
69+
;; These sections have custom names, so they won't be labeled as .hot or .unlikely.
70+
; CHECK: .type hot_data_custom_bar_section,@object
71+
; CHECK-NEXT: .section bar,"aw"
72+
; CHECK: .type cold_data_custom_foo_section,@object
73+
; CHECK-NEXT: .section foo,"aw"
10374
@hot_data_custom_bar_section = internal global i32 101 #0
104-
@external_cold_bss = global i32 0
75+
@cold_data_custom_foo_section = internal global i32 100, section "foo"
10576

10677
define void @cold_func(i32 %0) !prof !15 {
10778
%2 = load i32, ptr @cold_bss
10879
%3 = load i32, ptr @cold_data
109-
%4 = srem i32 %0, 2
110-
%5 = sext i32 %4 to i64
111-
%6 = getelementptr inbounds [2 x ptr], ptr @cold_relro_array, i64 0, i64 %5
112-
%7 = load ptr, ptr %6
113-
%8 = load i32, ptr %7
114-
%9 = load i32, ptr @data_with_unknown_hotness
11580
%11 = load i32, ptr @external_hot_data
11681
%12 = load i32, ptr @cold_data_custom_foo_section
117-
%val = load i32, ptr @external_cold_bss
118-
%13 = call i32 (...) @func_taking_arbitrary_param(ptr @.str.2, i32 %2, i32 %3, i32 %8, i32 %9, i32 %11, i32 %12, i32 %val)
82+
%13 = call i32 (...) @func_taking_arbitrary_param(ptr @.str.2, i32 %2, i32 %3, i32 %11, i32 %12)
11983
ret void
12084
}
12185

12286
define i32 @unprofiled_func() {
123-
%a = load i32, ptr @data_with_unknown_hotness
12487
%b = load i32, ptr @external_hot_data
12588
%c = load i32, ptr @hot_bss
126-
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %a, i32 %b, i32 %c)
89+
%ret = call i32 (...) @func_taking_arbitrary_param(i32 %b, i32 %c)
12790
ret i32 %ret
12891
}
12992

13093
define void @hot_func(i32 %0) !prof !14 {
13194
%2 = call i32 (...) @func_taking_arbitrary_param(ptr @.str)
13295
%3 = srem i32 %0, 2
13396
%4 = sext i32 %3 to i64
134-
%5 = getelementptr inbounds [2 x ptr], ptr @hot_relro_array, i64 0, i64 %4
97+
%5 = getelementptr inbounds [2 x ptr], ptr @relro_var, i64 0, i64 %4
13598
%6 = load ptr, ptr %5
13699
%7 = load i32, ptr %6
137100
%8 = load i32, ptr @external_hot_data
@@ -166,4 +129,3 @@ attributes #0 = {"data-section"="bar"}
166129
!16 = !{!"branch_weights", i32 1, i32 99999}
167130
!17 = !{!"section_prefix", !"hot"}
168131
!18 = !{!"section_prefix", !"unlikely"}
169-

0 commit comments

Comments
 (0)