Path: blob/main/contrib/llvm-project/llvm/lib/IR/LLVMRemarkStreamer.cpp
35233 views
//===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- 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//8// This file contains the implementation of the conversion between IR9// Diagnostics and serializable remarks::Remark objects.10//11//===----------------------------------------------------------------------===//1213#include "llvm/IR/LLVMRemarkStreamer.h"14#include "llvm/IR/DiagnosticInfo.h"15#include "llvm/IR/Function.h"16#include "llvm/IR/GlobalValue.h"17#include "llvm/Remarks/RemarkStreamer.h"18#include "llvm/Support/FileSystem.h"19#include "llvm/Support/ToolOutputFile.h"20#include <optional>2122using namespace llvm;2324/// DiagnosticKind -> remarks::Type25static remarks::Type toRemarkType(enum DiagnosticKind Kind) {26switch (Kind) {27default:28return remarks::Type::Unknown;29case DK_OptimizationRemark:30case DK_MachineOptimizationRemark:31return remarks::Type::Passed;32case DK_OptimizationRemarkMissed:33case DK_MachineOptimizationRemarkMissed:34return remarks::Type::Missed;35case DK_OptimizationRemarkAnalysis:36case DK_MachineOptimizationRemarkAnalysis:37return remarks::Type::Analysis;38case DK_OptimizationRemarkAnalysisFPCommute:39return remarks::Type::AnalysisFPCommute;40case DK_OptimizationRemarkAnalysisAliasing:41return remarks::Type::AnalysisAliasing;42case DK_OptimizationFailure:43return remarks::Type::Failure;44}45}4647/// DiagnosticLocation -> remarks::RemarkLocation.48static std::optional<remarks::RemarkLocation>49toRemarkLocation(const DiagnosticLocation &DL) {50if (!DL.isValid())51return std::nullopt;52StringRef File = DL.getRelativePath();53unsigned Line = DL.getLine();54unsigned Col = DL.getColumn();55return remarks::RemarkLocation{File, Line, Col};56}5758/// LLVM Diagnostic -> Remark59remarks::Remark60LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {61remarks::Remark R; // The result.62R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));63R.PassName = Diag.getPassName();64R.RemarkName = Diag.getRemarkName();65R.FunctionName =66GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());67R.Loc = toRemarkLocation(Diag.getLocation());68R.Hotness = Diag.getHotness();6970for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {71R.Args.emplace_back();72R.Args.back().Key = Arg.Key;73R.Args.back().Val = Arg.Val;74R.Args.back().Loc = toRemarkLocation(Arg.Loc);75}7677return R;78}7980void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {81if (!RS.matchesFilter(Diag.getPassName()))82return;8384// First, convert the diagnostic to a remark.85remarks::Remark R = toRemark(Diag);86// Then, emit the remark through the serializer.87RS.getSerializer().emit(R);88}8990char LLVMRemarkSetupFileError::ID = 0;91char LLVMRemarkSetupPatternError::ID = 0;92char LLVMRemarkSetupFormatError::ID = 0;9394Expected<std::unique_ptr<ToolOutputFile>> llvm::setupLLVMOptimizationRemarks(95LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,96StringRef RemarksFormat, bool RemarksWithHotness,97std::optional<uint64_t> RemarksHotnessThreshold) {98if (RemarksWithHotness || RemarksHotnessThreshold.value_or(1))99Context.setDiagnosticsHotnessRequested(true);100101Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);102103if (RemarksFilename.empty())104return nullptr;105106Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);107if (Error E = Format.takeError())108return make_error<LLVMRemarkSetupFormatError>(std::move(E));109110std::error_code EC;111auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF112: sys::fs::OF_None;113auto RemarksFile =114std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);115// We don't use llvm::FileError here because some diagnostics want the file116// name separately.117if (EC)118return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));119120Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =121remarks::createRemarkSerializer(122*Format, remarks::SerializerMode::Separate, RemarksFile->os());123if (Error E = RemarkSerializer.takeError())124return make_error<LLVMRemarkSetupFormatError>(std::move(E));125126// Create the main remark streamer.127Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(128std::move(*RemarkSerializer), RemarksFilename));129130// Create LLVM's optimization remarks streamer.131Context.setLLVMRemarkStreamer(132std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));133134if (!RemarksPasses.empty())135if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))136return make_error<LLVMRemarkSetupPatternError>(std::move(E));137138return std::move(RemarksFile);139}140141Error llvm::setupLLVMOptimizationRemarks(142LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,143StringRef RemarksFormat, bool RemarksWithHotness,144std::optional<uint64_t> RemarksHotnessThreshold) {145if (RemarksWithHotness || RemarksHotnessThreshold.value_or(1))146Context.setDiagnosticsHotnessRequested(true);147148Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);149150Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);151if (Error E = Format.takeError())152return make_error<LLVMRemarkSetupFormatError>(std::move(E));153154Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =155remarks::createRemarkSerializer(*Format,156remarks::SerializerMode::Separate, OS);157if (Error E = RemarkSerializer.takeError())158return make_error<LLVMRemarkSetupFormatError>(std::move(E));159160// Create the main remark streamer.161Context.setMainRemarkStreamer(162std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));163164// Create LLVM's optimization remarks streamer.165Context.setLLVMRemarkStreamer(166std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));167168if (!RemarksPasses.empty())169if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))170return make_error<LLVMRemarkSetupPatternError>(std::move(E));171172return Error::success();173}174175176