HotnessThresholdParser.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- HotnessThresholdParser.h - Parser for hotness threshold --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements a simple parser to decode commandline option for
  16. /// remarks hotness threshold that supports both int and a special 'auto' value.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H
  20. #define LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/Support/CommandLine.h"
  23. namespace llvm {
  24. namespace remarks {
  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 None Optional if 'auto' is specified, indicating the value will
  31. // be filled later during PSI.
  32. inline Expected<Optional<uint64_t>> parseHotnessThresholdOption(StringRef Arg) {
  33. if (Arg == "auto")
  34. return None;
  35. int64_t Val;
  36. if (Arg.getAsInteger(10, Val))
  37. return createStringError(llvm::inconvertibleErrorCode(),
  38. "Not an integer: %s", Arg.data());
  39. // Negative integer effectively means no threshold
  40. return Val < 0 ? 0 : Val;
  41. }
  42. // A simple CL parser for '*-remarks-hotness-threshold='
  43. class HotnessThresholdParser : public cl::parser<Optional<uint64_t>> {
  44. public:
  45. HotnessThresholdParser(cl::Option &O) : cl::parser<Optional<uint64_t>>(O) {}
  46. bool parse(cl::Option &O, StringRef ArgName, StringRef Arg,
  47. Optional<uint64_t> &V) {
  48. auto ResultOrErr = parseHotnessThresholdOption(Arg);
  49. if (!ResultOrErr)
  50. return O.error("Invalid argument '" + Arg +
  51. "', only integer or 'auto' is supported.");
  52. V = *ResultOrErr;
  53. return false;
  54. }
  55. };
  56. } // namespace remarks
  57. } // namespace llvm
  58. #endif // LLVM_REMARKS_HOTNESSTHRESHOLDPARSER_H
  59. #ifdef __GNUC__
  60. #pragma GCC diagnostic pop
  61. #endif