blob: 707adf46d1f4dd9de4ac42d34c6056f4cf923d03 [file] [log] [blame]
Michael Kruse72448522018-12-12 17:32:52 +00001//===- LoopTransformWarning.cpp - ----------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Kruse72448522018-12-12 17:32:52 +00006//
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/OptimizationRemarkEmitter.h"
15#include "llvm/Transforms/Utils/LoopUtils.h"
16
17using namespace llvm;
18
19#define DEBUG_TYPE "transform-warning"
20
21/// Emit warnings for forced (i.e. user-defined) loop transformations which have
22/// still not been performed.
23static void warnAboutLeftoverTransformations(Loop *L,
24 OptimizationRemarkEmitter *ORE) {
25 if (hasUnrollTransformation(L) == TM_ForcedByUser) {
26 LLVM_DEBUG(dbgs() << "Leftover unroll transformation\n");
27 ORE->emit(
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");
34 }
35
36 if (hasUnrollAndJamTransformation(L) == TM_ForcedByUser) {
37 LLVM_DEBUG(dbgs() << "Leftover unroll-and-jam transformation\n");
38 ORE->emit(
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");
45 }
46
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");
53
Alina Sbirleaa34bcbf2019-01-25 21:12:08 +000054 if (VectorizeWidth.getValueOr(0) != 1)
Michael Kruse72448522018-12-12 17:32:52 +000055 ORE->emit(
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");
Alina Sbirleaa34bcbf2019-01-25 21:12:08 +000062 else if (InterleaveCount.getValueOr(0) != 1)
Michael Kruse72448522018-12-12 17:32:52 +000063 ORE->emit(
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");
70 }
71
72 if (hasDistributeTransformation(L) == TM_ForcedByUser) {
73 LLVM_DEBUG(dbgs() << "Leftover distribute transformation\n");
74 ORE->emit(
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");
81 }
82}
83
84static void warnAboutLeftoverTransformations(Function *F, LoopInfo *LI,
85 OptimizationRemarkEmitter *ORE) {
86 for (auto *L : LI->getLoopsInPreorder())
87 warnAboutLeftoverTransformations(L, ORE);
88}
89
90// New pass manager boilerplate
91PreservedAnalyses
92WarnMissedTransformationsPass::run(Function &F, FunctionAnalysisManager &AM) {
Michael Kruseea9ef342018-12-14 19:45:43 +000093 // Do not warn about not applied transformations if optimizations are
94 // disabled.
Evandro Menezes85bd3972019-04-04 22:40:06 +000095 if (F.hasOptNone())
Michael Kruseea9ef342018-12-14 19:45:43 +000096 return PreservedAnalyses::all();
97
Michael Kruse72448522018-12-12 17:32:52 +000098 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
99 auto &LI = AM.getResult<LoopAnalysis>(F);
100
101 warnAboutLeftoverTransformations(&F, &LI, &ORE);
102
103 return PreservedAnalyses::all();
104}
105
106// Legacy pass manager boilerplate
107namespace {
108class WarnMissedTransformationsLegacy : public FunctionPass {
109public:
110 static char ID;
111
112 explicit WarnMissedTransformationsLegacy() : FunctionPass(ID) {
113 initializeWarnMissedTransformationsLegacyPass(
114 *PassRegistry::getPassRegistry());
115 }
116
117 bool runOnFunction(Function &F) override {
118 if (skipFunction(F))
119 return false;
120
121 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
122 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
123
124 warnAboutLeftoverTransformations(&F, &LI, &ORE);
125 return false;
126 }
127
128 void getAnalysisUsage(AnalysisUsage &AU) const override {
129 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
130 AU.addRequired<LoopInfoWrapperPass>();
131
132 AU.setPreservesAll();
133 }
134};
135} // end anonymous namespace
136
137char WarnMissedTransformationsLegacy::ID = 0;
138
139INITIALIZE_PASS_BEGIN(WarnMissedTransformationsLegacy, "transform-warning",
140 "Warn about non-applied transformations", false, false)
141INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
142INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
143INITIALIZE_PASS_END(WarnMissedTransformationsLegacy, "transform-warning",
144 "Warn about non-applied transformations", false, false)
145
146Pass *llvm::createWarnMissedTransformationsPass() {
147 return new WarnMissedTransformationsLegacy();
148}