blob: 9493c5848165f463f0a5e4af8b900c65adc6a701 [file] [log] [blame]
Clement Courbetd939f6d2018-09-13 07:40:53 +00001//===-- 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 Chatelet7f8d3102018-09-26 11:57:24 +000021#include "CodeTemplate.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000022#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
31namespace exegesis {
32
33// A class representing failures that happened during Benchmark, they are used
34// to report informations to the user.
35class SnippetGeneratorFailure : public llvm::StringError {
36public:
37 SnippetGeneratorFailure(const llvm::Twine &S);
38};
39
40// Common code for all benchmark modes.
41class SnippetGenerator {
42public:
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 Chateletc96a97b2018-09-20 12:22:18 +000052 std::vector<RegisterValue> computeRegisterInitialValues(
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000053 const std::vector<InstructionTemplate> &Snippet) const;
Clement Courbetd939f6d2018-09-13 07:40:53 +000054
55protected:
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
67private:
68 // API to be implemented by subclasses.
69 virtual llvm::Expected<CodeTemplate>
70 generateCodeTemplate(unsigned Opcode) const = 0;
71};
72
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +000073// 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.
76std::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.
80size_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.
84void 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`.
89void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs,
90 InstructionTemplate &IT);
91
Clement Courbetd939f6d2018-09-13 07:40:53 +000092} // namespace exegesis
93
94#endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H