Skip to content

Commit 0d94882

Browse files
authored
[Instrumentation][X86] Limit setting large section flag to medium/large code models (#75542)
In #74514 and #74778 we marked various instrumentation-added sections as large. This causes an extra PT_LOAD segment if using the small code model. Since people using the small code model presumably aren't hitting relocation limits, disable this when using the small code model to avoid the extra segment. This uses Module::getCodeModel() which isn't necessarily reliable since it reads module metadata (which right now only the clang frontend sets), but it would be nice to get to a point where we reliably put this sort of information (e.g. PIC/code model/etc) in the IR. This requires duplicating the existing tests since opt/llc currently don't set these metadata. If we get to a point where they do set the code model metadata based on command line arguments then we can deduplicate these tests.
1 parent 43cb8f0 commit 0d94882

File tree

8 files changed

+138
-39
lines changed

8 files changed

+138
-39
lines changed

llvm/lib/Transforms/Instrumentation/Instrumentation.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,13 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
8787

8888
void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
8989
GlobalVariable &GV) {
90-
if (TargetTriple.getArch() == Triple::x86_64 &&
91-
TargetTriple.getObjectFormat() == Triple::ELF) {
92-
GV.setCodeModel(CodeModel::Large);
93-
}
90+
// Limit to x86-64 ELF.
91+
if (TargetTriple.getArch() != Triple::x86_64 ||
92+
TargetTriple.getObjectFormat() != Triple::ELF)
93+
return;
94+
// Limit to medium/large code models.
95+
std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
96+
if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
97+
return;
98+
GV.setCodeModel(CodeModel::Large);
9499
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
;; Check that asan_globals is marked large under x86-64 medium code model.
2+
; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefixes=CHECK,X8664
3+
; RUN: opt < %s -mtriple=ppc64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefixes=CHECK,PPC
4+
5+
; CHECK: @__asan_global_global =
6+
; X8664-SAME: code_model "large"
7+
; PPC-NOT: code_model "large"
8+
9+
@global = global i32 0, align 4
10+
11+
!llvm.module.flags = !{!0}
12+
13+
!0 = !{i32 1, !"Code Model", i32 3}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;; Check that asan_globals is not marked large without an explicit code model.
2+
; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s
3+
4+
; CHECK: @__asan_global_global =
5+
; CHECK-NOT: code_model "large"
6+
7+
@global = global i32 0, align 4

llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll

Lines changed: 0 additions & 10 deletions
This file was deleted.

llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
; RUN: opt %s -mtriple=powerpc64-ibm-aix -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
1616
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
1717

18-
;; Check that globals have the proper code model.
19-
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-X8664
20-
; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-PPC
21-
2218
@__profn_foo = private constant [3 x i8] c"foo"
2319
@__profn_bar = private constant [3 x i8] c"bar"
2420

@@ -78,24 +74,3 @@ attributes #0 = { nounwind }
7874
; ALIGN: @__profd_bar = private global {{.*}} section "__llvm_prf_data",{{.*}} align 8
7975
; ALIGN: @__llvm_prf_vnodes = private global {{.*}} section "__llvm_prf_vnds",{{.*}} align 8
8076
; ALIGN: @__llvm_prf_nm = private constant {{.*}} section "__llvm_prf_names",{{.*}} align 1
81-
82-
; CODEMODEL: @__profc_foo =
83-
; CODEMODEL-NOT: code_model "large"
84-
; CODEMODEL: @__profvp_foo =
85-
; CODEMODEL-X8664-SAME: code_model "large"
86-
; CODEMODEL-PPC-NOT: code_model
87-
; CODEMODEL: @__profd_foo =
88-
; CODEMODEL-NOT: code_model "large"
89-
; CODEMODEL: @__profc_bar =
90-
; CODEMODEL-NOT: code_model "large"
91-
; CODEMODEL: @__profvp_bar =
92-
; CODEMODEL-X8664-SAME: code_model "large"
93-
; CODEMODEL-PPC-NOT: code_model
94-
; CODEMODEL: @__profd_bar =
95-
; CODEMODEL-NOT: code_model "large"
96-
; CODEMODEL: @__llvm_prf_vnodes =
97-
; CODEMODEL-X8664-SAME: code_model "large"
98-
; CODEMODEL-PPC-NOT: code_model
99-
; CODEMODEL: @__llvm_prf_nm =
100-
; CODEMODEL-X8664-SAME: code_model "large"
101-
; CODEMODEL-PPC-NOT: code_model
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; Check that certain globals are in large sections under x86-64 large code model.
2+
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s
3+
4+
@__profn_foo = private constant [3 x i8] c"foo"
5+
6+
define i32 @foo(ptr) {
7+
call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
8+
%2 = ptrtoint ptr %0 to i64
9+
call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
10+
%3 = tail call i32 %0()
11+
ret i32 %3
12+
}
13+
14+
; Function Attrs: nounwind
15+
declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
16+
17+
; Function Attrs: nounwind
18+
declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
19+
20+
attributes #0 = { nounwind }
21+
22+
!llvm.module.flags = !{!0}
23+
24+
!0 = !{i32 1, !"Code Model", i32 4}
25+
26+
; CHECK: @__profc_foo =
27+
; CHECK-NOT: code_model "large"
28+
; CHECK: @__profvp_foo =
29+
; CHECK-SAME: code_model "large"
30+
; CHECK: @__profd_foo =
31+
; CHECK-NOT: code_model "large"
32+
; CHECK: @__llvm_prf_vnodes =
33+
; CHECK-SAME: code_model "large"
34+
; CHECK: @__llvm_prf_nm =
35+
; CHECK-SAME: code_model "large"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
;; Check that certain globals are in large sections under x86-64 medium code model (but not in other arches).
2+
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,X8664
3+
; RUN: opt %s -mtriple=ppc64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,PPC
4+
5+
@__profn_foo = private constant [3 x i8] c"foo"
6+
7+
define i32 @foo(ptr) {
8+
call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
9+
%2 = ptrtoint ptr %0 to i64
10+
call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
11+
%3 = tail call i32 %0()
12+
ret i32 %3
13+
}
14+
15+
; Function Attrs: nounwind
16+
declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
17+
18+
; Function Attrs: nounwind
19+
declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
20+
21+
attributes #0 = { nounwind }
22+
23+
!llvm.module.flags = !{!0}
24+
25+
!0 = !{i32 1, !"Code Model", i32 3}
26+
27+
; CHECK: @__profc_foo =
28+
; CHECK-NOT: code_model "large"
29+
; CHECK: @__profvp_foo =
30+
; X8664-SAME: code_model "large"
31+
; PPC-NOT: code_model "large"
32+
; CHECK: @__profd_foo =
33+
; CHECK-NOT: code_model "large"
34+
; CHECK: @__llvm_prf_vnodes =
35+
; X8664-SAME: code_model "large"
36+
; PPC-NOT: code_model "large"
37+
; CHECK: @__llvm_prf_nm =
38+
; X8664-SAME: code_model "large"
39+
; PPC-NOT: code_model "large"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; Check that globals are not marked large under x86-64 small code model.
2+
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s
3+
4+
@__profn_foo = private constant [3 x i8] c"foo"
5+
6+
define i32 @foo(ptr) {
7+
call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
8+
%2 = ptrtoint ptr %0 to i64
9+
call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
10+
%3 = tail call i32 %0()
11+
ret i32 %3
12+
}
13+
14+
; Function Attrs: nounwind
15+
declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
16+
17+
; Function Attrs: nounwind
18+
declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
19+
20+
attributes #0 = { nounwind }
21+
22+
!llvm.module.flags = !{!0}
23+
24+
!0 = !{i32 1, !"Code Model", i32 1}
25+
26+
; CHECK: @__profc_foo =
27+
; CHECK-NOT: code_model "large"
28+
; CHECK: @__profvp_foo =
29+
; CHECK-NOT: code_model "large"
30+
; CHECK: @__profd_foo =
31+
; CHECK-NOT: code_model "large"
32+
; CHECK: @__llvm_prf_vnodes =
33+
; CHECK-NOT: code_model "large"
34+
; CHECK: @__llvm_prf_nm =
35+
; CHECK-NOT: code_model "large"

0 commit comments

Comments
 (0)