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/OptimizationRemarkEmitter.h"
15 #include "llvm/Transforms/Utils/LoopUtils.h"
19 #define DEBUG_TYPE "transform-warning"
21 /// Emit warnings for forced (i.e. user-defined) loop transformations which have
22 /// still not been performed.
23 static void warnAboutLeftoverTransformations(Loop
*L
,
24 OptimizationRemarkEmitter
*ORE
) {
25 if (hasUnrollTransformation(L
) == TM_ForcedByUser
) {
26 LLVM_DEBUG(dbgs() << "Leftover unroll transformation\n");
28 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
29 "FailedRequestedUnrolling",
30 L
->getStartLoc(), L
->getHeader())
31 << "loop not unrolled: the optimizer was unable to perform the "
32 "requested transformation; the transformation might be disabled or "
33 "specified as part of an unsupported transformation ordering");
36 if (hasUnrollAndJamTransformation(L
) == TM_ForcedByUser
) {
37 LLVM_DEBUG(dbgs() << "Leftover unroll-and-jam transformation\n");
39 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
40 "FailedRequestedUnrollAndJamming",
41 L
->getStartLoc(), L
->getHeader())
42 << "loop not unroll-and-jammed: the optimizer was unable to perform "
43 "the requested transformation; the transformation might be disabled "
44 "or specified as part of an unsupported transformation ordering");
47 if (hasVectorizeTransformation(L
) == TM_ForcedByUser
) {
48 LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n");
49 Optional
<int> VectorizeWidth
=
50 getOptionalIntLoopAttribute(L
, "llvm.loop.vectorize.width");
51 Optional
<int> InterleaveCount
=
52 getOptionalIntLoopAttribute(L
, "llvm.loop.interleave.count");
54 if (VectorizeWidth
.getValueOr(0) != 1)
56 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
57 "FailedRequestedVectorization",
58 L
->getStartLoc(), L
->getHeader())
59 << "loop not vectorized: the optimizer was unable to perform the "
60 "requested transformation; the transformation might be disabled "
61 "or specified as part of an unsupported transformation ordering");
62 else if (InterleaveCount
.getValueOr(0) != 1)
64 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
65 "FailedRequestedInterleaving",
66 L
->getStartLoc(), L
->getHeader())
67 << "loop not interleaved: 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");
72 if (hasDistributeTransformation(L
) == TM_ForcedByUser
) {
73 LLVM_DEBUG(dbgs() << "Leftover distribute transformation\n");
75 DiagnosticInfoOptimizationFailure(DEBUG_TYPE
,
76 "FailedRequestedDistribution",
77 L
->getStartLoc(), L
->getHeader())
78 << "loop not distributed: the optimizer was unable to perform the "
79 "requested transformation; the transformation might be disabled or "
80 "specified as part of an unsupported transformation ordering");
84 static void warnAboutLeftoverTransformations(Function
*F
, LoopInfo
*LI
,
85 OptimizationRemarkEmitter
*ORE
) {
86 for (auto *L
: LI
->getLoopsInPreorder())
87 warnAboutLeftoverTransformations(L
, ORE
);
90 // New pass manager boilerplate
92 WarnMissedTransformationsPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
93 // Do not warn about not applied transformations if optimizations are
95 if (F
.hasFnAttribute(Attribute::OptimizeNone
))
96 return PreservedAnalyses::all();
98 auto &ORE
= AM
.getResult
<OptimizationRemarkEmitterAnalysis
>(F
);
99 auto &LI
= AM
.getResult
<LoopAnalysis
>(F
);
101 warnAboutLeftoverTransformations(&F
, &LI
, &ORE
);
103 return PreservedAnalyses::all();
106 // Legacy pass manager boilerplate
108 class WarnMissedTransformationsLegacy
: public FunctionPass
{
112 explicit WarnMissedTransformationsLegacy() : FunctionPass(ID
) {
113 initializeWarnMissedTransformationsLegacyPass(
114 *PassRegistry::getPassRegistry());
117 bool runOnFunction(Function
&F
) override
{
121 auto &ORE
= getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
122 auto &LI
= getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
124 warnAboutLeftoverTransformations(&F
, &LI
, &ORE
);
128 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
129 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
130 AU
.addRequired
<LoopInfoWrapperPass
>();
132 AU
.setPreservesAll();
135 } // end anonymous namespace
137 char WarnMissedTransformationsLegacy::ID
= 0;
139 INITIALIZE_PASS_BEGIN(WarnMissedTransformationsLegacy
, "transform-warning",
140 "Warn about non-applied transformations", false, false)
141 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
142 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
143 INITIALIZE_PASS_END(WarnMissedTransformationsLegacy
, "transform-warning",
144 "Warn about non-applied transformations", false, false)
146 Pass
*llvm::createWarnMissedTransformationsPass() {
147 return new WarnMissedTransformationsLegacy();