| 1 | //===- HotnessThresholdParser.h - Parser for hotness threshold --*- 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file implements a simple parser to decode commandline option for |
| 11 | /// remarks hotness threshold that supports both int and a special 'auto' value. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H |
| 16 | #define |
| 17 | |
| 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/Error.h" |
| 20 | #include <optional> |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace remarks { |
| 24 | |
| 25 | // Parse remarks hotness threshold argument value. |
| 26 | // Valid option values are |
| 27 | // 1. integer: manually specified threshold; or |
| 28 | // 2. string 'auto': automatically get threshold from profile summary. |
| 29 | // |
| 30 | // Return std::nullopt Optional if 'auto' is specified, indicating the value |
| 31 | // will be filled later during PSI. |
| 32 | inline Expected<std::optional<uint64_t>> (StringRef Arg) { |
| 33 | if (Arg == "auto" ) |
| 34 | return std::nullopt; |
| 35 | |
| 36 | int64_t Val; |
| 37 | if (Arg.getAsInteger(Radix: 10, Result&: Val)) |
| 38 | return createStringError(EC: llvm::inconvertibleErrorCode(), |
| 39 | Fmt: "Not an integer: %s" , Vals: Arg.data()); |
| 40 | |
| 41 | // Negative integer effectively means no threshold |
| 42 | return Val < 0 ? 0 : Val; |
| 43 | } |
| 44 | |
| 45 | // A simple CL parser for '*-remarks-hotness-threshold=' |
| 46 | class : public cl::parser<std::optional<uint64_t>> { |
| 47 | public: |
| 48 | (cl::Option &O) : cl::parser<std::optional<uint64_t>>(O) {} |
| 49 | |
| 50 | bool (cl::Option &O, StringRef ArgName, StringRef Arg, |
| 51 | std::optional<uint64_t> &V) { |
| 52 | auto ResultOrErr = parseHotnessThresholdOption(Arg); |
| 53 | if (!ResultOrErr) |
| 54 | return O.error(Message: "Invalid argument '" + Arg + |
| 55 | "', only integer or 'auto' is supported." ); |
| 56 | |
| 57 | V = *ResultOrErr; |
| 58 | return false; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | } // namespace remarks |
| 63 | } // namespace llvm |
| 64 | #endif // LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H |
| 65 | |