LLVM 20.0.0git
StableFunctionMap.h
Go to the documentation of this file.
1//===- StableFunctionMap.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// This defines the StableFunctionMap class, to track similar functions.
10// It provides a mechanism to map stable hashes of functions to their
11// corresponding metadata. It includes structures for storing function details
12// and methods for managing and querying these mappings.
13//
14//===---------------------------------------------------------------------===//
15
16#ifndef LLVM_CGDATA_STABLEFUNCTIONMAP_H
17#define LLVM_CGDATA_STABLEFUNCTIONMAP_H
18
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
22
23namespace llvm {
24
25using IndexPairHash = std::pair<IndexPair, stable_hash>;
27
28/// A stable function is a function with a stable hash while tracking the
29/// locations of ignored operands and their hashes.
31 /// The combined stable hash of the function.
33 /// The name of the function.
34 std::string FunctionName;
35 /// The name of the module the function is in.
36 std::string ModuleName;
37 /// The number of instructions.
38 unsigned InstCount;
39 /// A vector of pairs of IndexPair and operand hash which was skipped.
41
43 const std::string ModuleName, unsigned InstCount,
48 StableFunction() = default;
49};
50
52 /// An efficient form of StableFunction for fast look-up
54 /// The combined stable hash of the function.
56 /// Id of the function name.
58 /// Id of the module name.
59 unsigned ModuleNameId;
60 /// The number of instructions.
61 unsigned InstCount;
62 /// A map from an IndexPair to a stable_hash which was skipped.
63 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap;
64
66 stable_hash Hash, unsigned FunctionNameId, unsigned ModuleNameId,
67 unsigned InstCount,
68 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap)
72 };
73
76
77 /// Get the HashToFuncs map for serialization.
78 const HashFuncsMapType &getFunctionMap() const { return HashToFuncs; }
79
80 /// Get the NameToId vector for serialization.
81 const SmallVector<std::string> getNames() const { return IdToName; }
82
83 /// Get an existing ID associated with the given name or create a new ID if it
84 /// doesn't exist.
86
87 /// Get the name associated with a given ID
88 std::optional<std::string> getNameForId(unsigned Id) const;
89
90 /// Insert a `StableFunction` object into the function map. This method
91 /// handles the uniquing of string names and create a `StableFunctionEntry`
92 /// for insertion.
93 void insert(const StableFunction &Func);
94
95 /// Merge a \p OtherMap into this function map.
96 void merge(const StableFunctionMap &OtherMap);
97
98 /// \returns true if there is no stable function entry.
99 bool empty() const { return size() == 0; }
100
101 enum SizeType {
102 UniqueHashCount, // The number of unique hashes in HashToFuncs.
103 TotalFunctionCount, // The number of total functions in HashToFuncs.
104 MergeableFunctionCount, // The number of functions that can be merged based
105 // on their hash.
106 };
107
108 /// \returns the size of StableFunctionMap.
109 /// \p Type is the type of size to return.
110 size_t size(SizeType Type = UniqueHashCount) const;
111
112 /// Finalize the stable function map by trimming content.
113 void finalize(bool SkipTrim = false);
114
115private:
116 /// Insert a `StableFunctionEntry` into the function map directly. This
117 /// method assumes that string names have already been uniqued and the
118 /// `StableFunctionEntry` is ready for insertion.
119 void insert(std::unique_ptr<StableFunctionEntry> FuncEntry) {
120 assert(!Finalized && "Cannot insert after finalization");
121 HashToFuncs[FuncEntry->Hash].emplace_back(std::move(FuncEntry));
122 }
123
124 /// A map from a stable_hash to a vector of functions with that hash.
125 HashFuncsMapType HashToFuncs;
126 /// A vector of strings to hold names.
127 SmallVector<std::string> IdToName;
128 /// A map from StringRef (name) to an ID.
129 StringMap<unsigned> NameToId;
130 /// True if the function map is finalized with minimal content.
131 bool Finalized = false;
132
134};
135
136} // namespace llvm
137
138#endif
This file defines the StringMap class.
arc branch finalize
This file defines the DenseMap class.
std::string Name
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::pair< IndexPair, stable_hash > IndexPairHash
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
An efficient form of StableFunction for fast look-up.
StableFunctionEntry(stable_hash Hash, unsigned FunctionNameId, unsigned ModuleNameId, unsigned InstCount, std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap)
stable_hash Hash
The combined stable hash of the function.
std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap
A map from an IndexPair to a stable_hash which was skipped.
unsigned InstCount
The number of instructions.
unsigned FunctionNameId
Id of the function name.
unsigned ModuleNameId
Id of the module name.
size_t size(SizeType Type=UniqueHashCount) const
void insert(const StableFunction &Func)
Insert a StableFunction object into the function map.
const SmallVector< std::string > getNames() const
Get the NameToId vector for serialization.
const HashFuncsMapType & getFunctionMap() const
Get the HashToFuncs map for serialization.
void merge(const StableFunctionMap &OtherMap)
Merge a OtherMap into this function map.
std::optional< std::string > getNameForId(unsigned Id) const
Get the name associated with a given ID.
unsigned getIdOrCreateForName(StringRef Name)
Get an existing ID associated with the given name or create a new ID if it doesn't exist.
DenseMap< stable_hash, SmallVector< std::unique_ptr< StableFunctionEntry > > > HashFuncsMapType
A stable function is a function with a stable hash while tracking the locations of ignored operands a...
unsigned InstCount
The number of instructions.
StableFunction()=default
StableFunction(stable_hash Hash, const std::string FunctionName, const std::string ModuleName, unsigned InstCount, IndexOperandHashVecType &&IndexOperandHashes)
stable_hash Hash
The combined stable hash of the function.
IndexOperandHashVecType IndexOperandHashes
A vector of pairs of IndexPair and operand hash which was skipped.
std::string FunctionName
The name of the function.
std::string ModuleName
The name of the module the function is in.