[PATCH] OptDiag: Summarize the instruction count in asm-printer

Ahmed Bougacha via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 3 11:32:30 PST 2017


LGTM, but +Adam for the ORE stuff.

On Fri, Feb 3, 2017 at 10:58 AM, Justin Bogner via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
> This teaches the AsmPrinter to remark on the number of instructions in
> each emitted function.
>
> Okay to commit?
>
> commit 124e69d48f506369da86eb0a0ffe46d99a673b06
> Author: Justin Bogner <jbogner at apple.com>
> Date:   Thu Feb 2 18:09:59 2017 -0800
>     OptDiag: Summarize the instruction count in asm-printer
>
>     Add an optimization remark to asm-printer that summarizes the number
>     of instructions emitted per function.
> diff --git a/include/llvm/CodeGen/AsmPrinter.h b/include/llvm/CodeGen/AsmPrinter.h
> index 4daca0347b7..4b6788c69ee 100644
> --- a/include/llvm/CodeGen/AsmPrinter.h
> +++ b/include/llvm/CodeGen/AsmPrinter.h
> @@ -46,6 +46,7 @@ class MachineLoop;
>  class MachineConstantPoolValue;
>  class MachineJumpTableInfo;
>  class MachineModuleInfo;
> +class MachineOptimizationRemarkEmitter;
>  class MCAsmInfo;
>  class MCCFIInstruction;
>  class MCContext;
> @@ -89,6 +90,9 @@ public:
>    /// This is a pointer to the current MachineModuleInfo.
>    MachineModuleInfo *MMI;
>
> +  /// Optimization remark emitter.
> +  MachineOptimizationRemarkEmitter *ORE;
> +
>    /// The symbol for the current function. This is recalculated at the beginning
>    /// of each call to runOnMachineFunction().
>    ///
> diff --git a/include/llvm/CodeGen/MachineFunction.h b/include/llvm/CodeGen/MachineFunction.h
> index 0c21b325463..d135be6ec32 100644
> --- a/include/llvm/CodeGen/MachineFunction.h
> +++ b/include/llvm/CodeGen/MachineFunction.h
> @@ -498,6 +498,13 @@ public:
>    /// it are renumbered.
>    void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
>
> +  /// Return a debug location for the start of this function.
> +  ///
> +  /// This creates an artificial DebugLoc based on the line information in the
> +  /// function's DISubprogram, such that it's suitable to represent the location
> +  /// of the function itself.
> +  DebugLoc getStartLoc() const;
> +
>    /// print - Print out the MachineFunction in a format suitable for debugging
>    /// to the specified stream.
>    ///
> diff --git a/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h b/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
> index fd48347d51a..345e70ecbf4 100644
> --- a/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
> +++ b/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
> @@ -29,7 +29,7 @@ class DiagnosticInfoMIROptimization : public DiagnosticInfoOptimizationBase {
>  public:
>    DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName,
>                                  StringRef RemarkName, const DebugLoc &DLoc,
> -                                MachineBasicBlock *MBB)
> +                                const MachineBasicBlock *MBB)

Looks good regardless of the AsmPrinter changes.

