[llvm] [clang] [Instrumentation][X86] Limit setting large section flag to medium/large code models (PR #75542)

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 14 16:44:18 PST 2023


https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/75542

>From 884347b735555d68e2d469b3903f248e72921434 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 14 Dec 2023 15:41:31 -0800
Subject: [PATCH 1/2] [Instrumentation][X86] Limit setting large section flag
 to medium/large code models

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 is annoying API-wise because we need to pass TargetMachine around
to any pass that wants to do this. Module::getCodeModel(), like anything
else that reads Module metadata, is unreliable as a source of truth.
---
 clang/lib/CodeGen/BackendUtil.cpp             | 10 ++++-----
 .../include/llvm/Transforms/Instrumentation.h |  6 ++++--
 .../Instrumentation/AddressSanitizer.h        |  5 ++++-
 .../Instrumentation/InstrProfiling.h          |  9 +++++---
 llvm/lib/Passes/PassBuilderPipelines.cpp      |  4 ++--
 llvm/lib/Passes/PassRegistry.def              |  4 ++--
 .../Instrumentation/AddressSanitizer.cpp      | 21 +++++++++++--------
 .../Instrumentation/InstrProfiling.cpp        | 16 +++++++-------
 .../Instrumentation/Instrumentation.cpp       | 19 ++++++++++++-----
 .../global_metadata_code_model.ll             |  4 +++-
 .../InstrProfiling/icall-comdat.ll            | 21 ++++++++++---------
 11 files changed, 72 insertions(+), 47 deletions(-)

diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 7d16de33763a0d..49de29e6580e3f 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -635,7 +635,7 @@ static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
       });
 }
 
