Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analysis] Add a generic powerset lattice #6059

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/analysis/lattices/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef wasm_analysis_lattices_array_h
#define wasm_analysis_lattices_array_h

#include <array>
#include <utility>

#include "../lattice.h"
#include "bool.h"
#include "flat.h"

namespace wasm::analysis {

// A lattice whose elements are N-tuples of elements of L. Also written as L^N.
// N is supplied at compile time rather than run time like it is for Vector.
template<Lattice L, size_t N> struct Array {
using Element = std::array<typename L::Element, N>;

L lattice;

Array(L&& lattice) : lattice(std::move(lattice)) {}

private:
// Use a template parameter pack to generate N copies of
// `lattice.getBottom()`. TODO: Use C++20 lambda template parameters instead
// of a separate helper function.
template<size_t... I>
Element getBottomImpl(std::index_sequence<I...>) const noexcept {
return {((void)I, lattice.getBottom())...};
}
template<size_t... I>
Element getTopImpl(std::index_sequence<I...>) const noexcept {
return {((void)I, lattice.getTop())...};
}

public:
Element getBottom() const noexcept {
return getBottomImpl(std::make_index_sequence<N>());
}

Element getTop() const noexcept
#if __cplusplus >= 202002L
requires FullLattice<L>
#endif
{
return getTopImpl(std::make_index_sequence<N>());
}

// `a` <= `b` if all their elements are pairwise <=, etc. Unless we determine
// that there is no relation, we must check all the elements.
LatticeComparison compare(const Element& a, const Element& b) const noexcept {
auto result = EQUAL;
for (size_t i = 0; i < N; ++i) {
switch (lattice.compare(a[i], b[i])) {
case NO_RELATION:
return NO_RELATION;
case EQUAL:
continue;
case LESS:
if (result == GREATER) {
// Cannot be both less and greater.
return NO_RELATION;
}
result = LESS;
continue;
case GREATER:
if (result == LESS) {
// Cannot be both greater and less.
return NO_RELATION;
}
result = GREATER;
continue;
}
}
return result;
}

// Pairwise join on the elements.
bool join(Element& joinee, const Element& joiner) const noexcept {
bool result = false;
for (size_t i = 0; i < N; ++i) {
result |= lattice.join(joinee[i], joiner[i]);
}
return result;
}

// Pairwise meet on the elements.
bool meet(Element& meetee, const Element& meeter) const noexcept
#if __cplusplus >= 202002L
requires FullLattice<L>
#endif
{
bool result = false;
for (size_t i = 0; i < N; ++i) {
result |= lattice.meet(meetee[i], meeter[i]);
}
return result;
}
};

#if __cplusplus >= 202002L
static_assert(FullLattice<Array<Bool, 1>>);
static_assert(Lattice<Array<Flat<bool>, 1>>);
#endif

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_array_h
106 changes: 106 additions & 0 deletions src/analysis/lattices/powerset2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <unordered_set>

#include "../lattice.h"
#include "support/bitset.h"

#ifndef wasm_analysis_lattices_powerset2_h
#define wasm_analysis_lattices_powerset2_h

namespace wasm::analysis {

// A powerset lattice whose elements are sets (represented concretely with type
// `Set`) ordered by subset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could call this a "Set Lattice", that is, a lattice over sets and ordered by the natural subset relation? "Powerset" is a specific mathematical operation to generate all subsets but this lattice here does not create a powerset nor does it require that the elements actually be a powerset IIANM.

If so then the one below could be "Powerset Lattice" (without "finite" in the name, which feels odd there to me).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true we never materialize the full powerset, but this lattice does represent a powerset. The lattice itself is the powerset, since its elements are subsets of the set of all T. For that reason, and to be consistent with the literature, we should continue calling this a powerset lattice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, but why is the latter called "finite" then, if both are finite?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is not necessarily finite. There is no bound on the size of the elements it supports.

template<typename Set> struct Powerset2 {
using Element = Set;

Element getBottom() const noexcept { return Set{}; }

LatticeComparison compare(const Set& a, const Set& b) const noexcept {
auto sizeA = a.size();
auto sizeB = b.size();
if (sizeA <= sizeB) {
for (const auto& val : a) {
if (!b.count(val)) {
// At least one member differs between A and B.
return NO_RELATION;
}
}
// All elements in A were also in B.
return sizeA == sizeB ? EQUAL : LESS;
}
for (const auto& val : b) {
if (!a.count(val)) {
// At least one member differs between A and B.
return NO_RELATION;
}
}
// A was larger and contained all the elements of B.
return GREATER;
}

bool join(Set& joinee, const Set& joiner) const noexcept {
bool result = false;
for (const auto& val : joiner) {
result |= joinee.insert(val).second;
}
return result;
}
};

// A powerset lattice initialized with a list of all elements in the universe,
// making it possible to produce a top elements that contains all of them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// making it possible to produce a top elements that contains all of them.
// making it possible to produce a top element that contains all of them.

template<typename Set> struct FinitePowerset2 : Powerset2<Set> {
private:
const Set top;

public:
using Element = Set;

FinitePowerset2(std::initializer_list<typename Set::value_type>&& vals)
: top(std::move(vals)) {}

template<typename Vals>
FinitePowerset2(const Vals& vals) : top(vals.begin(), vals.end()) {}

Element getTop() const noexcept { return top; }

bool meet(Set& meetee, const Set& meeter) const noexcept {
bool result = false;
for (auto it = meetee.begin(); it != meetee.end();) {
if (!meeter.count(*it)) {
it = meetee.erase(it);
result = true;
} else {
++it;
}
}
return result;
}
};

#if __cplusplus >= 202002L
static_assert(Lattice<Powerset2<BitSet>>);
static_assert(Lattice<Powerset2<std::unordered_set<int>>>);
static_assert(FullLattice<FinitePowerset2<BitSet>>);
static_assert(FullLattice<FinitePowerset2<std::unordered_set<int>>>);
#endif

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_powerset2_h
147 changes: 147 additions & 0 deletions src/analysis/lattices/tuple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef wasm_analysis_lattices_tuple_h
#define wasm_analysis_lattices_tuple_h

#include <tuple>
#include <utility>

#include "bool.h"
#include "support/utilities.h"

namespace wasm::analysis {

template<Lattice... Ls> struct Tuple {
using Element = std::tuple<typename Ls::Element...>;

std::tuple<Ls...> lattices;

Tuple(Ls&&... lattices) : lattices({std::move(lattices)...}) {}

private:
template<size_t... I>
Element getBottomImpl(std::index_sequence<I...>) const noexcept {
return {std::get<I>(lattices).getBottom()...};
}

template<size_t... I>
Element getTopImpl(std::index_sequence<I...>) const noexcept {
return {std::get<I>(lattices).getTop()...};
}

LatticeComparison compareImpl(const Element& a,
const Element& b,
LatticeComparison result,
std::index_sequence<>) const noexcept {
// Base case: there is nothing left to compare.
return result;
}

template<size_t I, size_t... Is>
LatticeComparison compareImpl(const Element& a,
const Element& b,
LatticeComparison result,
std::index_sequence<I, Is...>) const noexcept {
// Recursive case: compare the current elements, update `result`, and
// recurse to the next elements if necessary.
switch (std::get<I>(lattices).compare(std::get<I>(a), std::get<I>(b))) {
case EQUAL:
return compareImpl(a, b, result, std::index_sequence<Is...>{});
case LESS:
if (result == GREATER) {
// Cannot be both less and greater.
return NO_RELATION;
}
return compareImpl(a, b, LESS, std::index_sequence<Is...>{});
case GREATER:
if (result == LESS) {
// Cannot be both greater and less.
return NO_RELATION;
}
return compareImpl(a, b, GREATER, std::index_sequence<Is...>{});
case NO_RELATION:
return NO_RELATION;
}
WASM_UNREACHABLE("unexpected comparison");
}

int joinImpl(Element& joinee,
const Element& joiner,
std::index_sequence<>) const noexcept {
// Base case: there is nothing left to join.
return false;
}

template<size_t I, size_t... Is>
int joinImpl(Element& joinee,
const Element& joiner,
std::index_sequence<I, Is...>) const noexcept {
// Recursive case: join the current element and recurse to the next
// elements.
return std::get<I>(lattices).join(std::get<I>(joinee),
std::get<I>(joiner)) |
joinImpl(joinee, joiner, std::index_sequence<Is...>{});
}

int meetImpl(Element& meetee,
const Element& meeter,
std::index_sequence<>) const noexcept {
// Base case: there is nothing left to mee.
return false;
}

template<size_t I, size_t... Is>
int meetImpl(Element& meetee,
const Element& meeter,
std::index_sequence<I, Is...>) const noexcept {
// Recursive case: meet the current element and recurse to the next
// elements.
return (std::get<I>(lattices).meet(std::get<I>(meetee),
std::get<I>(meeter))) |
meetImpl(meetee, meeter, std::index_sequence<Is...>{});
}

public:
Element getBottom() const noexcept {
return getBottomImpl(std::index_sequence_for<Ls...>());
}

Element getTop() const noexcept {
return getTopImpl(std::index_sequence_for<Ls...>());
}

LatticeComparison compare(const Element& a, const Element& b) const noexcept {
return compareImpl(a, b, EQUAL, std::index_sequence_for<Ls...>());
}

bool join(Element& joinee, const Element& joiner) const noexcept {
return joinImpl(joinee, joiner, std::index_sequence_for<Ls...>());
}

bool meet(Element& meetee, const Element& meeter) const noexcept {
return meetImpl(meetee, meeter, std::index_sequence_for<Ls...>());
}
};

#if __cplusplus >= 202002L
static_assert(FullLattice<Tuple<>>);
static_assert(FullLattice<Tuple<Bool>>);
#endif

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_tuple_h
Loading
Loading