[llvm] [LV] Check loop-preheader for loop-legality when pass-remarks enabled (PR #166310)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 3 21:52:26 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: hanbeom (ParkHanbum)

<details>
<summary>Changes</summary>

When `-pass-remarks=loop-vectorize` is specified, the subsequent logic is executed to display detailed debug messages even if no PreHeader exists in the loop.

Therefore, an assert occurs when the `getLoopPreHeader()` function is called. This commit resolves that issue.

Fixed: #<!-- -->165377

---
Full diff: https://github.com/llvm/llvm-project/pull/166310.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+8) 
- (added) llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll (+33) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 8e09e6f8d4935..311cf4ecbe121 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -795,6 +795,14 @@ static bool canWidenCallReturnType(Type *Ty) {
 bool LoopVectorizationLegality::canVectorizeInstrs() {
   BasicBlock *Header = TheLoop->getHeader();
 
+  /// Generally, if `TheLoop` lacks a PreHeader, it won't reach this point.
+  /// However, when executed with remarks options like `-pass-remarks=vector`
+  /// it will reach here to output extra debug messages, so need to check.
+  /// Additionally, before reaching here, the debug messages would have
+  /// included `Loop doesn't have a legal pre-header`, so we omit that.
+  if (ORE->allowExtraAnalysis(DEBUG_TYPE) && !TheLoop->getLoopPreheader())
+    return false;
+
   // For each block in the loop.
   for (BasicBlock *BB : TheLoop->blocks()) {
     // Scan the instructions in the block and look for hazards.
diff --git a/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll b/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll
new file mode 100644
index 0000000000000..ec5dcacb0f765
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/loop-legality-checks-remarks.ll
@@ -0,0 +1,33 @@
+; RUN: opt < %s -passes=loop-vectorize -pass-remarks=loop-vectorize -debug -disable-output 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure LV legal bails out when the loop doesn't have a legal pre-header.
+; CHECK-LABEL: 'not_exist_preheader'
+; CHECK: LV: Not vectorizing: Loop doesn't have a legal pre-header.
+define void @not_exist_preheader(ptr %currMB, i1 %arg, ptr %arg2) nounwind uwtable {
+entry:
+  br i1 %arg, label %start.exit, label %if.then.i
+
+if.then.i:
+  unreachable
+
+start.exit:
+  indirectbr ptr %arg2, [label %0, label %for.bodyprime]
+
+0:
+  unreachable
+
+for.bodyprime:
+  %i.057375 = phi i32 [0, %start.exit], [%1, %for.bodyprime]
+  %arrayidx8prime = getelementptr inbounds i32, ptr %currMB, i32 %i.057375
+  store i32 0, ptr %arrayidx8prime, align 4
+  %1 = add i32 %i.057375, 1
+  %cmp5prime = icmp slt i32 %1, 4
+  br i1 %cmp5prime, label %for.bodyprime, label %for.endprime
+
+for.endprime:
+  br label %for.body23prime
+
+for.body23prime:
+  br label %for.body23prime
+}

``````````

</details>


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


More information about the llvm-commits mailing list