[UpdateCCTestChecks] Detect function mangled name on separate line
[llvm-core.git] / tools / llvm-exegesis / lib / SnippetGenerator.h
blob8e8cd6fcb9952f572a4d9f94c1fde12c5fb385b4
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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
18 #include "Assembler.h"
19 #include "BenchmarkCode.h"
20 #include "CodeTemplate.h"
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>
30 namespace llvm {
31 namespace exegesis {
33 std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT);
35 // Generates code templates that has a self-dependency.
36 llvm::Expected<std::vector<CodeTemplate>>
37 generateSelfAliasingCodeTemplates(const Instruction &Instr);
39 // Generates code templates without assignment constraints.
40 llvm::Expected<std::vector<CodeTemplate>>
41 generateUnconstrainedCodeTemplates(const Instruction &Instr,
42 llvm::StringRef Msg);
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);
51 // Common code for all benchmark modes.
52 class SnippetGenerator {
53 public:
54 struct Options {
55 unsigned MaxConfigsPerOpcode = 1;
58 explicit SnippetGenerator(const LLVMState &State, const Options &Opts);
60 virtual ~SnippetGenerator();
62 // Calls generateCodeTemplate and expands it into one or more BenchmarkCode.
63 llvm::Expected<std::vector<BenchmarkCode>>
64 generateConfigurations(const Instruction &Instr,
65 const llvm::BitVector &ExtraForbiddenRegs) const;
67 // Given a snippet, computes which registers the setup code needs to define.
68 std::vector<RegisterValue> computeRegisterInitialValues(
69 const std::vector<InstructionTemplate> &Snippet) const;
71 protected:
72 const LLVMState &State;
73 const Options Opts;
75 private:
76 // API to be implemented by subclasses.
77 virtual llvm::Expected<std::vector<CodeTemplate>>
78 generateCodeTemplates(const Instruction &Instr,
79 const BitVector &ForbiddenRegisters) const = 0;
82 // A global Random Number Generator to randomize configurations.
83 // FIXME: Move random number generation into an object and make it seedable for
84 // unit tests.
85 std::mt19937 &randomGenerator();
87 // Picks a random unsigned integer from 0 to Max (inclusive).
88 size_t randomIndex(size_t Max);
90 // Picks a random bit among the bits set in Vector and returns its index.
91 // Precondition: Vector must have at least one bit set.
92 size_t randomBit(const llvm::BitVector &Vector);
94 // Picks a random configuration, then selects a random def and a random use from
95 // it and finally set the selected values in the provided InstructionInstances.
96 void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
97 InstructionTemplate &DefIB, InstructionTemplate &UseIB);
99 // Assigns a Random Value to all Variables in IT that are still Invalid.
100 // Do not use any of the registers in `ForbiddenRegs`.
101 void randomizeUnsetVariables(const ExegesisTarget &Target,
102 const llvm::BitVector &ForbiddenRegs,
103 InstructionTemplate &IT);
105 } // namespace exegesis
106 } // namespace llvm
108 #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H