Skip to content
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

Workaround -Wglobal-constructor warning. #94699

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

schweitzpgi
Copy link
Contributor

This line was tripping the -Wglobal-constructor warning which was causing a build failure when -Werror was turned on.

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 6, 2024

@llvm/pr-subscribers-llvm-support

Author: Eric Schweitz (schweitzpgi)

Changes

This line was tripping the -Wglobal-constructor warning which was causing a build failure when -Werror was turned on.


Full diff: https://github.com/llvm/llvm-project/pull/94699.diff

1 Files Affected:

  • (modified) llvm/lib/Support/CodeGenCoverage.cpp (+5-2)
diff --git a/llvm/lib/Support/CodeGenCoverage.cpp b/llvm/lib/Support/CodeGenCoverage.cpp
index 4d41c42e527e2..f87cbf18ef1a5 100644
--- a/llvm/lib/Support/CodeGenCoverage.cpp
+++ b/llvm/lib/Support/CodeGenCoverage.cpp
@@ -21,7 +21,10 @@
 
 using namespace llvm;
 
-static sys::SmartMutex<true> OutputMutex;
+static sys::SmartMutex<true> &OutputMutex() {
+  static sys::SmartMutex<true> mutex;
+  return mutex;
+}
 
 CodeGenCoverage::CodeGenCoverage() = default;
 
@@ -79,7 +82,7 @@ bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
 bool CodeGenCoverage::emit(StringRef CoveragePrefix,
                            StringRef BackendName) const {
   if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
-    sys::SmartScopedLock<true> Lock(OutputMutex);
+    sys::SmartScopedLock<true> Lock(OutputMutex());
 
     // We can handle locking within a process easily enough but we don't want to
     // manage it between multiple processes. Use the process ID to ensure no

@dwblaikie
Copy link
Collaborator

dwblaikie commented Jun 6, 2024

Huh, I'm surprised we're remotely -Wglobal-constructors clean... but guess so, maybe.

Was this a recent regression? Wouldn't hurt to reference the commit hash of the blame and cc the author on this patch?

@schweitzpgi
Copy link
Contributor Author

This regards the global added by @danielsanders in f76f315.

Just hiding the mutex in a function body to avoid the warning.

@schweitzpgi
Copy link
Contributor Author

Was this a recent regression?

It was a "recent" regression in that our project was changing how it was built and packaged. But this global variable has been around since 2017.

@schweitzpgi
Copy link
Contributor Author

Here's the evidence/snippage that the warning/error actually can be triggered in our environ.

$ make
[  0%] Built target LLVMDemangle
[  0%] Built target LLVMSupportBlake3
Consolidate compiler generated dependencies of target LLVMSupport
[  0%] Building CXX object lib/Support/CMakeFiles/LLVMSupport.dir/CodeGenCoverage.cpp.o
/home/eric/tpls/llvm/llvm/lib/Support/CodeGenCoverage.cpp:24:30: error: declaration requires a global destructor [-Werror,-Wglobal-constructors]
static sys::SmartMutex<true> OutputMutex;
                             ^
1 error generated.
make[2]: *** [lib/Support/CMakeFiles/LLVMSupport.dir/build.make:426: lib/Support/CMakeFiles/LLVMSupport.dir/CodeGenCoverage.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:20373: lib/Support/CMakeFiles/LLVMSupport.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

@dwblaikie
Copy link
Collaborator

If it's been around for 7 years, it seems like a bit of a change to start enforcing a build invariant now - and maybe a hassle for you if it's not an invariant that the rest of the developers on LLVM are actively enforcing... (& so may be better for you to disable this warning when building LLVM, rather than trying to clean it up)

And is this really the only instance/problem? As much as LLVM's tried to avoid global ctors, my understanding was that it was far from clean. I'd imagine once this is fixed, you'd hit more/other instances of this?

@efriedma-quic
Copy link
Collaborator

libSupport is supposed to support this... but there's a specific carveout for targets that don't support constant initialization of mutexes. See https://reviews.llvm.org/D105959 / 402461b / etc.

CC @joker-eph

@dwblaikie
Copy link
Collaborator

libSupport is supposed to support this... but there's a specific carveout for targets that don't support constant initialization of mutexes. See https://reviews.llvm.org/D105959 / 402461b / etc.

CC @joker-eph

oooh right... I remember that vaguely.

Though in this case it's a global /dtor/ hmm, nope, the carveout seems to do the right thing for that+mutexes.

SmartMutex has a recursive_mutex and unsigned member, so it should have the global ctor/dtor impact that recursive_mutex has, which is tested by the carveout @efriedma-quic linked...

@schweitzpgi could you look into why that carveout isn't working for you?

@@ -79,7 +82,7 @@ bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
bool CodeGenCoverage::emit(StringRef CoveragePrefix,
StringRef BackendName) const {
if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
sys::SmartScopedLock<true> Lock(OutputMutex);
sys::SmartScopedLock<true> Lock(OutputMutex());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only place it is used, you can do simpler I believe:

Suggested change
sys::SmartScopedLock<true> Lock(OutputMutex());
static sys::SmartMutex<true> OutputMutex;
sys::SmartScopedLock<true> Lock(OutputMutex);

The global is only useful if used in multiple places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the variable is static, this is the only use.

@schweitzpgi
Copy link
Contributor Author

If it's been around for 7 years, it seems like a bit of a change to start enforcing a build invariant now - and maybe a hassle for you if it's not an invariant that the rest of the developers on LLVM are actively enforcing... (& so may be better for you to disable this warning when building LLVM, rather than trying to clean it up)

And is this really the only instance/problem? As much as LLVM's tried to avoid global ctors, my understanding was that it was far from clean. I'd imagine once this is fixed, you'd hit more/other instances of this?

It is the only instance. It would be nice to fix this upstream instead of have to patch around it in the build environment since it is trivial.

@schweitzpgi
Copy link
Contributor Author

@schweitzpgi could you look into why that carveout isn't working for you?

No clue.

This line was tripping the -Wglobal-constructor warning which was
causing a build failure when -Werror was turned on.

code review comment
Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

As a followup, if we really don't need the carveout for mutexes anymore, could you look at removing it?

@schweitzpgi schweitzpgi merged commit 7326aa7 into llvm:main Jun 10, 2024
4 of 6 checks passed
@schweitzpgi schweitzpgi deleted the ch-global-constructor branch June 10, 2024 18:29
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
This line was tripping the -Wglobal-constructor warning which was
causing a build failure when -Werror was turned on.
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants