@@ -7,7 +7,7 @@ constructs, of which MLIR has many (Block-based branches, Region-based branches,
77CallGraph, etc), and it isn't always clear how best to go about performing the
88propagation. To help writing these types of analyses in MLIR, this document
99details several utilities that simplify the process and make it a bit more
10- approachable. The code from that tutorial can be found in ` mlir/examples/dataflow ` .
10+ approachable. The code from this tutorial can be found in ` mlir/examples/dataflow ` .
1111
1212## Forward Dataflow Analysis
1313
@@ -79,60 +79,54 @@ struct MetadataLatticeValue {
7979 }
8080 }
8181
82- /// This method conservatively joins the information held by ` lhs ` and ` rhs `
83- /// into a new value. This method is required to be monotonic. ` monotonicity `
84- /// is implied by the satisfaction of the following axioms:
85- /// * idempotence: join(x,x) == x
86- /// * commutativity: join(x,y) == join(y,x)
87- /// * associativity: join(x,join(y,z)) == join(join(x,y),z)
88- ///
89- /// When the above axioms are satisfied, we achieve ` monotonicity ` :
90- /// * monotonicity: join(x, join(x,y)) == join(x,y)
9182 static MetadataLatticeValue join(const MetadataLatticeValue &lhs,
9283 const MetadataLatticeValue &rhs) {
93- // To join ` lhs ` and ` rhs ` we will define a simple policy, which is that we
94- // directly insert the metadata of rhs into the metadata of lhs.If lhs and rhs
95- // have overlapping attributes, keep the attribute value in lhs unchanged.
96- MetadataLatticeValue result;
97- for (auto &&lhsIt : lhs.metadata) {
98- result.metadata.insert(
99- std::pair<StringRef , Attribute>(lhsIt.getKey() , lhsIt.getValue() ));
100- }
84+ // To join ` lhs ` and ` rhs ` we will define a simple policy, which is that we
85+ // directly insert the metadata of rhs into the metadata of lhs.If lhs and rhs
86+ // have overlapping attributes, keep the attribute value in lhs unchanged.
87+ MetadataLatticeValue result;
88+ for (auto &&lhsIt : lhs.metadata) {
89+ result.metadata.insert(
90+ std::pair<StringAttr , Attribute>(lhsIt.first , lhsIt.second ));
91+ }
10192
102- for (auto &&rhsIt : rhs.metadata) {
103- result.metadata.insert(
104- std::pair<StringRef, Attribute>(rhsIt.getKey(), rhsIt.getValue()));
105- }
106- return result;
93+ for (auto &&rhsIt : rhs.metadata) {
94+ result.metadata.insert(
95+ std::pair<StringAttr, Attribute>(rhsIt.first, rhsIt.second));
10796 }
97+ return result;
98+ }
10899
109100 /// A simple comparator that checks to see if this value is equal to the one
110101 /// provided.
111102 bool operator==(const MetadataLatticeValue &rhs) const {
112- if (metadata.size() != rhs.metadata.size())
113- return false;
103+ if (metadata.size() != rhs.metadata.size())
104+ return false;
114105
115- // Check that `rhs` contains the same metadata.
116- for (auto &&it : metadata) {
117- auto rhsIt = rhs.metadata.find(it.getKey());
118- if (rhsIt == rhs.metadata.end() || it.second != rhsIt->second)
119- return false;
120- }
121- return true;
106+ // Check that ` rhs ` contains the same metadata.
107+ for (auto &&it : metadata) {
108+ auto rhsIt = rhs.metadata.find(it.first);
109+ if (rhsIt == rhs.metadata.end() || it.second != rhsIt->second)
110+ return false;
122111 }
112+ return true;
113+ }
123114
124115 /// Print data in metadata.
125- void print(llvm::raw_ostream &os) const {
126- os << "{";
127- for (auto&& iter : metadata) {
128- os << iter.getKey() << ": " << iter.getValue() << ", ";
129- }
130- os << "\b\b}\n";
116+ void print(llvm::raw_ostream &os) const {
117+ SmallVector<StringAttr > metadataKey(metadata.keys());
118+ std::sort(metadataKey.begin(), metadataKey.end(),
119+ [ &] (StringAttr a, StringAttr b) { return a < b; });
120+ os << "{";
121+ for (StringAttr key : metadataKey) {
122+ os << key << ": " << metadata.at(key) << ", ";
131123 }
124+ os << "\b\b}\n";
125+ }
132126
133127 /// Our value represents the combined metadata, which is originally a
134128 /// DictionaryAttr, so we use a map.
135- llvm::StringMap< Attribute > metadata;
129+ DenseMap<StringAttr, Attribute> metadata;
136130};
137131```
138132
@@ -314,6 +308,6 @@ func.func @single_join(%arg0 : index, %arg1 : index) -> index {
314308The above IR will print the following after running pass.
315309
316310```
317- { likes_pizza: true}
318- { likes_pizza: true}
311+ %0 = arith.addi %arg0, %arg1 {metadata = { likes_pizza = true}} : index : {"likes_pizza": true}
312+ %1 = arith.addi %0, %arg1 : index : {" likes_pizza" : true}
319313```
0 commit comments