SnippetGenerator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===-- SnippetGenerator.h --------------------------------------*- 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. /// Defines the abstract SnippetGenerator class for generating code that allows
  11. /// measuring a certain property of instructions (e.g. latency).
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
  15. #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
  16. #include "Assembler.h"
  17. #include "BenchmarkCode.h"
  18. #include "CodeTemplate.h"
  19. #include "LlvmState.h"
  20. #include "MCInstrDescView.h"
  21. #include "RegisterAliasing.h"
  22. #include "llvm/ADT/CombinationGenerator.h"
  23. #include "llvm/MC/MCInst.h"
  24. #include "llvm/Support/Error.h"
  25. #include <cstdlib>
  26. #include <memory>
  27. #include <vector>
  28. namespace llvm {
  29. namespace exegesis {
  30. std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT);
  31. // Generates code templates that has a self-dependency.
  32. Expected<std::vector<CodeTemplate>>
  33. generateSelfAliasingCodeTemplates(InstructionTemplate Variant);
  34. // Generates code templates without assignment constraints.
  35. Expected<std::vector<CodeTemplate>>
  36. generateUnconstrainedCodeTemplates(const InstructionTemplate &Variant,
  37. StringRef Msg);
  38. // A class representing failures that happened during Benchmark, they are used
  39. // to report informations to the user.
  40. class SnippetGeneratorFailure : public StringError {
  41. public:
  42. SnippetGeneratorFailure(const Twine &S);
  43. };
  44. // Common code for all benchmark modes.
  45. class SnippetGenerator {
  46. public:
  47. struct Options {
  48. unsigned MaxConfigsPerOpcode = 1;
  49. };
  50. explicit SnippetGenerator(const LLVMState &State, const Options &Opts);
  51. virtual ~SnippetGenerator();
  52. // Calls generateCodeTemplate and expands it into one or more BenchmarkCode.
  53. Error generateConfigurations(const InstructionTemplate &Variant,
  54. std::vector<BenchmarkCode> &Benchmarks,
  55. const BitVector &ExtraForbiddenRegs) const;
  56. // Given a snippet, computes which registers the setup code needs to define.
  57. std::vector<RegisterValue> computeRegisterInitialValues(
  58. const std::vector<InstructionTemplate> &Snippet) const;
  59. protected:
  60. const LLVMState &State;
  61. const Options Opts;
  62. private:
  63. // API to be implemented by subclasses.
  64. virtual Expected<std::vector<CodeTemplate>>
  65. generateCodeTemplates(InstructionTemplate Variant,
  66. const BitVector &ForbiddenRegisters) const = 0;
  67. };
  68. // A global Random Number Generator to randomize configurations.
  69. // FIXME: Move random number generation into an object and make it seedable for
  70. // unit tests.
  71. std::mt19937 &randomGenerator();
  72. // Picks a random unsigned integer from 0 to Max (inclusive).
  73. size_t randomIndex(size_t Max);
  74. // Picks a random bit among the bits set in Vector and returns its index.
  75. // Precondition: Vector must have at least one bit set.
  76. size_t randomBit(const BitVector &Vector);
  77. // Picks a random configuration, then selects a random def and a random use from
  78. // it and finally set the selected values in the provided InstructionInstances.
  79. void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
  80. InstructionTemplate &DefIB, InstructionTemplate &UseIB);
  81. // Assigns a Random Value to all Variables in IT that are still Invalid.
  82. // Do not use any of the registers in `ForbiddenRegs`.
  83. Error randomizeUnsetVariables(const LLVMState &State,
  84. const BitVector &ForbiddenRegs,
  85. InstructionTemplate &IT);
  86. } // namespace exegesis
  87. } // namespace llvm
  88. #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H