[llvm] [DFAJumpThreading] Add an early exit heuristic for unpredictable values (PR #85015)

Usman Nadeem via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 15 13:17:03 PDT 2024


================
@@ -439,18 +449,31 @@ struct MainSwitch {
         // initial switch values that can be ignored (they will hit the
         // unthreaded switch) but this assumption will get checked later after
         // paths have been enumerated (in function getStateDefMap).
+
+        // If the unpredictable value comes from the same inner loop it is
+        // likely that it will also be on the enumerated paths, causing us to
+        // exit after we have enumerated all the paths. This heuristic save
+        // compile time because a search for all the paths can become expensive.
+        if (EarlyExitHeuristic && LI->getLoopFor(SI->getParent()) ==
+                                      LI->getLoopFor(CurrentIncomingBB)) {
----------------
UsmanNadeem wrote:

> I don't really get why this cares about the loop of the incoming BB, rather than the loop where the instruction is defined.

I don't have a good answer for this, except that the compile time won't will be any better if I check the instruction's loop. 

The added test3 shows this case. Even without this patch `getStateDefMap` rejects the switch because it checks for the incoming BB and not the value's defining BB,  I am not sure why the pass was written to disallow such cases. I think the switch is still threadable but maybe there are additional complications I am not aware of?

https://github.com/llvm/llvm-project/pull/85015


More information about the llvm-commits mailing list