[llvm] r280347 - Add a counter-function insertion pass
Hal Finkel via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 1 02:42:39 PDT 2016
Author: hfinkel
Date: Thu Sep 1 04:42:39 2016
New Revision: 280347
URL: http://llvm.org/viewvc/llvm-project?rev=280347&view=rev
Log:
Add a counter-function insertion pass
As discussed in https://reviews.llvm.org/D22666, our current mechanism to
support -pg profiling, where we insert calls to mcount(), or some similar
function, is fundamentally broken. We insert these calls in the frontend, which
means they get duplicated when inlining, and so the accumulated execution
counts for the inlined-into functions are wrong.
Because we don't want the presence of these functions to affect optimizaton,
they should be inserted in the backend. Here's a pass which would do just that.
The knowledge of the name of the counting function lives in the frontend, so
we're passing it here as a function attribute. Clang will be updated to use
this mechanism.
Differential Revision: https://reviews.llvm.org/D22825
Added:
llvm/trunk/lib/CodeGen/CountingFunctionInserter.cpp
llvm/trunk/test/CodeGen/PowerPC/mcount-insertion.ll
llvm/trunk/test/Transforms/CountingFunctionInserter/
llvm/trunk/test/Transforms/CountingFunctionInserter/mcount.ll
Modified:
llvm/trunk/include/llvm/CodeGen/Passes.h
llvm/trunk/include/llvm/InitializePasses.h
llvm/trunk/include/llvm/LinkAllPasses.h
llvm/trunk/lib/CodeGen/CMakeLists.txt
llvm/trunk/lib/CodeGen/CodeGen.cpp
llvm/trunk/lib/CodeGen/TargetPassConfig.cpp
llvm/trunk/tools/llc/llc.cpp
llvm/trunk/tools/opt/opt.cpp
Modified: llvm/trunk/include/llvm/CodeGen/Passes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/Passes.h?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/Passes.h (original)
+++ llvm/trunk/include/llvm/CodeGen/Passes.h Thu Sep 1 04:42:39 2016
@@ -43,6 +43,9 @@ namespace llvm {
/// the entry block.
FunctionPass *createUnreachableBlockEliminationPass();
+ /// Insert mcount-like function calls.
+ FunctionPass *createCountingFunctionInserterPass();
+
/// MachineFunctionPrinter pass - This pass prints out the machine function to
/// the given stream as a debugging tool.
MachineFunctionPass *
Modified: llvm/trunk/include/llvm/InitializePasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/include/llvm/InitializePasses.h (original)
+++ llvm/trunk/include/llvm/InitializePasses.h Thu Sep 1 04:42:39 2016
@@ -92,6 +92,7 @@ void initializeCallGraphPrinterLegacyPas
void initializeCallGraphViewerPass(PassRegistry&);
void initializeCallGraphWrapperPassPass(PassRegistry &);
void initializeCodeGenPreparePass(PassRegistry&);
+void initializeCountingFunctionInserterPass(PassRegistry&);
void initializeConstantHoistingLegacyPassPass(PassRegistry&);
void initializeConstantMergeLegacyPassPass(PassRegistry &);
void initializeConstantPropagationPass(PassRegistry&);
Modified: llvm/trunk/include/llvm/LinkAllPasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)
+++ llvm/trunk/include/llvm/LinkAllPasses.h Thu Sep 1 04:42:39 2016
@@ -160,6 +160,7 @@ namespace {
(void) llvm::createInstCountPass();
(void) llvm::createConstantHoistingPass();
(void) llvm::createCodeGenPreparePass();
+ (void) llvm::createCountingFunctionInserterPass();
(void) llvm::createEarlyCSEPass();
(void) llvm::createGVNHoistPass();
(void) llvm::createMergedLoadStoreMotionPass();
Modified: llvm/trunk/lib/CodeGen/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CMakeLists.txt?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CMakeLists.txt (original)
+++ llvm/trunk/lib/CodeGen/CMakeLists.txt Thu Sep 1 04:42:39 2016
@@ -10,6 +10,7 @@ add_llvm_library(LLVMCodeGen
CallingConvLower.cpp
CodeGen.cpp
CodeGenPrepare.cpp
+ CountingFunctionInserter.cpp
CriticalAntiDepBreaker.cpp
DeadMachineInstructionElim.cpp
DetectDeadLanes.cpp
Modified: llvm/trunk/lib/CodeGen/CodeGen.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGen.cpp?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CodeGen.cpp (original)
+++ llvm/trunk/lib/CodeGen/CodeGen.cpp Thu Sep 1 04:42:39 2016
@@ -23,6 +23,7 @@ void llvm::initializeCodeGen(PassRegistr
initializeAtomicExpandPass(Registry);
initializeBranchFolderPassPass(Registry);
initializeCodeGenPreparePass(Registry);
+ initializeCountingFunctionInserterPass(Registry);
initializeDeadMachineInstructionElimPass(Registry);
initializeDetectDeadLanesPass(Registry);
initializeDwarfEHPreparePass(Registry);
Added: llvm/trunk/lib/CodeGen/CountingFunctionInserter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CountingFunctionInserter.cpp?rev=280347&view=auto
==============================================================================
--- llvm/trunk/lib/CodeGen/CountingFunctionInserter.cpp (added)
+++ llvm/trunk/lib/CodeGen/CountingFunctionInserter.cpp Thu Sep 1 04:42:39 2016
@@ -0,0 +1,62 @@
+//===- CountingFunctionInserter.cpp - Insert mcount-like function calls ---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Insert calls to counter functions, such as mcount, intended to be called
+// once per function, at the beginning of each function.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/GlobalsModRef.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Pass.h"
+using namespace llvm;
+
+namespace {
+ struct CountingFunctionInserter : public FunctionPass {
+ static char ID; // Pass identification, replacement for typeid
+ CountingFunctionInserter() : FunctionPass(ID) {
+ initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.addPreserved<GlobalsAAWrapperPass>();
+ }
+
+ bool runOnFunction(Function &F) override {
+ std::string CountingFunctionName =
+ F.getFnAttribute("counting-function").getValueAsString();
+ if (CountingFunctionName.empty())
+ return false;
+
+ Type *VoidTy = Type::getVoidTy(F.getContext());
+ Constant *CountingFn =
+ F.getParent()->getOrInsertFunction(CountingFunctionName,
+ VoidTy, nullptr);
+ CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
+ return true;
+ }
+ };
+
+ char CountingFunctionInserter::ID = 0;
+}
+
+INITIALIZE_PASS(CountingFunctionInserter, "cfinserter",
+ "Inserts calls to mcount-like functions", false, false)
+
+//===----------------------------------------------------------------------===//
+//
+// CountingFunctionInserter - Give any unnamed non-void instructions "tmp" names.
+//
+FunctionPass *llvm::createCountingFunctionInserterPass() {
+ return new CountingFunctionInserter();
+}
Modified: llvm/trunk/lib/CodeGen/TargetPassConfig.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetPassConfig.cpp?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetPassConfig.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetPassConfig.cpp Thu Sep 1 04:42:39 2016
@@ -477,6 +477,9 @@ void TargetPassConfig::addIRPasses() {
if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
addPass(createPartiallyInlineLibCallsPass());
+
+ // Insert calls to mcount-like functions.
+ addPass(createCountingFunctionInserterPass());
}
/// Turn exception handling constructs into something the code generators can
Added: llvm/trunk/test/CodeGen/PowerPC/mcount-insertion.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/PowerPC/mcount-insertion.ll?rev=280347&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/PowerPC/mcount-insertion.ll (added)
+++ llvm/trunk/test/CodeGen/PowerPC/mcount-insertion.ll Thu Sep 1 04:42:39 2016
@@ -0,0 +1,16 @@
+; RUN: llc < %s | FileCheck %s
+target datalayout = "E-m:e-i64:64-n32:64"
+target triple = "powerpc64-bgq-linux"
+
+define void @test1() #0 {
+entry:
+ ret void
+
+; CHECK-LABEL: @test1
+; CHECK: bl mcount
+; CHECK-NOT: mcount
+; CHECK: blr
+}
+
+attributes #0 = { "counting-function"="mcount" }
+
Added: llvm/trunk/test/Transforms/CountingFunctionInserter/mcount.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/CountingFunctionInserter/mcount.ll?rev=280347&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/CountingFunctionInserter/mcount.ll (added)
+++ llvm/trunk/test/Transforms/CountingFunctionInserter/mcount.ll Thu Sep 1 04:42:39 2016
@@ -0,0 +1,27 @@
+; RUN: opt -S -cfinserter < %s | FileCheck %s
+target datalayout = "E-m:e-i64:64-n32:64"
+target triple = "powerpc64-bgq-linux"
+
+define void @test1() #0 {
+entry:
+ ret void
+
+; CHECK-LABEL: define void @test1()
+; CHECK: entry:
+; CHECK-NEXT: call void @mcount()
+; CHECK: ret void
+}
+
+define void @test2() #1 {
+entry:
+ ret void
+
+; CHECK-LABEL: define void @test2()
+; CHECK: entry:
+; CHECK-NEXT: call void @.mcount()
+; CHECK: ret void
+}
+
+attributes #0 = { "counting-function"="mcount" }
+attributes #1 = { "counting-function"=".mcount" }
+
Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Thu Sep 1 04:42:39 2016
@@ -254,6 +254,7 @@ int main(int argc, char **argv) {
initializeCodeGen(*Registry);
initializeLoopStrengthReducePass(*Registry);
initializeLowerIntrinsicsPass(*Registry);
+ initializeCountingFunctionInserterPass(*Registry);
initializeUnreachableBlockElimLegacyPassPass(*Registry);
// Register the target printer for --version.
Modified: llvm/trunk/tools/opt/opt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=280347&r1=280346&r2=280347&view=diff
==============================================================================
--- llvm/trunk/tools/opt/opt.cpp (original)
+++ llvm/trunk/tools/opt/opt.cpp Thu Sep 1 04:42:39 2016
@@ -384,6 +384,7 @@ int main(int argc, char **argv) {
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
initializeGlobalMergePass(Registry);
initializeInterleavedAccessPass(Registry);
+ initializeCountingFunctionInserterPass(Registry);
initializeUnreachableBlockElimLegacyPassPass(Registry);
#ifdef LINK_POLLY_INTO_TOOLS
More information about the llvm-commits
mailing list