Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===- HotnessThresholdParser.h - Parser for hotness threshold --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file implements a simple parser to decode commandline option for
0011 /// remarks hotness threshold that supports both int and a special 'auto' value.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H
0016 #define LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H
0017 
0018 #include "llvm/Support/CommandLine.h"
0019 #include "llvm/Support/Error.h"
0020 #include <optional>
0021 
0022 namespace llvm {
0023 namespace remarks {
0024 
0025 // Parse remarks hotness threshold argument value.
0026 // Valid option values are
0027 // 1. integer: manually specified threshold; or
0028 // 2. string 'auto': automatically get threshold from profile summary.
0029 //
0030 // Return std::nullopt Optional if 'auto' is specified, indicating the value
0031 // will be filled later during PSI.
0032 inline Expected<std::optional<uint64_t>> parseHotnessThresholdOption(StringRef Arg) {
0033   if (Arg == "auto")
0034     return std::nullopt;
0035 
0036   int64_t Val;
0037   if (Arg.getAsInteger(10, Val))
0038     return createStringError(llvm::inconvertibleErrorCode(),
0039                              "Not an integer: %s", Arg.data());
0040 
0041   // Negative integer effectively means no threshold
0042   return Val < 0 ? 0 : Val;
0043 }
0044 
0045 // A simple CL parser for '*-remarks-hotness-threshold='
0046 class HotnessThresholdParser : public cl::parser<std::optional<uint64_t>> {
0047 public:
0048   HotnessThresholdParser(cl::Option &O) : cl::parser<std::optional<uint64_t>>(O) {}
0049 
0050   bool parse(cl::Option &O, StringRef ArgName, StringRef Arg,
0051              std::optional<uint64_t> &V) {
0052     auto ResultOrErr = parseHotnessThresholdOption(Arg);
0053     if (!ResultOrErr)
0054       return O.error("Invalid argument '" + Arg +
0055                      "', only integer or 'auto' is supported.");
0056 
0057     V = *ResultOrErr;
0058     return false;
0059   }
0060 };
0061 
0062 } // namespace remarks
0063 } // namespace llvm
0064 #endif // LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H