Commit 3a81e03
authored
[DAG] SDPatternMatch - Fix m_Reassociatable mismatching (#170061)
Fixes #169645
The issue was caused by a for-loop improperly overwriting SDValue binds
when m_Reassociatable is given two or more patterns that (1) call
m_Value with an SDValue parameter and (2) differ by that parameter. This
fix comes with added unit tests relevant to SDValue bindings inside
m_Reassociatable patterns.
Essentially, the original implementation first tried to match every
combination of leaf node and pattern possible and stored that in a
matrix-like structure, and then did a recursive search on that matrix to
check if it's possible to pair every leaf with a pattern. The problem is
that m_Value has a side effect where it changes an SDValue, and the
creation of this matrix was corrupting these values. Below is an example
of this, following the order of execution in the original implementation
and using the case brought by issue #169645, where this behavior was
found. The example tries to match ((a >> 1) + (b >> 1) + (a & b & 1)),
using uppercase letters for the SDValue variables themselves and
lowercase for their values. The result is that the pattern matches the
same value for A and B, which was the behavior observed in the issue:
| Line | Leaf | Pattern | Match? | Effect |
|--------|--------|--------|--------|--------|
| 1 | a >> 1 | m_Srl(m_Value(A), m_One()) | Yes | A <- a |
| 2 | a >> 1 | m_Srl(m_Value(B), m_One()) | Yes | B <- a |
| 3 | a >> 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One())
| No | -- |
| 4 | b >> 1 | m_Srl(m_Value(A), m_One()) | Yes | A <- b |
| 5 | b >> 1 | m_Srl(m_Value(B), m_One()) | Yes | B <- b |
| 6 | b >> 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One())
| No | -- |
| 7 | a & b & 1 | m_Srl(m_Value(A), m_One()) | No | -- |
| 8 | a & b & 1 | m_Srl(m_Value(B), m_One()) | No | -- |
| 9 | a & b & 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B),
m_One()) | a == b | -- |
To fix this, the function now matches the patterns during the recursive
search itself, instead of preparing the matrix beforehand. Although this
does fix the issue, it does mean that we're performing a best case of n
and worst case of n! matching attempts, instead of the fixed nˆ2 in the
original, where n is the number of patterns provided. Going back to the
table above, using this fix the lines 2, 3, 4, 6, 7, and 8 do not
happen, and so the only effects happening are A <- a and B <- b, which
then will result in line 9 matching correctly.1 parent 88273a0 commit 3a81e03
File tree
2 files changed
+52
-19
lines changed- llvm
- include/llvm/CodeGen
- unittests/CodeGen
2 files changed
+52
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1315 | 1315 | | |
1316 | 1316 | | |
1317 | 1317 | | |
1318 | | - | |
1319 | | - | |
1320 | | - | |
1321 | | - | |
1322 | | - | |
1323 | | - | |
1324 | | - | |
1325 | | - | |
1326 | | - | |
1327 | | - | |
1328 | | - | |
1329 | 1318 | | |
1330 | | - | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
1331 | 1324 | | |
1332 | 1325 | | |
1333 | 1326 | | |
| |||
1339 | 1332 | | |
1340 | 1333 | | |
1341 | 1334 | | |
| 1335 | + | |
| 1336 | + | |
1342 | 1337 | | |
1343 | | - | |
1344 | | - | |
1345 | | - | |
1346 | | - | |
1347 | | - | |
1348 | | - | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
1349 | 1343 | | |
1350 | 1344 | | |
1351 | | - | |
| 1345 | + | |
1352 | 1346 | | |
1353 | 1347 | | |
1354 | 1348 | | |
1355 | 1349 | | |
1356 | 1350 | | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
1357 | 1358 | | |
1358 | 1359 | | |
1359 | 1360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
832 | 832 | | |
833 | 833 | | |
834 | 834 | | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
835 | 867 | | |
836 | 868 | | |
837 | 869 | | |
| |||
0 commit comments