1 //===- LoopTransformWarning.cpp - ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Emit warnings if forced code transformations have not been performed.
11 //===----------------------------------------------------------------------===//
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"
20 #define DEBUG_TYPE "transform-warning"
22 /// Emit warnings for forced (i.e. user-defined) loop transformations which have
23 /// still not been performed.
24 static void warnAboutLeftoverTransformations(Loop
*L
,
25 OptimizationRemarkEmitter
*ORE
) {
26 if (hasUnrollTransformation(L
) == TM_ForcedByUser
) {
27 LLVM_DEBUG(dbgs() << "Leftover unroll transformation\n");
29 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
30 "FailedRequestedUnrolling",
31 L
->getStartLoc(), L
->getHeader())
32 << "loop not unrolled: the optimizer was unable to perform the "
33 "requested transformation; the transformation might be disabled or "
34 "specified as part of an unsupported transformation ordering");
37 if (hasUnrollAndJamTransformation(L
) == TM_ForcedByUser
) {
38 LLVM_DEBUG(dbgs() << "Leftover unroll-and-jam transformation\n");
40 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
41 "FailedRequestedUnrollAndJamming",
42 L
->getStartLoc(), L
->getHeader())
43 << "loop not unroll-and-jammed: the optimizer was unable to perform "
44 "the requested transformation; the transformation might be disabled "
45 "or specified as part of an unsupported transformation ordering");
48 if (hasVectorizeTransformation(L
) == TM_ForcedByUser
) {
49 LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n");
50 std::optional
<ElementCount
> VectorizeWidth
=
51 getOptionalElementCountLoopAttribute(L
);
52 std::optional
<int> InterleaveCount
=
53 getOptionalIntLoopAttribute(L
, "llvm.loop.interleave.count");
55 if (!VectorizeWidth
|| VectorizeWidth
->isVector())
57 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
58 "FailedRequestedVectorization",
59 L
->getStartLoc(), L
->getHeader())
60 << "loop not vectorized: the optimizer was unable to perform the "
61 "requested transformation; the transformation might be disabled "
62 "or specified as part of an unsupported transformation ordering");
63 else if (InterleaveCount
.value_or(0) != 1)
65 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
66 "FailedRequestedInterleaving",
67 L
->getStartLoc(), L
->getHeader())
68 << "loop not interleaved: the optimizer was unable to perform the "
69 "requested transformation; the transformation might be disabled "
70 "or specified as part of an unsupported transformation ordering");
73 if (hasDistributeTransformation(L
) == TM_ForcedByUser
) {
74 LLVM_DEBUG(dbgs() << "Leftover distribute transformation\n");
76 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
77 "FailedRequestedDistribution",
78 L
->getStartLoc(), L
->getHeader())
79 << "loop not distributed: the optimizer was unable to perform the "
80 "requested transformation; the transformation might be disabled or "
81 "specified as part of an unsupported transformation ordering");
85 static void warnAboutLeftoverTransformations(Function
*F
, LoopInfo
*LI
,
86 OptimizationRemarkEmitter
*ORE
) {
87 for (auto *L
: LI
->getLoopsInPreorder())
88 warnAboutLeftoverTransformations(L
, ORE
);
91 // New pass manager boilerplate
93 WarnMissedTransformationsPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
94 // Do not warn about not applied transformations if optimizations are
97 return PreservedAnalyses::all();
99 auto &ORE
= AM
.getResult
<OptimizationRemarkEmitterAnalysis
>(F
);
100 auto &LI
= AM
.getResult
<LoopAnalysis
>(F
);
102 warnAboutLeftoverTransformations(&F
, &LI
, &ORE
);
104 return PreservedAnalyses::all();