[compiler-rt] [MemProf] Use correct print_text value (PR #125793)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 4 17:26:06 PST 2025


https://github.com/ellishg created https://github.com/llvm/llvm-project/pull/125793

Use the correct value for `print_text` in `FinishAndWrite()`.

This value was initialized in the constructor of a global variable 
https://github.com/llvm/llvm-project/blob/65683b081fd049750e57d95a311575a3ba324344/compiler-rt/lib/memprof/memprof_allocator.cpp#L641

The flag values in `flags()` are populated in `InitializeFlags()`, which is also called in a constructor of a global variable
https://github.com/llvm/llvm-project/blob/65683b081fd049750e57d95a311575a3ba324344/compiler-rt/lib/memprof/memprof_rtl.cpp#L241-L255

I think the order that these two functions run is undefined, and the value of the field `print_text` depends on this order when the user overrides default values.

>From b73f4df99ada7077413db3cb4a0213dfc1beea6e Mon Sep 17 00:00:00 2001
From: Ellis Hoag <ellishoag at meta.com>
Date: Tue, 4 Feb 2025 17:15:21 -0800
Subject: [PATCH] [MemProf] Use correct print_text value

---
 compiler-rt/lib/memprof/memprof_allocator.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/memprof/memprof_allocator.cpp b/compiler-rt/lib/memprof/memprof_allocator.cpp
index c3448c22532bb89..60f5c853f9d76ff 100644
--- a/compiler-rt/lib/memprof/memprof_allocator.cpp
+++ b/compiler-rt/lib/memprof/memprof_allocator.cpp
@@ -291,10 +291,9 @@ struct Allocator {
 
   atomic_uint8_t destructing;
   atomic_uint8_t constructed;
-  bool print_text;
 
   // ------------------- Initialization ------------------------
-  explicit Allocator(LinkerInitialized) : print_text(flags()->print_text) {
+  explicit Allocator(LinkerInitialized) {
     atomic_store_relaxed(&destructing, 0);
     atomic_store_relaxed(&constructed, 1);
   }
@@ -350,13 +349,13 @@ struct Allocator {
   }
 
   void FinishAndWrite() {
-    if (print_text && common_flags()->print_module_map)
+    if (flags()->print_text && common_flags()->print_module_map)
       DumpProcessMap();
 
     allocator.ForceLock();
 
     InsertLiveBlocks();
-    if (print_text) {
+    if (flags()->print_text) {
       if (!flags()->print_terse)
         Printf("Recorded MIBs (incl. live on exit):\n");
       MIBMap.ForEach(PrintCallback,



More information about the llvm-commits mailing list