Skip to content

Commit 291bae7

Browse files
skip annotating global variables with explicit sections for real
1 parent a6e5cc6 commit 291bae7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvm/lib/Transforms/Instrumentation/MemProfUse.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,23 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
767767
return PreservedAnalyses::none();
768768
}
769769

770+
// Returns true iff the global variable has custom section either by
771+
// __attribute__((section("name")))
772+
// (https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate)
773+
// or #pragma clang section directives
774+
// (https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section).
775+
static bool hasExplicitSectionName(const GlobalVariable &GVar) {
776+
if (GVar.hasSection())
777+
return true;
778+
779+
auto Attrs = GVar.getAttributes();
780+
if (Attrs.hasAttribute("bss-section") || Attrs.hasAttribute("data-section") ||
781+
Attrs.hasAttribute("relro-section") ||
782+
Attrs.hasAttribute("rodata-section"))
783+
return true;
784+
return false;
785+
}
786+
770787
bool MemProfUsePass::annotateGlobalVariables(
771788
Module &M, const memprof::DataAccessProfData *DataAccessProf) {
772789
if (!AnnotateStaticDataSectionPrefix || M.globals().empty())
@@ -793,6 +810,12 @@ bool MemProfUsePass::annotateGlobalVariables(
793810
if (GVar.isDeclarationForLinker())
794811
continue;
795812

813+
if (hasExplicitSectionName(GVar)) {
814+
LLVM_DEBUG(dbgs() << "Global variable " << GVar.getName()
815+
<< " has explicit section name. Skip annotating.\n");
816+
continue;
817+
}
818+
796819
StringRef Name = GVar.getName();
797820
// Skip string literals as their mangled names don't stay stable across
798821
// binary binary releases.

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
; RUN: opt -passes='memprof-use<profile-filename=memprof.profdata>' -memprof-annotate-static-data-prefix \
1616
; RUN: -debug-only=memprof -S input.ll -o - 2>&1 | FileCheck %s --check-prefixes=LOG,PREFIX
1717

18-
1918
; RUN: opt -passes='memprof-use<profile-filename=memprof.profdata>' -memprof-annotate-static-data-prefix=false \
2019
; RUN: -debug-only=memprof -S input.ll -o - 2>&1 | FileCheck %s --implicit-check-not="section_prefix"
2120

@@ -24,6 +23,8 @@
2423
; LOG: Global variable var2.llvm.125 is annotated as hot
2524
; LOG: Global variable bar is not annotated
2625
; LOG: Global variable foo is annotated as unlikely
26+
; LOG: Global variable var3 has explicit section name. Skip annotating.
27+
; LOG: Global variable var4 has explicit section name. Skip annotating.
2728

2829
;; String literals are not annotated.
2930
; PREFIX: @.str = unnamed_addr constant [5 x i8] c"abcde"
@@ -42,7 +43,10 @@
4243
;; @foo is unlikely.
4344
; PREFIX-NEXT: @foo = global i8 2, !section_prefix !1
4445

46+
; PREFIX-NEXT: @var3 = constant [2 x i32] [i32 12345, i32 6789], section "sec1"
47+
; PREFIX-NEXT: @var4 = constant [1 x i64] [i64 98765] #0
4548

49+
; PREFIX: attributes #0 = { "rodata-section"="sec2" }
4650

4751
; PREFIX: !0 = !{!"section_prefix", !"hot"}
4852
; PREFIX-NEXT: !1 = !{!"section_prefix", !"unlikely"}
@@ -87,6 +91,8 @@ target triple = "x86_64-unknown-linux-gnu"
8791
@var2.llvm.125 = global i64 0
8892
@bar = global i16 3
8993
@foo = global i8 2
94+
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
95+
@var4 = constant [1 x i64][i64 98765] #0
9096

9197
define i32 @func() {
9298
%a = load i32, ptr @var1
@@ -96,6 +102,9 @@ define i32 @func() {
96102
}
97103

98104
declare i32 @func_taking_arbitrary_param(...)
105+
106+
attributes #0 = { "rodata-section"="sec2" }
107+
99108
;--- funcless-module.ll
100109

101110
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"
@@ -106,4 +115,7 @@ target triple = "x86_64-unknown-linux-gnu"
106115
@var2.llvm.125 = global i64 0
107116
@bar = global i16 3
108117
@foo = global i8 2
118+
@var3 = constant [2 x i32][i32 12345, i32 6789], section "sec1"
119+
@var4 = constant [1 x i64][i64 98765] #0
109120

121+
attributes #0 = { "rodata-section"="sec2" }

0 commit comments

Comments
 (0)