LLVM 24.0.0git
StructuralHash.h
Go to the documentation of this file.
1//===- llvm/IR/StructuralHash.h - IR Hashing --------------------*- 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 file provides hashing of the LLVM IR structure to be used to check
10// Passes modification status.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_STRUCTURALHASH_H
15#define LLVM_IR_STRUCTURALHASH_H
16
17#include "llvm/ADT/MapVector.h"
19#include "llvm/IR/Instruction.h"
21#include <cstdint>
22
23namespace llvm {
24
25class Function;
26class Module;
27
28/// Returns a hash of the function \p F.
29/// \param F The function to hash.
30/// \param DetailedHash Whether or not to encode additional information in the
31/// hash. The additional information added into the hash when this flag is set
32/// to true includes instruction and operand type information.
34 bool DetailedHash = false);
35
36/// Returns a hash of the global variable \p G.
37/// \param SourceQualifyLocalGlobals Whether to combine the hash of a global
38/// with local linkage with the source file name of its module. Such globals
39/// are hashed by name, and the same name in different modules denotes
40/// different storage; qualifying with the source lets clients that compare
41/// hashes across modules tell them apart. Globals hashed by content are not
42/// affected.
44 bool SourceQualifyLocalGlobals = false);
45
46/// Returns a hash of the module \p M by hashing all functions and global
47/// variables contained within. \param M The module to hash. \param DetailedHash
48/// Whether or not to encode additional information in the function hashes that
49/// composed the module hash.
50LLVM_ABI stable_hash StructuralHash(const Module &M, bool DetailedHash = false);
51
52/// The pair of an instruction index and a operand index.
53using IndexPair = std::pair<unsigned, unsigned>;
54
55/// A map from an instruction index to an instruction pointer.
57
58/// A map from an IndexPair to a stable hash.
60
61/// A function that takes an instruction and an operand index and returns true
62/// if the operand should be ignored in the function hash computation.
63using IgnoreOperandFunc = std::function<bool(const Instruction *, unsigned)>;
64
66 /// A hash value representing the structural content of the function
68 /// A mapping from instruction indices to instruction pointers
69 std::unique_ptr<IndexInstrMap> IndexInstruction;
70 /// A mapping from pairs of instruction indices and operand indices
71 /// to the hashes of the operands. This can be used to analyze or
72 /// reconstruct the differences in ignored operands
73 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap;
74
76 std::unique_ptr<IndexInstrMap> IndexInstruction,
77 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap)
78 : FunctionHash(FuntionHash),
81};
82
83/// Computes a structural hash of a given function, considering the structure
84/// and content of the function's instructions while allowing for selective
85/// ignoring of certain operands based on custom criteria. This hash can be used
86/// to identify functions that are structurally similar or identical, which is
87/// useful in optimizations, deduplication, or analysis tasks.
88/// \param F The function to hash.
89/// \param IgnoreOp A callable that takes an instruction and an operand index,
90/// and returns true if the operand should be ignored in the hash computation.
91/// \param SourceQualifyLocalGlobals Whether to qualify the hashes of globals
92/// with local linkage with their module's source file name.
93/// \return A FunctionHashInfo structure
94LLVM_ABI FunctionHashInfo
96 bool SourceQualifyLocalGlobals = false);
97
98} // end namespace llvm
99
100#endif
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
#define G(x, y, z)
Definition MD5.cpp:55
This file implements a map that provides insertion order iteration.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This is an optimization pass for GlobalISel generic memory operations.
std::function< bool(const Instruction *, unsigned)> IgnoreOperandFunc
A function that takes an instruction and an operand index and returns true if the operand should be i...
uint64_t stable_hash
An opaque object representing a stable hash code.
MapVector< unsigned, Instruction * > IndexInstrMap
A map from an instruction index to an instruction pointer.
DenseMap< IndexPair, stable_hash > IndexOperandHashMapType
A map from an IndexPair to a stable hash.
std::pair< unsigned, unsigned > IndexPair
The pair of an instruction index and a operand index.
LLVM_ABI FunctionHashInfo StructuralHashWithDifferences(const Function &F, IgnoreOperandFunc IgnoreOp, bool SourceQualifyLocalGlobals=false)
Computes a structural hash of a given function, considering the structure and content of the function...
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:1933
LLVM_ABI stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
stable_hash FunctionHash
A hash value representing the structural content of the function.
std::unique_ptr< IndexInstrMap > IndexInstruction
A mapping from instruction indices to instruction pointers.
std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap
A mapping from pairs of instruction indices and operand indices to the hashes of the operands.
FunctionHashInfo(stable_hash FuntionHash, std::unique_ptr< IndexInstrMap > IndexInstruction, std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap)