<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60132>60132</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
SelectionDAG's SimplifySelectOps can take a pathological amount of time
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Keno
</td>
</tr>
</table>
<pre>
Consider the attached .bc file. This file is quite large (about 4MB uncompressed,
about 300k IR statements, all in one function with very large BBs). As such, it is no surprise that
compiling it may take same time. However, on LLVM master the compile time (`llc A2.bc`)
is excessive (I haven't actually been patient enough to let it finish yet, but
at least three hours).
The compile time improves dramatically if I disable the SimplifySelectOps load reassociation:
```
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index dd7f692ede90..17f96ab2efe5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -24969,6 +24969,7 @@ bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
LLD->getBasePtr().getValueType()))
return false;
+ return false;
// Check that the select condition doesn't reach either load. If so,
// folding this will induce a cycle into the DAG. If not, this is safe to
```
With this patch applied, the whole compilation finishes in about 25s, which seems
reasonable to me for this size IR.
I think there are some low-hanging fruits to improve performance slightly, but the
overall problem here is algorithmic. The
`SDNode::hasPredecessorHelper(LLD, Visited, Worklist))` check that is used to avoid
introducing cycles needs to essentially walk the entire function, which in functions
as large as this is very expensive.
I think there's a couple of options here:
1. Try some sort of different search heuristic. For example, we might do a BFS of successors instead from both points to establish a local topological order. The intuition here is that while in the common case the two nodes are not each other's predecessors, they likely flow into common successors, making that a more fruitful search direction.
2. We could try maintaining an incremental topological order. The changes that DAGCombine makes are fairly local, which avoids some of the usual worst case behavior in topological maintenance algorithms.
3. We could just put a MaxNodes cutoff into this optimization. This is probably the easiest option, but not particularly satisfying.
I'm of course also working on making the frontend emit something nicer here and maybe have it split the function into a few parts, but it would be good to fix it in both places if possible.
[A2.bc.tar.gz](https://github.com/llvm/llvm-project/files/10451463/A2.bc.tar.gz)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykV99v4zgO_mvcF6KB46RJ85CH_rjMFNfZW2wHM8-0TMfayqJPlJNm_voDJafp3O0ecHuAkTaWRJEfv49kUMTuPdG2uLkvbh6vcIwdh-3fyfNVzc1p-8BebEMBYkeAMaLpqIFZbaC1jmbwtbOS_gUr8M_RRgKHYU9QVLdY8xhh-eUeRm-4HwKJUFNUD0X5WJR3eXlRlq_w9BtIxEg9-ShF9QDoHFgP7Ana0Zto2cPRxg4OFE7TDff3UlSbGdwJyGg6PWajuuEZZAxDsEIQO4z5OvXAOuv3uqvHE0R8JRDsCaLtaQaf-UgHCmqHPTw_f_sCPUqcYs_H814NrliVzhm4q2a1KVZlUW3yNVaA3gyJ2EPa9wQdHsgX1ToCmjiicyeoiTwMGC35COR53HcQGRxFda613koHJ4rqSz1OAWAERygRYheIoOMxZADycv78-u-u2n4IfCCBJmCP0ZrkgG3hCRorWOu-juDF9oOz7emFHJn4j0HAMTYQCEXYWNQMFIvpEo03P-lrY9sWrq_3NgIW1c65Q69_bF1Uuwdu6JOGv8uWLfvHu09FtXu8-_TAfW09hZkZBqj_6skJdt_QGzTNul1tKmpoU85m83W7WWFdUUs3MC_L1XKZN19fX_91TycIqvv8_L-OF8uyWJZwXS03q01RPaygqO7PX9YwLdfMDj6c11Qs7v4jaUV1-_L4CzfKPKVCXlAWvTx-QzcSPH9-eRcgPD8_XheLv-0p3qPQrzEor6vNbE8x7f56Gii_mp58LFAcg4cWnVCxuJ_evuMCAP91i65DUe2KagcPHZnXJNLEQkn-gmHf2KT5hkmydgKh6YBs7Cgkbs4AnloQvoTzwW7LrlGpRy1PR5uqSTMaAgRzMlqtfOR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