Skip to content

[llvm] properly guard dump methods in Support lib classes #139938

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 1 commit into from
May 14, 2025

Conversation

andrurogerz
Copy link
Contributor

Purpose

Add proper preprocessor guards for all dump() methods in the LLVM support library. This change ensures these methods are not part of the public ABI for release builds.

Overview

  • Annotates all dump methods in Support and ADT source with the LLVM_DUMP_METHOD macro.
  • Conditionally includes all dump method definitions in Support and ADT source so they are only present on debug/assert builds and when LLVM_ENABLE_DUMP is explicitly defined.

NOTE: For many of these dump methods, the implementation was already properly guarded but the declaration in the header file was not.

Background

This PR is a redo of #139804 with some changes to fix clang and unit test build breaks.

This issue was raised in comments on #136629. I am addressing it as a separate change since it is independent from the changes being made in that PR.

According to this documentation, dump methods should be annotated with LLVM_DUMP_METHOD and conditionally included as follows:

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif

Validation

  • Local release build succeeds.
  • CI

@andrurogerz andrurogerz marked this pull request as ready for review May 14, 2025 19:17
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer llvm:support llvm:adt labels May 14, 2025
@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-llvm-adt
@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Andrew Rogers (andrurogerz)

Changes

Purpose

Add proper preprocessor guards for all dump() methods in the LLVM support library. This change ensures these methods are not part of the public ABI for release builds.

Overview

  • Annotates all dump methods in Support and ADT source with the LLVM_DUMP_METHOD macro.
  • Conditionally includes all dump method definitions in Support and ADT source so they are only present on debug/assert builds and when LLVM_ENABLE_DUMP is explicitly defined.

NOTE: For many of these dump methods, the implementation was already properly guarded but the declaration in the header file was not.

Background

This PR is a redo of #139804 with some changes to fix clang and unit test build breaks.

This issue was raised in comments on #136629. I am addressing it as a separate change since it is independent from the changes being made in that PR.

According to this documentation, dump methods should be annotated with LLVM_DUMP_METHOD and conditionally included as follows:

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif

Validation

  • Local release build succeeds.
  • CI

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

20 Files Affected:

  • (modified) clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h (+2)
  • (modified) llvm/include/llvm/ADT/APFixedPoint.h (+4-1)
  • (modified) llvm/include/llvm/ADT/APFloat.h (+4-1)
  • (modified) llvm/include/llvm/ADT/APInt.h (+3-1)
  • (modified) llvm/include/llvm/ADT/DynamicAPInt.h (+2)
  • (modified) llvm/include/llvm/ADT/SlowDynamicAPInt.h (+3)
  • (modified) llvm/include/llvm/ADT/TrieRawHashMap.h (+6)
  • (modified) llvm/include/llvm/ADT/Twine.h (+6-4)
  • (modified) llvm/include/llvm/Support/BranchProbability.h (+3-1)
  • (modified) llvm/include/llvm/Support/DebugCounter.h (+3-1)
  • (modified) llvm/include/llvm/Support/KnownBits.h (+4-1)
  • (modified) llvm/include/llvm/Support/SMTAPI.h (+12-4)
  • (modified) llvm/include/llvm/Support/ScaledNumber.h (+10-2)
  • (modified) llvm/lib/Support/APFixedPoint.cpp (+3)
  • (modified) llvm/lib/Support/DebugCounter.cpp (+2)
  • (modified) llvm/lib/Support/DynamicAPInt.cpp (+3-1)
  • (modified) llvm/lib/Support/KnownBits.cpp (+4-1)
  • (modified) llvm/lib/Support/ScaledNumber.cpp (+3-1)
  • (modified) llvm/lib/Support/SlowDynamicAPInt.cpp (+3-1)
  • (modified) llvm/lib/Support/Z3Solver.cpp (+2)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
index d4052ef90de6e..3105dfa4dae55 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
@@ -301,8 +301,10 @@ class SMTConstraintManager : public clang::ento::SimpleConstraintManager {
     llvm_unreachable("Unsupported expression to reason about!");
   }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// Dumps SMT formula
   LLVM_DUMP_METHOD void dump() const { Solver->dump(); }
+#endif
 
 protected:
   // Check whether a new model is satisfiable, and update the program state.
diff --git a/llvm/include/llvm/ADT/APFixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h
index 70d7f325702cf..89d2a93a06a26 100644
--- a/llvm/include/llvm/ADT/APFixedPoint.h
+++ b/llvm/include/llvm/ADT/APFixedPoint.h
@@ -249,7 +249,10 @@ class APFixedPoint {
   }
 
   void print(raw_ostream &) const;
-  void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   // If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
   int compare(const APFixedPoint &Other) const;
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index ed49380cfc05f..b88cbc56c105c 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1483,7 +1483,10 @@ class APFloat : public APFloatBase {
   }
 
   void print(raw_ostream &) const;
