Skip to content

Commit 7f24fce

Browse files
authored
Modernize code to C++17 (#3104)
1 parent 3799916 commit 7f24fce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+269
-535
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ if(MSVC)
165165
add_compile_flag("/arch:sse2")
166166
endif()
167167
endif()
168-
add_compile_flag("/std:c++17") # VS2017 generates "error C2429: attribute 'fallthrough' requires compiler flag '/std:c++17'"
168+
add_compile_flag("/std:c++${CXX_STANDARD}")
169169
add_compile_flag("/wd4146") # Ignore warning "warning C4146: unary minus operator applied to unsigned type, result still unsigned", this pattern is used somewhat commonly in the code.
170170
# 4267 and 4244 are conversion/truncation warnings. We might want to fix these but they are currently pervasive.
171171
add_compile_flag("/wd4267")

src/cfg/Relooper.cpp

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ static wasm::Expression* HandleFollowupMultiples(wasm::Expression* Ret,
6262
if (!Multiple) {
6363
break;
6464
}
65-
for (auto& iter : Multiple->InnerMap) {
66-
int Id = iter.first;
67-
Shape* Body = iter.second;
65+
for (auto& [Id, Body] : Multiple->InnerMap) {
6866
Curr->name = Builder.getBlockBreakName(Id);
6967
Curr->finalize(); // it may now be reachable, via a break
7068
auto* Outer = Builder.makeBlock(Curr);
@@ -222,13 +220,13 @@ wasm::Expression* Block::Render(RelooperBuilder& Builder, bool InLoop) {
222220
Block* DefaultTarget = nullptr;
223221

224222
// Find the default target, the one without a condition
225-
for (auto& iter : ProcessedBranchesOut) {
226-
if ((!SwitchCondition && !iter.second->Condition) ||
227-
(SwitchCondition && !iter.second->SwitchValues)) {
223+
for (auto& [Target, Details] : ProcessedBranchesOut) {
224+
if ((!SwitchCondition && !Details->Condition) ||
225+
(SwitchCondition && !Details->SwitchValues)) {
228226
assert(!DefaultTarget &&
229227
"block has branches without a default (nullptr for the "
230228
"condition)"); // Must be exactly one default // nullptr
231-
DefaultTarget = iter.first;
229+
DefaultTarget = Target;
232230
}
233231
}
234232
// Since each Block* must* branch somewhere, this must be set
@@ -338,9 +336,7 @@ wasm::Expression* Block::Render(RelooperBuilder& Builder, bool InLoop) {
338336
auto* Outer = Builder.makeBlock();
339337
auto* Inner = Outer;
340338
std::vector<wasm::Name> Table;
341-
for (auto& iter : ProcessedBranchesOut) {
342-
Block* Target = iter.first;
343-
Branch* Details = iter.second;
339+
for (auto& [Target, Details] : ProcessedBranchesOut) {
344340
wasm::Name CurrName;
345341
if (Details->SwitchValues) {
346342
CurrName = wasm::Name(Base + "$case$" + std::to_string(Target->Id));
@@ -435,9 +431,9 @@ wasm::Expression* MultipleShape::Render(RelooperBuilder& Builder, bool InLoop) {
435431
wasm::If* FirstIf = nullptr;
436432
wasm::If* CurrIf = nullptr;
437433
std::vector<wasm::If*> finalizeStack;
438-
for (auto& iter : InnerMap) {
439-
auto* Now = Builder.makeIf(Builder.makeCheckLabel(iter.first),
440-
iter.second->Render(Builder, InLoop));
434+
for (auto& [Id, Body] : InnerMap) {
435+
auto* Now =
436+
Builder.makeIf(Builder.makeCheckLabel(Id), Body->Render(Builder, InLoop));
441437
finalizeStack.push_back(Now);
442438
if (!CurrIf) {
443439
FirstIf = CurrIf = Now;
@@ -610,8 +606,7 @@ struct Optimizer : public RelooperRecursor {
610606
void CanonicalizeCode() {
611607
for (auto& Block : Parent->Blocks) {
612608
Block->Code = Canonicalize(Block->Code);
613-
for (auto& iter : Block->BranchesOut) {
614-
auto* Branch = iter.second;
609+
for (auto& [_, Branch] : Block->BranchesOut) {
615610
if (Branch->Code) {
616611
Branch->Code = Canonicalize(Branch->Code);
617612
}
@@ -647,15 +642,14 @@ struct Optimizer : public RelooperRecursor {
647642
Next = Replacement = NextNext;
648643
// If we've already seen this, stop - it's an infinite loop of
649644
// empty blocks we can skip through.
650-
if (Seen.count(Replacement)) {
645+
if (!Seen.emplace(Replacement).second) {
651646
// Stop here. Note that if we started from X and ended up with X
652647
// once more, then Replacement == First and so lower down we
653648
// will not report that we did any work, avoiding an infinite
654649
// loop due to always thinking there is more work to do.
655650
break;
656651
} else {
657652
// Otherwise, keep going.
658-
Seen.insert(Replacement);
659653
continue;
660654
}
661655
}
@@ -698,12 +692,10 @@ struct Optimizer : public RelooperRecursor {
698692
if (ParentBlock->BranchesOut.size() >= 2) {
699693
std::unordered_map<size_t, std::vector<BranchBlock>> HashedBranchesOut;
700694
std::vector<Block*> BlocksToErase;
701-
for (auto& iter : ParentBlock->BranchesOut) {
702-
Block* CurrBlock = iter.first;
695+
for (auto& [CurrBlock, CurrBranch] : ParentBlock->BranchesOut) {
703696
#if RELOOPER_OPTIMIZER_DEBUG
704697
std::cout << " consider child " << CurrBlock->Id << '\n';
705698
#endif
706-
Branch* CurrBranch = iter.second;
707699
if (CurrBranch->Code) {
708700
// We can't merge code; ignore
709701
continue;
@@ -712,9 +704,7 @@ struct Optimizer : public RelooperRecursor {
712704
auto& HashedSiblings = HashedBranchesOut[HashValue];
713705
// Check if we are equivalent to any of them - if so, merge us.
714706
bool Merged = false;
715-
for (auto& Pair : HashedSiblings) {
716-
Branch* SiblingBranch = Pair.first;
717-
Block* SiblingBlock = Pair.second;
707+
for (auto& [SiblingBranch, SiblingBlock] : HashedSiblings) {
718708
if (HaveEquivalentContents(CurrBlock, SiblingBlock)) {
719709
#if RELOOPER_OPTIMIZER_DEBUG
720710
std::cout << " equiv! to " << SiblingBlock->Id << '\n';
@@ -809,8 +799,8 @@ struct Optimizer : public RelooperRecursor {
809799
} else {
810800
// If the block has no switch, the branches must not as well.
811801
#ifndef NDEBUG
812-
for (auto& iter : ParentBlock->BranchesOut) {
813-
assert(!iter.second->SwitchValues);
802+
for (auto& [_, CurrBranch] : ParentBlock->BranchesOut) {
803+
assert(!CurrBranch->SwitchValues);
814804
}
815805
#endif
816806
}
@@ -923,9 +913,7 @@ struct Optimizer : public RelooperRecursor {
923913
if (A->BranchesOut.size() != B->BranchesOut.size()) {
924914
return false;
925915
}
926-
for (auto& aiter : A->BranchesOut) {
927-
Block* ABlock = aiter.first;
928-
Branch* ABranch = aiter.second;
916+
for (auto& [ABlock, ABranch] : A->BranchesOut) {
929917
if (B->BranchesOut.count(ABlock) == 0) {
930918
return false;
931919
}
@@ -1030,11 +1018,11 @@ struct Optimizer : public RelooperRecursor {
10301018
wasm::ExpressionAnalyzer::hash(Curr->SwitchCondition));
10311019
}
10321020
wasm::rehash(digest, uint8_t(2));
1033-
for (auto& Pair : Curr->BranchesOut) {
1021+
for (auto& [CurrBlock, CurrBranch] : Curr->BranchesOut) {
10341022
// Hash the Block* as a pointer TODO: full hash?
1035-
wasm::rehash(digest, reinterpret_cast<size_t>(Pair.first));
1023+
wasm::rehash(digest, reinterpret_cast<size_t>(CurrBlock));
10361024
// Hash the Branch info properly
1037-
wasm::hash_combine(digest, Hash(Pair.second));
1025+
wasm::hash_combine(digest, Hash(CurrBranch));
10381026
}
10391027
return digest;
10401028
}
@@ -1077,8 +1065,8 @@ void Relooper::Calculate(Block* Entry) {
10771065
if (!contains(Live.Live, Curr)) {
10781066
continue;
10791067
}
1080-
for (auto& iter : Curr->BranchesOut) {
1081-
iter.first->BranchesIn.insert(Curr);
1068+
for (auto& [CurrBlock, _] : Curr->BranchesOut) {
1069+
CurrBlock->BranchesIn.insert(Curr);
10821070
}
10831071
}
10841072

@@ -1092,9 +1080,9 @@ void Relooper::Calculate(Block* Entry) {
10921080
void GetBlocksOut(Block* Source,
10931081
BlockSet& Entries,
10941082
BlockSet* LimitTo = nullptr) {
1095-
for (auto& iter : Source->BranchesOut) {
1096-
if (!LimitTo || contains(*LimitTo, iter.first)) {
1097-
Entries.insert(iter.first);
1083+
for (auto& [CurrBlock, _] : Source->BranchesOut) {
1084+
if (!LimitTo || contains(*LimitTo, CurrBlock)) {
1085+
Entries.insert(CurrBlock);
10981086
}
10991087
}
11001088
}
@@ -1174,8 +1162,7 @@ void Relooper::Calculate(Block* Entry) {
11741162
assert(InnerBlocks.size() > 0);
11751163

11761164
for (auto* Curr : InnerBlocks) {
1177-
for (auto& iter : Curr->BranchesOut) {
1178-
Block* Possible = iter.first;
1165+
for (auto& [Possible, _] : Curr->BranchesOut) {
11791166
if (!contains(InnerBlocks, Possible)) {
11801167
NextEntries.insert(Possible);
11811168
}
@@ -1289,8 +1276,7 @@ void Relooper::Calculate(Block* Entry) {
12891276
// may have been seen before and invalidated already
12901277
if (Ownership[Invalidatee]) {
12911278
Ownership[Invalidatee] = nullptr;
1292-
for (auto& iter : Invalidatee->BranchesOut) {
1293-
Block* Target = iter.first;
1279+
for (auto& [Target, _] : Invalidatee->BranchesOut) {
12941280
auto Known = Ownership.find(Target);
12951281
if (Known != Ownership.end()) {
12961282
Block* TargetOwner = Known->second;
@@ -1330,8 +1316,7 @@ void Relooper::Calculate(Block* Entry) {
13301316
continue;
13311317
}
13321318
// Add all children
1333-
for (auto& iter : Curr->BranchesOut) {
1334-
Block* New = iter.first;
1319+
for (auto& [New, _] : Curr->BranchesOut) {
13351320
auto Known = Helper.Ownership.find(New);
13361321
if (Known == Helper.Ownership.end()) {
13371322
// New node. Add it, and put it in the queue
@@ -1404,9 +1389,7 @@ void Relooper::Calculate(Block* Entry) {
14041389
IndependentGroups.size());
14051390
MultipleShape* Multiple = Parent->AddMultipleShape();
14061391
BlockSet CurrEntries;
1407-
for (auto& iter : IndependentGroups) {
1408-
Block* CurrEntry = iter.first;
1409-
BlockSet& CurrBlocks = iter.second;
1392+
for (auto& [CurrEntry, CurrBlocks] : IndependentGroups) {
14101393
PrintDebug(" multiple group with entry %d:\n", CurrEntry->Id);
14111394
DebugDump(CurrBlocks, " ");
14121395
// Create inner block
@@ -1563,8 +1546,7 @@ void Relooper::Calculate(Block* Entry) {
15631546
bool DeadEnd = true;
15641547
BlockSet& SmallGroup = IndependentGroups[SmallEntry];
15651548
for (auto* Curr : SmallGroup) {
1566-
for (auto& iter : Curr->BranchesOut) {
1567-
Block* Target = iter.first;
1549+
for (auto& [Target, _] : Curr->BranchesOut) {
15681550
if (!contains(SmallGroup, Target)) {
15691551
DeadEnd = false;
15701552
break;
@@ -1685,8 +1667,8 @@ void Debugging::Dump(Shape* S, const char* prefix) {
16851667
printf("<< Simple with block %d\n", Simple->Inner->Id);
16861668
} else if (MultipleShape* Multiple = Shape::IsMultiple(S)) {
16871669
printf("<< Multiple\n");
1688-
for (auto& iter : Multiple->InnerMap) {
1689-
printf(" with entry %d\n", iter.first);
1670+
for (auto& [Entry, _] : Multiple->InnerMap) {
1671+
printf(" with entry %d\n", Entry);
16901672
}
16911673
} else if (Shape::IsLoop(S)) {
16921674
printf("<< Loop\n");

src/cfg/cfg-traversal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ struct CFGWalker : public ControlFlowWalker<SubType, VisitorType> {
536536
void checkDuplicates(std::vector<BasicBlock*>& list) {
537537
std::unordered_set<BasicBlock*> seen;
538538
for (auto* curr : list) {
539-
assert(seen.count(curr) == 0);
540-
seen.insert(curr);
539+
auto res = seen.emplace(curr);
540+
assert(res.second);
541541
}
542542
}
543543

src/dataflow/graph.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
#include "ir/literal-utils.h"
3232
#include "wasm.h"
3333

34-
namespace wasm {
35-
36-
namespace DataFlow {
34+
namespace wasm::DataFlow {
3735

3836
// Main logic to generate IR for a function. This is implemented as a
3937
// visitor on the wasm, where visitors return a Node* that either
@@ -797,8 +795,6 @@ struct Graph : public UnifiedExpressionVisitor<Graph, Node*> {
797795
const Name FAKE_CALL = "fake$dfo$call";
798796
};
799797

800-
} // namespace DataFlow
801-
802-
} // namespace wasm
798+
} // namespace wasm::DataFlow
803799

804800
#endif // wasm_dataflow_graph_h

src/dataflow/node.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
#include "ir/utils.h"
2929
#include "wasm.h"
3030

31-
namespace wasm {
32-
33-
namespace DataFlow {
31+
namespace wasm::DataFlow {
3432

3533
//
3634
// The core IR representation in DataFlow: a Node.
@@ -218,8 +216,6 @@ struct Node {
218216
}
219217
};
220218

221-
} // namespace DataFlow
222-
223-
} // namespace wasm
219+
} // namespace wasm::DataFlow
224220

225221
#endif // wasm_dataflow_node

src/dataflow/users.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828
#include "dataflow/graph.h"
2929

30-
namespace wasm {
31-
32-
namespace DataFlow {
30+
namespace wasm::DataFlow {
3331

3432
// Calculates the users of each node.
3533
// users[x] = { y, z, .. }
@@ -95,8 +93,6 @@ class Users {
9593
void removeAllUsesOf(Node* node) { users.erase(node); }
9694
};
9795

98-
} // namespace DataFlow
99-
100-
} // namespace wasm
96+
} // namespace wasm::DataFlow
10197

10298
#endif // wasm_dataflow_users

src/dataflow/utils.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
#include "dataflow/node.h"
3030
#include "wasm.h"
3131

32-
namespace wasm {
33-
34-
namespace DataFlow {
32+
namespace wasm::DataFlow {
3533

3634
inline std::ostream& dump(Node* node, std::ostream& o, size_t indent = 0) {
3735
auto doIndent = [&]() {
@@ -147,8 +145,6 @@ inline bool allInputsConstant(Node* node) {
147145
return false;
148146
}
149147

150-
} // namespace DataFlow
151-
152-
} // namespace wasm
148+
} // namespace wasm::DataFlow
153149

154150
#endif // wasm_dataflow_utils

src/ir/ExpressionManipulator.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
#include "ir/load-utils.h"
1818
#include "ir/utils.h"
1919

20-
namespace wasm {
21-
22-
namespace ExpressionManipulator {
20+
namespace wasm::ExpressionManipulator {
2321

2422
Expression*
2523
flexibleCopy(Expression* original, Module& wasm, CustomCopier custom) {
@@ -118,6 +116,4 @@ void spliceIntoBlock(Block* block, Index index, Expression* add) {
118116
block->finalize(block->type);
119117
}
120118

121-
} // namespace ExpressionManipulator
122-
123-
} // namespace wasm
119+
} // namespace wasm::ExpressionManipulator

0 commit comments

Comments
 (0)