Skip to content

Commit 4d32ea8

Browse files
authored
[NFC][LLVM][IR] Fix namespace usage in several files (#161756)
- Move standalone functions/variables out of anonymous namespace and make them static. - Use `using namespace llvm` instead of wrapping all the code in a file in `namespace llvm { }`. - Restrict anonymous namespace to just class/struct/enum declarations.
1 parent c2765b7 commit 4d32ea8

File tree

8 files changed

+62
-68
lines changed

8 files changed

+62
-68
lines changed

llvm/lib/IR/Assumptions.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
using namespace llvm;
2222

23-
namespace {
24-
bool hasAssumption(const Attribute &A,
25-
const KnownAssumptionString &AssumptionStr) {
23+
static bool hasAssumption(const Attribute &A,
24+
const KnownAssumptionString &AssumptionStr) {
2625
if (!A.isValid())
2726
return false;
2827
assert(A.isStringAttribute() && "Expected a string attribute!");
@@ -33,7 +32,7 @@ bool hasAssumption(const Attribute &A,
3332
return llvm::is_contained(Strings, AssumptionStr);
3433
}
3534

36-
DenseSet<StringRef> getAssumptions(const Attribute &A) {
35+
static DenseSet<StringRef> getAssumptions(const Attribute &A) {
3736
if (!A.isValid())
3837
return DenseSet<StringRef>();
3938
assert(A.isStringAttribute() && "Expected a string attribute!");
@@ -47,8 +46,8 @@ DenseSet<StringRef> getAssumptions(const Attribute &A) {
4746
}
4847

4948
template <typename AttrSite>
50-
bool addAssumptionsImpl(AttrSite &Site,
51-
const DenseSet<StringRef> &Assumptions) {
49+
static bool addAssumptionsImpl(AttrSite &Site,
50+
const DenseSet<StringRef> &Assumptions) {
5251
if (Assumptions.empty())
5352
return false;
5453

@@ -64,7 +63,6 @@ bool addAssumptionsImpl(AttrSite &Site,
6463

6564
return true;
6665
}
67-
} // namespace
6866

6967
bool llvm::hasAssumption(const Function &F,
7068
const KnownAssumptionString &AssumptionStr) {

llvm/lib/IR/DiagnosticHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct PassRemarksOpt {
3636
}
3737
}
3838
};
39+
} // namespace
3940

4041
static PassRemarksOpt PassRemarksPassedOptLoc;
4142
static PassRemarksOpt PassRemarksMissedOptLoc;
@@ -66,7 +67,6 @@ static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
6667
"Enable optimization analysis remarks from passes whose name match "
6768
"the given regular expression"),
6869
cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired);
69-
}
7070

7171
bool DiagnosticHandler::isAnalysisRemarkEnabled(StringRef PassName) const {
7272
return (PassRemarksAnalysisOptLoc.Pattern &&

llvm/lib/IR/ModuleSummaryIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ struct Edge {
409409
GlobalValue::GUID Src;
410410
GlobalValue::GUID Dst;
411411
};
412-
}
412+
} // namespace
413413

414414
void Attributes::add(const Twine &Name, const Twine &Value,
415415
const Twine &Comment) {

llvm/lib/IR/PassInstrumentation.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/IR/PassManager.h"
1717

18-
namespace llvm {
18+
using namespace llvm;
1919

2020
template struct LLVM_EXPORT_TEMPLATE Any::TypeId<const Module *>;
2121
template struct LLVM_EXPORT_TEMPLATE Any::TypeId<const Function *>;
@@ -42,13 +42,12 @@ PassInstrumentationCallbacks::getPassNameForClassName(StringRef ClassName) {
4242

4343
AnalysisKey PassInstrumentationAnalysis::Key;
4444

45-
bool isSpecialPass(StringRef PassID, const std::vector<StringRef> &Specials) {
45+
bool llvm::isSpecialPass(StringRef PassID,
46+
const std::vector<StringRef> &Specials) {
4647
size_t Pos = PassID.find('<');
4748
StringRef Prefix = PassID;
4849
if (Pos != StringRef::npos)
4950
Prefix = PassID.substr(0, Pos);
5051
return any_of(Specials,
5152
[Prefix](StringRef S) { return Prefix.ends_with(S); });
5253
}
53-
54-
} // namespace llvm

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
using namespace llvm;
2626

27-
namespace {
28-
2927
// MD_prof nodes have the following layout
3028
//
3129
// In general:
@@ -41,14 +39,15 @@ namespace {
4139
// correctly, and can change the behavior in the future if the layout changes
4240

4341
// the minimum number of operands for MD_prof nodes with branch weights
44-
constexpr unsigned MinBWOps = 3;
42+
static constexpr unsigned MinBWOps = 3;
4543

4644
// the minimum number of operands for MD_prof nodes with value profiles
47-
constexpr unsigned MinVPOps = 5;
45+
static constexpr unsigned MinVPOps = 5;
4846

4947
// We may want to add support for other MD_prof types, so provide an abstraction
5048
// for checking the metadata type.
51-
bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
49+
static bool isTargetMD(const MDNode *ProfData, const char *Name,
50+
unsigned MinOps) {
5251
// TODO: This routine may be simplified if MD_prof used an enum instead of a
5352
// string to differentiate the types of MD_prof nodes.
5453
if (!ProfData || !Name || MinOps < 2)
@@ -101,14 +100,11 @@ static SmallVector<uint32_t> fitWeights(ArrayRef<uint64_t> Weights) {
101100
return Ret;
102101
}
103102

104-
} // namespace
105-
106-
namespace llvm {
107-
cl::opt<bool> ElideAllZeroBranchWeights("elide-all-zero-branch-weights",
103+
static cl::opt<bool> ElideAllZeroBranchWeights("elide-all-zero-branch-weights",
108104
#if defined(LLVM_ENABLE_PROFCHECK)
109-
cl::init(false)
105+
cl::init(false)
110106
#else
111-
cl::init(true)
107+
cl::init(true)
112108
#endif
113109
);
114110
const char *MDProfLabels::BranchWeights = "branch_weights";
@@ -118,21 +114,21 @@ const char *MDProfLabels::FunctionEntryCount = "function_entry_count";
118114
const char *MDProfLabels::SyntheticFunctionEntryCount =
119115
"synthetic_function_entry_count";
120116
const char *MDProfLabels::UnknownBranchWeightsMarker = "unknown";
121-
const char *LLVMLoopEstimatedTripCount = "llvm.loop.estimated_trip_count";
117+
const char *llvm::LLVMLoopEstimatedTripCount = "llvm.loop.estimated_trip_count";
122118

123-
bool hasProfMD(const Instruction &I) {
119+
bool llvm::hasProfMD(const Instruction &I) {
124120
return I.hasMetadata(LLVMContext::MD_prof);
125121
}
126122

127-
bool isBranchWeightMD(const MDNode *ProfileData) {
123+
bool llvm::isBranchWeightMD(const MDNode *ProfileData) {
128124
return isTargetMD(ProfileData, MDProfLabels::BranchWeights, MinBWOps);
129125
}
130126

131-
bool isValueProfileMD(const MDNode *ProfileData) {
127+
bool llvm::isValueProfileMD(const MDNode *ProfileData) {
132128
return isTargetMD(ProfileData, MDProfLabels::ValueProfile, MinVPOps);
133129
}
134130

135-
bool hasBranchWeightMD(const Instruction &I) {
131+
bool llvm::hasBranchWeightMD(const Instruction &I) {
136132
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
137133
return isBranchWeightMD(ProfileData);
138134
}
@@ -147,16 +143,16 @@ static bool hasCountTypeMD(const Instruction &I) {
147143
return isa<CallBase>(I) && !isBranchWeightMD(ProfileData);
148144
}
149145

150-
bool hasValidBranchWeightMD(const Instruction &I) {
146+
bool llvm::hasValidBranchWeightMD(const Instruction &I) {
151147
return getValidBranchWeightMDNode(I);
152148
}
153149

154-
bool hasBranchWeightOrigin(const Instruction &I) {
150+
bool llvm::hasBranchWeightOrigin(const Instruction &I) {
155151
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
156152
return hasBranchWeightOrigin(ProfileData);
157153
}
158154

159-
bool hasBranchWeightOrigin(const MDNode *ProfileData) {
155+
bool llvm::hasBranchWeightOrigin(const MDNode *ProfileData) {
160156
if (!isBranchWeightMD(ProfileData))
161157
return false;
162158
auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(1));
@@ -168,54 +164,54 @@ bool hasBranchWeightOrigin(const MDNode *ProfileData) {
168164
return ProfDataName != nullptr;
169165
}
170166

171-
unsigned getBranchWeightOffset(const MDNode *ProfileData) {
167+
unsigned llvm::getBranchWeightOffset(const MDNode *ProfileData) {
172168
return hasBranchWeightOrigin(ProfileData) ? 2 : 1;
173169
}
174170

175-
unsigned getNumBranchWeights(const MDNode &ProfileData) {
171+
unsigned llvm::getNumBranchWeights(const MDNode &ProfileData) {
176172
return ProfileData.getNumOperands() - getBranchWeightOffset(&ProfileData);
177173
}
178174

179-
MDNode *getBranchWeightMDNode(const Instruction &I) {
175+
MDNode *llvm::getBranchWeightMDNode(const Instruction &I) {
180176
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
181177
if (!isBranchWeightMD(ProfileData))
182178
return nullptr;
183179
return ProfileData;
184180
}
185181

186-
MDNode *getValidBranchWeightMDNode(const Instruction &I) {
182+
MDNode *llvm::getValidBranchWeightMDNode(const Instruction &I) {
187183
auto *ProfileData = getBranchWeightMDNode(I);
188184
if (ProfileData && getNumBranchWeights(*ProfileData) == I.getNumSuccessors())
189185
return ProfileData;
190186
return nullptr;
191187
}
192188

193-
void extractFromBranchWeightMD32(const MDNode *ProfileData,
194-
SmallVectorImpl<uint32_t> &Weights) {
189+
void llvm::extractFromBranchWeightMD32(const MDNode *ProfileData,
190+
SmallVectorImpl<uint32_t> &Weights) {
195191
extractFromBranchWeightMD(ProfileData, Weights);
196192
}
197193

198-
void extractFromBranchWeightMD64(const MDNode *ProfileData,
199-
SmallVectorImpl<uint64_t> &Weights) {
194+
void llvm::extractFromBranchWeightMD64(const MDNode *ProfileData,
195+
SmallVectorImpl<uint64_t> &Weights) {
200196
extractFromBranchWeightMD(ProfileData, Weights);
201197
}
202198

203-
bool extractBranchWeights(const MDNode *ProfileData,
204-
SmallVectorImpl<uint32_t> &Weights) {
199+
bool llvm::extractBranchWeights(const MDNode *ProfileData,
200+
SmallVectorImpl<uint32_t> &Weights) {
205201
if (!isBranchWeightMD(ProfileData))
206202
return false;
207203
extractFromBranchWeightMD(ProfileData, Weights);
208204
return true;
209205
}
210206

211-
bool extractBranchWeights(const Instruction &I,
212-
SmallVectorImpl<uint32_t> &Weights) {
207+
bool llvm::extractBranchWeights(const Instruction &I,
208+
SmallVectorImpl<uint32_t> &Weights) {
213209
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
214210
return extractBranchWeights(ProfileData, Weights);
215211
}
216212

217-
bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
218-
uint64_t &FalseVal) {
213+
bool llvm::extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
214+
uint64_t &FalseVal) {
219215
assert((I.getOpcode() == Instruction::Br ||
220216
I.getOpcode() == Instruction::Select) &&
221217
"Looking for branch weights on something besides branch, select, or "
@@ -234,7 +230,8 @@ bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
234230
return true;
235231
}
236232

237-
bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
233+
bool llvm::extractProfTotalWeight(const MDNode *ProfileData,
234+
uint64_t &TotalVal) {
238235
TotalVal = 0;
239236
if (!ProfileData)
240237
return false;
@@ -262,11 +259,12 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
262259
return false;
263260
}
264261

265-
bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
262+
bool llvm::extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
266263
return extractProfTotalWeight(I.getMetadata(LLVMContext::MD_prof), TotalVal);
267264
}
268265

269-
void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName) {
266+
void llvm::setExplicitlyUnknownBranchWeights(Instruction &I,
267+
StringRef PassName) {
270268
MDBuilder MDB(I.getContext());
271269
I.setMetadata(
272270
LLVMContext::MD_prof,
@@ -275,14 +273,16 @@ void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName) {
275273
MDB.createString(PassName)}));
276274
}
277275

278-
void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, Function &F,
279-
StringRef PassName) {
276+
void llvm::setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
277+
Function &F,
278+
StringRef PassName) {
280279
if (std::optional<Function::ProfileCount> EC = F.getEntryCount();
281280
EC && EC->getCount() > 0)
282281
setExplicitlyUnknownBranchWeights(I, PassName);
283282
}
284283

285-
void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {
284+
void llvm::setExplicitlyUnknownFunctionEntryCount(Function &F,
285+
StringRef PassName) {
286286
MDBuilder MDB(F.getContext());
287287
F.setMetadata(
288288
LLVMContext::MD_prof,
@@ -291,21 +291,21 @@ void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {
291291
MDB.createString(PassName)}));
292292
}
293293

294-
bool isExplicitlyUnknownProfileMetadata(const MDNode &MD) {
294+
bool llvm::isExplicitlyUnknownProfileMetadata(const MDNode &MD) {
295295
if (MD.getNumOperands() != 2)
296296
return false;
297297
return MD.getOperand(0).equalsStr(MDProfLabels::UnknownBranchWeightsMarker);
298298
}
299299

300-
bool hasExplicitlyUnknownBranchWeights(const Instruction &I) {
300+
bool llvm::hasExplicitlyUnknownBranchWeights(const Instruction &I) {
301301
auto *MD = I.getMetadata(LLVMContext::MD_prof);
302302
if (!MD)
303303
return false;
304304
return isExplicitlyUnknownProfileMetadata(*MD);
305305
}
306306

307-
void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
308-
bool IsExpected, bool ElideAllZero) {
307+
void llvm::setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
308+
bool IsExpected, bool ElideAllZero) {
309309
if ((ElideAllZeroBranchWeights && ElideAllZero) &&
310310
llvm::all_of(Weights, [](uint32_t V) { return V == 0; })) {
311311
I.setMetadata(LLVMContext::MD_prof, nullptr);
@@ -317,13 +317,14 @@ void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
317317
I.setMetadata(LLVMContext::MD_prof, BranchWeights);
318318
}
319319

320-
void setFittedBranchWeights(Instruction &I, ArrayRef<uint64_t> Weights,
321-
bool IsExpected, bool ElideAllZero) {
320+
void llvm::setFittedBranchWeights(Instruction &I, ArrayRef<uint64_t> Weights,
321+
bool IsExpected, bool ElideAllZero) {
322322
setBranchWeights(I, fitWeights(Weights), IsExpected, ElideAllZero);
323323
}
324324

325-
SmallVector<uint32_t> downscaleWeights(ArrayRef<uint64_t> Weights,
326-
std::optional<uint64_t> KnownMaxCount) {
325+
SmallVector<uint32_t>
326+
llvm::downscaleWeights(ArrayRef<uint64_t> Weights,
327+
std::optional<uint64_t> KnownMaxCount) {
327328
uint64_t MaxCount = KnownMaxCount.has_value() ? KnownMaxCount.value()
328329
: *llvm::max_element(Weights);
329330
assert(MaxCount > 0 && "Bad max count");
@@ -334,7 +335,7 @@ SmallVector<uint32_t> downscaleWeights(ArrayRef<uint64_t> Weights,
334335
return DownscaledWeights;
335336
}
336337

337-
void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
338+
void llvm::scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
338339
assert(T != 0 && "Caller should guarantee");
339340
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
340341
if (ProfileData == nullptr)
@@ -387,5 +388,3 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
387388
}
388389
I.setMetadata(LLVMContext::MD_prof, MDNode::get(C, Vals));
389390
}
390-
391-
} // namespace llvm

llvm/lib/IR/SafepointIRVerifier.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ class CFGDeadness {
196196
static void Verify(const Function &F, const DominatorTree &DT,
197197
const CFGDeadness &CD);
198198

199-
namespace llvm {
200199
PreservedAnalyses SafepointIRVerifierPass::run(Function &F,
201200
FunctionAnalysisManager &AM) {
202201
const auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
@@ -205,7 +204,6 @@ PreservedAnalyses SafepointIRVerifierPass::run(Function &F,
205204
Verify(F, DT, CD);
206205
return PreservedAnalyses::all();
207206
}
208-
} // namespace llvm
209207

210208
namespace {
211209

llvm/lib/IR/VFABIDemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ using namespace llvm;
2020

2121
#define DEBUG_TYPE "vfabi-demangler"
2222

23-
namespace {
2423
/// Utilities for the Vector Function ABI name parser.
2524

25+
namespace {
2626
/// Return types for the parser functions.
2727
enum class ParseRet {
2828
OK, // Found.
2929
None, // Not found.
3030
Error // Syntax error.
3131
};
32+
} // namespace
3233

3334
/// Extracts the `<isa>` information from the mangled string, and
3435
/// sets the `ISA` accordingly. If successful, the <isa> token is removed
@@ -372,7 +373,6 @@ getScalableECFromSignature(const FunctionType *Signature, const VFISAKind ISA,
372373

373374
return std::nullopt;
374375
}
375-
} // namespace
376376

377377
// Format of the ABI name:
378378
// _ZGV<isa><mask><vlen><parameters>_<scalarname>[(<redirection>)]

0 commit comments

Comments
 (0)