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