Skip to content

Commit b7ce778

Browse files
committed
[HashRecognize] Fix the analysis result
Make the analysis return a proper analysis result that can be cached, instead of a HashRecognize object which has to be called to perform the actual analysis. This also means that we need a function_ref to generate the Sarwate table returned along with the analysis result. The issue was discovered when attempting to use the analysis to perform a transform.
1 parent 156a64c commit b7ce778

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

llvm/include/llvm/Analysis/HashRecognize.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ struct PolynomialInfo {
6666
// Set to true in the case of big-endian.
6767
bool ByteOrderSwapped;
6868

69+
// A function_ref to generate the Sarwate lookup-table, which can be used to
70+
// optimize CRC in the absence of target-specific instructions.
71+
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable;
72+
6973
// An optional auxiliary checksum that augments the LHS. In the case of CRC,
7074
// it is XOR'ed with the LHS, so that the computation's final remainder is
7175
// zero.
7276
Value *LHSAux;
7377

7478
PolynomialInfo(unsigned TripCount, Value *LHS, const APInt &RHS,
7579
Value *ComputedValue, bool ByteOrderSwapped,
80+
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable,
7681
Value *LHSAux = nullptr);
7782
};
7883

@@ -84,12 +89,9 @@ class HashRecognize {
8489
public:
8590
HashRecognize(const Loop &L, ScalarEvolution &SE);
8691

87-
// The main analysis entry point.
92+
// The main analysis entry points.
8893
std::variant<PolynomialInfo, ErrBits, StringRef> recognizeCRC() const;
89-
90-
// Auxilary entry point after analysis to interleave the generating polynomial
91-
// and return a 256-entry CRC table.
92-
CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped) const;
94+
std::optional<PolynomialInfo> getResult() const;
9395

9496
void print(raw_ostream &OS) const;
9597

@@ -107,15 +109,6 @@ class HashRecognizePrinterPass
107109
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
108110
LoopStandardAnalysisResults &AR, LPMUpdater &);
109111
};
110-
111-
class HashRecognizeAnalysis : public AnalysisInfoMixin<HashRecognizeAnalysis> {
112-
friend AnalysisInfoMixin<HashRecognizeAnalysis>;
113-
static AnalysisKey Key;
114-
115-
public:
116-
using Result = HashRecognize;
117-
Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
118-
};
119112
} // namespace llvm
120113

121114
#endif

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,13 @@ getRecurrences(BasicBlock *LoopLatch, const PHINode *IndVar, const Loop &L) {
442442
return std::make_pair(SimpleRecurrence, ConditionalRecurrence);
443443
}
444444

445-
PolynomialInfo::PolynomialInfo(unsigned TripCount, Value *LHS, const APInt &RHS,
446-
Value *ComputedValue, bool ByteOrderSwapped,
447-
Value *LHSAux)
445+
PolynomialInfo::PolynomialInfo(
446+
unsigned TripCount, Value *LHS, const APInt &RHS, Value *ComputedValue,
447+
bool ByteOrderSwapped,
448+
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable, Value *LHSAux)
448449
: TripCount(TripCount), LHS(LHS), RHS(RHS), ComputedValue(ComputedValue),
449-
ByteOrderSwapped(ByteOrderSwapped), LHSAux(LHSAux) {}
450+
ByteOrderSwapped(ByteOrderSwapped), GenSarwateTable(GenSarwateTable),
451+
LHSAux(LHSAux) {}
450452

451453
/// In the big-endian case, checks the bottom N bits against CheckFn, and that
452454
/// the rest are unknown. In the little-endian case, checks the top N bits
@@ -471,8 +473,7 @@ static bool checkExtractBits(const KnownBits &Known, unsigned N,
471473
/// Generate a lookup table of 256 entries by interleaving the generating
472474
/// polynomial. The optimization technique of table-lookup for CRC is also
473475
/// called the Sarwate algorithm.
474-
CRCTable HashRecognize::genSarwateTable(const APInt &GenPoly,
475-
bool ByteOrderSwapped) const {
476+
static CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped) {
476477
unsigned BW = GenPoly.getBitWidth();
477478
CRCTable Table;
478479
Table[0] = APInt::getZero(BW);
@@ -625,7 +626,7 @@ HashRecognize::recognizeCRC() const {
625626

626627
Value *LHSAux = SimpleRecurrence ? SimpleRecurrence.Start : nullptr;
627628
return PolynomialInfo(TC, ConditionalRecurrence.Start, GenPoly, ComputedValue,
628-
*ByteOrderSwapped, LHSAux);
629+
*ByteOrderSwapped, genSarwateTable, LHSAux);
629630
}
630631

631632
void CRCTable::print(raw_ostream &OS) const {
@@ -679,27 +680,27 @@ void HashRecognize::print(raw_ostream &OS) const {
679680
OS << "\n";
680681
}
681682
OS.indent(2) << "Computed CRC lookup table:\n";
682-
genSarwateTable(Info.RHS, Info.ByteOrderSwapped).print(OS);
683+
Info.GenSarwateTable(Info.RHS, Info.ByteOrderSwapped).print(OS);
683684
}
684685

685686
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
686687
void HashRecognize::dump() const { print(dbgs()); }
687688
#endif
688689

690+
std::optional<PolynomialInfo> HashRecognize::getResult() const {
691+
auto Res = HashRecognize(L, SE).recognizeCRC();
692+
if (std::holds_alternative<PolynomialInfo>(Res))
693+
return std::get<PolynomialInfo>(Res);
694+
return std::nullopt;
695+
}
696+
689697
HashRecognize::HashRecognize(const Loop &L, ScalarEvolution &SE)
690698
: L(L), SE(SE) {}
691699

692700
PreservedAnalyses HashRecognizePrinterPass::run(Loop &L,
693701
LoopAnalysisManager &AM,
694702
LoopStandardAnalysisResults &AR,
695703
LPMUpdater &) {
696-
AM.getResult<HashRecognizeAnalysis>(L, AR).print(OS);
704+
HashRecognize(L, AR.SE).print(OS);
697705
return PreservedAnalyses::all();
698706
}
699-
700-
HashRecognize HashRecognizeAnalysis::run(Loop &L, LoopAnalysisManager &AM,
701-
LoopStandardAnalysisResults &AR) {
702-
return {L, AR.SE};
703-
}
704-
705-
AnalysisKey HashRecognizeAnalysis::Key;

llvm/lib/Passes/PassRegistry.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ LOOPNEST_PASS("no-op-loopnest", NoOpLoopNestPass())
672672
#define LOOP_ANALYSIS(NAME, CREATE_PASS)
673673
#endif
674674
LOOP_ANALYSIS("ddg", DDGAnalysis())
675-
LOOP_ANALYSIS("hash-recognize", HashRecognizeAnalysis())
676675
LOOP_ANALYSIS("iv-users", IVUsersAnalysis())
677676
LOOP_ANALYSIS("no-op-loop", NoOpLoopAnalysis())
678677
LOOP_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))

0 commit comments

Comments
 (0)