Skip to content

[analysis] Implement a Lift lattice #6040

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

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
1 change: 1 addition & 0 deletions src/analysis/lattices/flat.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct Flat {
return EQUAL;
}
}

bool join(Element& self, const Element& other) const noexcept {
switch (compare(self, other)) {
case LESS:
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/lattices/inverted.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <utility>

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

namespace wasm::analysis {

Expand All @@ -44,6 +45,10 @@ template<FullLattice L> struct Inverted {
}
};

#if __cplusplus >= 202002L
static_assert(Lattice<Inverted<Bool>>);
#endif

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_inverted_h
82 changes: 82 additions & 0 deletions src/analysis/lattices/lift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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_lift_h
#define wasm_analysis_lattices_lift_h

#include <optional>
#include <utility>

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

namespace wasm::analysis {

// A lattice created by "lifting" another lattice by inserting a new bottom
// element that is less than all elements in the lifted lattice.
template<Lattice L> struct Lift {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a comment on what Lift means?

// Represent the bottom element as an empty optional and any element of the
// lifted lattice as a non-empty optional. This representation is
// intentionally part of the public API.
struct Element : std::optional<typename L::Element> {
bool isBottom() const noexcept { return !this->has_value(); }
Copy link
Member

Choose a reason for hiding this comment

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

How should I read this has_value? Do we use the empty optional as the bottom perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, exactly. I'll add a comment.

bool operator==(const Element& other) const noexcept {
return (isBottom() && other.isBottom()) ||
(!isBottom() && !other.isBottom() && **this == *other);
Copy link
Member

Choose a reason for hiding this comment

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

Why wouldn't **this == *other work if both are bottom?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't evaluate **this if isBottom because that would be dereferencing an empty optional.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see, then the new bottom element is basically the "nothing" case of the optional? So the extra bit of the optional marks that new item, basically. That's what I was missing.

At the risk of seeming repetitive: comments 😃

}
};

L lattice;
Lift(L&& lattice) : lattice(std::move(lattice)) {}

Element getBottom() const noexcept { return {std::nullopt}; }
Element get(typename L::Element&& val) const noexcept {
return Element{std::move(val)};
}

LatticeComparison compare(const Element& a, const Element& b) const noexcept {
if (a.isBottom() && b.isBottom()) {
return EQUAL;
} else if (a.isBottom()) {
return LESS;
} else if (b.isBottom()) {
return GREATER;
} else {
return lattice.compare(*a, *b);
}
}

bool join(Element& self, const Element& other) const noexcept {
if (self.isBottom() && other.isBottom()) {
return false;
} else if (self.isBottom()) {
self = other;
return true;
} else if (other.isBottom()) {
return false;
} else {
return lattice.join(*self, *other);
}
}
};

#if __cplusplus >= 202002L
static_assert(Lattice<Lift<Bool>>);
#endif

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_lift_h
77 changes: 77 additions & 0 deletions test/gtest/lattices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "analysis/lattices/flat.h"
#include "analysis/lattices/int.h"
#include "analysis/lattices/inverted.h"
#include "analysis/lattices/lift.h"
#include "gtest/gtest.h"

using namespace wasm;
Expand Down Expand Up @@ -297,3 +298,79 @@ TEST(FlatLattice, Join) {
EXPECT_FALSE(flat.join(elem, flat.getTop()));
EXPECT_TRUE(elem.isTop());
}

TEST(LiftLattice, GetBottom) {
analysis::Lift lift{analysis::Bool{}};
EXPECT_TRUE(lift.getBottom().isBottom());
EXPECT_FALSE(lift.getBottom().has_value());
}

TEST(LiftLattice, GetVal) {
analysis::Lift lift{analysis::Bool{}};
EXPECT_FALSE(lift.get(false).isBottom());
EXPECT_FALSE(lift.get(true).isBottom());
EXPECT_FALSE(*lift.get(false));
EXPECT_TRUE(*lift.get(true));

analysis::Lift liftInt{analysis::Int32{}};
EXPECT_FALSE(liftInt.get(10).isBottom());
EXPECT_EQ(*liftInt.get(0), 0);
EXPECT_EQ(*liftInt.get(10), 10);
}

TEST(LiftLattice, Compare) {
analysis::Lift lift{analysis::Flat<bool>{}};
auto& liftee = lift.lattice;
auto bot = lift.getBottom();
auto lifteeBot = lift.get(liftee.getBottom());
auto a = lift.get(liftee.get(false));
auto b = lift.get(liftee.get(true));
auto top = lift.get(liftee.getTop());

EXPECT_EQ(lift.compare(bot, bot), analysis::EQUAL);
EXPECT_EQ(lift.compare(bot, lifteeBot), analysis::LESS);
EXPECT_EQ(lift.compare(bot, a), analysis::LESS);
EXPECT_EQ(lift.compare(bot, b), analysis::LESS);
EXPECT_EQ(lift.compare(bot, top), analysis::LESS);

EXPECT_EQ(lift.compare(lifteeBot, bot), analysis::GREATER);
EXPECT_EQ(lift.compare(a, bot), analysis::GREATER);
EXPECT_EQ(lift.compare(b, bot), analysis::GREATER);
EXPECT_EQ(lift.compare(top, bot), analysis::GREATER);

EXPECT_EQ(lift.compare(a, b), analysis::NO_RELATION);
EXPECT_EQ(lift.compare(a, top), analysis::LESS);
EXPECT_EQ(lift.compare(a, lifteeBot), analysis::GREATER);
}

TEST(LiftLattice, Join) {
analysis::Lift lift{analysis::Bool{}};
auto bot = lift.getBottom();
auto lifteeBot = lift.get(false);
auto top = lift.get(true);

// bot u bot = bot
auto elem = bot;
EXPECT_FALSE(lift.join(elem, bot));
EXPECT_EQ(elem, bot);

// bot u lifteeBot = lifteeBot
EXPECT_TRUE(lift.join(elem, lifteeBot));
EXPECT_EQ(elem, lifteeBot);

// lifteeBot u bot = lifteeBot
EXPECT_FALSE(lift.join(elem, bot));
EXPECT_EQ(elem, lifteeBot);

// lifteeBot u lifteeBot = lifteeBot
EXPECT_FALSE(lift.join(elem, lifteeBot));
EXPECT_EQ(elem, lifteeBot);

// lifteeBot u top = top
EXPECT_TRUE(lift.join(elem, top));
EXPECT_EQ(elem, top);

// top u bot = top
EXPECT_FALSE(lift.join(elem, bot));
EXPECT_EQ(elem, top);
}