Skip to content

Commit 52c338d

Browse files
authored
[llvm][NFC] Rework Timer.cpp globals to ensure valid lifetimes (#121663)
This is intended to help with flang `-ftime-report` support: - #107270. With this change, I was able to cherry-pick #107270, uncomment `llvm::TimePassesIsEnabled = true;` and compile with `-ftime-report`. I also noticed that `clang/lib/Driver/OffloadBundler.cpp` was statically constructing a `TimerGroup` and changed it to lazily construct via ManagedStatic.
1 parent c3d1a50 commit 52c338d

File tree

3 files changed

+182
-95
lines changed

3 files changed

+182
-95
lines changed

clang/lib/Driver/OffloadBundler.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/Support/ErrorOr.h"
3838
#include "llvm/Support/FileSystem.h"
3939
#include "llvm/Support/MD5.h"
40+
#include "llvm/Support/ManagedStatic.h"
4041
#include "llvm/Support/MemoryBuffer.h"
4142
#include "llvm/Support/Path.h"
4243
#include "llvm/Support/Program.h"
@@ -63,9 +64,17 @@ using namespace llvm;
6364
using namespace llvm::object;
6465
using namespace clang;
6566

66-
static llvm::TimerGroup
67-
ClangOffloadBundlerTimerGroup("Clang Offload Bundler Timer Group",
68-
"Timer group for clang offload bundler");
67+
namespace {
68+
struct CreateClangOffloadBundlerTimerGroup {
69+
static void *call() {
70+
return new TimerGroup("Clang Offload Bundler Timer Group",
71+
"Timer group for clang offload bundler");
72+
}
73+
};
74+
} // namespace
75+
static llvm::ManagedStatic<llvm::TimerGroup,
76+
CreateClangOffloadBundlerTimerGroup>
77+
ClangOffloadBundlerTimerGroup;
6978

7079
/// Magic string that marks the existence of offloading data.
7180
#define OFFLOAD_BUNDLER_MAGIC_STR "__CLANG_OFFLOAD_BUNDLE__"
@@ -987,7 +996,7 @@ CompressedOffloadBundle::compress(llvm::compression::Params P,
987996
"Compression not supported");
988997

989998
llvm::Timer HashTimer("Hash Calculation Timer", "Hash calculation time",
990-
ClangOffloadBundlerTimerGroup);
999+
*ClangOffloadBundlerTimerGroup);
9911000
if (Verbose)
9921001
HashTimer.startTimer();
9931002
llvm::MD5 Hash;
@@ -1004,7 +1013,7 @@ CompressedOffloadBundle::compress(llvm::compression::Params P,
10041013
Input.getBuffer().size());
10051014

10061015
llvm::Timer CompressTimer("Compression Timer", "Compression time",
1007-
ClangOffloadBundlerTimerGroup);
1016+
*ClangOffloadBundlerTimerGroup);
10081017
if (Verbose)
10091018
CompressTimer.startTimer();
10101019
llvm::compression::compress(P, BufferUint8, CompressedBuffer);
@@ -1119,7 +1128,7 @@ CompressedOffloadBundle::decompress(const llvm::MemoryBuffer &Input,
11191128
"Unknown compressing method");
11201129

11211130
llvm::Timer DecompressTimer("Decompression Timer", "Decompression time",
1122-
ClangOffloadBundlerTimerGroup);
1131+
*ClangOffloadBundlerTimerGroup);
11231132
if (Verbose)
11241133
DecompressTimer.startTimer();
11251134

@@ -1141,7 +1150,7 @@ CompressedOffloadBundle::decompress(const llvm::MemoryBuffer &Input,
11411150
// Recalculate MD5 hash for integrity check
11421151
llvm::Timer HashRecalcTimer("Hash Recalculation Timer",
11431152
"Hash recalculation time",
1144-
ClangOffloadBundlerTimerGroup);
1153+
*ClangOffloadBundlerTimerGroup);
11451154
HashRecalcTimer.startTimer();
11461155
llvm::MD5 Hash;
11471156
llvm::MD5::MD5Result Result;

llvm/include/llvm/Support/Timer.h

+6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
#include "llvm/ADT/StringMap.h"
1313
#include "llvm/ADT/StringRef.h"
1414
#include "llvm/Support/DataTypes.h"
15+
#include "llvm/Support/Mutex.h"
1516
#include <cassert>
1617
#include <memory>
1718
#include <string>
1819
#include <vector>
1920

2021
namespace llvm {
2122

23+
class TimerGlobals;
2224
class TimerGroup;
2325
class raw_ostream;
2426

@@ -196,6 +198,10 @@ class TimerGroup {
196198
TimerGroup(const TimerGroup &TG) = delete;
197199
void operator=(const TimerGroup &TG) = delete;
198200

201+
friend class TimerGlobals;
202+
explicit TimerGroup(StringRef Name, StringRef Description,
203+
sys::SmartMutex<true> &lock);
204+
199205
public:
200206
explicit TimerGroup(StringRef Name, StringRef Description);
201207

0 commit comments

Comments
 (0)