Skip to content

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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 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 00:33
@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-llvm-adt

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 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/139804.diff

21 Files Affected:

  • (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/BalancedPartitioning.h (+3-1)
  • (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/BalancedPartitioning.cpp (+3-1)
  • (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/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/BalancedPartitioning.h b/llvm/include/llvm/Support/BalancedPartitioning.h
index 05307d74c209c..e744a9344b2a4 100644
--- a/llvm/include/llvm/Support/BalancedPartitioning.h
+++ b/llvm/include/llvm/Support/BalancedPartitioning.h
@@ -68,7 +68,9 @@ class BPFunctionNode {
   /// The ID of this node
   IDT Id;
 
-  LLVM_ABI void dump(raw_ostream &OS) const;
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
+#endif
 
 protected:
   /// The list of utility nodes associated with this node
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/BalancedPartitioning.cpp b/llvm/lib/Support/BalancedPartitioning.cpp
index ed3b149c03daf..b185ef86cd29e 100644
--- a/llvm/lib/Support/BalancedPartitioning.cpp
+++ b/llvm/lib/Support/BalancedPartitioning.cpp
@@ -21,10 +21,12 @@
 using namespace llvm;
 #define DEBUG_TYPE "balanced-partitioning"
 
-void BPFunctionNode::dump(raw_ostream &OS) const {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void BPFunctionNode::dump(raw_ostream &OS) const {
   OS << formatv("{{ID={0} Utilities={{{1:$[,]}} Bucket={2}}", Id,
                 make_range(UtilityNodes.begin(), UtilityNodes.end()), Bucket);
 }
+#endif
 
 template <typename Func>
 void BalancedPartitioning::BPThreadPool::async(Func &&F) {
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

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.

3 participants