-  void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   bool getExactInverse(APFloat *inv) const {
     APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 7fbf09b44e6c4..44260c7eca309 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1896,8 +1896,10 @@ class [[nodiscard]] APInt {
   ///  FoldingSets.
   void Profile(FoldingSetNodeID &id) const;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// debug method
-  void dump() const;
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   /// Returns whether this instance allocated memory.
   bool needsCleanup() const { return !isSingleWord(); }
diff --git a/llvm/include/llvm/ADT/DynamicAPInt.h b/llvm/include/llvm/ADT/DynamicAPInt.h
index ff958d48e7731..bb65a08a968d9 100644
--- a/llvm/include/llvm/ADT/DynamicAPInt.h
+++ b/llvm/include/llvm/ADT/DynamicAPInt.h
@@ -216,7 +216,9 @@ class DynamicAPInt {
   void static_assert_layout(); // NOLINT
 
   raw_ostream &print(raw_ostream &OS) const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const DynamicAPInt &X) {
diff --git a/llvm/include/llvm/ADT/SlowDynamicAPInt.h b/llvm/include/llvm/ADT/SlowDynamicAPInt.h
index ec1021892cf4d..c9aef96b9e1c3 100644
--- a/llvm/include/llvm/ADT/SlowDynamicAPInt.h
+++ b/llvm/include/llvm/ADT/SlowDynamicAPInt.h
@@ -79,7 +79,10 @@ class SlowDynamicAPInt {
   unsigned getBitWidth() const { return Val.getBitWidth(); }
 
   void print(raw_ostream &OS) const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const SlowDynamicAPInt &X) {
diff --git a/llvm/include/llvm/ADT/TrieRawHashMap.h b/llvm/include/llvm/ADT/TrieRawHashMap.h
index e312967edeb58..1382eac1c768f 100644
--- a/llvm/include/llvm/ADT/TrieRawHashMap.h
+++ b/llvm/include/llvm/ADT/TrieRawHashMap.h
@@ -90,7 +90,10 @@ class ThreadSafeTrieRawHashMapBase {
   static void *operator new(size_t Size) { return ::operator new(Size); }
   void operator delete(void *Ptr) { ::operator delete(Ptr); }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
+
   void print(raw_ostream &OS) const;
 
 protected:
@@ -214,7 +217,10 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
   using ThreadSafeTrieRawHashMapBase::operator delete;
   using HashType = HashT;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   using ThreadSafeTrieRawHashMapBase::dump;
+#endif
+
   using ThreadSafeTrieRawHashMapBase::print;
 
 private:
diff --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h
index 1f1fd1967efbc..d9e553a8a8c77 100644
--- a/llvm/include/llvm/ADT/Twine.h
+++ b/llvm/include/llvm/ADT/Twine.h
@@ -507,14 +507,16 @@ namespace llvm {
     /// stream \p OS.
     void print(raw_ostream &OS) const;
 
-    /// Dump the concatenated string represented by this twine to stderr.
-    void dump() const;
-
     /// Write the representation of this twine to the stream \p OS.
     void printRepr(raw_ostream &OS) const;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+    /// Dump the concatenated string represented by this twine to stderr.
+    LLVM_DUMP_METHOD void dump() const;
+
     /// Dump the representation of this twine to stderr.
-    void dumpRepr() const;
+    LLVM_DUMP_METHOD void dumpRepr() const;
+#endif
 
     /// @}
   };
diff --git a/llvm/include/llvm/Support/BranchProbability.h b/llvm/include/llvm/Support/BranchProbability.h
index 570531e6b9e92..42fe225709ef8 100644
--- a/llvm/include/llvm/Support/BranchProbability.h
+++ b/llvm/include/llvm/Support/BranchProbability.h
@@ -77,7 +77,9 @@ class BranchProbability {
 
   LLVM_ABI raw_ostream &print(raw_ostream &OS) const;
 
-  LLVM_ABI void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   /// Scale a large integer.
   ///
diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h
index 529a9f86f2e34..9611586a92c3b 100644
--- a/llvm/include/llvm/Support/DebugCounter.h
+++ b/llvm/include/llvm/Support/DebugCounter.h
@@ -119,8 +119,10 @@ class DebugCounter {
     Counter.CurrChunkIdx = State.ChunkIdx;
   }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   // Dump or print the current counter set into llvm::dbgs().
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   LLVM_ABI void print(raw_ostream &OS) const;
 
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 6a14328d431a4..e8dc1c2422646 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -513,7 +513,10 @@ struct KnownBits {
   bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
 
   LLVM_ABI void print(raw_ostream &OS) const;
-  LLVM_ABI void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 private:
   // Internal helper for getting the initial KnownBits for an `srem` or `urem`
diff --git a/llvm/include/llvm/Support/SMTAPI.h b/llvm/include/llvm/Support/SMTAPI.h
index f1bb86cf81f1c..aed6241219c39 100644
--- a/llvm/include/llvm/Support/SMTAPI.h
+++ b/llvm/include/llvm/Support/SMTAPI.h
@@ -71,7 +71,9 @@ class SMTSort {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 protected:
   /// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -118,7 +120,9 @@ class SMTExpr {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 protected:
   /// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -136,7 +140,9 @@ class SMTSolverStatistics {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 /// Shared pointer for SMTExprs, used by SMTSolver API.
@@ -152,7 +158,9 @@ class SMTSolver {
   SMTSolver() = default;
   virtual ~SMTSolver() = default;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   // Returns an appropriate floating-point sort for the given bitwidth.
   SMTSortRef getFloatSort(unsigned BitWidth) {
diff --git a/llvm/include/llvm/Support/ScaledNumber.h b/llvm/include/llvm/Support/ScaledNumber.h
index 87a56809976a3..3d38677f0eb61 100644
--- a/llvm/include/llvm/Support/ScaledNumber.h
+++ b/llvm/include/llvm/Support/ScaledNumber.h
@@ -424,7 +424,10 @@ class ScaledNumberBase {
 public:
   static constexpr int DefaultPrecision = 10;
 
-  LLVM_ABI static void dump(uint64_t D, int16_t E, int Width);
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD static void dump(uint64_t D, int16_t E, int Width);
+#endif
+
   LLVM_ABI static raw_ostream &print(raw_ostream &OS, uint64_t D, int16_t E,
                                      int Width, unsigned Precision);
   LLVM_ABI static std::string toString(uint64_t D, int16_t E, int Width,
@@ -607,7 +610,12 @@ template <class DigitsT> class ScaledNumber : ScaledNumberBase {
                      unsigned Precision = DefaultPrecision) const {
     return ScaledNumberBase::print(OS, Digits, Scale, Width, Precision);
   }
-  void dump() const { return ScaledNumberBase::dump(Digits, Scale, Width); }
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const {
+    return ScaledNumberBase::dump(Digits, Scale, Width);
+  }
+#endif
 
   ScaledNumber &operator+=(const ScaledNumber &X) {
     std::tie(Digits, Scale) =
diff --git a/llvm/lib/Support/APFixedPoint.cpp b/llvm/lib/Support/APFixedPoint.cpp
index f395919287b72..9a7caa4112625 100644
--- a/llvm/lib/Support/APFixedPoint.cpp
+++ b/llvm/lib/Support/APFixedPoint.cpp
@@ -439,7 +439,10 @@ void APFixedPoint::print(raw_ostream &OS) const {
   Sema.print(OS);
   OS << "})";
 }
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void APFixedPoint::dump() const { print(llvm::errs()); }
+#endif
 
 APFixedPoint APFixedPoint::negate(bool *Overflow) const {
   if (!isSaturated()) {
diff --git a/llvm/lib/Support/DebugCounter.cpp b/llvm/lib/Support/DebugCounter.cpp
index a6de07a55482a..9c4a4429ca0ee 100644
--- a/llvm/lib/Support/DebugCounter.cpp
+++ b/llvm/lib/Support/DebugCounter.cpp
@@ -248,6 +248,8 @@ bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
   return true;
 }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void DebugCounter::dump() const {
   print(dbgs());
 }
+#endif
diff --git a/llvm/lib/Support/DynamicAPInt.cpp b/llvm/lib/Support/DynamicAPInt.cpp
index bfcb97e0cc96a..9def5c782af4c 100644
--- a/llvm/lib/Support/DynamicAPInt.cpp
+++ b/llvm/lib/Support/DynamicAPInt.cpp
@@ -32,4 +32,6 @@ raw_ostream &DynamicAPInt::print(raw_ostream &OS) const {
   return OS << ValLarge;
 }
 
-void DynamicAPInt::dump() const { print(dbgs()); }
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void DynamicAPInt::dump() const { print(dbgs()); }
+#endif
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 16229598b612a..94a04ab90987a 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -1152,7 +1152,10 @@ void KnownBits::print(raw_ostream &OS) const {
       OS << "?";
   }
 }
-void KnownBits::dump() const {
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void KnownBits::dump() const {
   print(dbgs());
   dbgs() << "\n";
 }
+#endif
diff --git a/llvm/lib/Support/ScaledNumber.cpp b/llvm/lib/Support/ScaledNumber.cpp
index 85d7afbea5c69..33e8cc3030873 100644
--- a/llvm/lib/Support/ScaledNumber.cpp
+++ b/llvm/lib/Support/ScaledNumber.cpp
@@ -317,7 +317,9 @@ raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
   return OS << toString(D, E, Width, Precision);
 }
 
-void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
   print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
                                 << "]";
 }
+#endif
diff --git a/llvm/lib/Support/SlowDynamicAPInt.cpp b/llvm/lib/Support/SlowDynamicAPInt.cpp
index 8b4030ddf9fc4..a57fec2f824e1 100644
--- a/llvm/lib/Support/SlowDynamicAPInt.cpp
+++ b/llvm/lib/Support/SlowDynamicAPInt.cpp
@@ -283,4 +283,6 @@ SlowDynamicAPInt &SlowDynamicAPInt::operator--() {
 /// ---------------------------------------------------------------------------
 void SlowDynamicAPInt::print(raw_ostream &OS) const { OS << Val; }
 
-void SlowDynamicAPInt::dump() const { print(dbgs()); }
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void SlowDynamicAPInt::dump() const { print(dbgs()); }
+#endif
diff --git a/llvm/lib/Support/Z3Solver.cpp b/llvm/lib/Support/Z3Solver.cpp
index 9aece099b0629..27027093a0c6f 100644
--- a/llvm/lib/Support/Z3Solver.cpp
+++ b/llvm/lib/Support/Z3Solver.cpp
@@ -989,7 +989,9 @@ llvm::SMTSolverRef llvm::CreateZ3Solver() {
 #endif
 }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void SMTSort::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTExpr::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTSolver::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTSolverStatistics::dump() const { print(llvm::errs()); }
+#endif

@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-llvm-support

Author: Andrew Rogers (andrurogerz)

Changes

Purpose

Add proper preprocessor guards for all dump() methods in the LLVM support library. This change ensures these methods are not part of the public ABI for release builds.

Overview

  • Annotates all dump methods in Support and ADT source with the LLVM_DUMP_METHOD macro.
  • Conditionally includes all dump method definitions in Support and ADT source so they are only present on debug/assert builds and when LLVM_ENABLE_DUMP is explicitly defined.

NOTE: For many of these dump methods, the implementation was already properly guarded but the declaration in the header file was not.

Background

This PR is a redo of #139804 with some changes to fix clang and unit test build breaks.

This issue was raised in comments on #136629. I am addressing it as a separate change since it is independent from the changes being made in that PR.

According to this documentation, dump methods should be annotated with LLVM_DUMP_METHOD and conditionally included as follows:

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif

Validation

  • Local release build succeeds.
  • CI

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

20 Files Affected:

  • (modified) clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h (+2)
  • (modified) llvm/include/llvm/ADT/APFixedPoint.h (+4-1)
  • (modified) llvm/include/llvm/ADT/APFloat.h (+4-1)
  • (modified) llvm/include/llvm/ADT/APInt.h (+3-1)
  • (modified) llvm/include/llvm/ADT/DynamicAPInt.h (+2)
  • (modified) llvm/include/llvm/ADT/SlowDynamicAPInt.h (+3)
  • (modified) llvm/include/llvm/ADT/TrieRawHashMap.h (+6)
  • (modified) llvm/include/llvm/ADT/Twine.h (+6-4)
  • (modified) llvm/include/llvm/Support/BranchProbability.h (+3-1)
  • (modified) llvm/include/llvm/Support/DebugCounter.h (+3-1)
  • (modified) llvm/include/llvm/Support/KnownBits.h (+4-1)
  • (modified) llvm/include/llvm/Support/SMTAPI.h (+12-4)
  • (modified) llvm/include/llvm/Support/ScaledNumber.h (+10-2)
  • (modified) llvm/lib/Support/APFixedPoint.cpp (+3)
  • (modified) llvm/lib/Support/DebugCounter.cpp (+2)
  • (modified) llvm/lib/Support/DynamicAPInt.cpp (+3-1)
  • (modified) llvm/lib/Support/KnownBits.cpp (+4-1)
  • (modified) llvm/lib/Support/ScaledNumber.cpp (+3-1)
  • (modified) llvm/lib/Support/SlowDynamicAPInt.cpp (+3-1)
  • (modified) llvm/lib/Support/Z3Solver.cpp (+2)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
index d4052ef90de6e..3105dfa4dae55 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConstraintManager.h
@@ -301,8 +301,10 @@ class SMTConstraintManager : public clang::ento::SimpleConstraintManager {
     llvm_unreachable("Unsupported expression to reason about!");
   }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// Dumps SMT formula
   LLVM_DUMP_METHOD void dump() const { Solver->dump(); }
+#endif
 
 protected:
   // Check whether a new model is satisfiable, and update the program state.
diff --git a/llvm/include/llvm/ADT/APFixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h
index 70d7f325702cf..89d2a93a06a26 100644
--- a/llvm/include/llvm/ADT/APFixedPoint.h
+++ b/llvm/include/llvm/ADT/APFixedPoint.h
@@ -249,7 +249,10 @@ class APFixedPoint {
   }
 
   void print(raw_ostream &) const;
-  void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   // If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
   int compare(const APFixedPoint &Other) const;
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index ed49380cfc05f..b88cbc56c105c 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1483,7 +1483,10 @@ class APFloat : public APFloatBase {
   }
 
   void print(raw_ostream &) const;
-  void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   bool getExactInverse(APFloat *inv) const {
     APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 7fbf09b44e6c4..44260c7eca309 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1896,8 +1896,10 @@ class [[nodiscard]] APInt {
   ///  FoldingSets.
   void Profile(FoldingSetNodeID &id) const;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// debug method
-  void dump() const;
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   /// Returns whether this instance allocated memory.
   bool needsCleanup() const { return !isSingleWord(); }
diff --git a/llvm/include/llvm/ADT/DynamicAPInt.h b/llvm/include/llvm/ADT/DynamicAPInt.h
index ff958d48e7731..bb65a08a968d9 100644
--- a/llvm/include/llvm/ADT/DynamicAPInt.h
+++ b/llvm/include/llvm/ADT/DynamicAPInt.h
@@ -216,7 +216,9 @@ class DynamicAPInt {
   void static_assert_layout(); // NOLINT
 
   raw_ostream &print(raw_ostream &OS) const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const DynamicAPInt &X) {
diff --git a/llvm/include/llvm/ADT/SlowDynamicAPInt.h b/llvm/include/llvm/ADT/SlowDynamicAPInt.h
index ec1021892cf4d..c9aef96b9e1c3 100644
--- a/llvm/include/llvm/ADT/SlowDynamicAPInt.h
+++ b/llvm/include/llvm/ADT/SlowDynamicAPInt.h
@@ -79,7 +79,10 @@ class SlowDynamicAPInt {
   unsigned getBitWidth() const { return Val.getBitWidth(); }
 
   void print(raw_ostream &OS) const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const SlowDynamicAPInt &X) {
diff --git a/llvm/include/llvm/ADT/TrieRawHashMap.h b/llvm/include/llvm/ADT/TrieRawHashMap.h
index e312967edeb58..1382eac1c768f 100644
--- a/llvm/include/llvm/ADT/TrieRawHashMap.h
+++ b/llvm/include/llvm/ADT/TrieRawHashMap.h
@@ -90,7 +90,10 @@ class ThreadSafeTrieRawHashMapBase {
   static void *operator new(size_t Size) { return ::operator new(Size); }
   void operator delete(void *Ptr) { ::operator delete(Ptr); }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   LLVM_DUMP_METHOD void dump() const;
+#endif
+
   void print(raw_ostream &OS) const;
 
 protected:
@@ -214,7 +217,10 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
   using ThreadSafeTrieRawHashMapBase::operator delete;
   using HashType = HashT;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   using ThreadSafeTrieRawHashMapBase::dump;
+#endif
+
   using ThreadSafeTrieRawHashMapBase::print;
 
 private:
diff --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h
index 1f1fd1967efbc..d9e553a8a8c77 100644
--- a/llvm/include/llvm/ADT/Twine.h
+++ b/llvm/include/llvm/ADT/Twine.h
@@ -507,14 +507,16 @@ namespace llvm {
     /// stream \p OS.
     void print(raw_ostream &OS) const;
 
-    /// Dump the concatenated string represented by this twine to stderr.
-    void dump() const;
-
     /// Write the representation of this twine to the stream \p OS.
     void printRepr(raw_ostream &OS) const;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+    /// Dump the concatenated string represented by this twine to stderr.
+    LLVM_DUMP_METHOD void dump() const;
+
     /// Dump the representation of this twine to stderr.
-    void dumpRepr() const;
+    LLVM_DUMP_METHOD void dumpRepr() const;
+#endif
 
     /// @}
   };
diff --git a/llvm/include/llvm/Support/BranchProbability.h b/llvm/include/llvm/Support/BranchProbability.h
index 570531e6b9e92..42fe225709ef8 100644
--- a/llvm/include/llvm/Support/BranchProbability.h
+++ b/llvm/include/llvm/Support/BranchProbability.h
@@ -77,7 +77,9 @@ class BranchProbability {
 
   LLVM_ABI raw_ostream &print(raw_ostream &OS) const;
 
-  LLVM_ABI void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   /// Scale a large integer.
   ///
diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h
index 529a9f86f2e34..9611586a92c3b 100644
--- a/llvm/include/llvm/Support/DebugCounter.h
+++ b/llvm/include/llvm/Support/DebugCounter.h
@@ -119,8 +119,10 @@ class DebugCounter {
     Counter.CurrChunkIdx = State.ChunkIdx;
   }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   // Dump or print the current counter set into llvm::dbgs().
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   LLVM_ABI void print(raw_ostream &OS) const;
 
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 6a14328d431a4..e8dc1c2422646 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -513,7 +513,10 @@ struct KnownBits {
   bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
 
   LLVM_ABI void print(raw_ostream &OS) const;
-  LLVM_ABI void dump() const;
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 private:
   // Internal helper for getting the initial KnownBits for an `srem` or `urem`
diff --git a/llvm/include/llvm/Support/SMTAPI.h b/llvm/include/llvm/Support/SMTAPI.h
index f1bb86cf81f1c..aed6241219c39 100644
--- a/llvm/include/llvm/Support/SMTAPI.h
+++ b/llvm/include/llvm/Support/SMTAPI.h
@@ -71,7 +71,9 @@ class SMTSort {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 protected:
   /// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -118,7 +120,9 @@ class SMTExpr {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
 protected:
   /// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -136,7 +140,9 @@ class SMTSolverStatistics {
 
   virtual void print(raw_ostream &OS) const = 0;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 };
 
 /// Shared pointer for SMTExprs, used by SMTSolver API.
@@ -152,7 +158,9 @@ class SMTSolver {
   SMTSolver() = default;
   virtual ~SMTSolver() = default;
 
-  LLVM_ABI LLVM_DUMP_METHOD void dump() const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const;
+#endif
 
   // Returns an appropriate floating-point sort for the given bitwidth.
   SMTSortRef getFloatSort(unsigned BitWidth) {
diff --git a/llvm/include/llvm/Support/ScaledNumber.h b/llvm/include/llvm/Support/ScaledNumber.h
index 87a56809976a3..3d38677f0eb61 100644
--- a/llvm/include/llvm/Support/ScaledNumber.h
+++ b/llvm/include/llvm/Support/ScaledNumber.h
@@ -424,7 +424,10 @@ class ScaledNumberBase {
 public:
   static constexpr int DefaultPrecision = 10;
 
-  LLVM_ABI static void dump(uint64_t D, int16_t E, int Width);
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD static void dump(uint64_t D, int16_t E, int Width);
+#endif
+
   LLVM_ABI static raw_ostream &print(raw_ostream &OS, uint64_t D, int16_t E,
                                      int Width, unsigned Precision);
   LLVM_ABI static std::string toString(uint64_t D, int16_t E, int Width,
@@ -607,7 +610,12 @@ template <class DigitsT> class ScaledNumber : ScaledNumberBase {
                      unsigned Precision = DefaultPrecision) const {
     return ScaledNumberBase::print(OS, Digits, Scale, Width, Precision);
   }
-  void dump() const { return ScaledNumberBase::dump(Digits, Scale, Width); }
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump() const {
+    return ScaledNumberBase::dump(Digits, Scale, Width);
+  }
+#endif
 
   ScaledNumber &operator+=(const ScaledNumber &X) {
     std::tie(Digits, Scale) =
diff --git a/llvm/lib/Support/APFixedPoint.cpp b/llvm/lib/Support/APFixedPoint.cpp
index f395919287b72..9a7caa4112625 100644
--- a/llvm/lib/Support/APFixedPoint.cpp
+++ b/llvm/lib/Support/APFixedPoint.cpp
@@ -439,7 +439,10 @@ void APFixedPoint::print(raw_ostream &OS) const {
   Sema.print(OS);
   OS << "})";
 }
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void APFixedPoint::dump() const { print(llvm::errs()); }
+#endif
 
 APFixedPoint APFixedPoint::negate(bool *Overflow) const {
   if (!isSaturated()) {
diff --git a/llvm/lib/Support/DebugCounter.cpp b/llvm/lib/Support/DebugCounter.cpp
index a6de07a55482a..9c4a4429ca0ee 100644
--- a/llvm/lib/Support/DebugCounter.cpp
+++ b/llvm/lib/Support/DebugCounter.cpp
@@ -248,6 +248,8 @@ bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
   return true;
 }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void DebugCounter::dump() const {
   print(dbgs());
 }
+#endif
diff --git a/llvm/lib/Support/DynamicAPInt.cpp b/llvm/lib/Support/DynamicAPInt.cpp
index bfcb97e0cc96a..9def5c782af4c 100644
--- a/llvm/lib/Support/DynamicAPInt.cpp
+++ b/llvm/lib/Support/DynamicAPInt.cpp
@@ -32,4 +32,6 @@ raw_ostream &DynamicAPInt::print(raw_ostream &OS) const {
   return OS << ValLarge;
 }
 
-void DynamicAPInt::dump() const { print(dbgs()); }
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void DynamicAPInt::dump() const { print(dbgs()); }
+#endif
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 16229598b612a..94a04ab90987a 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -1152,7 +1152,10 @@ void KnownBits::print(raw_ostream &OS) const {
       OS << "?";
   }
 }
-void KnownBits::dump() const {
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void KnownBits::dump() const {
   print(dbgs());
   dbgs() << "\n";
 }
+#endif
diff --git a/llvm/lib/Support/ScaledNumber.cpp b/llvm/lib/Support/ScaledNumber.cpp
index 85d7afbea5c69..33e8cc3030873 100644
--- a/llvm/lib/Support/ScaledNumber.cpp
+++ b/llvm/lib/Support/ScaledNumber.cpp
@@ -317,7 +317,9 @@ raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
   return OS << toString(D, E, Width, Precision);
 }
 
-void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
   print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
                                 << "]";
 }
+#endif
diff --git a/llvm/lib/Support/SlowDynamicAPInt.cpp b/llvm/lib/Support/SlowDynamicAPInt.cpp
index 8b4030ddf9fc4..a57fec2f824e1 100644
--- a/llvm/lib/Support/SlowDynamicAPInt.cpp
+++ b/llvm/lib/Support/SlowDynamicAPInt.cpp
@@ -283,4 +283,6 @@ SlowDynamicAPInt &SlowDynamicAPInt::operator--() {
 /// ---------------------------------------------------------------------------
 void SlowDynamicAPInt::print(raw_ostream &OS) const { OS << Val; }
 
-void SlowDynamicAPInt::dump() const { print(dbgs()); }
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void SlowDynamicAPInt::dump() const { print(dbgs()); }
+#endif
diff --git a/llvm/lib/Support/Z3Solver.cpp b/llvm/lib/Support/Z3Solver.cpp
index 9aece099b0629..27027093a0c6f 100644
--- a/llvm/lib/Support/Z3Solver.cpp
+++ b/llvm/lib/Support/Z3Solver.cpp
@@ -989,7 +989,9 @@ llvm::SMTSolverRef llvm::CreateZ3Solver() {
 #endif
 }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD void SMTSort::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTExpr::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTSolver::dump() const { print(llvm::errs()); }
 LLVM_DUMP_METHOD void SMTSolverStatistics::dump() const { print(llvm::errs()); }
+#endif

@compnerd compnerd merged commit 0563186 into llvm:main May 14, 2025
18 checks passed
@andrurogerz andrurogerz deleted the dump-guards-v2 branch May 14, 2025 20:49
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 14, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve-vls running on linaro-g3-01 while building clang,llvm at step 7 "ninja check 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/143/builds/7708

Here is the relevant piece of the build log for the reference
Step 7 (ninja check 1) failure: stage 1 checked (failure)
...
[699/1183] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/LexicalScopesTest.cpp.o
[700/1183] Building CXX object unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/LegalizerTest.cpp.o
[700/1183] cd /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/stage1/runtimes/runtimes-bins && /usr/local/bin/cmake --build /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/stage1/runtimes/runtimes-bins/ --target runtimes-test-depends --config Release
[1/6] Linking CXX executable flang-rt/unittests/Evaluate/reshape.test
[2/6] Building CXX object flang-rt/unittests/third-party/unittest/UnitTestMain/CMakeFiles/llvm_gtest_main.dir/TestMain.cpp.o
[3/6] Linking CXX static library /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/stage1/lib/libllvm_gtest_main.a
[4/6] Linking CXX executable flang-rt/unittests/Evaluate/ISO-Fortran-binding.test
[5/6] Building CXX object flang-rt/unittests/Runtime/CMakeFiles/RuntimeTests.dir/AccessTest.cpp.o
[6/6] Linking CXX executable flang-rt/unittests/Runtime/RuntimeTests
[732/1183] Linking CXX executable unittests/CodeGen/GlobalISel/GlobalISelTests
FAILED: unittests/CodeGen/GlobalISel/GlobalISelTests 
: && /usr/local/bin/c++ -mcpu=neoverse-512tvb -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=lld -Wl,--color-diagnostics    -Wl,--gc-sections unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/ConstantFoldingTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/CSETest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/GIMatchTableExecutorTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/LegalizerTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/LegalizerHelperTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/LegalizerInfoTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/MachineIRBuilderTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/GISelMITest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/PatternMatchTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/KnownBitsTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/KnownBitsVectorTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/GISelUtilsTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/GISelAliasTest.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/CallLowering.cpp.o unittests/CodeGen/GlobalISel/CMakeFiles/GlobalISelTests.dir/InstructionSelectTest.cpp.o -o unittests/CodeGen/GlobalISel/GlobalISelTests  lib/libLLVMAArch64CodeGen.a  lib/libLLVMAArch64AsmParser.a  lib/libLLVMAArch64Desc.a  lib/libLLVMAArch64Disassembler.a  lib/libLLVMAArch64Info.a  lib/libLLVMAArch64Utils.a  lib/libLLVMAMDGPUCodeGen.a  lib/libLLVMAMDGPUAsmParser.a  lib/libLLVMAMDGPUDesc.a  lib/libLLVMAMDGPUDisassembler.a  lib/libLLVMAMDGPUInfo.a  lib/libLLVMAMDGPUUtils.a  lib/libLLVMARMCodeGen.a  lib/libLLVMARMAsmParser.a  lib/libLLVMARMDesc.a  lib/libLLVMARMDisassembler.a  lib/libLLVMARMInfo.a  lib/libLLVMARMUtils.a  lib/libLLVMAVRCodeGen.a  lib/libLLVMAVRAsmParser.a  lib/libLLVMAVRDesc.a  lib/libLLVMAVRDisassembler.a  lib/libLLVMAVRInfo.a  lib/libLLVMBPFCodeGen.a  lib/libLLVMBPFAsmParser.a  lib/libLLVMBPFDesc.a  lib/libLLVMBPFDisassembler.a  lib/libLLVMBPFInfo.a  lib/libLLVMHexagonCodeGen.a  lib/libLLVMHexagonAsmParser.a  lib/libLLVMHexagonDesc.a  lib/libLLVMHexagonDisassembler.a  lib/libLLVMHexagonInfo.a  lib/libLLVMLanaiCodeGen.a  lib/libLLVMLanaiAsmParser.a  lib/libLLVMLanaiDesc.a  lib/libLLVMLanaiDisassembler.a  lib/libLLVMLanaiInfo.a  lib/libLLVMLoongArchCodeGen.a  lib/libLLVMLoongArchAsmParser.a  lib/libLLVMLoongArchDesc.a  lib/libLLVMLoongArchDisassembler.a  lib/libLLVMLoongArchInfo.a  lib/libLLVMMipsCodeGen.a  lib/libLLVMMipsAsmParser.a  lib/libLLVMMipsDesc.a  lib/libLLVMMipsDisassembler.a  lib/libLLVMMipsInfo.a  lib/libLLVMMSP430CodeGen.a  lib/libLLVMMSP430AsmParser.a  lib/libLLVMMSP430Desc.a  lib/libLLVMMSP430Disassembler.a  lib/libLLVMMSP430Info.a  lib/libLLVMNVPTXCodeGen.a  lib/libLLVMNVPTXDesc.a  lib/libLLVMNVPTXInfo.a  lib/libLLVMPowerPCCodeGen.a  lib/libLLVMPowerPCAsmParser.a  lib/libLLVMPowerPCDesc.a  lib/libLLVMPowerPCDisassembler.a  lib/libLLVMPowerPCInfo.a  lib/libLLVMRISCVCodeGen.a  lib/libLLVMRISCVAsmParser.a  lib/libLLVMRISCVDesc.a  lib/libLLVMRISCVDisassembler.a  lib/libLLVMRISCVInfo.a  lib/libLLVMSparcCodeGen.a  lib/libLLVMSparcAsmParser.a  lib/libLLVMSparcDesc.a  lib/libLLVMSparcDisassembler.a  lib/libLLVMSparcInfo.a  lib/libLLVMSPIRVCodeGen.a  lib/libLLVMSPIRVDesc.a  lib/libLLVMSPIRVInfo.a  lib/libLLVMSystemZCodeGen.a  lib/libLLVMSystemZAsmParser.a  lib/libLLVMSystemZDesc.a  lib/libLLVMSystemZDisassembler.a  lib/libLLVMSystemZInfo.a  lib/libLLVMVECodeGen.a  lib/libLLVMVEAsmParser.a  lib/libLLVMVEDesc.a  lib/libLLVMVEDisassembler.a  lib/libLLVMVEInfo.a  lib/libLLVMWebAssemblyCodeGen.a  lib/libLLVMWebAssemblyAsmParser.a  lib/libLLVMWebAssemblyDesc.a  lib/libLLVMWebAssemblyDisassembler.a  lib/libLLVMWebAssemblyInfo.a  lib/libLLVMWebAssemblyUtils.a  lib/libLLVMX86CodeGen.a  lib/libLLVMX86AsmParser.a  lib/libLLVMX86Desc.a  lib/libLLVMX86Disassembler.a  lib/libLLVMX86Info.a  lib/libLLVMXCoreCodeGen.a  lib/libLLVMXCoreDesc.a  lib/libLLVMXCoreDisassembler.a  lib/libLLVMXCoreInfo.a  lib/libLLVMAsmParser.a  lib/libLLVMCodeGen.a  lib/libLLVMCodeGenTypes.a  lib/libLLVMCore.a  lib/libLLVMFileCheck.a  lib/libLLVMGlobalISel.a  lib/libLLVMMC.a  lib/libLLVMMIRParser.a  lib/libLLVMSupport.a  lib/libLLVMTarget.a  lib/libLLVMTargetParser.a  lib/libLLVMSupport.a  lib/libllvm_gtest_main.a  lib/libllvm_gtest.a  lib/libLLVMAArch64Desc.a  lib/libLLVMAArch64Info.a  lib/libLLVMAArch64Utils.a  lib/libLLVMAMDGPUDesc.a  lib/libLLVMAMDGPUInfo.a  lib/libLLVMAMDGPUUtils.a  lib/libLLVMARMDesc.a  lib/libLLVMARMInfo.a  lib/libLLVMARMUtils.a  lib/libLLVMPasses.a  lib/libLLVMHipStdPar.a  lib/libLLVMCoroutines.a  lib/libLLVMHexagonDesc.a  lib/libLLVMHexagonInfo.a  lib/libLLVMLanaiDesc.a  lib/libLLVMLanaiInfo.a  lib/libLLVMLoongArchDesc.a  lib/libLLVMLoongArchInfo.a  lib/libLLVMipo.a  lib/libLLVMVectorize.a  lib/libLLVMSandboxIR.a  lib/libLLVMFrontendOpenMP.a  lib/libLLVMFrontendOffloading.a  lib/libLLVMObjectYAML.a  lib/libLLVMFrontendAtomic.a  lib/libLLVMLinker.a  lib/libLLVMRISCVDesc.a  lib/libLLVMRISCVInfo.a  lib/libLLVMSPIRVAnalysis.a  lib/libLLVMSystemZDesc.a  lib/libLLVMSystemZInfo.a  lib/libLLVMWebAssemblyDesc.a  lib/libLLVMWebAssemblyInfo.a  lib/libLLVMGlobalISel.a  lib/libLLVMCFGuard.a  lib/libLLVMIRPrinter.a  lib/libLLVMInstrumentation.a  lib/libLLVMAsmPrinter.a  lib/libLLVMSelectionDAG.a  lib/libLLVMCodeGen.a  lib/libLLVMScalarOpts.a  lib/libLLVMAggressiveInstCombine.a  lib/libLLVMInstCombine.a  lib/libLLVMCGData.a  lib/libLLVMBitWriter.a  lib/libLLVMObjCARCOpts.a  lib/libLLVMTarget.a  lib/libLLVMTransformUtils.a  lib/libLLVMAnalysis.a  lib/libLLVMProfileData.a  lib/libLLVMSymbolize.a  lib/libLLVMDebugInfoGSYM.a  lib/libLLVMDebugInfoDWARF.a  lib/libLLVMDebugInfoPDB.a  lib/libLLVMObject.a  lib/libLLVMMCParser.a  lib/libLLVMIRReader.a  lib/libLLVMAsmParser.a  lib/libLLVMBitReader.a  lib/libLLVMCore.a  lib/libLLVMRemarks.a  lib/libLLVMBitstreamReader.a  lib/libLLVMTextAPI.a  lib/libLLVMDebugInfoCodeView.a  lib/libLLVMDebugInfoMSF.a  lib/libLLVMDebugInfoBTF.a  lib/libLLVMCodeGenTypes.a  lib/libLLVMMCDisassembler.a  lib/libLLVMMC.a  lib/libLLVMBinaryFormat.a  lib/libLLVMTargetParser.a  lib/libLLVMSupport.a  lib/libLLVMDemangle.a  -lrt  -ldl  -lm  /usr/lib/aarch64-linux-gnu/libz.so  -lpthread && :
ld.lld: error: cannot open lib/libllvm_gtest_main.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[734/1183] Building CXX object unittests/DebugInfo/DWARF/CMakeFiles/DebugInfoDWARFTests.dir/DwarfGenerator.cpp.o
[735/1183] Building CXX object unittests/CodeGen/CMakeFiles/CodeGenTests.dir/InstrRefLDVTest.cpp.o
[736/1183] Building CXX object unittests/DebugInfo/DWARF/CMakeFiles/DebugInfoDWARFTests.dir/DWARFDebugInfoTest.cpp.o
ninja: build stopped: subcommand failed.

@hvdijk
Copy link
Contributor

hvdijk commented May 19, 2025

This approach causes issues when LLVM is built in release mode (with -DNDEBUG) and external projects build against it in debug mode (with -UNDEBUG). I appreciate that it does what the comment says should be done, but I think what the comment says is wrong. Specifically, I now see in our own project:

module.cpp:(.text._ZNK4llvm12ScaledNumberImE4dumpEv[_ZNK4llvm12ScaledNumberImE4dumpEv]+0x34): undefined reference to `llvm::ScaledNumberBase::dump(unsigned long, short, int)'

where our module.cpp does not call any dump() anywhere (in fact our whole project does not call dump() anywhere), and the linker error does not tell where this reference is coming from.

There is a reason LLVM_ENABLE_ASSERTIONS can be toggled independently of build type (you can have CMAKE_BUILD_TYPE=Debug LLVM_ENABLE_ASSERTIONS=OFF if you like, or CMAKE_BUILD_TYPE=Release LLVM_ENABLE_ASSERTIONS=ON, CMAKE_BUILD_TYPE only affects the default value for LLVM_ENABLE_ASSERTIONS, it's never checked afterwards. I think we need to do the same for LLVM_ENABLE_DUMP.

I'm tracking down where this reference to dump() is and whether it can be worked around there, but if not, I think this PR should be reverted until we figure out how to proceed.

@hvdijk
Copy link
Contributor

hvdijk commented May 19, 2025

Minimal reproducer is

#include <llvm/IR/ModuleSummaryIndex.h>
int main() {}

Compile and link this without any special options (specifically: without -DNDEBUG) against an LLVM release build (this happened to be a TSan-enabled release build, I'm not sure if that is relevant), and it will now fail.

@andrurogerz
Copy link
Contributor Author

This approach causes issues when LLVM is built in release mode (with -DNDEBUG) and external projects build against it in debug mode (with -UNDEBUG). I appreciate that it does what the comment says should be done, but I think what the comment says is wrong. Specifically, I now see in our own project:

Taking a look now. Will revert today if there isn't a straight-forward fix.

@andrurogerz
Copy link
Contributor Author

Ok, this link error makes sense when building an external project in debug mode against an llvm release build. I didn't realize that was a thing, but it makes sense so my mistake.

The issue is that we must only guard dump methods (that have definitions outside of the header) if they are not referenced in a public header, even if that reference is also guarded. In this example, the static helper method llvm::ScaledNumberBase::dumpllvm::ScaledNumberBase::dump is called by another dump method in llvm/Support/ScaledNumber.h. I think this issue is safe enough to fix-forward, but I will revert if not. I'll get a PR up shortly either way.

@hvdijk
Copy link
Contributor

hvdijk commented May 19, 2025

Specifically here, ScaledNumber is a class template, that's what is causing it to be emitted in every translation unit that uses ScaledNumber<uint64_t>, even user code that isn't otherwise using it, and it's being emitted according to the then-current definition of NDEBUG.

The issue is that we must only guard dump methods (that have definitions outside of the header) if they are not referenced in a public header, even if that reference is also guarded.

That would avoid the error, so that would be a big improvement, thanks. From a user perspective, I think it's better if we make sure declarations of dump() are never available if the definitions are not available, but that is a bigger change, I'd happily accept a quick fix first, and I should be able to work on that bigger change afterwards.

@andrurogerz
Copy link
Contributor Author

@hvdijk thank you for the clear repro steps; I was able to reproduce the issue locally and verify that #140574 fixes it. It is a partial revert of only the ScaledNumber source changes; the rest of the changes in this PR do not appear to have the same issue.

compnerd pushed a commit that referenced this pull request May 19, 2025
…#140574)

## Purpose
Reverts the preprocessor guards added to the `dump()` methods in
`llvm/Support/ScaledNumber.h` in #139938 so that the header can be
included when building an external project in debug mode against a
release LLVM build.

## Overview
This is a clean revert of two files modified in #139938. The rest of
that change should not cause similar problems.

## Background
The following new build error was reported on #139938, which was merged
last week:
```
module.cpp:(.text._ZNK4llvm12ScaledNumberImE4dumpEv[_ZNK4llvm12ScaledNumberImE4dumpEv]+0x34): undefined reference to `llvm::ScaledNumberBase::dump(unsigned long, short, int)'
```
See further discussion on #139938.

## Validation
Implemented a simple external LLVM project to reproduce the issue.
Verified the the following link failure is observed on LLVM main
(Windows + Clang) without this change:
```
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -nostartfiles -nostdlib -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/llvm-dump-test.dir/main.cxx.obj -o llvm-dump-test.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:llvm-dump-test.lib -Xlinker /pdb:llvm-dump-test.pdb -Xlinker /version:0.0   -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
lld-link: error: undefined symbol: public: static void __cdecl llvm::ScaledNumberBase::dump(unsigned __int64, short, int)
>>> referenced by S:\llvm\llvm-project\llvm\include\llvm\Support\ScaledNumber.h:614
>>>               CMakeFiles/llvm-dump-test.dir/main.cxx.obj:(public: void __cdecl llvm::ScaledNumber<unsigned __int64>::dump(void) const)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
```
Verified the link issue is resolved after applying this change.

Also, manually included all header files that were modified in #139938
in the test program and verified there are no other link errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category llvm:adt llvm:support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants