[llvm] 0d94882 - [Instrumentation][X86] Limit setting large section flag to medium/large code models (#75542)

via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 09:47:03 PST 2023


Author: Arthur Eubanks
Date: 2023-12-15T09:46:59-08:00
New Revision: 0d948827d773be7e440d1e639ddf42c4d28ab461

URL: https://github.com/llvm/llvm-project/commit/0d948827d773be7e440d1e639ddf42c4d28ab461
DIFF: https://github.com/llvm/llvm-project/commit/0d948827d773be7e440d1e639ddf42c4d28ab461.diff

LOG: [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.

Added: 
    llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-medium.ll
    llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-small.ll
    llvm/test/Instrumentation/InstrProfiling/section-code-model-large.ll
    llvm/test/Instrumentation/InstrProfiling/section-code-model-medium.ll
    llvm/test/Instrumentation/InstrProfiling/section-code-model-small.ll

Modified: 
    llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
    llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll

Removed: 
    llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
index 7a03ee46d6fded..b842d9eef407c2 100644
--- a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
@@ -87,8 +87,13 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
 
 void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
                                          GlobalVariable &GV) {
-  if (TargetTriple.getArch() == Triple::x86_64 &&
-      TargetTriple.getObjectFormat() == Triple::ELF) {
-    GV.setCodeModel(CodeModel::Large);
-  }
+  // Limit to x86-64 ELF.
+  if (TargetTriple.getArch() != Triple::x86_64 ||
+      TargetTriple.getObjectFormat() != Triple::ELF)
+    return;
+  // Limit to medium/large code models.
+  std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
+  if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
+    return;
+  GV.setCodeModel(CodeModel::Large);
 }

diff  --git a/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-medium.ll b/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-medium.ll
new file mode 100644
index 00000000000000..cb42f159951214
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-medium.ll
@@ -0,0 +1,13 @@
+;; Check that asan_globals is marked large under x86-64 medium code model.
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefixes=CHECK,X8664
+; RUN: opt < %s -mtriple=ppc64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefixes=CHECK,PPC
+
+; CHECK: @__asan_global_global =
+; X8664-SAME: code_model "large"
+; PPC-NOT: code_model "large"
+
+ at global = global i32 0, align 4
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"Code Model", i32 3}
\ No newline at end of file

diff  --git a/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-small.ll b/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-small.ll
new file mode 100644
index 00000000000000..c034cad6d15da2
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-small.ll
@@ -0,0 +1,7 @@
+;; Check that asan_globals is not marked large without an explicit code model.
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s
+
+; CHECK: @__asan_global_global =
+; CHECK-NOT: code_model "large"
+
+ at global = global i32 0, align 4
\ No newline at end of file

diff  --git a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll b/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
deleted file mode 100644
index c1e7694d4cd536..00000000000000
--- a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
+++ /dev/null
@@ -1,10 +0,0 @@
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefix=LARGE
-; RUN: opt < %s -mtriple=aarch64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefix=NORMAL
-; RUN: opt < %s -mtriple=x86_64-pc-windows -passes=asan -S | FileCheck %s --check-prefix=NORMAL
-
-; check that asan globals metadata are emitted to a large section for x86-64 ELF
-
-; LARGE: @__asan_global_global = {{.*}}global {{.*}}, code_model "large"
-; NORMAL-NOT: code_model "large"
-
- at global = global i32 0, align 4

diff  --git a/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll b/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
index 9fbff456ff50b5..294f0f27b9230b 100644
--- a/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
@@ -15,10 +15,6 @@
 ; RUN: opt %s -mtriple=powerpc64-ibm-aix -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
 ; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
 
-;; Check that globals have the proper code model.
-; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-X8664
-; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-PPC
-
 @__profn_foo = private constant [3 x i8] c"foo"
 @__profn_bar = private constant [3 x i8] c"bar"
 
@@ -78,24 +74,3 @@ attributes #0 = { nounwind }
 ; ALIGN: @__profd_bar = private global {{.*}} section "__llvm_prf_data",{{.*}} align 8
 ; ALIGN: @__llvm_prf_vnodes = private global {{.*}} section "__llvm_prf_vnds",{{.*}} align 8
 ; ALIGN: @__llvm_prf_nm = private constant {{.*}} section "__llvm_prf_names",{{.*}} align 1
