Skip to content

Commit c9d05f3

Browse files
authored
[llvm] properly guard dump methods in Support lib classes (#139804)
## 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](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L637), `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
1 parent aa054c6 commit c9d05f3

21 files changed

+86
-22
lines changed

llvm/include/llvm/ADT/APFixedPoint.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ class APFixedPoint {
249249
}
250250

251251
void print(raw_ostream &) const;
252-
void dump() const;
252+
253+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
254+
LLVM_DUMP_METHOD void dump() const;
255+
#endif
253256

254257
// If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
255258
int compare(const APFixedPoint &Other) const;

llvm/include/llvm/ADT/APFloat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,10 @@ class APFloat : public APFloatBase {
14831483
}
14841484

14851485
void print(raw_ostream &) const;
1486-
void dump() const;
1486+
1487+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1488+
LLVM_DUMP_METHOD void dump() const;
1489+
#endif
14871490

14881491
bool getExactInverse(APFloat *inv) const {
14891492
APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));

llvm/include/llvm/ADT/APInt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,10 @@ class [[nodiscard]] APInt {
18961896
/// FoldingSets.
18971897
void Profile(FoldingSetNodeID &id) const;
18981898

1899+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
18991900
/// debug method
1900-
void dump() const;
1901+
LLVM_DUMP_METHOD void dump() const;
1902+
#endif
19011903

19021904
/// Returns whether this instance allocated memory.
19031905
bool needsCleanup() const { return !isSingleWord(); }

llvm/include/llvm/ADT/DynamicAPInt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ class DynamicAPInt {
216216
void static_assert_layout(); // NOLINT
217217

218218
raw_ostream &print(raw_ostream &OS) const;
219+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
219220
LLVM_DUMP_METHOD void dump() const;
221+
#endif
220222
};
221223

222224
inline raw_ostream &operator<<(raw_ostream &OS, const DynamicAPInt &X) {

llvm/include/llvm/ADT/SlowDynamicAPInt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ class SlowDynamicAPInt {
7979
unsigned getBitWidth() const { return Val.getBitWidth(); }
8080

8181
void print(raw_ostream &OS) const;
82+
83+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
8284
LLVM_DUMP_METHOD void dump() const;
85+
#endif
8386
};
8487

8588
inline raw_ostream &operator<<(raw_ostream &OS, const SlowDynamicAPInt &X) {

llvm/include/llvm/ADT/TrieRawHashMap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ class ThreadSafeTrieRawHashMapBase {
9090
static void *operator new(size_t Size) { return ::operator new(Size); }
9191
void operator delete(void *Ptr) { ::operator delete(Ptr); }
9292

93+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
9394
LLVM_DUMP_METHOD void dump() const;
95+
#endif
96+
9497
void print(raw_ostream &OS) const;
9598

9699
protected:
@@ -214,7 +217,10 @@ class ThreadSafeTrieRawHashMap : public ThreadSafeTrieRawHashMapBase {
214217
using ThreadSafeTrieRawHashMapBase::operator delete;
215218
using HashType = HashT;
216219

220+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
217221
using ThreadSafeTrieRawHashMapBase::dump;
222+
#endif
223+
218224
using ThreadSafeTrieRawHashMapBase::print;
219225

220226
private:

llvm/include/llvm/ADT/Twine.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,16 @@ namespace llvm {
507507
/// stream \p OS.
508508
void print(raw_ostream &OS) const;
509509

510-
/// Dump the concatenated string represented by this twine to stderr.
511-
void dump() const;
512-
513510
/// Write the representation of this twine to the stream \p OS.
514511
void printRepr(raw_ostream &OS) const;
515512

513+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
514+
/// Dump the concatenated string represented by this twine to stderr.
515+
LLVM_DUMP_METHOD void dump() const;
516+
516517
/// Dump the representation of this twine to stderr.
517-
void dumpRepr() const;
518+
LLVM_DUMP_METHOD void dumpRepr() const;
519+
#endif
518520

519521
/// @}
520522
};

llvm/include/llvm/Support/BalancedPartitioning.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class BPFunctionNode {
6868
/// The ID of this node
6969
IDT Id;
7070

71-
LLVM_ABI void dump(raw_ostream &OS) const;
71+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
72+
LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
73+
#endif
7274

7375
protected:
7476
/// The list of utility nodes associated with this node

llvm/include/llvm/Support/BranchProbability.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class BranchProbability {
7777

7878
LLVM_ABI raw_ostream &print(raw_ostream &OS) const;
7979

80-
LLVM_ABI void dump() const;
80+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
81+
LLVM_DUMP_METHOD void dump() const;
82+
#endif
8183

8284
/// Scale a large integer.
8385
///

llvm/include/llvm/Support/DebugCounter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ class DebugCounter {
119119
Counter.CurrChunkIdx = State.ChunkIdx;
120120
}
121121

122+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122123
// Dump or print the current counter set into llvm::dbgs().
123-
LLVM_ABI LLVM_DUMP_METHOD void dump() const;
124+
LLVM_DUMP_METHOD void dump() const;
125+
#endif
124126

125127
LLVM_ABI void print(raw_ostream &OS) const;
126128

llvm/include/llvm/Support/KnownBits.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ struct KnownBits {
513513
bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
514514

515515
LLVM_ABI void print(raw_ostream &OS) const;
516-
LLVM_ABI void dump() const;
516+
517+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
518+
LLVM_DUMP_METHOD void dump() const;
519+
#endif
517520

518521
private:
519522
// Internal helper for getting the initial KnownBits for an `srem` or `urem`

llvm/include/llvm/Support/SMTAPI.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class SMTSort {
7171

7272
virtual void print(raw_ostream &OS) const = 0;
7373

74-
LLVM_ABI LLVM_DUMP_METHOD void dump() const;
74+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
75+
LLVM_DUMP_METHOD void dump() const;
76+
#endif
7577

7678
protected:
7779
/// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -118,7 +120,9 @@ class SMTExpr {
118120

119121
virtual void print(raw_ostream &OS) const = 0;
120122

121-
LLVM_ABI LLVM_DUMP_METHOD void dump() const;
123+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
124+
LLVM_DUMP_METHOD void dump() const;
125+
#endif
122126

123127
protected:
124128
/// Query the SMT solver and returns true if two sorts are equal (same kind
@@ -136,7 +140,9 @@ class SMTSolverStatistics {
136140

137141
virtual void print(raw_ostream &OS) const = 0;
138142

139-
LLVM_ABI LLVM_DUMP_METHOD void dump() const;
143+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
144+
LLVM_DUMP_METHOD void dump() const;
145+
#endif
140146
};
141147

142148
/// Shared pointer for SMTExprs, used by SMTSolver API.
@@ -152,7 +158,9 @@ class SMTSolver {
152158
SMTSolver() = default;
153159
virtual ~SMTSolver() = default;
154160

155-
LLVM_ABI LLVM_DUMP_METHOD void dump() const;
161+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
162+
LLVM_DUMP_METHOD void dump() const;
163+
#endif
156164

157165
// Returns an appropriate floating-point sort for the given bitwidth.
158166
SMTSortRef getFloatSort(unsigned BitWidth) {

llvm/include/llvm/Support/ScaledNumber.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ class ScaledNumberBase {
424424
public:
425425
static constexpr int DefaultPrecision = 10;
426426

427-
LLVM_ABI static void dump(uint64_t D, int16_t E, int Width);
427+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
428+
LLVM_DUMP_METHOD static void dump(uint64_t D, int16_t E, int Width);
429+
#endif
430+
428431
LLVM_ABI static raw_ostream &print(raw_ostream &OS, uint64_t D, int16_t E,
429432
int Width, unsigned Precision);
430433
LLVM_ABI static std::string toString(uint64_t D, int16_t E, int Width,
@@ -607,7 +610,12 @@ template <class DigitsT> class ScaledNumber : ScaledNumberBase {
607610
unsigned Precision = DefaultPrecision) const {
608611
return ScaledNumberBase::print(OS, Digits, Scale, Width, Precision);
609612
}
610-
void dump() const { return ScaledNumberBase::dump(Digits, Scale, Width); }
613+
614+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
615+
LLVM_DUMP_METHOD void dump() const {
616+
return ScaledNumberBase::dump(Digits, Scale, Width);
617+
}
618+
#endif
611619

612620
ScaledNumber &operator+=(const ScaledNumber &X) {
613621
std::tie(Digits, Scale) =

llvm/lib/Support/APFixedPoint.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ void APFixedPoint::print(raw_ostream &OS) const {
439439
Sema.print(OS);
440440
OS << "})";
441441
}
442+
443+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
442444
LLVM_DUMP_METHOD void APFixedPoint::dump() const { print(llvm::errs()); }
445+
#endif
443446

444447
APFixedPoint APFixedPoint::negate(bool *Overflow) const {
445448
if (!isSaturated()) {

llvm/lib/Support/BalancedPartitioning.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
using namespace llvm;
2222
#define DEBUG_TYPE "balanced-partitioning"
2323

24-
void BPFunctionNode::dump(raw_ostream &OS) const {
24+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
25+
LLVM_DUMP_METHOD void BPFunctionNode::dump(raw_ostream &OS) const {
2526
OS << formatv("{{ID={0} Utilities={{{1:$[,]}} Bucket={2}}", Id,
2627
make_range(UtilityNodes.begin(), UtilityNodes.end()), Bucket);
2728
}
29+
#endif
2830

2931
template <typename Func>
3032
void BalancedPartitioning::BPThreadPool::async(Func &&F) {

llvm/lib/Support/DebugCounter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
248248
return true;
249249
}
250250

251+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
251252
LLVM_DUMP_METHOD void DebugCounter::dump() const {
252253
print(dbgs());
253254
}
255+
#endif

llvm/lib/Support/DynamicAPInt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ raw_ostream &DynamicAPInt::print(raw_ostream &OS) const {
3232
return OS << ValLarge;
3333
}
3434

35-
void DynamicAPInt::dump() const { print(dbgs()); }
35+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
36+
LLVM_DUMP_METHOD void DynamicAPInt::dump() const { print(dbgs()); }
37+
#endif

llvm/lib/Support/KnownBits.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,10 @@ void KnownBits::print(raw_ostream &OS) const {
11521152
OS << "?";
11531153
}
11541154
}
1155-
void KnownBits::dump() const {
1155+
1156+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1157+
LLVM_DUMP_METHOD void KnownBits::dump() const {
11561158
print(dbgs());
11571159
dbgs() << "\n";
11581160
}
1161+
#endif

llvm/lib/Support/ScaledNumber.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
317317
return OS << toString(D, E, Width, Precision);
318318
}
319319

320-
void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
320+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
321+
LLVM_DUMP_METHOD void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
321322
print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
322323
<< "]";
323324
}
325+
#endif

llvm/lib/Support/SlowDynamicAPInt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,6 @@ SlowDynamicAPInt &SlowDynamicAPInt::operator--() {
283283
/// ---------------------------------------------------------------------------
284284
void SlowDynamicAPInt::print(raw_ostream &OS) const { OS << Val; }
285285

286-
void SlowDynamicAPInt::dump() const { print(dbgs()); }
286+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287+
LLVM_DUMP_METHOD void SlowDynamicAPInt::dump() const { print(dbgs()); }
288+
#endif

llvm/lib/Support/Z3Solver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,9 @@ llvm::SMTSolverRef llvm::CreateZ3Solver() {
989989
#endif
990990
}
991991

992+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
992993
LLVM_DUMP_METHOD void SMTSort::dump() const { print(llvm::errs()); }
993994
LLVM_DUMP_METHOD void SMTExpr::dump() const { print(llvm::errs()); }
994995
LLVM_DUMP_METHOD void SMTSolver::dump() const { print(llvm::errs()); }
995996
LLVM_DUMP_METHOD void SMTSolverStatistics::dump() const { print(llvm::errs()); }
997+
#endif

0 commit comments

Comments
 (0)