Commit e72e59e9 authored by Francis Visoiu Mistrih's avatar Francis Visoiu Mistrih
Browse files

[Remarks] Add support for linking remarks

Remarks are usually emitted per-TU, and for generating a standalone
remark file that can be shipped with the linked binary we need some kind
of tool to merge everything together.

The remarks::RemarkLinker class takes care of this and:

* Deduplicates remarks
* Filters remarks with no debug location
* Merges string tables from all the entries

As an output, it provides an iterator range that can be used to
serialize the remarks to a file.

Differential Revision: https://reviews.llvm.org/D69141
parent f9061049
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -110,6 +110,21 @@ private:
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Remark, LLVMRemarkEntryRef)

/// Comparison operators for Remark objects and dependent objects.

template <typename T>
bool operator<(const Optional<T> &LHS, const Optional<T> &RHS) {
  // Sorting based on optionals should result in all `None` entries to appear
  // before the valid entries. For example, remarks with no debug location will
  // appear first.
  if (!LHS && !RHS)
    return false;
  if (!LHS && RHS)
    return true;
  if (LHS && !RHS)
    return false;
  return *LHS < *RHS;
}

inline bool operator==(const RemarkLocation &LHS, const RemarkLocation &RHS) {
  return LHS.SourceFilePath == RHS.SourceFilePath &&
         LHS.SourceLine == RHS.SourceLine &&
@@ -120,6 +135,11 @@ inline bool operator!=(const RemarkLocation &LHS, const RemarkLocation &RHS) {
  return !(LHS == RHS);
}

inline bool operator<(const RemarkLocation &LHS, const RemarkLocation &RHS) {
  return std::make_tuple(LHS.SourceFilePath, LHS.SourceLine, LHS.SourceColumn) <
         std::make_tuple(RHS.SourceFilePath, RHS.SourceLine, RHS.SourceColumn);
}

inline bool operator==(const Argument &LHS, const Argument &RHS) {
  return LHS.Key == RHS.Key && LHS.Val == RHS.Val && LHS.Loc == RHS.Loc;
}
@@ -128,6 +148,11 @@ inline bool operator!=(const Argument &LHS, const Argument &RHS) {
  return !(LHS == RHS);
}

inline bool operator<(const Argument &LHS, const Argument &RHS) {
  return std::make_tuple(LHS.Key, LHS.Val, LHS.Loc) <
         std::make_tuple(RHS.Key, RHS.Val, RHS.Loc);
}

inline bool operator==(const Remark &LHS, const Remark &RHS) {
  return LHS.RemarkType == RHS.RemarkType && LHS.PassName == RHS.PassName &&
         LHS.RemarkName == RHS.RemarkName &&
@@ -139,6 +164,13 @@ inline bool operator!=(const Remark &LHS, const Remark &RHS) {
  return !(LHS == RHS);
}

inline bool operator<(const Remark &LHS, const Remark &RHS) {
  return std::make_tuple(LHS.RemarkType, LHS.PassName, LHS.RemarkName,
                         LHS.FunctionName, LHS.Loc, LHS.Hotness, LHS.Args) <
         std::make_tuple(RHS.RemarkType, RHS.PassName, RHS.RemarkName,
                         RHS.FunctionName, RHS.Loc, RHS.Hotness, RHS.Args);
}

} // end namespace remarks
} // end namespace llvm

+3 −0
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ enum class Format { Unknown, YAML, YAMLStrTab, Bitstream };
/// Parse and validate a string for the remark format.
Expected<Format> parseFormat(StringRef FormatStr);

/// Parse and validate a magic number to a remark format.
Expected<Format> magicToFormat(StringRef Magic);

} // end namespace remarks
} // end namespace llvm

+100 −0
Original line number Diff line number Diff line
//===-- llvm/Remarks/RemarkLinker.h -----------------------------*- C++/-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides an interface to link together multiple remark files.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_REMARKS_REMARK_LINKER_H
#define LLVM_REMARKS_REMARK_LINKER_H

