From d38392ecd619f983baef5082e9e941904703d556 Mon Sep 17 00:00:00 2001 From: Xinliang David Li Date: Fri, 27 May 2016 23:20:16 +0000 Subject: [PATCH] [PM] Port the Sample FDO to new PM (part-2) llvm-svn: 271072 --- llvm/include/llvm/Transforms/SampleProfile.h | 27 +++++++++++++++++++ llvm/lib/Passes/PassBuilder.cpp | 1 + llvm/lib/Passes/PassRegistry.def | 1 + llvm/lib/Transforms/IPO/SampleProfile.cpp | 22 +++++++++++---- llvm/test/Transforms/SampleProfile/branch.ll | 1 + llvm/test/Transforms/SampleProfile/calls.ll | 1 + .../SampleProfile/cov-zero-samples.ll | 1 + .../SampleProfile/coverage-warning.ll | 1 + .../Transforms/SampleProfile/discriminator.ll | 1 + .../Transforms/SampleProfile/entry_counts.ll | 1 + llvm/test/Transforms/SampleProfile/fnptr.ll | 3 +++ .../Transforms/SampleProfile/gcc-simple.ll | 1 + .../SampleProfile/inline-combine.ll | 1 + .../SampleProfile/inline-coverage.ll | 1 + .../Transforms/SampleProfile/inline-hint.ll | 1 + llvm/test/Transforms/SampleProfile/inline.ll | 1 + .../Transforms/SampleProfile/nolocinfo.ll | 1 + llvm/test/Transforms/SampleProfile/offset.ll | 1 + .../Transforms/SampleProfile/propagate.ll | 1 + llvm/test/Transforms/SampleProfile/remarks.ll | 1 + llvm/test/Transforms/SampleProfile/syntax.ll | 9 +++++++ 21 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 llvm/include/llvm/Transforms/SampleProfile.h diff --git a/llvm/include/llvm/Transforms/SampleProfile.h b/llvm/include/llvm/Transforms/SampleProfile.h new file mode 100644 index 000000000000..aa065523ecb9 --- /dev/null +++ b/llvm/include/llvm/Transforms/SampleProfile.h @@ -0,0 +1,27 @@ +//===- Transforms/SampleProfile.h - SamplePGO pass--------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// \file +/// This file provides the interface for the sampled PGO loader pass. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_SAMPLEPROFILE_H +#define LLVM_TRANSFORMS_SAMPLEPROFILE_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +/// The instrumentation (profile-instr-gen) pass for IR based PGO. +class SampleProfileLoaderPass : public PassInfoMixin { +public: + PreservedAnalyses run(Module &M, AnalysisManager &AM); +}; + +} // End llvm namespace +#endif diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index 80cd918c2cce..fdf8c6982d58 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -62,6 +62,7 @@ #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/InstrProfiling.h" #include "llvm/Transforms/PGOInstrumentation.h" +#include "llvm/Transforms/SampleProfile.h" #include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/BDCE.h" #include "llvm/Transforms/Scalar/DCE.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index 10bbbae6b47c..9aaf17779da6 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -53,6 +53,7 @@ MODULE_PASS("pgo-instr-use", PGOInstrumentationUse()) MODULE_PASS("print", PrintModulePass(dbgs())) MODULE_PASS("print-callgraph", CallGraphPrinterPass(dbgs())) MODULE_PASS("print-lcg", LazyCallGraphPrinterPass(dbgs())) +MODULE_PASS("sample-profile", SampleProfileLoaderPass()) MODULE_PASS("strip-dead-prototypes", StripDeadPrototypesPass()) MODULE_PASS("verify", VerifierPass()) #undef MODULE_PASS diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index 6c0ce76be6dd..68310687f828 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -22,6 +22,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/SampleProfile.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallSet.h" @@ -34,8 +35,8 @@ #include "llvm/IR/Dominators.h" #include "llvm/IR/Function.h" #include "llvm/IR/InstIterator.h" -#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Metadata.h" @@ -1225,10 +1226,8 @@ bool SampleProfileLoader::emitAnnotations(Function &F) { } char SampleProfileLoaderLegacyPass::ID = 0; -INITIALIZE_PASS_BEGIN(SampleProfileLoaderLegacyPass, "sample-profile", - "Sample Profile loader", false, false) -INITIALIZE_PASS_END(SampleProfileLoaderLegacyPass, "sample-profile", - "Sample Profile loader", false, false) +INITIALIZE_PASS(SampleProfileLoaderLegacyPass, "sample-profile", + "Sample Profile loader", false, false) bool SampleProfileLoader::doInitialization(Module &M) { auto &Ctx = M.getContext(); @@ -1279,3 +1278,16 @@ bool SampleProfileLoader::runOnFunction(Function &F) { return emitAnnotations(F); return false; } + +PreservedAnalyses SampleProfileLoaderPass::run(Module &M, + AnalysisManager &AM) { + + SampleProfileLoader SampleLoader(SampleProfileFile); + + SampleLoader.doInitialization(M); + + if (!SampleLoader.runOnModule(M)) + return PreservedAnalyses::all(); + + return PreservedAnalyses::none(); +} diff --git a/llvm/test/Transforms/SampleProfile/branch.ll b/llvm/test/Transforms/SampleProfile/branch.ll index 19e85b45dba8..ac68fd857bd2 100644 --- a/llvm/test/Transforms/SampleProfile/branch.ll +++ b/llvm/test/Transforms/SampleProfile/branch.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/branch.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/branch.prof | opt -analyze -branch-prob | FileCheck %s ; Original C++ code for this test case: ; diff --git a/llvm/test/Transforms/SampleProfile/calls.ll b/llvm/test/Transforms/SampleProfile/calls.ll index b28d4fb88fbc..0105019c73ea 100644 --- a/llvm/test/Transforms/SampleProfile/calls.ll +++ b/llvm/test/Transforms/SampleProfile/calls.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/calls.prof | opt -analyze -branch-prob | FileCheck %s ; Original C++ test case ; diff --git a/llvm/test/Transforms/SampleProfile/cov-zero-samples.ll b/llvm/test/Transforms/SampleProfile/cov-zero-samples.ll index d5e33e2ec32e..dc3e6de8c3c9 100644 --- a/llvm/test/Transforms/SampleProfile/cov-zero-samples.ll +++ b/llvm/test/Transforms/SampleProfile/cov-zero-samples.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/cov-zero-samples.prof -sample-profile-check-record-coverage=100 -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s +; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/cov-zero-samples.prof -sample-profile-check-record-coverage=100 -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s ; ; CHECK: remark: cov-zero-samples.cc:9:29: Applied 404065 samples from profile (offset: 2.1) ; CHECK: remark: cov-zero-samples.cc:10:9: Applied 443089 samples from profile (offset: 3) diff --git a/llvm/test/Transforms/SampleProfile/coverage-warning.ll b/llvm/test/Transforms/SampleProfile/coverage-warning.ll index dd5bbace9e08..4b8349d0af97 100644 --- a/llvm/test/Transforms/SampleProfile/coverage-warning.ll +++ b/llvm/test/Transforms/SampleProfile/coverage-warning.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/coverage-warning.prof -sample-profile-check-record-coverage=90 -sample-profile-check-sample-coverage=100 -o /dev/null 2>&1 | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/coverage-warning.prof -sample-profile-check-record-coverage=90 -sample-profile-check-sample-coverage=100 -o /dev/null 2>&1 | FileCheck %s define i32 @foo(i32 %i) !dbg !4 { ; The profile has samples for line locations that are no longer present. ; Coverage does not reach 90%, so we should get this warning: diff --git a/llvm/test/Transforms/SampleProfile/discriminator.ll b/llvm/test/Transforms/SampleProfile/discriminator.ll index f383e1d1c6c3..862c2ee43c53 100644 --- a/llvm/test/Transforms/SampleProfile/discriminator.ll +++ b/llvm/test/Transforms/SampleProfile/discriminator.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s ; Original code ; diff --git a/llvm/test/Transforms/SampleProfile/entry_counts.ll b/llvm/test/Transforms/SampleProfile/entry_counts.ll index 49610f645a5f..1f7aceb8abb1 100644 --- a/llvm/test/Transforms/SampleProfile/entry_counts.ll +++ b/llvm/test/Transforms/SampleProfile/entry_counts.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/entry_counts.prof -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/entry_counts.prof -S | FileCheck %s ; According to the profile, function empty() was called 13,293 times. ; CHECK: {{.*}} = !{!"function_entry_count", i64 13293} diff --git a/llvm/test/Transforms/SampleProfile/fnptr.ll b/llvm/test/Transforms/SampleProfile/fnptr.ll index abb4f7dea631..540ddbd79c93 100644 --- a/llvm/test/Transforms/SampleProfile/fnptr.ll +++ b/llvm/test/Transforms/SampleProfile/fnptr.ll @@ -5,6 +5,9 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/fnptr.prof | opt -analyze -branch-prob | FileCheck %s ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/fnptr.binprof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/fnptr.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/fnptr.binprof | opt -analyze -branch-prob | FileCheck %s + ; CHECK: edge for.body3 -> if.then probability is 0x1a4f3959 / 0x80000000 = 20.55% ; CHECK: edge for.body3 -> if.else probability is 0x65b0c6a7 / 0x80000000 = 79.45% ; CHECK: edge for.inc -> for.inc12 probability is 0x33d4a4c1 / 0x80000000 = 40.49% diff --git a/llvm/test/Transforms/SampleProfile/gcc-simple.ll b/llvm/test/Transforms/SampleProfile/gcc-simple.ll index 2bd66060a067..cbd105ebc3b4 100644 --- a/llvm/test/Transforms/SampleProfile/gcc-simple.ll +++ b/llvm/test/Transforms/SampleProfile/gcc-simple.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/gcc-simple.afdo -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/gcc-simple.afdo -S | FileCheck %s ; XFAIL: powerpc64-, s390x, mips-, mips64-, sparc ; Original code: ; diff --git a/llvm/test/Transforms/SampleProfile/inline-combine.ll b/llvm/test/Transforms/SampleProfile/inline-combine.ll index 5bd18382388d..bcc1770bfae1 100644 --- a/llvm/test/Transforms/SampleProfile/inline-combine.ll +++ b/llvm/test/Transforms/SampleProfile/inline-combine.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/inline-combine.prof -S | FileCheck %s +; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/inline-combine.prof -S | FileCheck %s %"class.llvm::FoldingSetNodeID" = type { %"class.llvm::SmallVector" } %"class.llvm::SmallVector" = type { %"class.llvm::SmallVectorImpl.base", %"struct.llvm::SmallVectorStorage" } diff --git a/llvm/test/Transforms/SampleProfile/inline-coverage.ll b/llvm/test/Transforms/SampleProfile/inline-coverage.ll index ae08466c38e8..c88e7f865fa2 100644 --- a/llvm/test/Transforms/SampleProfile/inline-coverage.ll +++ b/llvm/test/Transforms/SampleProfile/inline-coverage.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -instcombine -sample-profile -sample-profile-file=%S/Inputs/inline-coverage.prof -sample-profile-check-record-coverage=100 -sample-profile-check-sample-coverage=110 -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s +; RUN: opt < %s -passes="function(instcombine),sample-profile" -sample-profile-file=%S/Inputs/inline-coverage.prof -sample-profile-check-record-coverage=100 -sample-profile-check-sample-coverage=110 -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s ; ; Original code: ; diff --git a/llvm/test/Transforms/SampleProfile/inline-hint.ll b/llvm/test/Transforms/SampleProfile/inline-hint.ll index 20cdd3039b19..b95b02684c95 100644 --- a/llvm/test/Transforms/SampleProfile/inline-hint.ll +++ b/llvm/test/Transforms/SampleProfile/inline-hint.ll @@ -1,4 +1,5 @@ ; RUN: opt %s -sample-profile -sample-profile-file=%S/Inputs/inline-hint.prof -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s +; RUN: opt %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline-hint.prof -pass-remarks=sample-profile -o /dev/null 2>&1 | FileCheck %s ; ; CHECK: Applied cold hint to globally cold function '_Z7cold_fnRxi' with 0.1 define void @_Z7cold_fnRxi() !dbg !4 { diff --git a/llvm/test/Transforms/SampleProfile/inline.ll b/llvm/test/Transforms/SampleProfile/inline.ll index f37ddb2b8452..ed353834137b 100644 --- a/llvm/test/Transforms/SampleProfile/inline.ll +++ b/llvm/test/Transforms/SampleProfile/inline.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.prof -sample-profile-inline-hot-threshold=1 -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.prof -sample-profile-inline-hot-threshold=1 -S | FileCheck %s ; Original C++ test case ; diff --git a/llvm/test/Transforms/SampleProfile/nolocinfo.ll b/llvm/test/Transforms/SampleProfile/nolocinfo.ll index 1467ca979838..bd6a10b33f2c 100644 --- a/llvm/test/Transforms/SampleProfile/nolocinfo.ll +++ b/llvm/test/Transforms/SampleProfile/nolocinfo.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/nolocinfo.prof -S -pass-remarks=sample-profile 2>&1 | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/nolocinfo.prof -S -pass-remarks=sample-profile 2>&1 | FileCheck %s define i32 @foo(i32 %i) !dbg !4 { entry: diff --git a/llvm/test/Transforms/SampleProfile/offset.ll b/llvm/test/Transforms/SampleProfile/offset.ll index 6403ea783621..c8d52408e864 100644 --- a/llvm/test/Transforms/SampleProfile/offset.ll +++ b/llvm/test/Transforms/SampleProfile/offset.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/offset.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/offset.prof | opt -analyze -branch-prob | FileCheck %s ; Original C++ code for this test case: ; diff --git a/llvm/test/Transforms/SampleProfile/propagate.ll b/llvm/test/Transforms/SampleProfile/propagate.ll index 9a72edb6ee94..a5796695ca04 100644 --- a/llvm/test/Transforms/SampleProfile/propagate.ll +++ b/llvm/test/Transforms/SampleProfile/propagate.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/propagate.prof | opt -analyze -branch-prob | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/propagate.prof | opt -analyze -branch-prob | FileCheck %s ; Original C++ code for this test case: ; diff --git a/llvm/test/Transforms/SampleProfile/remarks.ll b/llvm/test/Transforms/SampleProfile/remarks.ll index 586f07b478a8..908e4f8b10b4 100644 --- a/llvm/test/Transforms/SampleProfile/remarks.ll +++ b/llvm/test/Transforms/SampleProfile/remarks.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile 2>&1 | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile 2>&1 | FileCheck %s ; ; Original test case. ; diff --git a/llvm/test/Transforms/SampleProfile/syntax.ll b/llvm/test/Transforms/SampleProfile/syntax.ll index debbc7c87ddb..7114dfa61574 100644 --- a/llvm/test/Transforms/SampleProfile/syntax.ll +++ b/llvm/test/Transforms/SampleProfile/syntax.ll @@ -7,6 +7,15 @@ ; RUN: not opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_samples.prof 2>&1 | FileCheck -check-prefix=BAD-SAMPLES %s ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_mangle.prof 2>&1 >/dev/null +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/syntax.prof 2>&1 | FileCheck -check-prefix=NO-DEBUG %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=missing.prof 2>&1 | FileCheck -check-prefix=MISSING-FILE %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_fn_header.prof 2>&1 | FileCheck -check-prefix=BAD-FN-HEADER %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_sample_line.prof 2>&1 | FileCheck -check-prefix=BAD-SAMPLE-LINE %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_line_values.prof 2>&1 | FileCheck -check-prefix=BAD-LINE-VALUES %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_discriminator_value.prof 2>&1 | FileCheck -check-prefix=BAD-DISCRIMINATOR-VALUE %s +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_samples.prof 2>&1 | FileCheck -check-prefix=BAD-SAMPLES %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad_mangle.prof 2>&1 >/dev/null + define void @empty() { entry: ret void