brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · 9e1e459 Raw
198 lines · cpp
1//===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- C++ ---------*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains the implementation of the conversion between IR10// Diagnostics and serializable remarks::Remark objects.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/IR/LLVMRemarkStreamer.h"15#include "llvm/IR/DiagnosticInfo.h"16#include "llvm/IR/Function.h"17#include "llvm/IR/GlobalValue.h"18#include "llvm/Remarks/RemarkStreamer.h"19#include "llvm/Support/FileSystem.h"20#include "llvm/Support/ToolOutputFile.h"21#include <optional>22 23using namespace llvm;24 25/// DiagnosticKind -> remarks::Type26static remarks::Type toRemarkType(enum DiagnosticKind Kind) {27  switch (Kind) {28  default:29    return remarks::Type::Unknown;30  case DK_OptimizationRemark:31  case DK_MachineOptimizationRemark:32    return remarks::Type::Passed;33  case DK_OptimizationRemarkMissed:34  case DK_MachineOptimizationRemarkMissed:35    return remarks::Type::Missed;36  case DK_OptimizationRemarkAnalysis:37  case DK_MachineOptimizationRemarkAnalysis:38    return remarks::Type::Analysis;39  case DK_OptimizationRemarkAnalysisFPCommute:40    return remarks::Type::AnalysisFPCommute;41  case DK_OptimizationRemarkAnalysisAliasing:42    return remarks::Type::AnalysisAliasing;43  case DK_OptimizationFailure:44    return remarks::Type::Failure;45  }46}47 48/// DiagnosticLocation -> remarks::RemarkLocation.49static std::optional<remarks::RemarkLocation>50toRemarkLocation(const DiagnosticLocation &DL) {51  if (!DL.isValid())52    return std::nullopt;53  StringRef File = DL.getRelativePath();54  unsigned Line = DL.getLine();55  unsigned Col = DL.getColumn();56  return remarks::RemarkLocation{File, Line, Col};57}58 59/// LLVM Diagnostic -> Remark60remarks::Remark61LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {62  remarks::Remark R; // The result.63  R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));64  R.PassName = Diag.getPassName();65  R.RemarkName = Diag.getRemarkName();66  R.FunctionName =67      GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());68  R.Loc = toRemarkLocation(Diag.getLocation());69  R.Hotness = Diag.getHotness();70 71  for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {72    R.Args.emplace_back();73    R.Args.back().Key = Arg.Key;74    R.Args.back().Val = Arg.Val;75    R.Args.back().Loc = toRemarkLocation(Arg.Loc);76  }77 78  return R;79}80 81void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {82  if (!RS.matchesFilter(Diag.getPassName()))83      return;84 85  // First, convert the diagnostic to a remark.86  remarks::Remark R = toRemark(Diag);87  // Then, emit the remark through the serializer.88  RS.getSerializer().emit(R);89}90 91char LLVMRemarkSetupFileError::ID = 0;92char LLVMRemarkSetupPatternError::ID = 0;93char LLVMRemarkSetupFormatError::ID = 0;94 95Expected<LLVMRemarkFileHandle> llvm::setupLLVMOptimizationRemarks(96    LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,97    StringRef RemarksFormat, bool RemarksWithHotness,98    std::optional<uint64_t> RemarksHotnessThreshold) {99  if (RemarksWithHotness || RemarksHotnessThreshold.value_or(1))100      Context.setDiagnosticsHotnessRequested(true);101 102  Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);103 104  if (RemarksFilename.empty())105    return LLVMRemarkFileHandle();106 107  Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);108  if (Error E = Format.takeError())109    return make_error<LLVMRemarkSetupFormatError>(std::move(E));110 111  std::error_code EC;112  auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF113                                                : sys::fs::OF_None;114  auto RemarksFile =115      std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);116  // We don't use llvm::FileError here because some diagnostics want the file117  // name separately.118  if (EC)119    return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));120 121  Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =122      remarks::createRemarkSerializer(*Format, RemarksFile->os());123  if (Error E = RemarkSerializer.takeError())124    return make_error<LLVMRemarkSetupFormatError>(std::move(E));125 126  auto RS = std::make_unique<remarks::RemarkStreamer>(127      std::move(*RemarkSerializer), RemarksFilename);128 129  if (!RemarksPasses.empty())130    if (Error E = RS->setFilter(RemarksPasses)) {131      RS->releaseSerializer();132      return make_error<LLVMRemarkSetupPatternError>(std::move(E));133    }134 135  // Install the main remark streamer. Only install this after setting the136  // filter, because this might fail.137  Context.setMainRemarkStreamer(std::move(RS));138 139  // Create LLVM's optimization remarks streamer.140  Context.setLLVMRemarkStreamer(141      std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));142 143  return LLVMRemarkFileHandle{std::move(RemarksFile), Context};144}145 146void LLVMRemarkFileHandle::Finalizer::finalize() {147  if (!Context)148    return;149  finalizeLLVMOptimizationRemarks(*Context);150  Context = nullptr;151}152 153Error llvm::setupLLVMOptimizationRemarks(154    LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,155    StringRef RemarksFormat, bool RemarksWithHotness,156    std::optional<uint64_t> RemarksHotnessThreshold) {157  if (RemarksWithHotness || RemarksHotnessThreshold.value_or(1))158    Context.setDiagnosticsHotnessRequested(true);159 160  Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);161 162  Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);163  if (Error E = Format.takeError())164    return make_error<LLVMRemarkSetupFormatError>(std::move(E));165 166  Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =167      remarks::createRemarkSerializer(*Format, OS);168  if (Error E = RemarkSerializer.takeError())169    return make_error<LLVMRemarkSetupFormatError>(std::move(E));170 171  auto RS =172      std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer));173 174  if (!RemarksPasses.empty())175    if (Error E = RS->setFilter(RemarksPasses)) {176      RS->releaseSerializer();177      return make_error<LLVMRemarkSetupPatternError>(std::move(E));178    }179 180  // Install the main remark streamer. Only install this after setting the181  // filter, because this might fail.182  Context.setMainRemarkStreamer(std::move(RS));183 184  // Create LLVM's optimization remarks streamer.185  Context.setLLVMRemarkStreamer(186      std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));187 188  return Error::success();189}190 191void llvm::finalizeLLVMOptimizationRemarks(LLVMContext &Context) {192  Context.setLLVMRemarkStreamer(nullptr);193  if (auto *RS = Context.getMainRemarkStreamer()) {194    RS->releaseSerializer();195    Context.setMainRemarkStreamer(nullptr);196  }197}198