diff options
| author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-03-28 17:53:22 +0000 |
|---|---|---|
| committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-03-28 17:53:22 +0000 |
| commit | 97140c9270fdadc8af2c6e0a8ff06f090ec7eaa7 (patch) | |
| tree | 6a288c7cb276c4761e61caa95dfbf6919dd2f550 /lib/CodeGen/CodeGenPGO.cpp | |
| parent | d12127df41ba85d7bbf46219c8c0f665d7a12ace (diff) | |
| download | clang-97140c9270fdadc8af2c6e0a8ff06f090ec7eaa7.tar.gz clang-97140c9270fdadc8af2c6e0a8ff06f090ec7eaa7.tar.bz2 clang-97140c9270fdadc8af2c6e0a8ff06f090ec7eaa7.tar.xz | |
InstrProf: Emit runtime hook directly in IRGen
-u behaviour is apparently not portable between linkers (see cfe-commits
discussions for r204379 and r205012). I've moved the logic to IRGen,
where it should have been in the first place.
I don't have a Linux system to test this on, so it's possible this logic
*still* doesn't pull in the instrumented profiling runtime on Linux.
I'm in the process of getting tests going on the compiler-rt side
(llvm-commits "[PATCH] InstrProf: Add initial compiler-rt test"). Once
we have tests for the full flow there, the runtime logic should get a
whole lot less brittle.
<rdar://problem/16458307>
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205023 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenPGO.cpp')
| -rw-r--r-- | lib/CodeGen/CodeGenPGO.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/CodeGen/CodeGenPGO.cpp b/lib/CodeGen/CodeGenPGO.cpp index 7b45650572..ef490ff679 100644 --- a/lib/CodeGen/CodeGenPGO.cpp +++ b/lib/CodeGen/CodeGenPGO.cpp @@ -814,6 +814,35 @@ namespace { }; } +static void emitRuntimeHook(CodeGenModule &CGM) { + constexpr const char *RuntimeVarName = "__llvm_profile_runtime"; + constexpr const char *RuntimeUserName = "__llvm_profile_runtime_user"; + if (CGM.getModule().getGlobalVariable(RuntimeVarName)) + return; + + // Declare the runtime hook. + llvm::LLVMContext &Ctx = CGM.getLLVMContext(); + auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); + auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false, + llvm::GlobalValue::ExternalLinkage, + nullptr, RuntimeVarName); + + // Make a function that uses it. + auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false), + llvm::GlobalValue::LinkOnceODRLinkage, + RuntimeUserName, &CGM.getModule()); + User->addFnAttr(llvm::Attribute::NoInline); + if (CGM.getCodeGenOpts().DisableRedZone) + User->addFnAttr(llvm::Attribute::NoRedZone); + CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User)); + auto *Load = Builder.CreateLoad(Var); + Builder.CreateRet(Load); + + // Create a use of the function. Now the definition of the runtime variable + // should get pulled in, along with any static initializears. + CGM.addUsedGlobal(User); +} + void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; PGOProfileData *PGOData = CGM.getPGOData(); @@ -839,8 +868,10 @@ void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { } mapRegionCounters(D); - if (InstrumentRegions) + if (InstrumentRegions) { + emitRuntimeHook(CGM); emitCounterVariables(); + } if (PGOData) { loadRegionCounts(PGOData); computeRegionCounts(D); |
