diff --git a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp index 7a03ee46d6fde..b842d9eef407c 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 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 0000000000000..cb42f15995121 --- /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" + +@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 0000000000000..c034cad6d15da --- /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" + +@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 c1e7694d4cd53..0000000000000 --- 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" - -@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 9fbff456ff50b..294f0f27b9230 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 0000000000000..5ebce67bab86a --- /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 + +@__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 0000000000000..0b269a87a6444 --- /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 + +@__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 0000000000000..11cc187512951 --- /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 + +@__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"