Path: blob/main/contrib/llvm-project/llvm/tools/llvm-mca/Views/SummaryView.cpp
35290 views
//===--------------------- SummaryView.cpp ----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7/// \file8///9/// This file implements the functionalities used by the SummaryView to print10/// the report information.11///12//===----------------------------------------------------------------------===//1314#include "Views/SummaryView.h"15#include "llvm/ADT/SmallVector.h"16#include "llvm/MCA/Support.h"17#include "llvm/Support/Format.h"1819namespace llvm {20namespace mca {2122#define DEBUG_TYPE "llvm-mca"2324SummaryView::SummaryView(const MCSchedModel &Model, ArrayRef<MCInst> S,25unsigned Width)26: SM(Model), Source(S), DispatchWidth(Width ? Width : Model.IssueWidth),27LastInstructionIdx(0), TotalCycles(0), NumMicroOps(0),28ProcResourceUsage(Model.getNumProcResourceKinds(), 0),29ProcResourceMasks(Model.getNumProcResourceKinds()),30ResIdx2ProcResID(Model.getNumProcResourceKinds(), 0) {31computeProcResourceMasks(SM, ProcResourceMasks);32for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {33unsigned Index = getResourceStateIndex(ProcResourceMasks[I]);34ResIdx2ProcResID[Index] = I;35}36}3738void SummaryView::onEvent(const HWInstructionEvent &Event) {39if (Event.Type == HWInstructionEvent::Dispatched)40LastInstructionIdx = Event.IR.getSourceIndex();4142// We are only interested in the "instruction retired" events generated by43// the retire stage for instructions that are part of iteration #0.44if (Event.Type != HWInstructionEvent::Retired ||45Event.IR.getSourceIndex() >= Source.size())46return;4748// Update the cumulative number of resource cycles based on the processor49// resource usage information available from the instruction descriptor. We50// need to compute the cumulative number of resource cycles for every51// processor resource which is consumed by an instruction of the block.52const Instruction &Inst = *Event.IR.getInstruction();53const InstrDesc &Desc = Inst.getDesc();54NumMicroOps += Desc.NumMicroOps;55for (const std::pair<uint64_t, ResourceUsage> &RU : Desc.Resources) {56if (RU.second.size()) {57unsigned ProcResID = ResIdx2ProcResID[getResourceStateIndex(RU.first)];58ProcResourceUsage[ProcResID] += RU.second.size();59}60}61}6263void SummaryView::printView(raw_ostream &OS) const {64std::string Buffer;65raw_string_ostream TempStream(Buffer);66DisplayValues DV;6768collectData(DV);69TempStream << "Iterations: " << DV.Iterations;70TempStream << "\nInstructions: " << DV.TotalInstructions;71TempStream << "\nTotal Cycles: " << DV.TotalCycles;72TempStream << "\nTotal uOps: " << DV.TotalUOps << '\n';73TempStream << "\nDispatch Width: " << DV.DispatchWidth;74TempStream << "\nuOps Per Cycle: "75<< format("%.2f", floor((DV.UOpsPerCycle * 100) + 0.5) / 100);76TempStream << "\nIPC: "77<< format("%.2f", floor((DV.IPC * 100) + 0.5) / 100);78TempStream << "\nBlock RThroughput: "79<< format("%.1f", floor((DV.BlockRThroughput * 10) + 0.5) / 10)80<< '\n';81TempStream.flush();82OS << Buffer;83}8485void SummaryView::collectData(DisplayValues &DV) const {86DV.Instructions = Source.size();87DV.Iterations = (LastInstructionIdx / DV.Instructions) + 1;88DV.TotalInstructions = DV.Instructions * DV.Iterations;89DV.TotalCycles = TotalCycles;90DV.DispatchWidth = DispatchWidth;91DV.TotalUOps = NumMicroOps * DV.Iterations;92DV.UOpsPerCycle = (double)DV.TotalUOps / TotalCycles;93DV.IPC = (double)DV.TotalInstructions / TotalCycles;94DV.BlockRThroughput = computeBlockRThroughput(SM, DispatchWidth, NumMicroOps,95ProcResourceUsage);96}9798json::Value SummaryView::toJSON() const {99DisplayValues DV;100collectData(DV);101json::Object JO({{"Iterations", DV.Iterations},102{"Instructions", DV.TotalInstructions},103{"TotalCycles", DV.TotalCycles},104{"TotaluOps", DV.TotalUOps},105{"DispatchWidth", DV.DispatchWidth},106{"uOpsPerCycle", DV.UOpsPerCycle},107{"IPC", DV.IPC},108{"BlockRThroughput", DV.BlockRThroughput}});109return JO;110}111} // namespace mca.112} // namespace llvm113114115