|
| 1 | +/* |
| 2 | + * Copyright 2023 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef wasm_analysis_lattices_shared_h |
| 18 | +#define wasm_analysis_lattices_shared_h |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "../lattice.h" |
| 24 | +#include "bool.h" |
| 25 | + |
| 26 | +namespace wasm::analysis { |
| 27 | + |
| 28 | +// A lattice whose elements are a single ascending chain in lattice `L`. |
| 29 | +// Internally, there is only ever a single monotonically increasing element of L |
| 30 | +// materialized. Dereferencing any element of the Shared lattice will produce |
| 31 | +// the current value of that single element of L, which is generally safe |
| 32 | +// because the current value always overapproximates (i.e. is higher in the |
| 33 | +// lattice than) the value at the time of the Shared element's construction. |
| 34 | +// |
| 35 | +// Each element of this lattice maintains a sequence number that corresponds to |
| 36 | +// a value the shared underlying element has had at some point in time. Higher |
| 37 | +// sequence numbers correspond to greater values of the underlying element. |
| 38 | +// Elements of this lattice are compared and joined using these sequence |
| 39 | +// numbers, so blocks will correctly be re-analyzed if the value has increased |
| 40 | +// since the last time they were analyzed. |
| 41 | +template<Lattice L> struct Shared { |
| 42 | + // If we ever have extremely long-running analyses, this may need to be |
| 43 | + // changed to uint64_t. |
| 44 | + using Seq = uint32_t; |
| 45 | + |
| 46 | + class Element { |
| 47 | + const typename L::Element* val; |
| 48 | + Seq seq = 0; |
| 49 | + |
| 50 | + Element(const typename L::Element* val) : val(val) {} |
| 51 | + |
| 52 | + public: |
| 53 | + Element() = default; |
| 54 | + Element(const Element&) = default; |
| 55 | + Element(Element&&) = default; |
| 56 | + Element& operator=(const Element&) = default; |
| 57 | + Element& operator=(Element&&) = default; |
| 58 | + |
| 59 | + // Only provide const references to the value to force all updates to go |
| 60 | + // through our API. |
| 61 | + const typename L::Element& operator*() const noexcept { return *val; } |
| 62 | + const typename L::Element* operator->() const noexcept { return val; } |
| 63 | + |
| 64 | + bool operator==(const Element& other) const noexcept { |
| 65 | + assert(val == other.val); |
| 66 | + return seq == other.seq; |
| 67 | + } |
| 68 | + |
| 69 | + bool operator!=(const Element& other) const noexcept { |
| 70 | + return !(*this == other); |
| 71 | + } |
| 72 | + |
| 73 | + friend Shared; |
| 74 | + }; |
| 75 | + |
| 76 | + L lattice; |
| 77 | + |
| 78 | + // The current value that all elements point to and the current maximum |
| 79 | + // sequence number. The sequence numbers monotonically increase along with |
| 80 | + // `val` and serve to provide ordering between elements of this lattice. These |
| 81 | + // are mutable because they are logically not part of the lattice itself, but |
| 82 | + // rather of its elements. They are only stored inside the lattice because it |
| 83 | + // is simpler and more efficient than using shared pointers. |
| 84 | + mutable typename L::Element val; |
| 85 | + mutable Seq seq = 0; |
| 86 | + |
| 87 | + Shared(L&& l) : lattice(std::move(l)), val(lattice.getBottom()) {} |
| 88 | + |
| 89 | + // TODO: Delete the move constructor and the move assignment operator. This |
| 90 | + // requires fixing the lattice fuzzer first, since it depends on lattices |
| 91 | + // being moveable. |
| 92 | + |
| 93 | + Element getBottom() const noexcept { return Element{&val}; } |
| 94 | + |
| 95 | + LatticeComparison compare(const Element& a, const Element& b) const noexcept { |
| 96 | + assert(a.val == b.val); |
| 97 | + return a.seq < b.seq ? LESS : a.seq == b.seq ? EQUAL : GREATER; |
| 98 | + } |
| 99 | + |
| 100 | + bool join(Element& joinee, const Element& joiner) const noexcept { |
| 101 | + assert(joinee.val == joiner.val); |
| 102 | + if (joinee.seq < joiner.seq) { |
| 103 | + joinee.seq = joiner.seq; |
| 104 | + return true; |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + bool join(Element& joinee, const typename L::Element& joiner) const noexcept { |
| 110 | + if (lattice.join(val, joiner)) { |
| 111 | + // We have moved to the next value in our ascending chain. Assign it a new |
| 112 | + // sequence number and update joinee with that sequence number. |
| 113 | + joinee.seq = ++seq; |
| 114 | + return true; |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +#if __cplusplus >= 202002L |
| 121 | +static_assert(Lattice<Shared<Bool>>); |
| 122 | +#endif // __cplusplus >= 202002L |
| 123 | + |
| 124 | +} // namespace wasm::analysis |
| 125 | + |
| 126 | +#endif // wasm_analysis_lattices_shared_h |
0 commit comments