LLVM 22.0.0git
StructuralHash.cpp
Go to the documentation of this file.
1//===- StructuralHash.cpp - Function Hash Printing ------------------------===//
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 defines the StructuralHashPrinterPass which is used to show
10// the structural hash of all functions in a module and the module itself.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/IR/Module.h"
17#include "llvm/Support/Format.h"
18
19using namespace llvm;
20
23 OS << "Module Hash: "
24 << format("%016" PRIx64,
26 << "\n";
27 for (Function &F : M) {
28 if (F.isDeclaration())
29 continue;
31 auto IgnoreOp = [&](const Instruction *I, unsigned OpndIdx) {
32 return I->getOpcode() == Instruction::Call &&
33 isa<Constant>(I->getOperand(OpndIdx));
34 };
35 auto FuncHashInfo = StructuralHashWithDifferences(F, IgnoreOp);
36 OS << "Function " << F.getName()
37 << " Hash: " << format("%016" PRIx64, FuncHashInfo.FunctionHash)
38 << "\n";
39 for (auto &[IndexPair, OpndHash] : *FuncHashInfo.IndexOperandHashMap) {
40 auto [InstIndex, OpndIndex] = IndexPair;
41 OS << "\tIgnored Operand Hash: " << format("%016" PRIx64, OpndHash)
42 << " at (" << InstIndex << "," << OpndIndex << ")\n";
43 }
44 } else {
45 OS << "Function " << F.getName() << " Hash: "
46 << format(
47 "%016" PRIx64,
49 << "\n";
50 }
51 }
53}
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
ModuleAnalysisManager MAM
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI FunctionHashInfo StructuralHashWithDifferences(const Function &F, IgnoreOperandFunc IgnoreOp)
Computes a structural hash of a given function, considering the structure and content of the function...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:126
std::pair< unsigned, unsigned > IndexPair
The pair of an instruction index and a operand index.
LLVM_ABI stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.
@ Detailed
Hash with opcode only.
@ CallTargetIgnored
Hash with opcode and operands.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39