Skip to content

Commit f8a1c8b

Browse files
[llvm] Use llvm::any_cast instead of any_cast (NFC) (#65565)
This patch replaces any_cast with llvm::any_cast. This in turn allows us to gracefully switch to std::any in future by forwarding llvm::Any and llvm::any_cast to: using Any = std::any; template <class T> T *any_cast(Any *Value) { return std::any_cast<T>(Value); } respectively. Without this patch, it's ambiguous whether any_cast refers to std::any_cast or llvm::any_cast. As an added bonus, this patch makes it easier to mechanically replace llvm::any_cast with std::any_cast without affecting other occurrences of any_cast (e.g. in libcxx).
1 parent a279bf0 commit f8a1c8b

File tree

7 files changed

+71
-67
lines changed

7 files changed

+71
-67
lines changed

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Error MachineFunctionPassManager::run(Module &M,
4141
// current pipeline is the top-level pipeline. Callbacks are not used after
4242
// current pipeline.
4343
PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) {
44-
assert(any_cast<const MachineFunction *>(&IR));
45-
const MachineFunction *MF = any_cast<const MachineFunction *>(IR);
44+
assert(llvm::any_cast<const MachineFunction *>(&IR));
45+
const MachineFunction *MF = llvm::any_cast<const MachineFunction *>(IR);
4646
assert(MF && "Machine function should be valid for printing");
4747
std::string Banner = std::string("After ") + std::string(PassID);
4848
verifyMachineFunction(&MFAM, Banner, *MF);

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ static cl::opt<std::string>
137137
/// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
138138
/// certain global filters. Will never return nullptr if \p Force is true.
139139
const Module *unwrapModule(Any IR, bool Force = false) {
140-
if (const auto **M = any_cast<const Module *>(&IR))
140+
if (const auto **M = llvm::any_cast<const Module *>(&IR))
141141
return *M;
142142

143-
if (const auto **F = any_cast<const Function *>(&IR)) {
143+
if (const auto **F = llvm::any_cast<const Function *>(&IR)) {
144144
if (!Force && !isFunctionInPrintList((*F)->getName()))
145145
return nullptr;
146146

147147
return (*F)->getParent();
148148
}
149149

150-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
150+
if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR)) {
151151
for (const LazyCallGraph::Node &N : **C) {
152152
const Function &F = N.getFunction();
153153
if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
@@ -158,7 +158,7 @@ const Module *unwrapModule(Any IR, bool Force = false) {
158158
return nullptr;
159159
}
160160

161-
if (const auto **L = any_cast<const Loop *>(&IR)) {
161+
if (const auto **L = llvm::any_cast<const Loop *>(&IR)) {
162162
const Function *F = (*L)->getHeader()->getParent();
163163
if (!Force && !isFunctionInPrintList(F->getName()))
164164
return nullptr;
@@ -201,16 +201,16 @@ void printIR(raw_ostream &OS, const Loop *L) {
201201
}
202202

203203
std::string getIRName(Any IR) {
204-
if (any_cast<const Module *>(&IR))
204+
if (llvm::any_cast<const Module *>(&IR))
205205
return "[module]";
206206

207-
if (const auto **F = any_cast<const Function *>(&IR))
207+
if (const auto **F = llvm::any_cast<const Function *>(&IR))
208208
return (*F)->getName().str();
209209

210-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
210+
if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR))
211211
return (*C)->getName();
212212

213-
if (const auto **L = any_cast<const Loop *>(&IR))
213+
if (const auto **L = llvm::any_cast<const Loop *>(&IR))
214214
return (*L)->getName().str();
215215

216216
llvm_unreachable("Unknown wrapped IR type");
@@ -233,16 +233,16 @@ bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
233233
}
234234

235235
bool shouldPrintIR(Any IR) {
236-
if (const auto **M = any_cast<const Module *>(&IR))
236+
if (const auto **M = llvm::any_cast<const Module *>(&IR))
237237
return moduleContainsFilterPrintFunc(**M);
238238

239-
if (const auto **F = any_cast<const Function *>(&IR))
239+
if (const auto **F = llvm::any_cast<const Function *>(&IR))
240240
return isFunctionInPrintList((*F)->getName());
241241

242-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
242+
if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR))
243243
return sccContainsFilterPrintFunc(**C);
244244

245-
if (const auto **L = any_cast<const Loop *>(&IR))
245+
if (const auto **L = llvm::any_cast<const Loop *>(&IR))
246246
return isFunctionInPrintList((*L)->getHeader()->getParent()->getName());
247247
llvm_unreachable("Unknown wrapped IR type");
248248
}
@@ -260,22 +260,22 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
260260
return;
261261
}
262262

263-
if (const auto **M = any_cast<const Module *>(&IR)) {
263+
if (const auto **M = llvm::any_cast<const Module *>(&IR)) {
264264
printIR(OS, *M);
265265
return;
266266
}
267267

268-
if (const auto **F = any_cast<const Function *>(&IR)) {
268+
if (const auto **F = llvm::any_cast<const Function *>(&IR)) {
269269
printIR(OS, *F);
270270
return;
271271
}
272272

273-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
273+
if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR)) {
274274
printIR(OS, *C);
275275
return;
276276
}
277277

278-
if (const auto **L = any_cast<const Loop *>(&IR)) {
278+
if (const auto **L = llvm::any_cast<const Loop *>(&IR)) {
279279
printIR(OS, *L);
280280
return;
281281
}
@@ -306,9 +306,9 @@ std::string makeHTMLReady(StringRef SR) {
306306

307307
// Return the module when that is the appropriate level of comparison for \p IR.
308308
const Module *getModuleForComparison(Any IR) {
309-
if (const auto **M = any_cast<const Module *>(&IR))
309+
if (const auto **M = llvm::any_cast<const Module *>(&IR))
310310
return *M;
311-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
311+
if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR))
312312
return (*C)
313313
->begin()
314314
->getFunction()
@@ -325,7 +325,7 @@ bool isInterestingFunction(const Function &F) {
325325
bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
326326
if (isIgnored(PassID) || !isPassInPrintList(PassName))
327327
return false;
328-
if (const auto **F = any_cast<const Function *>(&IR))
328+
if (const auto **F = llvm::any_cast<const Function *>(&IR))
329329
return isInterestingFunction(**F);
330330
return true;
331331
}
@@ -648,10 +648,10 @@ template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
648648
return;
649649
}
650650

651-
const Function **FPtr = any_cast<const Function *>(&IR);
651+
const Function **FPtr = llvm::any_cast<const Function *>(&IR);
652652
const Function *F = FPtr ? *FPtr : nullptr;
653653
if (!F) {
654-
const Loop **L = any_cast<const Loop *>(&IR);
654+
const Loop **L = llvm::any_cast<const Loop *>(&IR);
655655
assert(L && "Unknown IR unit.");
656656
F = (*L)->getHeader()->getParent();
657657
}
@@ -837,10 +837,10 @@ void OptNoneInstrumentation::registerCallbacks(
837837
}
838838

839839
bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
840-
const Function **FPtr = any_cast<const Function *>(&IR);
840+
const Function **FPtr = llvm::any_cast<const Function *>(&IR);
841841
const Function *F = FPtr ? *FPtr : nullptr;
842842
if (!F) {
843-
if (const auto **L = any_cast<const Loop *>(&IR))
843+
if (const auto **L = llvm::any_cast<const Loop *>(&IR))
844844
F = (*L)->getHeader()->getParent();
845845
}
846846
bool ShouldRun = !(F && F->hasOptNone());
@@ -916,13 +916,14 @@ void PrintPassInstrumentation::registerCallbacks(
916916

917917
auto &OS = print();
918918
OS << "Running pass: " << PassID << " on " << getIRName(IR);
919-
if (const auto **F = any_cast<const Function *>(&IR)) {
919+
if (const auto **F = llvm::any_cast<const Function *>(&IR)) {
920920
unsigned Count = (*F)->getInstructionCount();
921921
OS << " (" << Count << " instruction";
922922
if (Count != 1)
923923
OS << 's';
924924
OS << ')';
925-
} else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
925+
} else if (const auto **C =
926+
llvm::any_cast<const LazyCallGraph::SCC *>(&IR)) {
926927
int Count = (*C)->size();
927928
OS << " (" << Count << " node";
928929
if (Count != 1)
@@ -1138,9 +1139,9 @@ bool PreservedCFGCheckerInstrumentation::CFG::invalidate(
11381139
static SmallVector<Function *, 1> GetFunctions(Any IR) {
11391140
SmallVector<Function *, 1> Functions;
11401141

1141-
if (const auto **MaybeF = any_cast<const Function *>(&IR)) {
1142+
if (const auto **MaybeF = llvm::any_cast<const Function *>(&IR)) {
11421143
Functions.push_back(*const_cast<Function **>(MaybeF));
1143-
} else if (const auto **MaybeM = any_cast<const Module *>(&IR)) {
1144+
} else if (const auto **MaybeM = llvm::any_cast<const Module *>(&IR)) {
11441145
for (Function &F : **const_cast<Module **>(MaybeM))
11451146
Functions.push_back(&F);
11461147
}
@@ -1176,7 +1177,7 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
11761177
FAM.getResult<PreservedFunctionHashAnalysis>(*F);
11771178
}
11781179

1179-
if (auto *MaybeM = any_cast<const Module *>(&IR)) {
1180+
if (auto *MaybeM = llvm::any_cast<const Module *>(&IR)) {
11801181
Module &M = **const_cast<Module **>(MaybeM);
11811182
MAM.getResult<PreservedModuleHashAnalysis>(M);
11821183
}
@@ -1235,7 +1236,7 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
12351236
CheckCFG(P, F->getName(), *GraphBefore,
12361237
CFG(F, /* TrackBBLifetime */ false));
12371238
}
1238-
if (auto *MaybeM = any_cast<const Module *>(&IR)) {
1239+
if (auto *MaybeM = llvm::any_cast<const Module *>(&IR)) {
12391240
Module &M = **const_cast<Module **>(MaybeM);
12401241
if (auto *HashBefore =
12411242
MAM.getCachedResult<PreservedModuleHashAnalysis>(M)) {
@@ -1254,10 +1255,10 @@ void VerifyInstrumentation::registerCallbacks(
12541255
[this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
12551256
if (isIgnored(P) || P == "VerifierPass")
12561257
return;
1257-
const Function **FPtr = any_cast<const Function *>(&IR);
1258+
const Function **FPtr = llvm::any_cast<const Function *>(&IR);
12581259
const Function *F = FPtr ? *FPtr : nullptr;
12591260
if (!F) {
1260-
if (const auto **L = any_cast<const Loop *>(&IR))
1261+
if (const auto **L = llvm::any_cast<const Loop *>(&IR))
12611262
F = (*L)->getHeader()->getParent();
12621263
}
12631264

@@ -1268,10 +1269,11 @@ void VerifyInstrumentation::registerCallbacks(
12681269
if (verifyFunction(*F, &errs()))
12691270
report_fatal_error("Broken function found, compilation aborted!");
12701271
} else {
1271-
const Module **MPtr = any_cast<const Module *>(&IR);
1272+
const Module **MPtr = llvm::any_cast<const Module *>(&IR);
12721273
const Module *M = MPtr ? *MPtr : nullptr;
12731274
if (!M) {
1274-
if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
1275+
if (const auto **C =
1276+
llvm::any_cast<const LazyCallGraph::SCC *>(&IR))
12751277
M = (*C)->begin()->getFunction().getParent();
12761278
}
12771279

llvm/lib/Transforms/IPO/SampleProfileProbe.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ void PseudoProbeVerifier::runAfterPass(StringRef PassID, Any IR) {
9595
std::string Banner =
9696
"\n*** Pseudo Probe Verification After " + PassID.str() + " ***\n";
9797
dbgs() << Banner;
98-
if (const auto **M = any_cast<const Module *>(&IR))
98+
if (const auto **M = llvm::any_cast<const Module *>(&IR))
9999
runAfterPass(*M);
100-
else if (const auto **F = any_cast<const Function *>(&IR))
100+
else if (const auto **F = llvm::any_cast<const Function *>(&IR))
101101
runAfterPass(*F);
102-
else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
102+
else if (const auto **C = llvm::any_cast<const LazyCallGraph::SCC *>(&IR))
103103
runAfterPass(*C);
104-
else if (const auto **L = any_cast<const Loop *>(&IR))
104+
else if (const auto **L = llvm::any_cast<const Loop *>(&IR))
105105
runAfterPass(*L);
106106
else
107107
llvm_unreachable("Unknown IR unit");

llvm/lib/Transforms/Scalar/LoopPassManager.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,12 @@ PreservedAnalyses FunctionToLoopPassAdaptor::run(Function &F,
269269
PI.pushBeforeNonSkippedPassCallback([&LAR, &LI](StringRef PassID, Any IR) {
270270
if (isSpecialPass(PassID, {"PassManager"}))
271271
return;
272-
assert(any_cast<const Loop *>(&IR) || any_cast<const LoopNest *>(&IR));
273-
const Loop **LPtr = any_cast<const Loop *>(&IR);
272+
assert(llvm::any_cast<const Loop *>(&IR) ||
273+
llvm::any_cast<const LoopNest *>(&IR));
274+
const Loop **LPtr = llvm::any_cast<const Loop *>(&IR);
274275
const Loop *L = LPtr ? *LPtr : nullptr;
275276
if (!L)
276-
L = &any_cast<const LoopNest *>(IR)->getOutermostLoop();
277+
L = &llvm::any_cast<const LoopNest *>(IR)->getOutermostLoop();
277278
assert(L && "Loop should be valid for printing");
278279

279280
// Verify the loop structure and LCSSA form before visiting the loop.

llvm/lib/Transforms/Utils/Debugify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,13 @@ void DebugifyEachInstrumentation::registerCallbacks(
10351035
return;
10361036
PreservedAnalyses PA;
10371037
PA.preserveSet<CFGAnalyses>();
1038-
if (const auto **CF = any_cast<const Function *>(&IR)) {
1038+
if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {
10391039
Function &F = *const_cast<Function *>(*CF);
10401040
applyDebugify(F, Mode, DebugInfoBeforePass, P);
10411041
MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())
10421042
.getManager()
10431043
.invalidate(F, PA);
1044-
} else if (const auto **CM = any_cast<const Module *>(&IR)) {
1044+
} else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {
10451045
Module &M = *const_cast<Module *>(*CM);
10461046
applyDebugify(M, Mode, DebugInfoBeforePass, P);
10471047
MAM.invalidate(M, PA);
@@ -1053,7 +1053,7 @@ void DebugifyEachInstrumentation::registerCallbacks(
10531053
return;
10541054
PreservedAnalyses PA;
10551055
PA.preserveSet<CFGAnalyses>();
1056-
if (const auto **CF = any_cast<const Function *>(&IR)) {
1056+
if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {
10571057
auto &F = *const_cast<Function *>(*CF);
10581058
Module &M = *F.getParent();
10591059
auto It = F.getIterator();
@@ -1069,7 +1069,7 @@ void DebugifyEachInstrumentation::registerCallbacks(
10691069
MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())
10701070
.getManager()
10711071
.invalidate(F, PA);
1072-
} else if (const auto **CM = any_cast<const Module *>(&IR)) {
1072+
} else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {
10731073
Module &M = *const_cast<Module *>(*CM);
10741074
if (Mode == DebugifyMode::SyntheticDebugInfo)
10751075
checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",

llvm/unittests/ADT/AnyTest.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,55 @@ TEST(AnyTest, ConstructionAndAssignment) {
2424

2525
// An empty Any is not anything.
2626
EXPECT_FALSE(A.has_value());
27-
EXPECT_FALSE(any_cast<int>(&A));
27+
EXPECT_FALSE(llvm::any_cast<int>(&A));
2828

2929
// An int is an int but not something else.
3030
EXPECT_TRUE(B.has_value());
31-
EXPECT_TRUE(any_cast<int>(&B));
32-
EXPECT_FALSE(any_cast<float>(&B));
31+
EXPECT_TRUE(llvm::any_cast<int>(&B));
32+
EXPECT_FALSE(llvm::any_cast<float>(&B));
3333

3434
EXPECT_TRUE(C.has_value());
35-
EXPECT_TRUE(any_cast<int>(&C));
35+
EXPECT_TRUE(llvm::any_cast<int>(&C));
3636

3737
// A const char * is a const char * but not an int.
3838
EXPECT_TRUE(D.has_value());
39-
EXPECT_TRUE(any_cast<const char *>(&D));
40-
EXPECT_FALSE(any_cast<int>(&D));
39+
EXPECT_TRUE(llvm::any_cast<const char *>(&D));
40+
EXPECT_FALSE(llvm::any_cast<int>(&D));
4141

4242
// A double is a double but not a float.
4343
EXPECT_TRUE(E.has_value());
44-
EXPECT_TRUE(any_cast<double>(&E));
45-
EXPECT_FALSE(any_cast<float>(&E));
44+
EXPECT_TRUE(llvm::any_cast<double>(&E));
45+
EXPECT_FALSE(llvm::any_cast<float>(&E));
4646

4747
// After copy constructing from an int, the new item and old item are both
4848
// ints.
4949
llvm::Any F(B);
5050
EXPECT_TRUE(B.has_value());
5151
EXPECT_TRUE(F.has_value());
52-
EXPECT_TRUE(any_cast<int>(&F));
53-
EXPECT_TRUE(any_cast<int>(&B));
52+
EXPECT_TRUE(llvm::any_cast<int>(&F));
53+
EXPECT_TRUE(llvm::any_cast<int>(&B));
5454

5555
// After move constructing from an int, the new item is an int and the old one
5656
// isn't.
5757
llvm::Any G(std::move(C));
5858
EXPECT_FALSE(C.has_value());
5959
EXPECT_TRUE(G.has_value());
60-
EXPECT_TRUE(any_cast<int>(&G));
61-
EXPECT_FALSE(any_cast<int>(&C));
60+
EXPECT_TRUE(llvm::any_cast<int>(&G));
61+
EXPECT_FALSE(llvm::any_cast<int>(&C));
6262

6363
// After copy-assigning from an int, the new item and old item are both ints.
6464
A = F;
6565
EXPECT_TRUE(A.has_value());
6666
EXPECT_TRUE(F.has_value());
67-
EXPECT_TRUE(any_cast<int>(&A));
68-
EXPECT_TRUE(any_cast<int>(&F));
67+
EXPECT_TRUE(llvm::any_cast<int>(&A));
68+
EXPECT_TRUE(llvm::any_cast<int>(&F));
6969

7070
// After move-assigning from an int, the new item and old item are both ints.
7171
B = std::move(G);
7272
EXPECT_TRUE(B.has_value());
7373
EXPECT_FALSE(G.has_value());
74-
EXPECT_TRUE(any_cast<int>(&B));
75-
EXPECT_FALSE(any_cast<int>(&G));
74+
EXPECT_TRUE(llvm::any_cast<int>(&B));
75+
EXPECT_FALSE(llvm::any_cast<int>(&G));
7676
}
7777

7878
TEST(AnyTest, GoodAnyCast) {

llvm/unittests/IR/PassBuilderCallbacksTest.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,16 @@ template <> std::string getName(const StringRef &name) {
291291
}
292292

293293
template <> std::string getName(const Any &WrappedIR) {
294-
if (const auto *const *M = any_cast<const Module *>(&WrappedIR))
294+
if (const auto *const *M = llvm::any_cast<const Module *>(&WrappedIR))
295295
return (*M)->getName().str();
296-
if (const auto *const *F = any_cast<const Function *>(&WrappedIR))
296+
if (const auto *const *F = llvm::any_cast<const Function *>(&WrappedIR))
297297
return (*F)->getName().str();
298-
if (const auto *const *L = any_cast<const Loop *>(&WrappedIR))
298+
if (const auto *const *L = llvm::any_cast<const Loop *>(&WrappedIR))
299299
return (*L)->getName().str();
300-
if (const auto *const *L = any_cast<const LoopNest *>(&WrappedIR))
300+
if (const auto *const *L = llvm::any_cast<const LoopNest *>(&WrappedIR))
301301
return (*L)->getName().str();
302-
if (const auto *const *C = any_cast<const LazyCallGraph::SCC *>(&WrappedIR))
302+
if (const auto *const *C =
303+
llvm::any_cast<const LazyCallGraph::SCC *>(&WrappedIR))
303304
return (*C)->getName();
304305
return "<UNKNOWN>";
305306
}

0 commit comments

Comments
 (0)