llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open Nirhar opened this issue 1 year ago • 5 comments

Here is the godbolt link to the reproducer: https://godbolt.org/z/354qYx43T For the record here is the IR:

source_filename = "reduced.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
target triple = "x86_64-unknown-linux-gnu"

define void @widget() {
bb:
  %sitofp = sitofp i32 0 to double
  br label %bb1

bb1:                                              ; preds = %bb1, %bb
  %phi = phi i32 [ %add, %bb1 ], [ 0, %bb ]
  %phi2 = phi double [ %fsub, %bb1 ], [ 0.000000e+00, %bb ]
  %fsub = fsub double %phi2, %sitofp
  %add = add i32 %phi, 1
  %icmp = icmp ult i32 %phi, 252
  br i1 %icmp, label %bb1, label %bb3

bb3:                                              ; preds = %bb1
  %phi4 = phi double [ %phi2, %bb1 ]
  %phi5 = phi double [ %fsub, %bb1 ]
  ret void
}

which fails when running: opt -passes=loop-vectorize -slp-threshold=-99999 -force-vector-width=4 with the backtrace:

opt: /root/llvm-project/llvm/lib/IR/Constants.cpp:2231: static llvm::Constant* llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool): Assertion `CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize -slp-threshold=-99999 -force-vector-width=4 <source>
1.	Running pass "function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)" on module "<source>"
2.	Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "widget"
 #0 0x00000000054eff68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x54eff68)
 #1 0x00000000054ed8f4 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007ccdb7442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007ccdb74969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007ccdb7442476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007ccdb74287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007ccdb742871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007ccdb7439e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000005166c4b llvm::ConstantExpr::getCast(unsigned int, llvm::Constant*, llvm::Type*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5166c4b)
 #9 0x0000000000d41160 llvm::IRBuilderBase::CreateTrunc(llvm::Value*, llvm::Type*, llvm::Twine const&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xd41160)
#10 0x00000000045b9885 llvm::VPScalarIVStepsRecipe::execute(llvm::VPTransformState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x45b9885)
#11 0x0000000004576263 llvm::VPBasicBlock::executeRecipes(llvm::VPTransformState*, llvm::BasicBlock*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4576263)
#12 0x000000000457e734 llvm::VPBasicBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x457e734)
#13 0x000000000457f740 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x457f740)
#14 0x000000000458abda llvm::VPlan::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x458abda)
#15 0x000000000442b57c llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x442b57c)
#16 0x0000000004443b38 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4443b38)
#17 0x0000000004445ce0 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4445ce0)
#18 0x0000000004446333 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4446333)
#19 0x00000000031c103e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x31c103e)
#20 0x00000000052e90a0 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x52e90a0)
#21 0x0000000000e7bace llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe7bace)
#22 0x00000000052e7973 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x52e7973)
#23 0x0000000000e7a98e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe7a98e)
#24 0x00000000052e7380 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x52e7380)
#25 0x000000000094707a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x94707a)
#26 0x000000000093a84c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x93a84c)
#27 0x00007ccdb7429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#28 0x00007ccdb7429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#29 0x0000000000932485 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x932485)
Program terminated with signal: SIGSEGV
Compiler returned: 139

Nirhar avatar Apr 24 '25 14:04 Nirhar

As far as I can see, we are trying to truncate an i32 value to i64 type, which is illegal, here: https://github.com/llvm/llvm-project/blob/72cc868c65b0641f23d1fb0518a8503c73ecdb5a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp#L2137

Might be related to @fhahn 's commit here: 783a846507683c7b515473b833c9cf02cd2b1993

Nirhar avatar Apr 24 '25 15:04 Nirhar

I have also noticed the assertion as a result of commit 783a846507683c7b515473b833c9cf02cd2b1993 for invalid truncation.

@fhahn , Does the following fix make sense?

--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2124,7 +2124,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/regression-scalarivsext-crash.ll b/llvm/test/Transforms/LoopVectorize/PowerPC/regression-scalarivsext-crash.ll
new file mode 100644
index 000000000000..66b54a29b913
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/PowerPC/regression-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
+}
+

maryammo avatar Apr 28 '25 18:04 maryammo

@maryammo thanks, could you create a PR? For the test case, does it require a target-triple or can the one provided by @Nirhar be used without a target triple?

fhahn avatar Apr 29 '25 10:04 fhahn

@fhahn , Sure I do. Both tests need target triple to fail.

maryammo avatar Apr 29 '25 15:04 maryammo

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

maryammo avatar Apr 29 '25 16:04 maryammo