>        : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, RemarkName,
>                                         *MBB->getParent()->getFunction(), DLoc),
>          MBB(MBB) {}
> @@ -42,7 +42,7 @@ public:
>    const MachineBasicBlock *getBlock() const { return MBB; }
>
>  private:
> -  MachineBasicBlock *MBB;
> +  const MachineBasicBlock *MBB;
>  };
>
>  /// Diagnostic information for applied optimization remarks.
> @@ -54,7 +54,7 @@ public:
>    /// DLoc is the debug location and \p MBB is the block that the optimization
>    /// operates in.
>    MachineOptimizationRemark(const char *PassName, StringRef RemarkName,
> -                            const DebugLoc &DLoc, MachineBasicBlock *MBB)
> +                            const DebugLoc &DLoc, const MachineBasicBlock *MBB)
>        : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemark, PassName,
>                                        RemarkName, DLoc, MBB) {}
>
> @@ -77,7 +77,8 @@ public:
>    /// remark.  \p DLoc is the debug location and \p MBB is the block that the
>    /// optimization operates in.
>    MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName,
> -                                  const DebugLoc &DLoc, MachineBasicBlock *MBB)
> +                                  const DebugLoc &DLoc,
> +                                  const MachineBasicBlock *MBB)
>        : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkMissed,
>                                        PassName, RemarkName, DLoc, MBB) {}
>
> @@ -101,7 +102,7 @@ public:
>    /// optimization operates in.
>    MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName,
>                                      const DebugLoc &DLoc,
> -                                    MachineBasicBlock *MBB)
> +                                    const MachineBasicBlock *MBB)
>        : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkAnalysis,
>                                        PassName, RemarkName, DLoc, MBB) {}
>
> diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> index 0f6ca02b2a8..f8e3c327d22 100644
> --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> @@ -27,6 +27,7 @@
>  #include "llvm/CodeGen/MachineJumpTableInfo.h"
>  #include "llvm/CodeGen/MachineLoopInfo.h"
>  #include "llvm/CodeGen/MachineModuleInfoImpls.h"
> +#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
>  #include "llvm/IR/DataLayout.h"
>  #include "llvm/IR/DebugInfo.h"
>  #include "llvm/IR/Mangler.h"
> @@ -111,6 +112,7 @@ AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
>        isCFIMoveForDebugging(false), LastMI(nullptr), LastFn(0), Counter(~0U) {
>    DD = nullptr;
>    MMI = nullptr;
> +  ORE = nullptr;
>    LI = nullptr;
>    MF = nullptr;
>    CurExceptionSym = CurrentFnSym = CurrentFnSymForSize = nullptr;
> @@ -171,6 +173,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
>    AU.setPreservesAll();
>    MachineFunctionPass::getAnalysisUsage(AU);
>    AU.addRequired<MachineModuleInfo>();
> +  AU.addRequired<MachineOptimizationRemarkEmitterPass>();
>    AU.addRequired<GCModuleInfo>();
>    if (isVerbose())
>      AU.addRequired<MachineLoopInfo>();
> @@ -883,6 +886,7 @@ void AsmPrinter::EmitFunctionBody() {
>
>    // Print out code for the function.
>    bool HasAnyRealCode = false;
> +  int NumInstsInFunction = 0;
>    for (auto &MBB : *MF) {
>      // Print a label for the basic block.
>      EmitBasicBlockStart(MBB);
> @@ -892,7 +896,7 @@ void AsmPrinter::EmitFunctionBody() {
>        if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
>            !MI.isDebugValue()) {
>          HasAnyRealCode = true;
> -        ++EmittedInsts;
> +        ++NumInstsInFunction;
>        }
>
>        if (ShouldPrintDebugScopes) {
> @@ -953,6 +957,13 @@ void AsmPrinter::EmitFunctionBody() {
>      EmitBasicBlockEnd(MBB);
>    }
>
> +  EmittedInsts += NumInstsInFunction;
> +  MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "InstructionCount",
> +                                      MF->getStartLoc(), &MF->front());
> +  R << ore::NV("NumInstructions", NumInstsInFunction)
> +    << " instructions in function";

Nit: singular if 1?

> +  ORE->emit(R);
> +
>    // If the function is empty and the object file uses .subsections_via_symbols,
>    // then we need to emit *something* to the function body to prevent the
>    // labels from collapsing together.  Just emit a noop.
> @@ -1311,6 +1322,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
>        CurrentFnSymForSize = CurrentFnBegin;
>    }
>
> +  ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
>    if (isVerbose())
>      LI = &getAnalysis<MachineLoopInfo>();
>  }
> diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp
> index 831e7d859bb..8ca5d108ded 100644
> --- a/lib/CodeGen/MachineFunction.cpp
> +++ b/lib/CodeGen/MachineFunction.cpp
> @@ -412,6 +412,12 @@ const char *MachineFunction::createExternalSymbolName(StringRef Name) {
>    return Dest;
>  }
>
> +DebugLoc MachineFunction::getStartLoc() const {
> +  if (DISubprogram *DIS = getFunction()->getSubprogram())
> +    return DILocation::get(getFunction()->getContext(), DIS->getLine(), 0, DIS);
> +  return DebugLoc();
> +}
> +
>  #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
>  LLVM_DUMP_METHOD void MachineFunction::dump() const {
>    print(dbgs());
> diff --git a/test/CodeGen/AArch64/arm64-summary-remarks.ll b/test/CodeGen/AArch64/arm64-summary-remarks.ll
> new file mode 100644
> index 00000000000..dc3350d5b35
> --- /dev/null
> +++ b/test/CodeGen/AArch64/arm64-summary-remarks.ll
> @@ -0,0 +1,15 @@
> +; RUN: llc < %s -mtriple=arm64-apple-ios7.0 -pass-remarks-analysis=asm-printer 2>&1 | FileCheck %s
> +
> +; CHECK: arm64-summary-remarks.ll:5:0: 1 instructions in function
> +
> +define void @empty_func() nounwind ssp !dbg !3 {
> +  ret void
> +}
> +
> +!llvm.dbg.cu = !{!0}
> +!llvm.module.flags = !{!2}
> +
> +!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1)
> +!1 = !DIFile(filename: "arm64-summary-remarks.ll", directory: "")
> +!2 = !{i32 2, !"Debug Info Version", i32 3}
> +!3 = distinct !DISubprogram(name: "empty_func", scope: !1, file: !1, line: 5, unit: !0)


-Ahmed


More information about the llvm-commits mailing list