2121#include " llvm/ADT/DenseMap.h"
2222#include " llvm/Support/Error.h"
2323#include < memory>
24+ #include < mutex>
2425
2526namespace mlir ::link {
2627
@@ -37,8 +38,9 @@ enum LinkerFlags {
3738class LinkState {
3839public:
3940 LinkState (ModuleOp dst, mlir::SymbolTableCollection &symbolTableCollection)
40- : mapping(std::make_shared<IRMapping>()), builder(dst.getBodyRegion()),
41- symbolTableCollection (symbolTableCollection), moduleMaps() {}
41+ : mapping(std::make_shared<IRMapping>()), mutex(std::make_shared<std::mutex>()),
42+ builder (dst.getBodyRegion()), symbolTableCollection(symbolTableCollection),
43+ moduleMaps() {}
4244
4345 Operation *clone (Operation *src);
4446 Operation *cloneWithoutRegions (Operation *src);
@@ -49,7 +51,7 @@ class LinkState {
4951
5052 LinkState nest (ModuleOp submod) const ;
5153
52- IRMapping &getMapping ();
54+ std::pair< IRMapping &, std::mutex &> getMapping ();
5355 SymbolTableCollection &getSymbolTableCollection () {
5456 return symbolTableCollection;
5557 }
@@ -63,11 +65,14 @@ class LinkState {
6365private:
6466 // Private constructor used by nest()
6567 LinkState (ModuleOp dst, std::shared_ptr<IRMapping> mapping,
68+ std::shared_ptr<std::mutex> mutex,
6669 SymbolTableCollection &symbolTableCollection)
67- : mapping(std::move(mapping)), builder(dst.getBodyRegion()),
70+ : mapping(std::move(mapping)), mutex(std::move(mutex)),
71+ builder(dst.getBodyRegion()),
6872 symbolTableCollection(symbolTableCollection), moduleMaps() {}
6973
7074 std::shared_ptr<IRMapping> mapping;
75+ std::shared_ptr<std::mutex> mutex;
7176 OpBuilder builder;
7277 SymbolTableCollection &symbolTableCollection;
7378 DenseMap<ModuleOp, SymbolUserMap> moduleMaps;
@@ -95,7 +100,7 @@ class LinkerInterface : public DialectInterface::Base<ConcreteType> {
95100 virtual LogicalResult finalize (ModuleOp dst) const { return success (); }
96101
97102 // / Link operations from current summary using state builder
98- virtual LogicalResult link (LinkState &state) const = 0;
103+ virtual LogicalResult link (LinkState &state) = 0;
99104};
100105
101106// ===----------------------------------------------------------------------===//
@@ -140,7 +145,7 @@ class SymbolLinkerInterface : public LinkerInterface<SymbolLinkerInterface> {
140145 virtual void registerForLink (Operation *op, SymbolTableCollection &collection) = 0;
141146
142147 // / Materialize new operation for the given conflict src operation.
143- virtual Operation *materialize (Operation *src, LinkState &state) const {
148+ virtual Operation *materialize (Operation *src, LinkState &state) {
144149 return state.clone (src);
145150 }
146151
@@ -178,7 +183,7 @@ class SymbolAttrLinkerInterface : public SymbolLinkerInterface {
178183 using SymbolLinkerInterface::SymbolLinkerInterface;
179184
180185 // / Link operations from current summary using state builder
181- LogicalResult link (LinkState &state) const override ;
186+ LogicalResult link (LinkState &state) override ;
182187
183188 // / Returns the symbol for the given operation.
184189 StringRef getSymbol (Operation *op) const override ;
@@ -211,6 +216,9 @@ class SymbolAttrLinkerInterface : public SymbolLinkerInterface {
211216
212217 // Operations that are to be linked with unique names.
213218 SetVector<Operation *> uniqued;
219+
220+ // Mutex to protect summary and uniqued during parallel summarization.
221+ mutable std::mutex summaryMutex;
214222};
215223
216224// ===----------------------------------------------------------------------===//
0 commit comments