#include "llvm/Object/ObjectFile.h"
#include "llvm/Remarks/Remark.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkStringTable.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <set>

namespace llvm {
namespace remarks {

struct RemarkLinker {
private:
  /// Compare through the pointers.
  struct RemarkPtrCompare {
    bool operator()(const std::unique_ptr<Remark> &LHS,
                    const std::unique_ptr<Remark> &RHS) const {
      assert(LHS && RHS && "Invalid pointers to compare.");
      return *LHS < *RHS;
    };
  };

  /// The main string table for the remarks.
  /// Note: all remarks should use the strings from this string table to avoid
  /// dangling references.
  StringTable StrTab;

  /// A set holding unique remarks.
  /// FIXME: std::set is probably not the most appropriate data structure here.
  /// Due to the limitation of having a move-only key, there isn't another
  /// obvious choice for now.
  std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;

  /// A path to append before the external file path found in remark metadata.
  Optional<std::string> PrependPath;

  /// Keep this remark. If it's already in the set, discard it.
  Remark &keep(std::unique_ptr<Remark> Remark);

public:
  /// Set a path to prepend to the external file path.
  void setExternalFilePrependPath(StringRef PrependPath);

  /// Link the remarks found in \p Buffer.
  /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
  /// \p Buffer.
  /// \p Buffer can be either a standalone remark container or just
  /// metadata. This takes care of uniquing and merging the remarks.
  Error link(StringRef Buffer, Optional<Format> RemarkFormat = None);

  /// Link the remarks found in \p Obj by looking for the right section and
  /// calling the method above.
  Error link(const object::ObjectFile &Obj,
             Optional<Format> RemarkFormat = None);

  /// Serialize the linked remarks to the stream \p OS, using the format \p
  /// RemarkFormat.
  /// This clears internal state such as the string table.
  /// Note: this implies that the serialization mode is standalone.
  Error serialize(raw_ostream &OS, Format RemarksFormat) const;

  /// Check whether there are any remarks linked.
  bool empty() const { return Remarks.empty(); }

  /// Return a collection of the linked unique remarks to iterate on.
  /// Ex:
  /// for (const Remark &R : RL.remarks() { [...] }
  using iterator =
      pointee_iterator<std::set<std::unique_ptr<Remark>>::iterator>;

  iterator_range<iterator> remarks() const {
    return {Remarks.begin(), Remarks.end()};
  }
};

/// Returns a buffer with the contents of the remarks section depending on the
/// format of the file. If the section doesn't exist, this returns an empty
/// optional.
Expected<Optional<StringRef>>
getRemarksSectionContents(const object::ObjectFile &Obj);

} // end namespace remarks
} // end namespace llvm

#endif /* LLVM_REMARKS_REMARK_LINKER_H */
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ add_llvm_library(LLVMRemarks
  BitstreamRemarkSerializer.cpp
  Remark.cpp
  RemarkFormat.cpp
  RemarkLinker.cpp
  RemarkParser.cpp
  RemarkSerializer.cpp
  RemarkStringTable.cpp
+15 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@

#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Remarks/BitstreamRemarkContainer.h"

using namespace llvm;
using namespace llvm::remarks;
@@ -30,3 +31,17 @@ Expected<Format> llvm::remarks::parseFormat(StringRef FormatStr) {

  return Result;
}

Expected<Format> llvm::remarks::magicToFormat(StringRef Magic) {
  auto Result =
      StringSwitch<Format>(Magic)
          .StartsWith("--- ", Format::YAML) // This is only an assumption.
          .StartsWith(remarks::Magic, Format::YAMLStrTab)
          .StartsWith(remarks::ContainerMagic, Format::Bitstream)
          .Default(Format::Unknown);

  if (Result == Format::Unknown)
    return createStringError(std::make_error_code(std::errc::invalid_argument),
                             "Unknown remark magic: '%s'", Magic.data());
  return Result;
}
Loading