Skip to content

Commit b6eba84

Browse files
authored
Update from C++17 to C++20 (#8218)
As a test I converted all the `set::count` calls to `set::contains` when they were used for membership testing.
1 parent 2609e6c commit b6eba84

File tree

101 files changed

+297
-321
lines changed

Some content is hidden

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

101 files changed

+297
-321
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -383,40 +383,6 @@ jobs:
383383
- name: build
384384
run: cmake --build out
385385

386-
# Duplicates build-asan. Please keep in sync
387-
build-cxx20:
388-
name: c++20
389-
# Make sure we can still build on older Ubuntu
390-
runs-on: ubuntu-22.04
391-
env:
392-
CC: "gcc"
393-
CXX: "g++"
394-
steps:
395-
- uses: actions/setup-python@v5
396-
with:
397-
python-version: '3.x'
398-
- uses: actions/checkout@v4
399-
with:
400-
submodules: true
401-
- name: install ninja
402-
run: sudo apt-get install ninja-build
403-
- name: install v8
404-
run: |
405-
npm install jsvu -g
406-
jsvu --os=default --engines=v8
407-
- name: install Python dev dependencies
408-
run: pip3 install -r requirements-dev.txt
409-
- name: cmake
410-
run: |
411-
mkdir -p out
412-
cmake -S . -B out -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_C_COMPILER="$CC" -DCMAKE_CXX_COMPILER="$CXX" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20
413-
- name: build
414-
run: cmake --build out
415-
- name: test
416-
run: |
417-
python check.py --binaryen-bin=out/bin lit
418-
python check.py --binaryen-bin=out/bin gtest
419-
420386
# Ensures we can build in no-asserts mode (just building).
421387
build-noasserts:
422388
name: noasserts

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include(GNUInstallDirs)
1111
# The C++ standard whose features are required to build Binaryen.
1212
# Keep in sync with scripts/test/shared.py cxx_standard
1313
# The if condition allows embedding in a project with a higher default C++ standard set
14-
set(REQUIRED_CXX_STANDARD 17)
14+
set(REQUIRED_CXX_STANDARD 20)
1515
if(NOT CMAKE_CXX_STANDARD)
1616
set(CMAKE_CXX_STANDARD ${REQUIRED_CXX_STANDARD})
1717
elseif(CMAKE_CXX_STANDARD LESS ${REQUIRED_CXX_STANDARD})

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ After that you can build with CMake:
412412
cmake . && make
413413
```
414414

415-
A C++17 compiler is required. On macOS, you need to install `cmake`, for example, via `brew install cmake`. Note that you can also use `ninja` as your generator: `cmake -G Ninja . && ninja`.
415+
A C++20 compiler is required. On macOS, you need to install `cmake`, for
416+
example, via `brew install cmake`. Note that you can also use `ninja` as your
417+
generator: `cmake -G Ninja . && ninja`.
416418

417419
To avoid the gtest dependency, you can pass `-DBUILD_TESTS=OFF` to cmake.
418420

scripts/test/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# The C++ standard whose features are required to build Binaryen.
2929
# Keep in sync with CMakeLists.txt CXX_STANDARD
30-
cxx_standard = 17
30+
cxx_standard = 20
3131

3232

3333
def parse_args(args):

src/binaryen-c.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5816,7 +5816,7 @@ void BinaryenClearPassArguments(void) { globalPassOptions.arguments.clear(); }
58165816

58175817
bool BinaryenHasPassToSkip(const char* pass) {
58185818
assert(pass);
5819-
return globalPassOptions.passesToSkip.count(pass);
5819+
return globalPassOptions.passesToSkip.contains(pass);
58205820
}
58215821

58225822
void BinaryenAddPassToSkip(const char* pass) {
@@ -5873,7 +5873,7 @@ void BinaryenModuleRunPasses(BinaryenModuleRef module,
58735873
passRunner.options = globalPassOptions;
58745874
for (BinaryenIndex i = 0; i < numPasses; i++) {
58755875
passRunner.add(passes[i],
5876-
globalPassOptions.arguments.count(passes[i]) > 0
5876+
globalPassOptions.arguments.contains(passes[i])
58775877
? globalPassOptions.arguments[passes[i]]
58785878
: std::optional<std::string>());
58795879
}
@@ -6125,7 +6125,7 @@ void BinaryenFunctionRunPasses(BinaryenFunctionRef func,
61256125
passRunner.options = globalPassOptions;
61266126
for (BinaryenIndex i = 0; i < numPasses; i++) {
61276127
passRunner.add(passes[i],
6128-
globalPassOptions.arguments.count(passes[i]) > 0
6128+
globalPassOptions.arguments.contains(passes[i])
61296129
? globalPassOptions.arguments[passes[i]]
61306130
: std::optional<std::string>());
61316131
}

src/cfg/Relooper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace CFG {
4040

4141
template<class T, class U>
4242
static bool contains(const T& container, const U& contained) {
43-
return !!container.count(contained);
43+
return container.contains(contained);
4444
}
4545

4646
// Rendering utilities
@@ -667,7 +667,7 @@ struct Optimizer : public RelooperRecursor {
667667
// Add a branch to the target (which may be the unchanged original) in
668668
// the set of new branches. If it's a replacement, it may collide, and
669669
// we need to merge.
670-
if (NewBranchesOut.count(Replacement)) {
670+
if (NewBranchesOut.contains(Replacement)) {
671671
#if RELOOPER_OPTIMIZER_DEBUG
672672
std::cout << " merge\n";
673673
#endif
@@ -915,7 +915,7 @@ struct Optimizer : public RelooperRecursor {
915915
return false;
916916
}
917917
for (auto& [ABlock, ABranch] : A->BranchesOut) {
918-
if (B->BranchesOut.count(ABlock) == 0) {
918+
if (!B->BranchesOut.contains(ABlock)) {
919919
return false;
920920
}
921921
auto* BBranch = B->BranchesOut[ABlock];
@@ -1578,7 +1578,7 @@ void Relooper::Calculate(Block* Entry) {
15781578
// jumped to forward, without using the label variable
15791579
bool Checked = false;
15801580
for (auto* Entry : *Entries) {
1581-
if (InitialEntries.count(Entry)) {
1581+
if (InitialEntries.contains(Entry)) {
15821582
Checked = true;
15831583
break;
15841584
}

src/cfg/cfg-traversal.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
616616
queue.erase(iter);
617617
alive.insert(curr);
618618
for (auto* out : curr->out) {
619-
if (!alive.count(out)) {
619+
if (!alive.contains(out)) {
620620
queue.insert(out);
621621
}
622622
}
@@ -626,21 +626,21 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
626626

627627
void unlinkDeadBlocks(std::unordered_set<BasicBlock*> alive) {
628628
for (auto& block : basicBlocks) {
629-
if (!alive.count(block.get())) {
629+
if (!alive.contains(block.get())) {
630630
block->in.clear();
631631
block->out.clear();
632632
continue;
633633
}
634634
block->in.erase(std::remove_if(block->in.begin(),
635635
block->in.end(),
636636
[&alive](BasicBlock* other) {
637-
return !alive.count(other);
637+
return !alive.contains(other);
638638
}),
639639
block->in.end());
640640
block->out.erase(std::remove_if(block->out.begin(),
641641
block->out.end(),
642642
[&alive](BasicBlock* other) {
643-
return !alive.count(other);
643+
return !alive.contains(other);
644644
}),
645645
block->out.end());
646646
}
@@ -664,17 +664,17 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
664664
std::cout << "<==\nCFG [" << message << "]:\n";
665665
generateDebugIds();
666666
for (auto& block : basicBlocks) {
667-
assert(debugIds.count(block.get()) > 0);
667+
assert(debugIds.contains(block.get()));
668668
std::cout << " block " << debugIds[block.get()] << " (" << block.get()
669669
<< "):\n";
670670
block->contents.dump(static_cast<SubType*>(this)->getFunction());
671671
for (auto& in : block->in) {
672-
assert(debugIds.count(in) > 0);
672+
assert(debugIds.contains(in));
673673
assert(std::find(in->out.begin(), in->out.end(), block.get()) !=
674674
in->out.end()); // must be a parallel link back
675675
}
676676
for (auto& out : block->out) {
677-
assert(debugIds.count(out) > 0);
677+
assert(debugIds.contains(out));
678678
std::cout << " out: " << debugIds[out] << "\n";
679679
assert(std::find(out->in.begin(), out->in.end(), block.get()) !=
680680
out->in.end()); // must be a parallel link back

src/cfg/liveness-traversal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> {
222222
// keep working while stuff is flowing
223223
std::unordered_set<BasicBlock*> queue;
224224
for (auto& curr : CFGWalker<SubType, VisitorType, Liveness>::basicBlocks) {
225-
if (liveBlocks.count(curr.get()) == 0) {
225+
if (!liveBlocks.contains(curr.get())) {
226226
continue; // ignore dead blocks
227227
}
228228
queue.insert(curr.get());

src/ir/ExpressionAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ struct Hasher {
353353
// (block $x (br $x))
354354
// (block $y (br $y))
355355
// But if the name is not known to us, hash the absolute one.
356-
if (!internalNames.count(curr)) {
356+
if (!internalNames.contains(curr)) {
357357
rehash(digest, 1);
358358
// Perform the same hashing as a generic name.
359359
visitNonScopeName(curr);

src/ir/LocalGraph.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ struct LocalGraphFlower
341341
auto index = get->index;
342342

343343
// We must never repeat work.
344-
assert(!getSetsMap.count(get));
344+
assert(!getSetsMap.contains(get));
345345

346346
// Regardless of what we do below, ensure an entry for this get, so that we
347347
// know we computed it.
@@ -410,7 +410,7 @@ struct LocalGraphFlower
410410
auto index = set->index;
411411

412412
// We must never repeat work.
413-
assert(!setInfluences.count(set));
413+
assert(!setInfluences.contains(set));
414414

415415
// In theory we could flow the set forward, but to keep things simple we
416416
// reuse the logic for flowing gets backwards: We flow all the gets of the
@@ -419,7 +419,7 @@ struct LocalGraphFlower
419419
// doing work for local indexes we don't care about.
420420
for (auto* get : getsByIndex[index]) {
421421
// Don't repeat work.
422-
if (!getSetsMap.count(get)) {
422+
if (!getSetsMap.contains(get)) {
423423
computeGetSets(get);
424424
}
425425
}
@@ -632,7 +632,7 @@ void LocalGraph::computeSSAIndexes() {
632632
}
633633
}
634634

635-
bool LocalGraph::isSSA(Index x) { return SSAIndexes.count(x); }
635+
bool LocalGraph::isSSA(Index x) { return SSAIndexes.contains(x); }
636636

637637
// LazyLocalGraph
638638

@@ -672,7 +672,7 @@ LazyLocalGraph::~LazyLocalGraph() {
672672

673673
void LazyLocalGraph::computeGetSets(LocalGet* get) const {
674674
// We must never repeat work.
675-
assert(!getSetsMap.count(get));
675+
assert(!getSetsMap.contains(get));
676676

677677
if (!flower) {
678678
makeFlower();
@@ -682,7 +682,7 @@ void LazyLocalGraph::computeGetSets(LocalGet* get) const {
682682

683683
void LazyLocalGraph::computeSetInfluences(LocalSet* set) const {
684684
// We must never repeat work.
685-
assert(!setInfluences.count(set));
685+
assert(!setInfluences.contains(set));
686686

687687
if (!flower) {
688688
makeFlower();
@@ -705,7 +705,7 @@ void LazyLocalGraph::computeGetInfluences() const {
705705

706706
bool LazyLocalGraph::computeSSA(Index index) const {
707707
// We must never repeat work.
708-
assert(!SSAIndexes.count(index));
708+
assert(!SSAIndexes.contains(index));
709709

710710
if (!flower) {
711711
makeFlower();

0 commit comments

Comments
 (0)