Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 1 | //===-- SnippetGenerator.h --------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 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 | |
| 15 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |
| 16 | #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |
| 17 | |
| 18 | #include "Assembler.h" |
| 19 | #include "BenchmarkCode.h" |
Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 20 | #include "CodeTemplate.h" |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 21 | #include "LlvmState.h" |
| 22 | #include "MCInstrDescView.h" |
| 23 | #include "RegisterAliasing.h" |
| 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/Support/Error.h" |
| 26 | #include <cstdlib> |
| 27 | #include <memory> |
| 28 | #include <vector> |
| 29 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 30 | namespace llvm { |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 31 | namespace exegesis { |
| 32 | |
Guillaume Chatelet | fcbb6f3 | 2018-10-17 11:37:28 +0000 | [diff] [blame] | 33 | std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT); |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 34 | |
| 35 | // Generates code templates that has a self-dependency. |
| 36 | llvm::Expected<std::vector<CodeTemplate>> |
| 37 | generateSelfAliasingCodeTemplates(const Instruction &Instr); |
| 38 | |
| 39 | // Generates code templates without assignment constraints. |
| 40 | llvm::Expected<std::vector<CodeTemplate>> |
| 41 | generateUnconstrainedCodeTemplates(const Instruction &Instr, |
| 42 | llvm::StringRef Msg); |
| 43 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 44 | // A class representing failures that happened during Benchmark, they are used |
| 45 | // to report informations to the user. |
| 46 | class SnippetGeneratorFailure : public llvm::StringError { |
| 47 | public: |
| 48 | SnippetGeneratorFailure(const llvm::Twine &S); |
| 49 | }; |
| 50 | |
| 51 | // Common code for all benchmark modes. |
| 52 | class SnippetGenerator { |
| 53 | public: |
| 54 | explicit SnippetGenerator(const LLVMState &State); |
| 55 | |
| 56 | virtual ~SnippetGenerator(); |
| 57 | |
| 58 | // Calls generateCodeTemplate and expands it into one or more BenchmarkCode. |
| 59 | llvm::Expected<std::vector<BenchmarkCode>> |
Guillaume Chatelet | 9b59238 | 2018-10-10 14:57:32 +0000 | [diff] [blame] | 60 | generateConfigurations(const Instruction &Instr) const; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 61 | |
| 62 | // Given a snippet, computes which registers the setup code needs to define. |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 63 | std::vector<RegisterValue> computeRegisterInitialValues( |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 64 | const std::vector<InstructionTemplate> &Snippet) const; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 65 | |
| 66 | protected: |
| 67 | const LLVMState &State; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 68 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 69 | private: |
| 70 | // API to be implemented by subclasses. |
Guillaume Chatelet | 296a862 | 2018-10-15 09:09:19 +0000 | [diff] [blame] | 71 | virtual llvm::Expected<std::vector<CodeTemplate>> |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame^] | 72 | generateCodeTemplates(const Instruction &Instr, |
| 73 | const BitVector &ForbiddenRegisters) const = 0; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 76 | // A global Random Number Generator to randomize configurations. |
| 77 | // FIXME: Move random number generation into an object and make it seedable for |
| 78 | // unit tests. |
| 79 | std::mt19937 &randomGenerator(); |
| 80 | |
Roman Lebedev | a822358 | 2019-04-08 10:11:00 +0000 | [diff] [blame] | 81 | // Picks a random unsigned integer from 0 to Max (inclusive). |
| 82 | size_t randomIndex(size_t Max); |
| 83 | |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 84 | // Picks a random bit among the bits set in Vector and returns its index. |
| 85 | // Precondition: Vector must have at least one bit set. |
| 86 | size_t randomBit(const llvm::BitVector &Vector); |
| 87 | |
| 88 | // Picks a random configuration, then selects a random def and a random use from |
| 89 | // it and finally set the selected values in the provided InstructionInstances. |
| 90 | void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations, |
| 91 | InstructionTemplate &DefIB, InstructionTemplate &UseIB); |
| 92 | |
| 93 | // Assigns a Random Value to all Variables in IT that are still Invalid. |
| 94 | // Do not use any of the registers in `ForbiddenRegs`. |
Roman Lebedev | 404bdb1 | 2019-04-06 14:16:26 +0000 | [diff] [blame] | 95 | void randomizeUnsetVariables(const ExegesisTarget &Target, |
| 96 | const llvm::BitVector &ForbiddenRegs, |
Guillaume Chatelet | 415b2fb | 2018-10-01 12:19:10 +0000 | [diff] [blame] | 97 | InstructionTemplate &IT); |
| 98 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 99 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 100 | } // namespace llvm |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 101 | |
| 102 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H |