-
-; CODEMODEL: @__profc_foo =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__profvp_foo =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model
-; CODEMODEL: @__profd_foo =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__profc_bar =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__profvp_bar =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model
-; CODEMODEL: @__profd_bar =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__llvm_prf_vnodes =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model
-; CODEMODEL: @__llvm_prf_nm =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model

diff  --git a/llvm/test/Instrumentation/InstrProfiling/section-code-model-large.ll b/llvm/test/Instrumentation/InstrProfiling/section-code-model-large.ll
new file mode 100644
index 00000000000000..5ebce67bab86aa
--- /dev/null
+++ b/llvm/test/Instrumentation/InstrProfiling/section-code-model-large.ll
@@ -0,0 +1,35 @@
+;; Check that certain globals are in large sections under x86-64 large code model.
+; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s
+
+ at __profn_foo = private constant [3 x i8] c"foo"
+
+define i32 @foo(ptr) {
+  call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
+  %2 = ptrtoint ptr %0 to i64
+  call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
+  %3 = tail call i32 %0()
+  ret i32 %3
+}
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
+
+attributes #0 = { nounwind }
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"Code Model", i32 4}
+
+; CHECK: @__profc_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__profvp_foo =
+; CHECK-SAME: code_model "large"
+; CHECK: @__profd_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__llvm_prf_vnodes =
+; CHECK-SAME: code_model "large"
+; CHECK: @__llvm_prf_nm =
+; CHECK-SAME: code_model "large"

diff  --git a/llvm/test/Instrumentation/InstrProfiling/section-code-model-medium.ll b/llvm/test/Instrumentation/InstrProfiling/section-code-model-medium.ll
new file mode 100644
index 00000000000000..0b269a87a64448
--- /dev/null
+++ b/llvm/test/Instrumentation/InstrProfiling/section-code-model-medium.ll
@@ -0,0 +1,39 @@
+;; Check that certain globals are in large sections under x86-64 medium code model (but not in other arches).
+; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,X8664
+; RUN: opt %s -mtriple=ppc64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,PPC
+
+ at __profn_foo = private constant [3 x i8] c"foo"
+
+define i32 @foo(ptr) {
+  call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
+  %2 = ptrtoint ptr %0 to i64
+  call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
+  %3 = tail call i32 %0()
+  ret i32 %3
+}
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
+
+attributes #0 = { nounwind }
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"Code Model", i32 3}
+
+; CHECK: @__profc_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__profvp_foo =
+; X8664-SAME: code_model "large"
+; PPC-NOT: code_model "large"
+; CHECK: @__profd_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__llvm_prf_vnodes =
+; X8664-SAME: code_model "large"
+; PPC-NOT: code_model "large"
+; CHECK: @__llvm_prf_nm =
+; X8664-SAME: code_model "large"
+; PPC-NOT: code_model "large"

diff  --git a/llvm/test/Instrumentation/InstrProfiling/section-code-model-small.ll b/llvm/test/Instrumentation/InstrProfiling/section-code-model-small.ll
new file mode 100644
index 00000000000000..11cc1875129518
--- /dev/null
+++ b/llvm/test/Instrumentation/InstrProfiling/section-code-model-small.ll
@@ -0,0 +1,35 @@
+;; Check that globals are not marked large under x86-64 small code model.
+; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s
+
+ at __profn_foo = private constant [3 x i8] c"foo"
+
+define i32 @foo(ptr) {
+  call void @llvm.instrprof.increment(ptr @__profn_foo, i64 12884901887, i32 1, i32 0)
+  %2 = ptrtoint ptr %0 to i64
+  call void @llvm.instrprof.value.profile(ptr @__profn_foo, i64 12884901887, i64 %2, i32 0, i32 0)
+  %3 = tail call i32 %0()
+  ret i32 %3
+}
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #0
+
+; Function Attrs: nounwind
+declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
+
+attributes #0 = { nounwind }
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"Code Model", i32 1}
+
+; CHECK: @__profc_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__profvp_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__profd_foo =
+; CHECK-NOT: code_model "large"
+; CHECK: @__llvm_prf_vnodes =
+; CHECK-NOT: code_model "large"
+; CHECK: @__llvm_prf_nm =
+; CHECK-NOT: code_model "large"


        


More information about the llvm-commits mailing list