diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp @@ -1064,6 +1064,22 @@ return true; } +// Check for unreachable instructions in the loop to emit a remark on. +static void checkErrorHandling(SmallVector &ExitBBs, + Loop *TheLoop, OptimizationRemarkEmitter *ORE) { + for (BasicBlock *BB : ExitBBs) { + for (Instruction &Inst : *BB) { + if (auto *U = dyn_cast(&Inst)) { + reportVectorizationFailure( + "The loop must have no error handling", + "loop exit block contains control flow that does not return", + "CFGNotUnderstood", ORE, TheLoop, U); + return; + } + } + } +} + // Helper function to canVectorizeLoopNestCFG. bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp, bool UseVPlanNativePath) { @@ -1103,6 +1119,13 @@ return false; } + // Check that this loop does not contain error handling blocks + if (!Lp->getUniqueExitBlock() && DoExtraAnalysis) { + SmallVector ExitBBs; + Lp->getExitBlocks(ExitBBs); + checkErrorHandling(ExitBBs, TheLoop, ORE); + } + // We currently must have a single "exit block" after the loop. Note that // multiple "exiting blocks" inside the loop are allowed, provided they all // reach the single exit block. diff --git a/llvm/test/Transforms/LoopVectorize/error-handling-remarks.ll b/llvm/test/Transforms/LoopVectorize/error-handling-remarks.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopVectorize/error-handling-remarks.ll @@ -0,0 +1,80 @@ +; RUN: opt -force-vector-width=4 -loop-vectorize -pass-remarks-analysis=loop-vectorize -disable-output < %s 2>&1 | FileCheck %s + +; CHECK: remark: error-handling-remarks.cpp:5:5: loop not vectorized: loop exit block contains control flow that does not return +; CHECK: remark: error-handling-remarks.cpp:13:7: loop not vectorized: loop exit block contains control flow that does not return + +define void @f(double* nocapture %X, i64 %N) { +entry: + br label %for.body + +for.body: + %i = phi i64 [ %inc, %cond.end ], [ 0, %entry ] + %arrayidx = getelementptr inbounds double, double* %X, i64 %i + %0 = load double, double* %arrayidx, align 8 + %cmp1 = fcmp une double %0, 0.000000e+00 + br i1 %cmp1, label %cond.end, label %cond.false + +cond.false: + call void @__assert_fail(i8* null, i8* null, i32 5, i8* null) + unreachable, !dbg !9 + +cond.end: + %div = fdiv double 1.000000e+00, %0 + store double %div, double* %arrayidx, align 8 + %inc = add nuw i64 %i, 1 + %exitcond.not = icmp eq i64 %inc, %N + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body + +for.cond.cleanup: + ret void +} + +define void @g(double* nocapture %X, i64 %N) { +entry: + br label %for.body + +for.body: + %i.012 = phi i64 [ %inc, %if.end ], [ 0, %entry ] + %arrayidx = getelementptr inbounds double, double* %X, i64 %i.012 + %0 = load double, double* %arrayidx, align 8 + %cmp1 = fcmp oeq double %0, 0.000000e+00 + br i1 %cmp1, label %if.then, label %if.end + +if.then: + %exception = call i8* @__cxa_allocate_exception(i64 8) + %1 = bitcast i8* %exception to i8** + store i8* null, i8** %1, align 16 + call void @__cxa_throw(i8* %exception, i8* null, i8* null) + unreachable, !dbg !10 + +if.end: + %div = fdiv double 1.000000e+00, %0 + store double %div, double* %arrayidx, align 8 + %inc = add nuw i64 %i.012, 1 + %exitcond.not = icmp eq i64 %inc, %N + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body + +for.cond.cleanup: + ret void +} + +declare void @__assert_fail(i8*, i8*, i32, i8*) + +declare i8* @__cxa_allocate_exception(i64) + +declare void @__cxa_throw(i8*, i8*, i8*) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 13.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "error-handling-remarks.cpp", directory: "/home2/3n4/llvm/new/llvm-project/build") +!2 = !{} +!3 = !{i32 2, !"Debug Info Version", i32 3} +!4 = !{i32 1, !"wchar_size", i32 4} +!5 = !{!"clang version 13.0.0"} +!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!7 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 10, type: !8, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !2) +!9 = !DILocation(line: 5, column: 5, scope: !6) +!10 = !DILocation(line: 13, column: 7, scope: !7)