diff options
author | Jared Smolens <jared.smolens@arm.com> | 2021-12-01 10:11:08 -0800 |
---|---|---|
committer | Jared Smolens <jared.smolens@arm.com> | 2021-12-01 10:15:03 -0800 |
commit | fd3b7acb794aa453f1d86c24852e22e996d8e21b (patch) | |
tree | 06e8dba86ad2365a15e0a6f600c4dc1c1088d1ed /src/TosaSerialize.cpp | |
parent | 80a022fd103b26a03a04e0565c4d263f73d950b8 (diff) | |
download | tosa_mlir_translator-fd3b7acb794aa453f1d86c24852e22e996d8e21b.tar.gz |
Sort tensor list by name for serialization
- Tensors are stored in an unordered_map from mlir::Value::hash_value
to tensor name.
- Before serialization, sort the tensor list alphabetically by name
so that tensors always appear in a determinstic (and logical) order.
Signed-off-by: Jared Smolens <jared.smolens@arm.com>
Change-Id: I21c0e2ec04b7520e6a753b8bf62572a702002d5b
Diffstat (limited to 'src/TosaSerialize.cpp')
-rw-r--r-- | src/TosaSerialize.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/TosaSerialize.cpp b/src/TosaSerialize.cpp index 5699ffe..0557520 100644 --- a/src/TosaSerialize.cpp +++ b/src/TosaSerialize.cpp @@ -24,6 +24,7 @@ #include "tosa_serialization_handler.h" #include <functional> #include <unordered_map> +#include <map> // The namespace might be confusing here. We have mlir::tosa:: defined in MLIR // and tosa:: defined in serialization library @@ -1510,9 +1511,17 @@ mlir::LogicalResult TosaSerializationBlockBuilder::BuildAllOpsInRegion( } // Build tensor - for (auto pair : tensor_map) { - ser_tensor = BuildTosaSerializationTensor(pair.first /* val */, - pair.second /* name */); + + // The tensor_map is sorted by hashed mlir::Value types. + // For serialization, sort tensors alphabetically by name for a deterministic + // and human-friendly ordering. + std::map<std::string, mlir::Value> tensor_name_sort; + for (auto pair : tensor_map) + tensor_name_sort[pair.second] = pair.first; + + for (auto pair : tensor_name_sort) { + ser_tensor = BuildTosaSerializationTensor(pair.second /* val */, + pair.first /* name */); if (!ser_tensor) { llvm::errs() << "ERROR: Failed to build TosaSerializationTensor\n"; return mlir::failure(); |