Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 1 | //===-- SnippetGenerator.h --------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// Defines the abstract SnippetGenerator class for generating code that allows |
| 12 | /// measuring a certain property of instructions (e.g. latency). |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |
| 17 | #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |
| 18 | |
| 19 | #include "Assembler.h" |
| 20 | #include "BenchmarkCode.h" |
Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 21 | #include "CodeTemplate.h" |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 22 | #include "LlvmState.h" |
| 23 | #include "MCInstrDescView.h" |
| 24 | #include "RegisterAliasing.h" |
| 25 | #include "llvm/MC/MCInst.h" |
| 26 | #include "llvm/Support/Error.h" |
| 27 | #include <cstdlib> |
| 28 | #include <memory> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace exegesis { |
| 32 | |
| 33 | // A class representing failures that happened during Benchmark, they are used |
| 34 | // to report informations to the user. |
| 35 | class SnippetGeneratorFailure : public llvm::StringError { |
| 36 | public: |
| 37 | SnippetGeneratorFailure(const llvm::Twine &S); |
| 38 | }; |
| 39 | |
| 40 | // Common code for all benchmark modes. |
| 41 | class SnippetGenerator { |
| 42 | public: |
| 43 | explicit SnippetGenerator(const LLVMState &State); |
| 44 | |
| 45 | virtual ~SnippetGenerator(); |
| 46 | |
| 47 | // Calls generateCodeTemplate and expands it into one or more BenchmarkCode. |
| 48 | llvm::Expected<std::vector<BenchmarkCode>> |
| 49 | generateConfigurations(unsigned Opcode) const; |
| 50 | |
| 51 | // Given a snippet, computes which registers the setup code needs to define. |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 52 | std::vector<RegisterValue> computeRegisterInitialValues( |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 53 | const std::vector<InstructionTemplate> &Snippet) const; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 54 | |
| 55 | protected: |
| 56 | const LLVMState &State; |
| 57 | const RegisterAliasingTrackerCache RATC; |
| 58 | |
| 59 | // Generates a single code template that has a self-dependency. |
| 60 | llvm::Expected<CodeTemplate> |
| 61 | generateSelfAliasingCodeTemplate(const Instruction &Instr) const; |
| 62 | // Generates a single code template without assignment constraints. |
| 63 | llvm::Expected<CodeTemplate> |
| 64 | generateUnconstrainedCodeTemplate(const Instruction &Instr, |
| 65 | llvm::StringRef Msg) const; |
| 66 | |
| 67 | private: |
| 68 | // API to be implemented by subclasses. |
| 69 | virtual llvm::Expected<CodeTemplate> |
| 70 | generateCodeTemplate(unsigned Opcode) const = 0; |
| 71 | }; |
| 72 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame^] | 73 | // A global Random Number Generator to randomize configurations. |
| 74 | // FIXME: Move random number generation into an object and make it seedable for |
| 75 | // unit tests. |
| 76 | std::mt19937 &randomGenerator(); |
| 77 | |
| 78 | // Picks a random bit among the bits set in Vector and returns its index. |
| 79 | // Precondition: Vector must have at least one bit set. |
| 80 | size_t randomBit(const llvm::BitVector &Vector); |
| 81 | |
| 82 | // Picks a random configuration, then selects a random def and a random use from |
| 83 | // it and finally set the selected values in the provided InstructionInstances. |
| 84 | void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations, |
| 85 | InstructionTemplate &DefIB, InstructionTemplate &UseIB); |
| 86 | |
| 87 | // Assigns a Random Value to all Variables in IT that are still Invalid. |
| 88 | // Do not use any of the registers in `ForbiddenRegs`. |
| 89 | void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs, |
| 90 | InstructionTemplate &IT); |
| 91 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 92 | } // namespace exegesis |
| 93 | |
| 94 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |