Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VPlan][LV] Fix invalid truncation in VPScalarIVStepsRecipe #137832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2025

Conversation

maryammo
Copy link
Contributor

@maryammo maryammo commented Apr 29, 2025

Replace CreateTrunc with CreateSExtOrTrunc in VPScalarIVStepsRecipe to safely handle type conversion. This prevents assertion failures from invalid truncation when StartIdx0 has a smaller integer type than IntStepTy. The assertion was introduced by commit 783a846.
Fixes #137185

Replace CreateTrunc with CreateSExtOrTrunc in VPScalarIVStepsRecipe to safely
handle type conversion. This prevents assertion failures from invalid truncation
when StartIdx0 has a smaller integer type than IntStepTy. The assertion was
introduced by commit 783a846.
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-backend-powerpc
@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Maryam Moghadas (maryammo)

Changes

Replace CreateTrunc with CreateSExtOrTrunc in VPScalarIVStepsRecipe to safely handle type conversion. This prevents assertion failures from invalid truncation when StartIdx0 has a smaller integer type than IntStepTy. The assertion was introduced by commit 783a846.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp (+1-1)
  • (added) llvm/test/Transforms/LoopVectorize/PowerPC/vplan-scalarivsext-crash.ll (+31)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 75d056026025a..6ca43eec63dd4 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2134,7 +2134,7 @@ void VPScalarIVStepsRecipe::execute(VPTransformState &State) {
           Builder.CreateMul(StartIdx0, ConstantInt::get(StartIdx0->getType(),
                                                         getUnrollPart(*this)));
     }
-    StartIdx0 = Builder.CreateTrunc(StartIdx0, IntStepTy);
+    StartIdx0 = Builder.CreateSExtOrTrunc(StartIdx0, IntStepTy);
   }
 
   if (!FirstLaneOnly && State.VF.isScalable()) {
diff --git a/llvm/test/Transforms/LoopVectorize/PowerPC/vplan-scalarivsext-crash.ll b/llvm/test/Transforms/LoopVectorize/PowerPC/vplan-scalarivsext-crash.ll
new file mode 100644
index 0000000000000..66b54a29b913c
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/PowerPC/vplan-scalarivsext-crash.ll
@@ -0,0 +1,31 @@
+; RUN: opt -passes=loop-vectorize -S < %s > /dev/null
+; REQUIRES: asserts
+
+target datalayout = "E-m:a-p:32:32-Fi32-i64:64-n32"
+target triple = "powerpc-ibm-aix7.2.0.0"
+
+define void @__power_mod_NMOD_power_init(ptr %a, ptr %b, i32 %n) {
+entry:
+  br label %loop_entry
+
+loop_exit:                                        ; preds = %loop_header
+  br label %loop_entry
+
+loop_entry:                                       ; preds = %loop_exit, %entry
+  %sum.0 = phi double [ 0.000000e+00, %entry ], [ %sum.1, %loop_exit ]
+  %x = load double, ptr %a, align 8
+  br label %loop_header
+
+loop_header:                                      ; preds = %loop_body, %loop_entry
+  %sum.1 = phi double [ %sum.0, %loop_entry ], [ %sum.next, %loop_body ]
+  %i = phi i32 [ 0, %loop_entry ], [ %i.next, %loop_body ]
+  %cond = icmp sgt i32 %i, %n
+  br i1 %cond, label %loop_exit, label %loop_body
+
+loop_body:                                        ; preds = %loop_header
+  store double %sum.1, ptr %b, align 8
+  %sum.next = fadd reassoc double %sum.1, %x
+  %i.next = add i32 %i, 1
+  br label %loop_header
+}
+

@@ -0,0 +1,31 @@
; RUN: opt -passes=loop-vectorize -disable-output -S < %s
; REQUIRES: asserts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the test to check the output as @lukel97 suggested, we should check we get the expected result. You then can also remove REQUIRES: asserts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 11 to 17
loop_exit: ; preds = %loop_header
br label %loop_entry

loop_entry: ; preds = %loop_exit, %entry
%sum.0 = phi double [ 0.000000e+00, %entry ], [ %sum.1, %loop_exit ]
%x = load double, ptr %a, align 8
br label %loop_header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the loop nest or can this simply be a single loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the test to use a single loop.

@maryammo maryammo requested review from fhahn and lukel97 April 30, 2025 14:22
@Nirhar
Copy link
Contributor

Nirhar commented May 5, 2025

ping @fhahn @lukel97

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

Please add something like Fixes https://github.com/llvm/llvm-project/issues/137185 to the PR description to make sure the issue is closed when this lands.

Comment on lines +88 to +93
;.
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META1]]}
;.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use --check-globals=none to remove those uninteresting checks

@maryammo maryammo merged commit a750893 into llvm:main May 6, 2025
7 of 9 checks passed
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
)

Replace CreateTrunc with CreateSExtOrTrunc in VPScalarIVStepsRecipe to
safely handle type conversion. This prevents assertion failures from
invalid truncation when StartIdx0 has a smaller integer type than
IntStepTy. The assertion was introduced by commit 783a846.
Fixes llvm#137185
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Loop Vectorize] Crash: Assertion `CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"'
5 participants