1//===- LoopTransformWarning.cpp - ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Emit warnings if forced code transformations have not been performed.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
14#include "llvm/Analysis/LoopInfo.h"
15#include "llvm/Analysis/OptimizationRemarkEmitter.h"
16#include "llvm/Transforms/Utils/LoopUtils.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "transform-warning"
21
22/// Emit warnings for forced (i.e. user-defined) loop transformations which have
23/// still not been performed.
24static void warnAboutLeftoverTransformations(Loop *L,
25 OptimizationRemarkEmitter *ORE) {
26 if (hasUnrollTransformation(L) == TM_ForcedByUser) {
27 LLVM_DEBUG(dbgs() << "Leftover unroll transformation\n");
28
29 // Determine whether this loop originated from the vectorizer so we can
30 // produce more informative remarks.
31 StringRef LoopKind = getLoopVectorizeKindPrefix(L);
32
33 ORE->emit(
34 OptDiag: DiagnosticInfoOptimizationFailure(DEBUG_TYPE,
35 "FailedRequestedUnrolling",
36 L->getStartLoc(), L->getHeader())
37 << LoopKind.str() +
38 "loop not unrolled: the optimizer was unable to perform the "
39 "requested transformation; the transformation might be disabled "
40 "or "
41 "specified as part of an unsupported transformation ordering");
42 }
43
44 if (hasUnrollAndJamTransformation(L) == TM_ForcedByUser) {
45 LLVM_DEBUG(dbgs() << "Leftover unroll-and-jam transformation\n");
46 ORE->emit(
47 OptDiag: DiagnosticInfoOptimizationFailure(DEBUG_TYPE,
48 "FailedRequestedUnrollAndJamming",
49 L->getStartLoc(), L->getHeader())
50 << "loop not unroll-and-jammed: the optimizer was unable to perform "
51 "the requested transformation; the transformation might be disabled "
52 "or specified as part of an unsupported transformation ordering");
53 }
54
55 if (hasVectorizeTransformation(L) == TM_ForcedByUser) {
56 LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n");
57 std::optional<ElementCount> VectorizeWidth =
58 getOptionalElementCountLoopAttribute(TheLoop: L);
59 std::optional<int> InterleaveCount =
60 getOptionalIntLoopAttribute(TheLoop: L, Name: "llvm.loop.interleave.count");
61
62 if (!VectorizeWidth || VectorizeWidth->isVector())
63 ORE->emit(
64 OptDiag: DiagnosticInfoOptimizationFailure(DEBUG_TYPE,
65 "FailedRequestedVectorization",
66 L->getStartLoc(), L->getHeader())
67 << "loop not vectorized: the optimizer was unable to perform the "
68 "requested transformation; the transformation might be disabled "
69 "or specified as part of an unsupported transformation ordering");
70 else if (InterleaveCount.value_or(u: 0) != 1)
71 ORE->emit(
72 OptDiag: DiagnosticInfoOptimizationFailure(DEBUG_TYPE,
73 "FailedRequestedInterleaving",
74 L->getStartLoc(), L->getHeader())
75 << "loop not interleaved: the optimizer was unable to perform the "
76 "requested transformation; the transformation might be disabled "
77 "or specified as part of an unsupported transformation ordering");
78 }
79
80 if (hasDistributeTransformation(L) == TM_ForcedByUser) {
81 LLVM_DEBUG(dbgs() << "Leftover distribute transformation\n");
82 ORE->emit(
83 OptDiag: DiagnosticInfoOptimizationFailure(DEBUG_TYPE,
84 "FailedRequestedDistribution",
85 L->getStartLoc(), L->getHeader())
86 << "loop not distributed: the optimizer was unable to perform the "
87 "requested transformation; the transformation might be disabled or "
88 "specified as part of an unsupported transformation ordering");
89 }
90}
91
92static void warnAboutLeftoverTransformations(Function *F, LoopInfo *LI,
93 OptimizationRemarkEmitter *ORE) {
94 for (auto *L : LI->getLoopsInPreorder())
95 warnAboutLeftoverTransformations(L, ORE);
96}
97
98// New pass manager boilerplate
99PreservedAnalyses
100WarnMissedTransformationsPass::run(Function &F, FunctionAnalysisManager &AM) {
101 // Do not warn about not applied transformations if optimizations are
102 // disabled.
103 if (F.hasOptNone())
104 return PreservedAnalyses::all();
105
106 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: F);
107 auto &LI = AM.getResult<LoopAnalysis>(IR&: F);
108
109 warnAboutLeftoverTransformations(F: &F, LI: &LI, ORE: &ORE);
110
111 return PreservedAnalyses::all();
112}
113