[llvm] [NVPTX] Skip processing BasicBlocks with single unreachable instruction in `nvptx-lower-unreachable` pass. (PR #72641)

Tim Besard via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 6 00:02:56 PST 2024


================
@@ -138,7 +138,19 @@ bool NVPTXLowerUnreachable::runOnFunction(Function &F) {
   InlineAsm *Exit = InlineAsm::get(ExitFTy, "exit;", "", true);
 
   bool Changed = false;
-  for (auto &BB : F)
+
+  // In scenarios where a BasicBlock contains only one unreachable instruction,
+  // the joint action of nvptx-isel and unreachable-mbb-elimination
+  // effectively optimizes the BasicBlock out. However, adding an exit
+  // command to such a BasicBlock, as suggested by this pass, preserves it
+  // within the Control Flow Graph (CFG), thereby negatively impacting size and
+  // performance. To counteract this undesirable consequence, we choose to
+  // refrain from processing BasicBlocks with just one unreachable instruction
+  // in this pass.
+
----------------
maleadt wrote:

Additional reproducers can be found in https://github.com/JuliaGPU/CUDA.jl/issues/1746#issuecomment-1514760628, https://github.com/JuliaGPU/CUDA.jl/pull/431#issuecomment-760927545, https://github.com/JuliaGPU/CUDA.jl/issues/43#issue-625418275. Basically, we've been running into this bug with various user applications roughly every year or so, necessitating more and more questionable workarounds (both in code, and in our compiler) until we finally found the root cause. Needless to say I'd be strongly opposed to reverting the fix.

However, that the proposed `-nvptx-exit-on-unreachable=0` may be a viable default at some point in the future, because NVIDIA should have fixed `ptxas` to model `trap` like `exit`, i.e., we just need to make sure that every unreachable block ends with a `trap` terminator (which I guess is the current behavior). So that seems like a good option to add back, and would help with your performance issue right now.

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


More information about the llvm-commits mailing list