16 Commits

Author SHA1 Message Date
Ramkumar Ramachandra
3bc9b28fe5
[HashRecognize] Forbid optz when data.next has exit-block user (#165574)
The CRC optimization relies on stripping the auxiliary data completely,
and should hence be forbidden when it has a user in the exit-block.
Forbid this case, fixing a miscompile.

Fixes #165382.
2025-10-29 21:50:12 +00:00
David Spickett
78739ff84a
[llvm][HashRecognize] Fix compiler warning on Arm 32-bit (#161821)
```
/home/david.spickett/llvm-project/llvm/lib/Analysis/HashRecognize.cpp💯54: warning: comparison of integers of different signs:
'typename iterator_traits<ilist_iterator_w_bits<node_options<Instruction, true, false, void, true, BasicBlock>, false, false>>::difference_type' (aka 'int') and 'size_type' (aka 'unsigned int') [-Wsign-compare]
  100 |   return std::distance(Latch->begin(), Latch->end()) != Visited.size();
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~
```

By using Latch->size() instead.
2025-10-03 15:58:11 +01:00
Ramkumar Ramachandra
9f7877ffa3
[HashRecognize] Clarify hdr comment on GF(2^n) (NFC) (#157482)
Unify explanation for GF(2^n) and GF(2), which was previously
convoluted.
2025-09-08 15:57:16 +00:00
Ramkumar Ramachandra
ee312fc182
[HashRecognize] Strip excess-TC check (#157479)
Checking if trip-count exceeds 256 is no longer necessary, as we have
moved away from KnownBits computations to pattern-matching, which is
very cheap and independent of TC.
2025-09-08 16:23:13 +01:00
Ramkumar Ramachandra
c24e9e3d8f
[HashRecognize] Strip ValueEvolution (#148620)
The ValueEvolution logic is deeply flawed, and checking that zero-bits
are shifted can be exploited for miscompiles. In an effort to redo
HashRecognize with a pattern-matching based approach, extract and fix
the core logic of ValueEvolution, and strip it completely, showing that
none of the tests rely on the KnownBits computation of ValueEvolution.

Co-authored-by: Piotr Fusik <p.fusik@samsung.com>
2025-09-04 15:52:33 +01:00
Ramkumar Ramachandra
10d4652144
[HashRecognize] Track visited in ValueEvolution (#147812)
Require that all Instructions in the Loop are visited by ValueEvolution,
as any stray instructions would complicate life for the optimization.
2025-07-16 15:27:41 +01:00
Ramkumar Ramachandra
a695d6b9a7
[HashRecognize] Check TC against bitwidth of LHSAux (#144881)
The trip-count of a CRC algorithm can legitimately be greater than the
bitwidth of the result: what it cannot exceed is the bitwidth of the
data, or LHSAux. crc8.le.tc16 is now successfully recognized as a CRC
algorithm.
2025-07-03 18:07:15 +01:00
Piotr Fusik
1c305f711f
[HashRecognize][NFC] Add a missing "phi" in a comment (#146702) 2025-07-02 17:57:26 +02:00
Ramkumar Ramachandra
cbfd0d68ea
[HashRecognize] Rewrite arePHIsIntertwined (#144878)
The test crc8.le.tc16 is a valid CRC algorithm, but isn't recognized as
such due to a buggy arePHIsIntertwined, which is asymmetric in its
PHINode arguments. There is also a fundamental correctness issue: the
core functionality is to match a XOR that's a recurrence in both PHI
nodes, ignoring casts, but the user of the XOR is never checked. Rewrite
and rename the function.

crc8.le.tc16 is still not recognized as a valid CRC algorithm, due to an
incorrect check for loop iterations exceeding the bitwidth of the
result: in reality, it should not exceed the bitwidth of LHSAux, but we
leave this fix to a follow-up.

Co-authored-by: Piotr Fusik <p.fusik@samsung.com>
2025-07-02 15:36:27 +01:00
Ramkumar Ramachandra
74054cab7a
[HashRecognize] Make it a non-PM analysis (#144742)
Make HashRecognize a non-PassManager analysis that can be called to get
the result on-demand, creating a new getResult() entry-point. The issue
was discovered when attempting to use the analysis to perform a
transform in LoopIdiomRecognize.
2025-06-19 12:29:58 +01:00
Ramkumar Ramachandra
156a64c585
[HashRecognize] Tighten pre-conditions for analysis (#144757)
Exit early if the TC is not a byte-multiple, as optimization works by
dividing TC by 8. Also delay the SCEV TC query.
2025-06-18 20:19:25 +01:00
Ramkumar Ramachandra
f13b9e3643
[HashRecognize] Don't const-qualify Values in result (#144752)
Const-qualifying Values in the analysis result makes them unusable with
IRBuilder. The issue was discovered when attempting to use the result of
the analysis for a transform.
2025-06-18 20:18:53 +01:00
Ramkumar Ramachandra
a94eb27a29
[HashRecognize] Fix big-endian CRC tables (#144754)
Big-endian CRC tables are incorrect due to the initial value of CRC in
genSarwateTable being hard-coded for CRC-8. 128 is the signed-min value
for CRC-8, but it should be generalized to APInt::getSignedMinValue. The
issue was found when writing CRC verification tests for llvm-test-suite.
2025-06-18 20:18:22 +01:00
Ricardo Jesus
cca454b54c
[ValueTracking] Remove opcode whitelist from matchSimpleRecurrence. (#144031)
This also patches HashRecognize to avoid it mishandling some opcodes.
2025-06-16 09:12:42 +01:00
Ramkumar Ramachandra
0a25b50228
[HashRecognize] Introduce dump methods for debug (#142748)
Introduce dump methods to aid interactive debugging with GDB/LLDB. While
at it, also fix the header comment in HashRecognize.cpp.
2025-06-04 16:21:39 +01:00
Ramkumar Ramachandra
af2f8a8c14
[HashRecognize] Introduce new analysis (#139120)
Introduce a fresh analysis for recognizing polynomial hashes, with the
rationale that several targets have specific instructions to optimize
things like CRC and GHASH (eg. X86 and RISC-V crypto extension). We
limit the scope to polynomial hashes computed in a Galois field of
characteristic 2, since this class of operations can also be optimized
in the absence of target-specific instructions to use a lookup table.

At the moment, we only recognize the CRC algorithm.

RFC:
https://discourse.llvm.org/t/rfc-new-analysis-for-polynomial-hash-recognition/86268
2025-06-02 08:25:50 +01:00