-
Notifications
You must be signed in to change notification settings - Fork 786
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
// 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(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should I read this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't evaluate There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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?