Skip to content

Commit 24768bb

Browse files
committed
Address comments from ilovepi
1 parent 6c35be8 commit 24768bb

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
@@ -110,11 +110,6 @@ class StructuralHashImpl {
110110
}
111111

112112
switch (C->getValueID()) {
113-
case Value::UndefValueVal:
114-
case Value::PoisonValueVal:
115-
case Value::ConstantTokenNoneVal: {
116-
return stable_hash_combine(Hashes);
117-
}
118113
case Value::ConstantIntVal: {
119114
const APInt &Int = cast<ConstantInt>(C)->getValue();
120115
Hashes.emplace_back(hashAPInt(Int));
@@ -125,33 +120,11 @@ class StructuralHashImpl {
125120
Hashes.emplace_back(hashAPFloat(APF));
126121
return stable_hash_combine(Hashes);
127122
}
128-
case Value::ConstantArrayVal: {
129-
const ConstantArray *A = cast<ConstantArray>(C);
130-
for (auto &Op : A->operands()) {
131-
auto H = hashConstant(cast<Constant>(Op));
132-
Hashes.emplace_back(H);
133-
}
134-
return stable_hash_combine(Hashes);
135-
}
136-
case Value::ConstantStructVal: {
137-
const ConstantStruct *S = cast<ConstantStruct>(C);
138-
for (auto &Op : S->operands()) {
139-
auto H = hashConstant(cast<Constant>(Op));
140-
Hashes.emplace_back(H);
141-
}
142-
return stable_hash_combine(Hashes);
143-
}
144-
case Value::ConstantVectorVal: {
145-
const ConstantVector *V = cast<ConstantVector>(C);
146-
for (auto &Op : V->operands()) {
147-
auto H = hashConstant(cast<Constant>(Op));
148-
Hashes.emplace_back(H);
149-
}
150-
return stable_hash_combine(Hashes);
151-
}
123+
case Value::ConstantArrayVal:
124+
case Value::ConstantStructVal:
125+
case Value::ConstantVectorVal:
152126
case Value::ConstantExprVal: {
153-
const ConstantExpr *E = cast<ConstantExpr>(C);
154-
for (auto &Op : E->operands()) {
127+
for (const auto &Op : C->operands()) {
155128
auto H = hashConstant(cast<Constant>(Op));
156129
Hashes.emplace_back(H);
157130
}
@@ -307,9 +280,11 @@ class StructuralHashImpl {
307280
}
308281

309282
uint64_t getHash() const { return Hash; }
283+
310284
std::unique_ptr<IndexInstrMap> getIndexInstrMap() {
311285
return std::move(IndexInstruction);
312286
}
287+
313288
std::unique_ptr<IndexOperandHashMapType> getIndexPairOpndHashMap() {
314289
return std::move(IndexOperandHashMap);
315290
}

llvm/lib/Passes/PassBuilder.cpp

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

1178-
Expected<bool> parseStructuralHashPrinterPassOptions(StringRef Params) {
1179-
return PassBuilder::parseSinglePassOption(Params, "detailed",
1180-
"StructuralHashPrinterPass");
1178+
Expected<StructuralHashOptions>
1179+
parseStructuralHashPrinterPassOptions(StringRef Params) {
1180+
if (Params.empty())
1181+
return StructuralHashOptions::None;
1182+
else if (Params == "detailed")
1183+
return StructuralHashOptions::Detailed;
1184+
else if (Params == "call-target-ignored")
1185+
return StructuralHashOptions::CallTargetIgnored;
1186+
else
1187+
return make_error<StringError>(
1188+
formatv("invalid structural hash printer parameter '{0}' ", Params)
1189+
.str(),
1190+
inconvertibleErrorCode());
11811191
}
11821192

11831193
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)