Skip to content

Commit 925b847

Browse files
committed
Address comments from ilovepi
1 parent b877be6 commit 925b847

File tree

6 files changed

+71
-49
lines changed

6 files changed

+71
-49
lines changed

llvm/include/llvm/Analysis/StructuralHash.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313

1414
namespace llvm {
1515

16+
enum class StructuralHashOptions { None, Detailed, CallTargetIgnored };
17+
1618
/// Printer pass for StructuralHashes
1719
class StructuralHashPrinterPass
1820
: public PassInfoMixin<StructuralHashPrinterPass> {
1921
raw_ostream &OS;
20-
bool EnableDetailedStructuralHash;
22+
const StructuralHashOptions Options;
2123

2224
public:
23-
explicit StructuralHashPrinterPass(raw_ostream &OS, bool Detailed)
24-
: OS(OS), EnableDetailedStructuralHash(Detailed) {}
25+
explicit StructuralHashPrinterPass(raw_ostream &OS,
26+
StructuralHashOptions Options)
27+
: OS(OS), Options(Options) {}
2528

2629
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
2730

llvm/lib/Analysis/StructuralHash.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,33 @@ using namespace llvm;
2121
PreservedAnalyses StructuralHashPrinterPass::run(Module &M,
2222
ModuleAnalysisManager &MAM) {
2323
OS << "Module Hash: "
24-
<< format("%016" PRIx64, StructuralHash(M, EnableDetailedStructuralHash))
24+
<< format("%016" PRIx64,
25+
StructuralHash(M, Options != StructuralHashOptions::None))
2526
<< "\n";
2627
for (Function &F : M) {
2728
if (F.isDeclaration())
2829
continue;
29-
OS << "Function " << F.getName() << " Hash: "
30-
<< format("%016" PRIx64, StructuralHash(F, EnableDetailedStructuralHash))
31-
<< "\n";
30+
if (Options == StructuralHashOptions::CallTargetIgnored) {
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,
48+
StructuralHash(F, Options == StructuralHashOptions::Detailed))
49+
<< "\n";
50+
}
3251
}
3352
return PreservedAnalyses::all();
3453
}

llvm/lib/IR/StructuralHash.cpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ class StructuralHashImpl {
113113
}
114114

115115
switch (C->getValueID()) {
116-
case Value::UndefValueVal:
117-
case Value::PoisonValueVal:
118-
case Value::ConstantTokenNoneVal: {
119-
return stable_hash_combine(Hashes);
120-
}
121116
case Value::ConstantIntVal: {
122117
const APInt &Int = cast<ConstantInt>(C)->getValue();
123118
Hashes.emplace_back(hashAPInt(Int));
@@ -128,33 +123,11 @@ class StructuralHashImpl {
128123
Hashes.emplace_back(hashAPFloat(APF));
129124
return stable_hash_combine(Hashes);
130125
}
131-
case Value::ConstantArrayVal: {
132-
const ConstantArray *A = cast<ConstantArray>(C);
133-
for (auto &Op : A->operands()) {
134-
auto H = hashConstant(cast<Constant>(Op));
135-
Hashes.emplace_back(H);
136-
}
137-
return stable_hash_combine(Hashes);
138-
}
139-
case Value::ConstantStructVal: {
140-
const ConstantStruct *S = cast<ConstantStruct>(C);
141-
for (auto &Op : S->operands()) {
142-
auto H = hashConstant(cast<Constant>(Op));
143-
Hashes.emplace_back(H);
144-
}
145-
return stable_hash_combine(Hashes);
146-
}
147-
case Value::ConstantVectorVal: {
148-
const ConstantVector *V = cast<ConstantVector>(C);
149-
for (auto &Op : V->operands()) {
150-
auto H = hashConstant(cast<Constant>(Op));
151-
Hashes.emplace_back(H);
152-
}
153-
return stable_hash_combine(Hashes);
154-
}
126+
case Value::ConstantArrayVal:
127+
case Value::ConstantStructVal:
128+
case Value::ConstantVectorVal:
155129
case Value::ConstantExprVal: {
156-
const ConstantExpr *E = cast<ConstantExpr>(C);
157-
for (auto &Op : E->operands()) {
130+
for (const auto &Op : C->operands()) {
158131
auto H = hashConstant(cast<Constant>(Op));
159132
Hashes.emplace_back(H);
160133
}
@@ -313,9 +286,11 @@ class StructuralHashImpl {
313286
}
314287

315288
uint64_t getHash() const { return Hash; }
289+
316290
std::unique_ptr<IndexInstrMap> getIndexInstrMap() {
317291
return std::move(IndexInstruction);
318292
}
293+
319294
std::unique_ptr<IndexOperandHashMapType> getIndexPairOpndHashMap() {
320295
return std::move(IndexOperandHashMap);
321296
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,9 +1168,19 @@ Expected<std::string> parseMemProfUsePassOptions(StringRef Params) {
11681168
return Result;
11691169
}
11701170

1171-
Expected<bool> parseStructuralHashPrinterPassOptions(StringRef Params) {
1172-
return PassBuilder::parseSinglePassOption(Params, "detailed",
1173-
"StructuralHashPrinterPass");
1171+
Expected<StructuralHashOptions>
1172+
parseStructuralHashPrinterPassOptions(StringRef Params) {
1173+
if (Params.empty())
1174+
return StructuralHashOptions::None;
1175+
else if (Params == "detailed")
1176+
return StructuralHashOptions::Detailed;
1177+
else if (Params == "call-target-ignored")
1178+
return StructuralHashOptions::CallTargetIgnored;
1179+
else
1180+
return make_error<StringError>(
1181+
formatv("invalid structural hash printer parameter '{0}' ", Params)
1182+
.str(),
1183+
inconvertibleErrorCode());
11741184
}
11751185

11761186
Expected<bool> parseWinEHPrepareOptions(StringRef Params) {

llvm/lib/Passes/PassRegistry.def

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,11 @@ MODULE_PASS_WITH_PARAMS(
220220
parseMSanPassOptions, "recover;kernel;eager-checks;track-origins=N")
221221
MODULE_PASS_WITH_PARAMS(
222222
"print<structural-hash>", "StructuralHashPrinterPass",
223-
[](bool EnableDetailedStructuralHash) {
224-
return StructuralHashPrinterPass(dbgs(), EnableDetailedStructuralHash);
223+
[](StructuralHashOptions Options) {
224+
return StructuralHashPrinterPass(dbgs(), Options);
225225
},
226-
parseStructuralHashPrinterPassOptions, "detailed")
226+
parseStructuralHashPrinterPassOptions, "detailed;call-target-ignored")
227+
227228
#undef MODULE_PASS_WITH_PARAMS
228229

229230
#ifndef CGSCC_ANALYSIS
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
; RUN: opt -passes='print<structural-hash>' -disable-output %s 2>&1 | FileCheck %s
22
; RUN: opt -passes='print<structural-hash><detailed>' -disable-output %s 2>&1 | FileCheck %s -check-prefix=DETAILED-HASH
3+
; RUN: opt -passes='print<structural-hash><call-target-ignored>' -disable-output %s 2>&1 | FileCheck %s -check-prefix=CALLTARGETIGNORED-HASH
34

45
; Add a declaration so that we can test we skip it.
5-
declare i64 @d1()
6+
declare i64 @d1(i64)
7+
declare i64 @e1(i64)
68

79
define i64 @f1(i64 %a) {
810
%b = add i64 %a, 1
9-
ret i64 %b
11+
%c = call i64 @d1(i64 %b)
12+
ret i64 %c
1013
}
1114

12-
define i32 @f2(i32 %a) {
13-
%b = add i32 %a, 2
14-
ret i32 %b
15+
define i64 @f2(i64 %a) {
16+
%b = add i64 %a, 1
17+
%c = call i64 @e1(i64 %b)
18+
ret i64 %c
1519
}
1620

1721
; CHECK: Module Hash: {{([a-f0-9]{16,})}}
@@ -22,3 +26,13 @@ define i32 @f2(i32 %a) {
2226
; DETAILED-HASH-NEXT: Function f1 Hash: [[DF1H:([a-f0-9]{16,})]]
2327
; DETAILED-HASH-NOT: [[DF1H]]
2428
; DETAILED-HASH-NEXT: Function f2 Hash: {{([a-f0-9]{16,})}}
29+
30+
; When ignoring the call target, check if `f1` and `f2` produce the same function hash.
31+
; The index for the call instruction is 1, and the index of the call target operand is 1.
32+
; The ignored operand hashes for different call targets should be different.
33+
; CALLTARGETIGNORED-HASH: Module Hash: {{([a-f0-9]{16,})}}
34+
; CALLTARGETIGNORED-HASH-NEXT: Function f1 Hash: [[IF1H:([a-f0-9]{16,})]]
35+
; CALLTARGETIGNORED-HASH-NEXT: Ignored Operand Hash: [[IO1H:([a-f0-9]{16,})]] at (1,1)
36+
; CALLTARGETIGNORED-HASH-NEXT: Function f2 Hash: [[IF1H]]
37+
; CALLTARGETIGNORED-HASH-NOT: [[IO1H]]
38+
; CALLTARGETIGNORED-HASH-NEXT: Ignored Operand Hash: {{([a-f0-9]{16,})}} at (1,1)

0 commit comments

Comments
 (0)