1 //===- LoopTransformWarning.cpp -  ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Emit warnings if forced code transformations have not been performed.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
15 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
16 #include "llvm/Transforms/Utils/LoopUtils.h"
17 
18 using 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.
24 static void warnAboutLeftoverTransformations(Loop *L,
25                                              OptimizationRemarkEmitter *ORE) {
26   if (hasUnrollTransformation(L) == TM_ForcedByUser) {
27     LLVM_DEBUG(dbgs() << "Leftover unroll transformation\n");
28     ORE->emit(
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");
35   }
36 
37   if (hasUnrollAndJamTransformation(L) == TM_ForcedByUser) {
38     LLVM_DEBUG(dbgs() << "Leftover unroll-and-jam transformation\n");
39     ORE->emit(
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");
46   }
47 
48   if (hasVectorizeTransformation(L) == TM_ForcedByUser) {
49     LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n");
50     Optional<int> VectorizeWidth =
51         getOptionalIntLoopAttribute(L, "llvm.loop.vectorize.width");
52     Optional<int> InterleaveCount =
53         getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
54 
55     if (VectorizeWidth.getValueOr(0) != 1)
56       ORE->emit(
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.getValueOr(0) != 1)
64       ORE->emit(
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");
71   }
72 
73   if (hasDistributeTransformation(L) == TM_ForcedByUser) {
74     LLVM_DEBUG(dbgs() << "Leftover distribute transformation\n");
75     ORE->emit(
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");
82   }
83 }
84 
85 static void warnAboutLeftoverTransformations(Function *F, LoopInfo *LI,
86                                              OptimizationRemarkEmitter *ORE) {
87   for (auto *L : LI->getLoopsInPreorder())
88     warnAboutLeftoverTransformations(L, ORE);
89 }
90 
91 // New pass manager boilerplate
92 PreservedAnalyses
93 WarnMissedTransformationsPass::run(Function &F, FunctionAnalysisManager &AM) {
94   auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
95   auto &LI = AM.getResult<LoopAnalysis>(F);
96 
97   warnAboutLeftoverTransformations(&F, &LI, &ORE);
98 
99   return PreservedAnalyses::all();
100 }
101 
102 // Legacy pass manager boilerplate
103 namespace {
104 class WarnMissedTransformationsLegacy : public FunctionPass {
105 public:
106   static char ID;
107 
108   explicit WarnMissedTransformationsLegacy() : FunctionPass(ID) {
109     initializeWarnMissedTransformationsLegacyPass(
110         *PassRegistry::getPassRegistry());
111   }
112 
113   bool runOnFunction(Function &F) override {
114     if (skipFunction(F))
115       return false;
116 
117     auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
118     auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
119 
120     warnAboutLeftoverTransformations(&F, &LI, &ORE);
121     return false;
122   }
123 
124   void getAnalysisUsage(AnalysisUsage &AU) const override {
125     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
126     AU.addRequired<LoopInfoWrapperPass>();
127 
128     AU.setPreservesAll();
129   }
130 };
131 } // end anonymous namespace
132 
133 char WarnMissedTransformationsLegacy::ID = 0;
134 
135 INITIALIZE_PASS_BEGIN(WarnMissedTransformationsLegacy, "transform-warning",
136                       "Warn about non-applied transformations", false, false)
137 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
138 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
139 INITIALIZE_PASS_END(WarnMissedTransformationsLegacy, "transform-warning",
140                     "Warn about non-applied transformations", false, false)
141 
142 Pass *llvm::createWarnMissedTransformationsPass() {
143   return new WarnMissedTransformationsLegacy();
144 }
145