SnippetGenerator.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const BitVector &ForbiddenRegisters);
  35. // Generates code templates without assignment constraints.
  36. Expected<std::vector<CodeTemplate>>
  37. generateUnconstrainedCodeTemplates(const InstructionTemplate &Variant,
  38. StringRef Msg);
  39. // A class representing failures that happened during Benchmark, they are used
  40. // to report informations to the user.
  41. class SnippetGeneratorFailure : public StringError {
  42. public:
  43. SnippetGeneratorFailure(const Twine &S);
  44. };
  45. // Common code for all benchmark modes.
  46. class SnippetGenerator {
  47. public:
  48. struct Options {
  49. unsigned MaxConfigsPerOpcode = 1;
  50. };
  51. explicit SnippetGenerator(const LLVMState &State, const Options &Opts);
  52. virtual ~SnippetGenerator();
  53. // Calls generateCodeTemplate and expands it into one or more BenchmarkCode.
  54. Error generateConfigurations(const InstructionTemplate &Variant,
  55. std::vector<BenchmarkCode> &Benchmarks,
  56. const BitVector &ExtraForbiddenRegs) const;
  57. // Given a snippet, computes which registers the setup code needs to define.
  58. std::vector<RegisterValue> computeRegisterInitialValues(
  59. const std::vector<InstructionTemplate> &Snippet) const;
  60. protected:
  61. const LLVMState &State;
  62. const Options Opts;
  63. private:
  64. // API to be implemented by subclasses.
  65. virtual Expected<std::vector<CodeTemplate>>
  66. generateCodeTemplates(InstructionTemplate Variant,
  67. const BitVector &ForbiddenRegisters) const = 0;
  68. };
  69. // A global Random Number Generator to randomize configurations.
  70. // FIXME: Move random number generation into an object and make it seedable for
  71. // unit tests.
  72. std::mt19937 &randomGenerator();
  73. // Picks a random unsigned integer from 0 to Max (inclusive).
  74. size_t randomIndex(size_t Max);
  75. // Picks a random bit among the bits set in Vector and returns its index.
  76. // Precondition: Vector must have at least one bit set.
  77. size_t randomBit(const BitVector &Vector);
  78. // Picks a first bit that is common to these two vectors.
  79. std::optional<int> getFirstCommonBit(const BitVector &A, const BitVector &B);
  80. // Picks a random configuration, then selects a random def and a random use from
  81. // it and finally set the selected values in the provided InstructionInstances.
  82. void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
  83. InstructionTemplate &DefIB, InstructionTemplate &UseIB);
  84. // Assigns a Random Value to all Variables in IT that are still Invalid.
  85. // Do not use any of the registers in `ForbiddenRegs`.
  86. Error randomizeUnsetVariables(const LLVMState &State,
  87. const BitVector &ForbiddenRegs,
  88. InstructionTemplate &IT);
  89. } // namespace exegesis
  90. } // namespace llvm
  91. #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H