diff options
author | juj <jujjyl@gmail.com> | 2022-04-28 21:31:08 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-28 18:31:08 +0000 |
commit | ba64ac3ed47cb35a477aaaf185679ae9d0b79862 (patch) | |
tree | a35322a8d2162e07352c7272b97ffbd195d876a3 /src/support/sparse_square_matrix.h | |
parent | 408b2eb7df01f42157e24c2c58f01c4fa5c6b9d6 (diff) | |
download | binaryen-ba64ac3ed47cb35a477aaaf185679ae9d0b79862.tar.gz binaryen-ba64ac3ed47cb35a477aaaf185679ae9d0b79862.tar.bz2 binaryen-ba64ac3ed47cb35a477aaaf185679ae9d0b79862.zip |
Lift the restriction in liveness-traversal.h that supported max 65535 locals in a function. (#4567)
* Lift the restriction in liveness-traversal.h that supported max 65535 locals in a function.
* Lint
* Fix typo
* Fix static
* Lint
* Lint
* Lint
* Add needed canRun function
* lint
* Use either a sparse or a dense matrix for tracking liveness copies, depending on the locals count.
* Lint
* Fix lint
* Lint
* Implement sparse_square_matrix class and use that as a backing.
* Lint
* Lint
* Lint #includes
* Lint
* Lint includes
* Remove unnecessary code
* Fix canonical accesses to copies matrix
* Lint
* Add missing variable update
* Remove canRun() function
* Address review
* Update expected test results
* Update test name
* Add asserts to sparse_square_matrix set and get functions that they are not out of bound.
* Lint includes
* Update test expectation
* Use .clear() + .resize() to reset totalCopies vector
Diffstat (limited to 'src/support/sparse_square_matrix.h')
-rw-r--r-- | src/support/sparse_square_matrix.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/support/sparse_square_matrix.h b/src/support/sparse_square_matrix.h new file mode 100644 index 000000000..03ebcb5c6 --- /dev/null +++ b/src/support/sparse_square_matrix.h @@ -0,0 +1,76 @@ +/* + * Copyright 2022 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. + */ + +// A helper type for potentially sparse N*N matrix container. + +#pragma once + +#include <assert.h> +#include <stdint.h> +#include <unordered_map> +#include <vector> + +template<typename Ty> class sparse_square_matrix { + std::vector<Ty> denseStorage; + std::unordered_map<uint64_t, Ty> sparseStorage; + uint32_t N; + +public: + sparse_square_matrix() : N(0) {} + + explicit sparse_square_matrix(int N) : N(N) { + if (N < DenseLimit) { + denseStorage.resize(N * N); + } + } + + static const size_t DenseLimit = 8192; + + uint32_t width() const { return N; } + + bool usingDenseStorage() const { return !denseStorage.empty(); } + + void set(uint32_t i, uint32_t j, const Ty& value) { + assert(i < N); + assert(j < N); + if (usingDenseStorage()) { + denseStorage[i * N + j] = value; + } else { + sparseStorage[i * N + j] = value; + } + } + + const Ty get(uint32_t i, uint32_t j) const { + assert(i < N); + assert(j < N); + if (usingDenseStorage()) { + return denseStorage[i * N + j]; + } + auto iter = sparseStorage.find(i * N + j); + return iter == sparseStorage.end() ? Ty() : iter->second; + } + + // Resizes the matrix to a new n*n size, and clears all entries + // to the default-initialized value. + void recreate(uint32_t n) { + N = n; + denseStorage.clear(); + sparseStorage.clear(); + if (N < DenseLimit) { + denseStorage.resize(N * N); + } + } +}; |