From 446268a223452254e054714f252705858fc47563 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Milo=C5=A1=20Stojanovi=C4=87?= Date: Thu, 6 Feb 2020 17:08:05 +0100 Subject: [PATCH] [llvm-exegesis] Add a custom error for clustering All errors of type `Failure` are `StringError`s. In order for exit code mapping to detect that specifically a clustering error has occurred it needs to have a different type. This patch also prepares D74085 where termination `report_fatal_error()` will be replaced with emitting `StringError`s. Differential Revision: https://reviews.llvm.org/D74124 --- llvm/tools/llvm-exegesis/lib/CMakeLists.txt | 1 + llvm/tools/llvm-exegesis/lib/Clustering.cpp | 12 ++++++------ llvm/tools/llvm-exegesis/lib/Error.cpp | 25 +++++++++++++++++++++++++ llvm/tools/llvm-exegesis/lib/Error.h | 13 +++++++++++++ llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 llvm/tools/llvm-exegesis/lib/Error.cpp diff --git a/llvm/tools/llvm-exegesis/lib/CMakeLists.txt b/llvm/tools/llvm-exegesis/lib/CMakeLists.txt index aa11d54..a839874 100644 --- a/llvm/tools/llvm-exegesis/lib/CMakeLists.txt +++ b/llvm/tools/llvm-exegesis/lib/CMakeLists.txt @@ -27,6 +27,7 @@ add_library(LLVMExegesis BenchmarkRunner.cpp Clustering.cpp CodeTemplate.cpp + Error.cpp LatencyBenchmarkRunner.cpp LlvmState.cpp MCInstrDescView.cpp diff --git a/llvm/tools/llvm-exegesis/lib/Clustering.cpp b/llvm/tools/llvm-exegesis/lib/Clustering.cpp index 95d5cfb..33a8e01 100644 --- a/llvm/tools/llvm-exegesis/lib/Clustering.cpp +++ b/llvm/tools/llvm-exegesis/lib/Clustering.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "Clustering.h" +#include "Error.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallVector.h" @@ -106,14 +107,13 @@ Error InstructionBenchmarkClustering::validateAndSetup() { const auto *CurMeasurement = &Point.Measurements; if (LastMeasurement) { if (LastMeasurement->size() != CurMeasurement->size()) { - return make_error("inconsistent measurement dimensions", - inconvertibleErrorCode()); + return make_error( + "inconsistent measurement dimensions"); } for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) { if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) { - return make_error( - "inconsistent measurement dimensions keys", - inconvertibleErrorCode()); + return make_error( + "inconsistent measurement dimensions keys"); } } } @@ -333,7 +333,7 @@ Expected InstructionBenchmarkClustering::create( Clustering.stabilize(NumOpcodes.getValue()); } else /*if(Mode == ModeE::Naive)*/ { if (!NumOpcodes.hasValue()) - report_fatal_error( + return make_error( "'naive' clustering mode requires opcode count to be specified"); Clustering.clusterizeNaive(NumOpcodes.getValue()); } diff --git a/llvm/tools/llvm-exegesis/lib/Error.cpp b/llvm/tools/llvm-exegesis/lib/Error.cpp new file mode 100644 index 0000000..d8aaf78 --- /dev/null +++ b/llvm/tools/llvm-exegesis/lib/Error.cpp @@ -0,0 +1,25 @@ +//===-- Error.cpp -----------------------------------------------*- 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 +// +//===----------------------------------------------------------------------===// + +#include "Error.h" + +namespace llvm { +namespace exegesis { + +char ClusteringError::ID; + +void ClusteringError::log(raw_ostream &OS) const { + OS << Msg; +} + +std::error_code ClusteringError::convertToErrorCode() const { + return inconvertibleErrorCode(); +} + +} // namespace exegesis +} // namespace llvm diff --git a/llvm/tools/llvm-exegesis/lib/Error.h b/llvm/tools/llvm-exegesis/lib/Error.h index 720cc68..dc203c8 100644 --- a/llvm/tools/llvm-exegesis/lib/Error.h +++ b/llvm/tools/llvm-exegesis/lib/Error.h @@ -22,6 +22,19 @@ public: Failure(const Twine &S) : StringError(S, inconvertibleErrorCode()) {} }; +// A class representing failures that happened during clustering calculations. +class ClusteringError : public ErrorInfo { +public: + static char ID; + ClusteringError(const Twine&S) : Msg(S.str()) {} + + void log(raw_ostream &OS) const override; + + std::error_code convertToErrorCode() const override; +private: + std::string Msg; +}; + } // namespace exegesis } // namespace llvm diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp index c4574d3..a3670d5 100644 --- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp +++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp @@ -373,7 +373,7 @@ int main(int Argc, char **Argv) { cl::ParseCommandLineOptions(Argc, Argv, ""); exegesis::ExitOnErr.setExitCodeMapper([](const Error &Err) { - if (Err.isA()) + if (Err.isA()) return EXIT_SUCCESS; return EXIT_FAILURE; }); -- 2.7.4