-static void addSanitizers(const Triple &TargetTriple,
+static void addSanitizers(const TargetMachine *TM, const Triple &TargetTriple,
                           const CodeGenOptions &CodeGenOpts,
                           const LangOptions &LangOpts, PassBuilder &PB) {
   auto SanitizersCallback = [&](ModulePassManager &MPM,
@@ -696,7 +696,7 @@ static void addSanitizers(const Triple &TargetTriple,
         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
-        MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
+        MPM.addPass(AddressSanitizerPass(TM, Opts, UseGlobalGC, UseOdrIndicator,
                                          DestructorKind));
       }
     };
@@ -973,7 +973,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
     // done on PreLink stage.
     if (!IsThinLTOPostLink) {
-      addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
+      addSanitizers(TM.get(), TargetTriple, CodeGenOpts, LangOpts, PB);
       addKCFIPass(TargetTriple, LangOpts, PB);
     }
 
@@ -986,8 +986,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
     if (std::optional<InstrProfOptions> Options =
             getInstrProfOptions(CodeGenOpts, LangOpts))
       PB.registerPipelineStartEPCallback(
-          [Options](ModulePassManager &MPM, OptimizationLevel Level) {
-            MPM.addPass(InstrProfilingLoweringPass(*Options, false));
+          [this, Options](ModulePassManager &MPM, OptimizationLevel Level) {
+            MPM.addPass(InstrProfilingLoweringPass(TM.get(), *Options, false));
           });
 
     // TODO: Consider passing the MemoryProfileOutput to the pass builder via
diff --git a/llvm/include/llvm/Transforms/Instrumentation.h b/llvm/include/llvm/Transforms/Instrumentation.h
index ea97ab2562a5b0..a5866445529bfe 100644
--- a/llvm/include/llvm/Transforms/Instrumentation.h
+++ b/llvm/include/llvm/Transforms/Instrumentation.h
@@ -27,6 +27,7 @@
 namespace llvm {
 
 class Triple;
+class TargetMachine;
 class OptimizationRemarkEmitter;
 class Comdat;
 class CallBase;
@@ -52,8 +53,9 @@ Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
 // Place global in a large section for x86-64 ELF binaries to mitigate
 // relocation overflow pressure. This can be be used for metadata globals that
 // aren't directly accessed by code, which has no performance impact.
-void setGlobalVariableLargeSection(const Triple &TargetTriple,
-                                   GlobalVariable &GV);
+void setGlobalVariableLargeSection(GlobalVariable &GV,
+                                   const Triple &TargetTriple,
+                                   const TargetMachine *TM);
 
 // Insert GCOV profiling instrumentation
 struct GCOVOptions {
diff --git a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
index 6dfdfb729cf502..ae5eae604a245e 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
@@ -19,6 +19,7 @@
 namespace llvm {
 class Module;
 class raw_ostream;
+class TargetMachine;
 
 struct AddressSanitizerOptions {
   bool CompileKernel = false;
@@ -38,7 +39,8 @@ struct AddressSanitizerOptions {
 /// run intependently of the function address sanitizer.
 class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
 public:
-  AddressSanitizerPass(const AddressSanitizerOptions &Options,
+  AddressSanitizerPass(const TargetMachine *TM,
+                       const AddressSanitizerOptions &Options,
                        bool UseGlobalGC = true, bool UseOdrIndicator = true,
                        AsanDtorKind DestructorKind = AsanDtorKind::Global,
                        AsanCtorKind ConstructorKind = AsanCtorKind::Global);
@@ -48,6 +50,7 @@ class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
   static bool isRequired() { return true; }
 
 private:
+  const TargetMachine *TM;
   AddressSanitizerOptions Options;
   bool UseGlobalGC;
   bool UseOdrIndicator;
diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
index 0dd37c9ca58b7e..95187e55645261 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
@@ -24,14 +24,17 @@ class TargetLibraryInfo;
 /// instrumentation pass.
 class InstrProfilingLoweringPass
     : public PassInfoMixin<InstrProfilingLoweringPass> {
+  const TargetMachine *TM;
   const InstrProfOptions Options = {};
   // Is this lowering for the context-sensitive instrumentation.
   const bool IsCS = false;
 
 public:
-  InstrProfilingLoweringPass() = default;
-  InstrProfilingLoweringPass(const InstrProfOptions &Options, bool IsCS = false)
-      : Options(Options), IsCS(IsCS) {}
+  InstrProfilingLoweringPass(const TargetMachine *TM)
+      : TM(TM), Options(), IsCS() {}
+  InstrProfilingLoweringPass(const TargetMachine *TM,
+                             const InstrProfOptions &Options, bool IsCS = false)
+      : TM(TM), Options(Options), IsCS(IsCS) {}
 
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 };
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 5c6c391049a7b2..9958d53f87065b 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -810,7 +810,7 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
   Options.DoCounterPromotion = true;
   Options.UseBFIInPromotion = IsCS;
   Options.Atomic = AtomicCounterUpdate;
-  MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
+  MPM.addPass(InstrProfilingLoweringPass(TM, Options, IsCS));
 }
 
 void PassBuilder::addPGOInstrPassesForO0(
@@ -837,7 +837,7 @@ void PassBuilder::addPGOInstrPassesForO0(
   Options.DoCounterPromotion = false;
   Options.UseBFIInPromotion = IsCS;
   Options.Atomic = AtomicCounterUpdate;
-  MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
+  MPM.addPass(InstrProfilingLoweringPass(TM, Options, IsCS));
 }
 
 static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) {
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index d8fc7cd8a231ff..3d50f6da322561 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -78,7 +78,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
             ModuleInlinerWrapperPass(getInlineParams(), false))
 MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
 MODULE_PASS("instrorderfile", InstrOrderFilePass())
-MODULE_PASS("instrprof", InstrProfilingLoweringPass())
+MODULE_PASS("instrprof", InstrProfilingLoweringPass(TM))
 MODULE_PASS("internalize", InternalizePass())
 MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
 MODULE_PASS("iroutliner", IROutlinerPass())
@@ -147,7 +147,7 @@ MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
 #endif
 MODULE_PASS_WITH_PARAMS(
     "asan", "AddressSanitizerPass",
-    [](AddressSanitizerOptions Opts) { return AddressSanitizerPass(Opts); },
+    [this](AddressSanitizerOptions Opts) { return AddressSanitizerPass(TM, Opts); },
     parseASanPassOptions, "kernel")
 MODULE_PASS_WITH_PARAMS(
     "globaldce", "GlobalDCEPass",
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index b175e6f93f3e8f..69637031452f1c 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -787,12 +787,14 @@ struct AddressSanitizer {
 
 class ModuleAddressSanitizer {
 public:
-  ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
-                         bool CompileKernel = false, bool Recover = false,
-                         bool UseGlobalsGC = true, bool UseOdrIndicator = true,
+  ModuleAddressSanitizer(Module &M, const TargetMachine *TM,
+                         bool InsertVersionCheck, bool CompileKernel = false,
+                         bool Recover = false, bool UseGlobalsGC = true,
+                         bool UseOdrIndicator = true,
                          AsanDtorKind DestructorKind = AsanDtorKind::Global,
                          AsanCtorKind ConstructorKind = AsanCtorKind::Global)
-      : CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
+      : TM(TM),
+        CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
                                                             : CompileKernel),
         InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
                                ? ClInsertVersionCheck
@@ -869,6 +871,7 @@ class ModuleAddressSanitizer {
   uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
   int GetAsanVersion(const Module &M) const;
 
+  const TargetMachine *TM;
   bool CompileKernel;
   bool InsertVersionCheck;
   bool Recover;
@@ -1161,17 +1164,17 @@ void AddressSanitizerPass::printPipeline(
 }
 
 AddressSanitizerPass::AddressSanitizerPass(
-    const AddressSanitizerOptions &Options, bool UseGlobalGC,
-    bool UseOdrIndicator, AsanDtorKind DestructorKind,
+    const TargetMachine *TM, const AddressSanitizerOptions &Options,
+    bool UseGlobalGC, bool UseOdrIndicator, AsanDtorKind DestructorKind,
     AsanCtorKind ConstructorKind)
-    : Options(Options), UseGlobalGC(UseGlobalGC),
+    : TM(TM), Options(Options), UseGlobalGC(UseGlobalGC),
       UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind),
       ConstructorKind(ConstructorKind) {}
 
 PreservedAnalyses AddressSanitizerPass::run(Module &M,
                                             ModuleAnalysisManager &MAM) {
   ModuleAddressSanitizer ModuleSanitizer(
-      M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
+      M, TM, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
       UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
   bool Modified = false;
   auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
@@ -2146,7 +2149,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
   Metadata->setSection(getGlobalMetadataSection());
   // Place metadata in a large section for x86-64 ELF binaries to mitigate
   // relocation pressure.
-  setGlobalVariableLargeSection(TargetTriple, *Metadata);
+  setGlobalVariableLargeSection(*Metadata, TargetTriple, TM);
   return Metadata;
 }
 
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index fe5a0578bd9721..b9345273f88135 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -170,16 +170,18 @@ using LoadStorePair = std::pair<Instruction *, Instruction *>;
 
 class InstrLowerer final {
 public:
-  InstrLowerer(Module &M, const InstrProfOptions &Options,
+  InstrLowerer(Module &M, const TargetMachine *TM,
+               const InstrProfOptions &Options,
                std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
                bool IsCS)
-      : M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
-        GetTLI(GetTLI) {}
+      : M(M), TM(TM), Options(Options), TT(Triple(M.getTargetTriple())),
+        IsCS(IsCS), GetTLI(GetTLI) {}
 
   bool lower();
 
 private:
   Module &M;
+  const TargetMachine *TM;
   const InstrProfOptions Options;
   const Triple TT;
   // Is this lowering for the context-sensitive instrumentation.
@@ -577,7 +579,7 @@ PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
     return FAM.getResult<TargetLibraryAnalysis>(F);
   };
-  InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
+  InstrLowerer Lowerer(M, TM, Options, GetTLI, IsCS);
   if (!Lowerer.lower())
     return PreservedAnalyses::all();
 
@@ -1452,7 +1454,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
         M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
         getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
     ValuesVar->setVisibility(Visibility);
-    setGlobalVariableLargeSection(TT, *ValuesVar);
+    setGlobalVariableLargeSection(*ValuesVar, TT, TM);
     ValuesVar->setSection(
         getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
     ValuesVar->setAlignment(Align(8));
@@ -1590,7 +1592,7 @@ void InstrLowerer::emitVNodes() {
   auto *VNodesVar = new GlobalVariable(
       M, VNodesTy, false, GlobalValue::PrivateLinkage,
       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
-  setGlobalVariableLargeSection(TT, *VNodesVar);
+  setGlobalVariableLargeSection(*VNodesVar, TT, TM);
   VNodesVar->setSection(
       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
   VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
@@ -1618,7 +1620,7 @@ void InstrLowerer::emitNameData() {
                                 GlobalValue::PrivateLinkage, NamesVal,
                                 getInstrProfNamesVarName());
   NamesSize = CompressedNameStr.size();
-  setGlobalVariableLargeSection(TT, *NamesVar);
+  setGlobalVariableLargeSection(*NamesVar, TT, TM);
   NamesVar->setSection(
       ProfileCorrelate == InstrProfCorrelator::BINARY
           ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
diff --git a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
index 7a03ee46d6fded..f25ac521d275c8 100644
--- a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Transforms/Instrumentation.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Target/TargetMachine.h"
 #include "llvm/TargetParser/Triple.h"
 
 using namespace llvm;
@@ -85,10 +86,18 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
   return C;
 }
 
-void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
-                                         GlobalVariable &GV) {
-  if (TargetTriple.getArch() == Triple::x86_64 &&
-      TargetTriple.getObjectFormat() == Triple::ELF) {
-    GV.setCodeModel(CodeModel::Large);
+void llvm::setGlobalVariableLargeSection(GlobalVariable &GV,
+                                         const Triple &TargetTriple,
+                                         const TargetMachine *TM) {
+  // Limit to x86-64 ELF.
+  if (TargetTriple.getArch() != Triple::x86_64 ||
+      TargetTriple.getObjectFormat() != Triple::ELF) {
+    return;
   }
+  // Limit to medium/large code models.
+  if (!TM || (TM->getCodeModel() != CodeModel::Medium &&
+              TM->getCodeModel() != CodeModel::Large)) {
+    return;
+  }
+  GV.setCodeModel(CodeModel::Large);
 }
diff --git a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll b/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
index c1e7694d4cd536..8667edc2eb6c6a 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
@@ -1,4 +1,6 @@
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S | FileCheck %s --check-prefix=LARGE
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=large | FileCheck %s --check-prefix=LARGE
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=medium | FileCheck %s --check-prefix=LARGE
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=small | FileCheck %s --check-prefix=NORMAL
 ; 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
 
diff --git a/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll b/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
index 9fbff456ff50b5..6e9bdd50443da4 100644
--- a/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
@@ -16,8 +16,9 @@
 ; 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
+; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-NORMAL
+; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S -code-model=medium | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-LARGE
+; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-NORMAL
 
 @__profn_foo = private constant [3 x i8] c"foo"
 @__profn_bar = private constant [3 x i8] c"bar"
@@ -82,20 +83,20 @@ attributes #0 = { nounwind }
 ; CODEMODEL: @__profc_foo =
 ; CODEMODEL-NOT: code_model "large"
 ; CODEMODEL: @__profvp_foo =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model
+; CODEMODEL-LARGE-SAME: code_model "large"
+; CODEMODEL-NORMAL-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-LARGE-SAME: code_model "large"
+; CODEMODEL-NORMAL-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-LARGE-SAME: code_model "large"
+; CODEMODEL-NORMAL-NOT: code_model
 ; CODEMODEL: @__llvm_prf_nm =
-; CODEMODEL-X8664-SAME: code_model "large"
-; CODEMODEL-PPC-NOT: code_model
+; CODEMODEL-LARGE-SAME: code_model "large"
+; CODEMODEL-NORMAL-NOT: code_model

>From abdfa15c67940e961cd170121469db6695ce60d2 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Thu, 14 Dec 2023 16:43:54 -0800
Subject: [PATCH 2/2] use Module::getCodeModel() instead

---
 clang/lib/CodeGen/BackendUtil.cpp             | 10 ++---
 .../include/llvm/Transforms/Instrumentation.h |  6 +--
 .../Instrumentation/AddressSanitizer.h        |  5 +--
 .../Instrumentation/InstrProfiling.h          |  9 ++---
 llvm/lib/Passes/PassBuilderPipelines.cpp      |  4 +-
 llvm/lib/Passes/PassRegistry.def              |  4 +-
 .../Instrumentation/AddressSanitizer.cpp      | 21 +++++-----
 .../Instrumentation/InstrProfiling.cpp        | 16 ++++----
 .../Instrumentation/Instrumentation.cpp       | 14 +++----
 .../global-metadata-code-model-medium.ll      | 13 +++++++
 .../global-metadata-code-model-small.ll       |  7 ++++
 .../global_metadata_code_model.ll             | 12 ------
 .../InstrProfiling/icall-comdat.ll            | 26 -------------
 .../section-code-model-large.ll               | 35 +++++++++++++++++
 .../section-code-model-medium.ll              | 39 +++++++++++++++++++
 .../section-code-model-small.ll               | 35 +++++++++++++++++
 16 files changed, 165 insertions(+), 91 deletions(-)
 create mode 100644 llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-medium.ll
 create mode 100644 llvm/test/Instrumentation/AddressSanitizer/global-metadata-code-model-small.ll
 delete mode 100644 llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
 create mode 100644 llvm/test/Instrumentation/InstrProfiling/section-code-model-large.ll
 create mode 100644 llvm/test/Instrumentation/InstrProfiling/section-code-model-medium.ll
 create mode 100644 llvm/test/Instrumentation/InstrProfiling/section-code-model-small.ll

diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 49de29e6580e3f..7d16de33763a0d 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -635,7 +635,7 @@ static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
       });
 }
 
-static void addSanitizers(const TargetMachine *TM, const Triple &TargetTriple,
+static void addSanitizers(const Triple &TargetTriple,
                           const CodeGenOptions &CodeGenOpts,
                           const LangOptions &LangOpts, PassBuilder &PB) {
   auto SanitizersCallback = [&](ModulePassManager &MPM,
@@ -696,7 +696,7 @@ static void addSanitizers(const TargetMachine *TM, const Triple &TargetTriple,
         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
-        MPM.addPass(AddressSanitizerPass(TM, Opts, UseGlobalGC, UseOdrIndicator,
+        MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
                                          DestructorKind));
       }
     };
@@ -973,7 +973,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
     // done on PreLink stage.
     if (!IsThinLTOPostLink) {
-      addSanitizers(TM.get(), TargetTriple, CodeGenOpts, LangOpts, PB);
+      addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
       addKCFIPass(TargetTriple, LangOpts, PB);
     }
 
@@ -986,8 +986,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
     if (std::optional<InstrProfOptions> Options =
             getInstrProfOptions(CodeGenOpts, LangOpts))
       PB.registerPipelineStartEPCallback(
-          [this, Options](ModulePassManager &MPM, OptimizationLevel Level) {
-            MPM.addPass(InstrProfilingLoweringPass(TM.get(), *Options, false));
+          [Options](ModulePassManager &MPM, OptimizationLevel Level) {
+            MPM.addPass(InstrProfilingLoweringPass(*Options, false));
           });
 
     // TODO: Consider passing the MemoryProfileOutput to the pass builder via
diff --git a/llvm/include/llvm/Transforms/Instrumentation.h b/llvm/include/llvm/Transforms/Instrumentation.h
index a5866445529bfe..ea97ab2562a5b0 100644
--- a/llvm/include/llvm/Transforms/Instrumentation.h
+++ b/llvm/include/llvm/Transforms/Instrumentation.h
@@ -27,7 +27,6 @@
 namespace llvm {
 
 class Triple;
-class TargetMachine;
 class OptimizationRemarkEmitter;
 class Comdat;
 class CallBase;
@@ -53,9 +52,8 @@ Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
 // Place global in a large section for x86-64 ELF binaries to mitigate
 // relocation overflow pressure. This can be be used for metadata globals that
 // aren't directly accessed by code, which has no performance impact.
-void setGlobalVariableLargeSection(GlobalVariable &GV,
-                                   const Triple &TargetTriple,
-                                   const TargetMachine *TM);
+void setGlobalVariableLargeSection(const Triple &TargetTriple,
+                                   GlobalVariable &GV);
 
 // Insert GCOV profiling instrumentation
 struct GCOVOptions {
diff --git a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
index ae5eae604a245e..6dfdfb729cf502 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
@@ -19,7 +19,6 @@
 namespace llvm {
 class Module;
 class raw_ostream;
-class TargetMachine;
 
 struct AddressSanitizerOptions {
   bool CompileKernel = false;
@@ -39,8 +38,7 @@ struct AddressSanitizerOptions {
 /// run intependently of the function address sanitizer.
 class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
 public:
-  AddressSanitizerPass(const TargetMachine *TM,
-                       const AddressSanitizerOptions &Options,
+  AddressSanitizerPass(const AddressSanitizerOptions &Options,
                        bool UseGlobalGC = true, bool UseOdrIndicator = true,
                        AsanDtorKind DestructorKind = AsanDtorKind::Global,
                        AsanCtorKind ConstructorKind = AsanCtorKind::Global);
@@ -50,7 +48,6 @@ class AddressSanitizerPass : public PassInfoMixin<AddressSanitizerPass> {
   static bool isRequired() { return true; }
 
 private:
-  const TargetMachine *TM;
   AddressSanitizerOptions Options;
   bool UseGlobalGC;
   bool UseOdrIndicator;
diff --git a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
index 95187e55645261..0dd37c9ca58b7e 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
@@ -24,17 +24,14 @@ class TargetLibraryInfo;
 /// instrumentation pass.
 class InstrProfilingLoweringPass
     : public PassInfoMixin<InstrProfilingLoweringPass> {
-  const TargetMachine *TM;
   const InstrProfOptions Options = {};
   // Is this lowering for the context-sensitive instrumentation.
   const bool IsCS = false;
 
 public:
-  InstrProfilingLoweringPass(const TargetMachine *TM)
-      : TM(TM), Options(), IsCS() {}
-  InstrProfilingLoweringPass(const TargetMachine *TM,
-                             const InstrProfOptions &Options, bool IsCS = false)
-      : TM(TM), Options(Options), IsCS(IsCS) {}
+  InstrProfilingLoweringPass() = default;
+  InstrProfilingLoweringPass(const InstrProfOptions &Options, bool IsCS = false)
+      : Options(Options), IsCS(IsCS) {}
 
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 };
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 9958d53f87065b..5c6c391049a7b2 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -810,7 +810,7 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
   Options.DoCounterPromotion = true;
   Options.UseBFIInPromotion = IsCS;
   Options.Atomic = AtomicCounterUpdate;
-  MPM.addPass(InstrProfilingLoweringPass(TM, Options, IsCS));
+  MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
 }
 
 void PassBuilder::addPGOInstrPassesForO0(
@@ -837,7 +837,7 @@ void PassBuilder::addPGOInstrPassesForO0(
   Options.DoCounterPromotion = false;
   Options.UseBFIInPromotion = IsCS;
   Options.Atomic = AtomicCounterUpdate;
-  MPM.addPass(InstrProfilingLoweringPass(TM, Options, IsCS));
+  MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
 }
 
 static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) {
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 3d50f6da322561..d8fc7cd8a231ff 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -78,7 +78,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
             ModuleInlinerWrapperPass(getInlineParams(), false))
 MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
 MODULE_PASS("instrorderfile", InstrOrderFilePass())
-MODULE_PASS("instrprof", InstrProfilingLoweringPass(TM))
+MODULE_PASS("instrprof", InstrProfilingLoweringPass())
 MODULE_PASS("internalize", InternalizePass())
 MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
 MODULE_PASS("iroutliner", IROutlinerPass())
@@ -147,7 +147,7 @@ MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
 #endif
 MODULE_PASS_WITH_PARAMS(
     "asan", "AddressSanitizerPass",
-    [this](AddressSanitizerOptions Opts) { return AddressSanitizerPass(TM, Opts); },
+    [](AddressSanitizerOptions Opts) { return AddressSanitizerPass(Opts); },
     parseASanPassOptions, "kernel")
 MODULE_PASS_WITH_PARAMS(
     "globaldce", "GlobalDCEPass",
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 69637031452f1c..b175e6f93f3e8f 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -787,14 +787,12 @@ struct AddressSanitizer {
 
 class ModuleAddressSanitizer {
 public:
-  ModuleAddressSanitizer(Module &M, const TargetMachine *TM,
-                         bool InsertVersionCheck, bool CompileKernel = false,
-                         bool Recover = false, bool UseGlobalsGC = true,
-                         bool UseOdrIndicator = true,
+  ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
+                         bool CompileKernel = false, bool Recover = false,
+                         bool UseGlobalsGC = true, bool UseOdrIndicator = true,
                          AsanDtorKind DestructorKind = AsanDtorKind::Global,
                          AsanCtorKind ConstructorKind = AsanCtorKind::Global)
-      : TM(TM),
-        CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
+      : CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
                                                             : CompileKernel),
         InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
                                ? ClInsertVersionCheck
@@ -871,7 +869,6 @@ class ModuleAddressSanitizer {
   uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
   int GetAsanVersion(const Module &M) const;
 
-  const TargetMachine *TM;
   bool CompileKernel;
   bool InsertVersionCheck;
   bool Recover;
@@ -1164,17 +1161,17 @@ void AddressSanitizerPass::printPipeline(
 }
 
 AddressSanitizerPass::AddressSanitizerPass(
-    const TargetMachine *TM, const AddressSanitizerOptions &Options,
-    bool UseGlobalGC, bool UseOdrIndicator, AsanDtorKind DestructorKind,
+    const AddressSanitizerOptions &Options, bool UseGlobalGC,
+    bool UseOdrIndicator, AsanDtorKind DestructorKind,
     AsanCtorKind ConstructorKind)
-    : TM(TM), Options(Options), UseGlobalGC(UseGlobalGC),
+    : Options(Options), UseGlobalGC(UseGlobalGC),
       UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind),
       ConstructorKind(ConstructorKind) {}
 
 PreservedAnalyses AddressSanitizerPass::run(Module &M,
                                             ModuleAnalysisManager &MAM) {
   ModuleAddressSanitizer ModuleSanitizer(
-      M, TM, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
+      M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
       UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
   bool Modified = false;
   auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
@@ -2149,7 +2146,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
   Metadata->setSection(getGlobalMetadataSection());
   // Place metadata in a large section for x86-64 ELF binaries to mitigate
   // relocation pressure.
-  setGlobalVariableLargeSection(*Metadata, TargetTriple, TM);
+  setGlobalVariableLargeSection(TargetTriple, *Metadata);
   return Metadata;
 }
 
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index b9345273f88135..fe5a0578bd9721 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -170,18 +170,16 @@ using LoadStorePair = std::pair<Instruction *, Instruction *>;
 
 class InstrLowerer final {
 public:
-  InstrLowerer(Module &M, const TargetMachine *TM,
-               const InstrProfOptions &Options,
+  InstrLowerer(Module &M, const InstrProfOptions &Options,
                std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
                bool IsCS)
-      : M(M), TM(TM), Options(Options), TT(Triple(M.getTargetTriple())),
-        IsCS(IsCS), GetTLI(GetTLI) {}
+      : M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
+        GetTLI(GetTLI) {}
 
   bool lower();
 
 private:
   Module &M;
-  const TargetMachine *TM;
   const InstrProfOptions Options;
   const Triple TT;
   // Is this lowering for the context-sensitive instrumentation.
@@ -579,7 +577,7 @@ PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
     return FAM.getResult<TargetLibraryAnalysis>(F);
   };
-  InstrLowerer Lowerer(M, TM, Options, GetTLI, IsCS);
+  InstrLowerer Lowerer(M, Options, GetTLI, IsCS);
   if (!Lowerer.lower())
     return PreservedAnalyses::all();
 
@@ -1454,7 +1452,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
         M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
         getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
     ValuesVar->setVisibility(Visibility);
-    setGlobalVariableLargeSection(*ValuesVar, TT, TM);
+    setGlobalVariableLargeSection(TT, *ValuesVar);
     ValuesVar->setSection(
         getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
     ValuesVar->setAlignment(Align(8));
@@ -1592,7 +1590,7 @@ void InstrLowerer::emitVNodes() {
   auto *VNodesVar = new GlobalVariable(
       M, VNodesTy, false, GlobalValue::PrivateLinkage,
       Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
-  setGlobalVariableLargeSection(*VNodesVar, TT, TM);
+  setGlobalVariableLargeSection(TT, *VNodesVar);
   VNodesVar->setSection(
       getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
   VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
@@ -1620,7 +1618,7 @@ void InstrLowerer::emitNameData() {
                                 GlobalValue::PrivateLinkage, NamesVal,
                                 getInstrProfNamesVarName());
   NamesSize = CompressedNameStr.size();
-  setGlobalVariableLargeSection(*NamesVar, TT, TM);
+  setGlobalVariableLargeSection(TT, *NamesVar);
   NamesVar->setSection(
       ProfileCorrelate == InstrProfCorrelator::BINARY
           ? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
diff --git a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
index f25ac521d275c8..b842d9eef407c2 100644
--- a/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
@@ -14,7 +14,6 @@
 #include "llvm/Transforms/Instrumentation.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
-#include "llvm/Target/TargetMachine.h"
 #include "llvm/TargetParser/Triple.h"
 
 using namespace llvm;
@@ -86,18 +85,15 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
   return C;
 }
 
-void llvm::setGlobalVariableLargeSection(GlobalVariable &GV,
-                                         const Triple &TargetTriple,
-                                         const TargetMachine *TM) {
+void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
+                                         GlobalVariable &GV) {
   // Limit to x86-64 ELF.
   if (TargetTriple.getArch() != Triple::x86_64 ||
-      TargetTriple.getObjectFormat() != Triple::ELF) {
+      TargetTriple.getObjectFormat() != Triple::ELF)
     return;
-  }
   // Limit to medium/large code models.
-  if (!TM || (TM->getCodeModel() != CodeModel::Medium &&
-              TM->getCodeModel() != CodeModel::Large)) {
+  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 8667edc2eb6c6a..00000000000000
--- a/llvm/test/Instrumentation/AddressSanitizer/global_metadata_code_model.ll
+++ /dev/null
@@ -1,12 +0,0 @@
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=large | FileCheck %s --check-prefix=LARGE
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=medium | FileCheck %s --check-prefix=LARGE
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes=asan -S -code-model=small | FileCheck %s --check-prefix=NORMAL
-; 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 6e9bdd50443da4..294f0f27b9230b 100644
--- a/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll
@@ -15,11 +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-NORMAL
-; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S -code-model=medium | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-LARGE
-; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-NORMAL
-
 @__profn_foo = private constant [3 x i8] c"foo"
 @__profn_bar = private constant [3 x i8] c"bar"
 
@@ -79,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-LARGE-SAME: code_model "large"
-; CODEMODEL-NORMAL-NOT: code_model
-; CODEMODEL: @__profd_foo =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__profc_bar =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__profvp_bar =
-; CODEMODEL-LARGE-SAME: code_model "large"
-; CODEMODEL-NORMAL-NOT: code_model
-; CODEMODEL: @__profd_bar =
-; CODEMODEL-NOT: code_model "large"
-; CODEMODEL: @__llvm_prf_vnodes =
-; CODEMODEL-LARGE-SAME: code_model "large"
-; CODEMODEL-NORMAL-NOT: code_model
-; CODEMODEL: @__llvm_prf_nm =
-; CODEMODEL-LARGE-SAME: code_model "large"
-; CODEMODEL-NORMAL-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..a69748d0a6e4af
--- /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 (but not in other arches).
+; 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 cfe-commits mailing list