Skip to content

[llvm][NFC] Rework Timer.cpp globals to ensure valid lifetimes #121663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions clang/lib/Driver/OffloadBundler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
Expand All @@ -63,9 +64,17 @@ using namespace llvm;
using namespace llvm::object;
using namespace clang;

static llvm::TimerGroup
ClangOffloadBundlerTimerGroup("Clang Offload Bundler Timer Group",
"Timer group for clang offload bundler");
namespace {
struct CreateClangOffloadBundlerTimerGroup {
static void *call() {
return new TimerGroup("Clang Offload Bundler Timer Group",
"Timer group for clang offload bundler");
}
};
} // namespace
static llvm::ManagedStatic<llvm::TimerGroup,
CreateClangOffloadBundlerTimerGroup>
ClangOffloadBundlerTimerGroup;

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

llvm::Timer HashTimer("Hash Calculation Timer", "Hash calculation time",
ClangOffloadBundlerTimerGroup);
*ClangOffloadBundlerTimerGroup);
if (Verbose)
HashTimer.startTimer();
llvm::MD5 Hash;
Expand All @@ -1004,7 +1013,7 @@ CompressedOffloadBundle::compress(llvm::compression::Params P,
Input.getBuffer().size());

llvm::Timer CompressTimer("Compression Timer", "Compression time",
ClangOffloadBundlerTimerGroup);
*ClangOffloadBundlerTimerGroup);
if (Verbose)
CompressTimer.startTimer();
llvm::compression::compress(P, BufferUint8, CompressedBuffer);
Expand Down Expand Up @@ -1119,7 +1128,7 @@ CompressedOffloadBundle::decompress(const llvm::MemoryBuffer &Input,
"Unknown compressing method");

llvm::Timer DecompressTimer("Decompression Timer", "Decompression time",
ClangOffloadBundlerTimerGroup);
*ClangOffloadBundlerTimerGroup);
if (Verbose)
DecompressTimer.startTimer();

Expand All @@ -1141,7 +1150,7 @@ CompressedOffloadBundle::decompress(const llvm::MemoryBuffer &Input,
// Recalculate MD5 hash for integrity check
llvm::Timer HashRecalcTimer("Hash Recalculation Timer",
"Hash recalculation time",
ClangOffloadBundlerTimerGroup);
*ClangOffloadBundlerTimerGroup);
HashRecalcTimer.startTimer();
llvm::MD5 Hash;
llvm::MD5::MD5Result Result;
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/Support/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Mutex.h"
#include <cassert>
#include <memory>
#include <string>
#include <vector>

namespace llvm {

class TimerGlobals;
class TimerGroup;
class raw_ostream;

Expand Down Expand Up @@ -196,6 +198,10 @@ class TimerGroup {
TimerGroup(const TimerGroup &TG) = delete;
void operator=(const TimerGroup &TG) = delete;

friend class TimerGlobals;
explicit TimerGroup(StringRef Name, StringRef Description,
sys::SmartMutex<true> &lock);

public:
explicit TimerGroup(StringRef Name, StringRef Description);

Expand Down
Loading
Loading