Bug Summary

File:build/source/llvm/lib/IR/PassTimingInfo.cpp
Warning:line 211, column 16
Value stored to 'OS' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name PassTimingInfo.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/IR -I /build/source/llvm/lib/IR -I include -I /build/source/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1683717183 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-05-10-133810-16478-1 -x c++ /build/source/llvm/lib/IR/PassTimingInfo.cpp
1//===- PassTimingInfo.cpp - LLVM Pass Timing Implementation ---------------===//
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// This file implements the LLVM Pass Timing infrastructure for both
10// new and legacy pass managers.
11//
12// PassTimingInfo Class - This class is used to calculate information about the
13// amount of time each pass takes to execute. This only happens when
14// -time-passes is enabled on the command line.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/IR/PassTimingInfo.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/IR/PassInstrumentation.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/FormatVariadic.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/Mutex.h"
27#include "llvm/Support/TypeName.h"
28#include "llvm/Support/raw_ostream.h"
29#include <string>
30
31using namespace llvm;
32
33#define DEBUG_TYPE"time-passes" "time-passes"
34
35namespace llvm {
36
37bool TimePassesIsEnabled = false;
38bool TimePassesPerRun = false;
39
40static cl::opt<bool, true> EnableTiming(
41 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
42 cl::desc("Time each pass, printing elapsed time for each on exit"));
43
44static cl::opt<bool, true> EnableTimingPerRun(
45 "time-passes-per-run", cl::location(TimePassesPerRun), cl::Hidden,
46 cl::desc("Time each pass run, printing elapsed time for each run on exit"),
47 cl::callback([](const bool &) { TimePassesIsEnabled = true; }));
48
49namespace {
50namespace legacy {
51
52//===----------------------------------------------------------------------===//
53// Legacy pass manager's PassTimingInfo implementation
54
55/// Provides an interface for collecting pass timing information.
56///
57/// It was intended to be generic but now we decided to split
58/// interfaces completely. This is now exclusively for legacy-pass-manager use.
59class PassTimingInfo {
60public:
61 using PassInstanceID = void *;
62
63private:
64 StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
65 DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
66 TimerGroup TG;
67
68public:
69 /// Default constructor for yet-inactive timeinfo.
70 /// Use \p init() to activate it.
71 PassTimingInfo();
72
73 /// Print out timing information and release timers.
74 ~PassTimingInfo();
75
76 /// Initializes the static \p TheTimeInfo member to a non-null value when
77 /// -time-passes is enabled. Leaves it null otherwise.
78 ///
79 /// This method may be called multiple times.
80 static void init();
81
82 /// Prints out timing information and then resets the timers.
83 /// By default it uses the stream created by CreateInfoOutputFile().
84 void print(raw_ostream *OutStream = nullptr);
85
86 /// Returns the timer for the specified pass if it exists.
87 Timer *getPassTimer(Pass *, PassInstanceID);
88
89 static PassTimingInfo *TheTimeInfo;
90
91private:
92 Timer *newPassTimer(StringRef PassID, StringRef PassDesc);
93};
94
95static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
96
97PassTimingInfo::PassTimingInfo() : TG("pass", "Pass execution timing report") {}
98
99PassTimingInfo::~PassTimingInfo() {
100 // Deleting the timers accumulates their info into the TG member.
101 // Then TG member is (implicitly) deleted, actually printing the report.
102 TimingData.clear();
103}
104
105void PassTimingInfo::init() {
106 if (!TimePassesIsEnabled || TheTimeInfo)
107 return;
108
109 // Constructed the first time this is called, iff -time-passes is enabled.
110 // This guarantees that the object will be constructed after static globals,
111 // thus it will be destroyed before them.
112 static ManagedStatic<PassTimingInfo> TTI;
113 TheTimeInfo = &*TTI;
114}
115
116/// Prints out timing information and then resets the timers.
117void PassTimingInfo::print(raw_ostream *OutStream) {
118 TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
119}
120
121Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
122 unsigned &num = PassIDCountMap[PassID];
123 num++;
124 // Appending description with a pass-instance number for all but the first one
125 std::string PassDescNumbered =
126 num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
127 return new Timer(PassID, PassDescNumbered, TG);
128}
129
130Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
131 if (P->getAsPMDataManager())
132 return nullptr;
133
134 init();
135 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
136 std::unique_ptr<Timer> &T = TimingData[Pass];
137
138 if (!T) {
139 StringRef PassName = P->getPassName();
140 StringRef PassArgument;
141 if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
142 PassArgument = PI->getPassArgument();
143 T.reset(newPassTimer(PassArgument.empty() ? PassName : PassArgument, PassName));
144 }
145 return T.get();
146}
147
148PassTimingInfo *PassTimingInfo::TheTimeInfo;
149} // namespace legacy
150} // namespace
151
152Timer *getPassTimer(Pass *P) {
153 legacy::PassTimingInfo::init();
154 if (legacy::PassTimingInfo::TheTimeInfo)
155 return legacy::PassTimingInfo::TheTimeInfo->getPassTimer(P, P);
156 return nullptr;
157}
158
159/// If timing is enabled, report the times collected up to now and then reset
160/// them.
161void reportAndResetTimings(raw_ostream *OutStream) {
162 if (legacy::PassTimingInfo::TheTimeInfo)
163 legacy::PassTimingInfo::TheTimeInfo->print(OutStream);
164}
165
166//===----------------------------------------------------------------------===//
167// Pass timing handling for the New Pass Manager
168//===----------------------------------------------------------------------===//
169
170/// Returns the timer for the specified pass invocation of \p PassID.
171/// Each time it creates a new timer.
172Timer &TimePassesHandler::getPassTimer(StringRef PassID, bool IsPass) {
173 TimerGroup &TG = IsPass ? PassTG : AnalysisTG;
174 if (!PerRun) {
175 TimerVector &Timers = TimingData[PassID];
176 if (Timers.size() == 0)
177 Timers.emplace_back(new Timer(PassID, PassID, TG));
178 return *Timers.front();
179 }
180
181 // Take a vector of Timers created for this \p PassID and append
182 // one more timer to it.
183 TimerVector &Timers = TimingData[PassID];
184 unsigned Count = Timers.size() + 1;
185
186 std::string FullDesc = formatv("{0} #{1}", PassID, Count).str();
187
188 Timer *T = new Timer(PassID, FullDesc, TG);
189 Timers.emplace_back(T);
190 assert(Count == Timers.size() && "Timers vector not adjusted correctly.")(static_cast <bool> (Count == Timers.size() && "Timers vector not adjusted correctly."
) ? void (0) : __assert_fail ("Count == Timers.size() && \"Timers vector not adjusted correctly.\""
, "llvm/lib/IR/PassTimingInfo.cpp", 190, __extension__ __PRETTY_FUNCTION__
))
;
191
192 return *T;
193}
194
195TimePassesHandler::TimePassesHandler(bool Enabled, bool PerRun)
196 : PassTG("pass", "Pass execution timing report"),
197 AnalysisTG("analysis", "Analysis execution timing report"),
198 Enabled(Enabled), PerRun(PerRun) {}
199
200TimePassesHandler::TimePassesHandler()
201 : TimePassesHandler(TimePassesIsEnabled, TimePassesPerRun) {}
202
203void TimePassesHandler::setOutStream(raw_ostream &Out) {
204 OutStream = &Out;
205}
206
207void TimePassesHandler::print() {
208 if (!Enabled)
209 return;
210 std::unique_ptr<raw_ostream> MaybeCreated;
211 raw_ostream *OS = OutStream;
Value stored to 'OS' during its initialization is never read
212 if (OutStream) {
213 OS = OutStream;
214 } else {
215 MaybeCreated = CreateInfoOutputFile();
216 OS = &*MaybeCreated;
217 }
218 PassTG.print(*OS, true);
219 AnalysisTG.print(*OS, true);
220}
221
222LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void TimePassesHandler::dump() const {
223 dbgs() << "Dumping timers for " << getTypeName<TimePassesHandler>()
224 << ":\n\tRunning:\n";
225 for (auto &I : TimingData) {
226 StringRef PassID = I.getKey();
227 const TimerVector& MyTimers = I.getValue();
228 for (unsigned idx = 0; idx < MyTimers.size(); idx++) {
229 const Timer* MyTimer = MyTimers[idx].get();
230 if (MyTimer && MyTimer->isRunning())
231 dbgs() << "\tTimer " << MyTimer << " for pass " << PassID << "(" << idx << ")\n";
232 }
233 }
234 dbgs() << "\tTriggered:\n";
235 for (auto &I : TimingData) {
236 StringRef PassID = I.getKey();
237 const TimerVector& MyTimers = I.getValue();
238 for (unsigned idx = 0; idx < MyTimers.size(); idx++) {
239 const Timer* MyTimer = MyTimers[idx].get();
240 if (MyTimer && MyTimer->hasTriggered() && !MyTimer->isRunning())
241 dbgs() << "\tTimer " << MyTimer << " for pass " << PassID << "(" << idx << ")\n";
242 }
243 }
244}
245
246static bool shouldIgnorePass(StringRef PassID) {
247 return isSpecialPass(PassID,
248 {"PassManager", "PassAdaptor", "AnalysisManagerProxy",
249 "ModuleInlinerWrapperPass", "DevirtSCCRepeatedPass"});
250}
251
252void TimePassesHandler::startPassTimer(StringRef PassID) {
253 if (shouldIgnorePass(PassID))
254 return;
255 assert(!ActivePassTimer && "should only have one pass timer at a time")(static_cast <bool> (!ActivePassTimer && "should only have one pass timer at a time"
) ? void (0) : __assert_fail ("!ActivePassTimer && \"should only have one pass timer at a time\""
, "llvm/lib/IR/PassTimingInfo.cpp", 255, __extension__ __PRETTY_FUNCTION__
))
;
256 Timer &MyTimer = getPassTimer(PassID, /*IsPass*/ true);
257 ActivePassTimer = &MyTimer;
258 assert(!MyTimer.isRunning())(static_cast <bool> (!MyTimer.isRunning()) ? void (0) :
__assert_fail ("!MyTimer.isRunning()", "llvm/lib/IR/PassTimingInfo.cpp"
, 258, __extension__ __PRETTY_FUNCTION__))
;
259 MyTimer.startTimer();
260}
261
262void TimePassesHandler::stopPassTimer(StringRef PassID) {
263 if (shouldIgnorePass(PassID))
264 return;
265 assert(ActivePassTimer)(static_cast <bool> (ActivePassTimer) ? void (0) : __assert_fail
("ActivePassTimer", "llvm/lib/IR/PassTimingInfo.cpp", 265, __extension__
__PRETTY_FUNCTION__))
;
266 assert(ActivePassTimer->isRunning())(static_cast <bool> (ActivePassTimer->isRunning()) ?
void (0) : __assert_fail ("ActivePassTimer->isRunning()",
"llvm/lib/IR/PassTimingInfo.cpp", 266, __extension__ __PRETTY_FUNCTION__
))
;
267 ActivePassTimer->stopTimer();
268 ActivePassTimer = nullptr;
269}
270
271void TimePassesHandler::startAnalysisTimer(StringRef PassID) {
272 // Stop the previous analysis timer to prevent double counting when an
273 // analysis requests another analysis.
274 if (!AnalysisActiveTimerStack.empty()) {
275 assert(AnalysisActiveTimerStack.back()->isRunning())(static_cast <bool> (AnalysisActiveTimerStack.back()->
isRunning()) ? void (0) : __assert_fail ("AnalysisActiveTimerStack.back()->isRunning()"
, "llvm/lib/IR/PassTimingInfo.cpp", 275, __extension__ __PRETTY_FUNCTION__
))
;
276 AnalysisActiveTimerStack.back()->stopTimer();
277 }
278
279 Timer &MyTimer = getPassTimer(PassID, /*IsPass*/ false);
280 AnalysisActiveTimerStack.push_back(&MyTimer);
281 if (!MyTimer.isRunning())
282 MyTimer.startTimer();
283}
284
285void TimePassesHandler::stopAnalysisTimer(StringRef PassID) {
286 assert(!AnalysisActiveTimerStack.empty() && "empty stack in popTimer")(static_cast <bool> (!AnalysisActiveTimerStack.empty() &&
"empty stack in popTimer") ? void (0) : __assert_fail ("!AnalysisActiveTimerStack.empty() && \"empty stack in popTimer\""
, "llvm/lib/IR/PassTimingInfo.cpp", 286, __extension__ __PRETTY_FUNCTION__
))
;
287 Timer *MyTimer = AnalysisActiveTimerStack.pop_back_val();
288 assert(MyTimer && "timer should be present")(static_cast <bool> (MyTimer && "timer should be present"
) ? void (0) : __assert_fail ("MyTimer && \"timer should be present\""
, "llvm/lib/IR/PassTimingInfo.cpp", 288, __extension__ __PRETTY_FUNCTION__
))
;
289 if (MyTimer->isRunning())
290 MyTimer->stopTimer();
291
292 // Restart the previously stopped timer.
293 if (!AnalysisActiveTimerStack.empty()) {
294 assert(!AnalysisActiveTimerStack.back()->isRunning())(static_cast <bool> (!AnalysisActiveTimerStack.back()->
isRunning()) ? void (0) : __assert_fail ("!AnalysisActiveTimerStack.back()->isRunning()"
, "llvm/lib/IR/PassTimingInfo.cpp", 294, __extension__ __PRETTY_FUNCTION__
))
;
295 AnalysisActiveTimerStack.back()->startTimer();
296 }
297}
298
299void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
300 if (!Enabled)
301 return;
302
303 PIC.registerBeforeNonSkippedPassCallback(
304 [this](StringRef P, Any) { this->startPassTimer(P); });
305 PIC.registerAfterPassCallback(
306 [this](StringRef P, Any, const PreservedAnalyses &) {
307 this->stopPassTimer(P);
308 });
309 PIC.registerAfterPassInvalidatedCallback(
310 [this](StringRef P, const PreservedAnalyses &) {
311 this->stopPassTimer(P);
312 });
313 PIC.registerBeforeAnalysisCallback(
314 [this](StringRef P, Any) { this->startAnalysisTimer(P); });
315 PIC.registerAfterAnalysisCallback(
316 [this](StringRef P, Any) { this->stopAnalysisTimer(P); });
317}
318
319} // namespace llvm