@@ -188,8 +188,9 @@ static void getEffectedValues(Operation *op, SmallVectorImpl<Value> &values) {
188
188
189
189
// / Add `op` to MDG creating a new node and adding its memory accesses (affine
190
190
// / or non-affine to memrefAccesses (memref -> list of nodes with accesses) map.
191
- Node *addNodeToMDG (Operation *nodeOp, MemRefDependenceGraph &mdg,
192
- DenseMap<Value, SetVector<unsigned >> &memrefAccesses) {
191
+ static Node *
192
+ addNodeToMDG (Operation *nodeOp, MemRefDependenceGraph &mdg,
193
+ DenseMap<Value, SetVector<unsigned >> &memrefAccesses) {
193
194
auto &nodes = mdg.nodes ;
194
195
// Create graph node 'id' to represent top-level 'forOp' and record
195
196
// all loads and store accesses it contains.
@@ -359,14 +360,14 @@ bool MemRefDependenceGraph::init() {
359
360
}
360
361
361
362
// Returns the graph node for 'id'.
362
- Node *MemRefDependenceGraph::getNode (unsigned id) {
363
+ const Node *MemRefDependenceGraph::getNode (unsigned id) const {
363
364
auto it = nodes.find (id);
364
365
assert (it != nodes.end ());
365
366
return &it->second ;
366
367
}
367
368
368
369
// Returns the graph node for 'forOp'.
369
- Node *MemRefDependenceGraph::getForOpNode (AffineForOp forOp) {
370
+ const Node *MemRefDependenceGraph::getForOpNode (AffineForOp forOp) const {
370
371
for (auto &idAndNode : nodes)
371
372
if (idAndNode.second .op == forOp)
372
373
return &idAndNode.second ;
@@ -390,7 +391,7 @@ void MemRefDependenceGraph::removeNode(unsigned id) {
390
391
}
391
392
}
392
393
// Remove each edge in 'outEdges[id]'.
393
- if (outEdges.count (id) > 0 ) {
394
+ if (outEdges.contains (id)) {
394
395
SmallVector<Edge, 2 > oldOutEdges = outEdges[id];
395
396
for (auto &outEdge : oldOutEdges) {
396
397
removeEdge (id, outEdge.id , outEdge.value );
@@ -404,8 +405,8 @@ void MemRefDependenceGraph::removeNode(unsigned id) {
404
405
405
406
// Returns true if node 'id' writes to any memref which escapes (or is an
406
407
// argument to) the block. Returns false otherwise.
407
- bool MemRefDependenceGraph::writesToLiveInOrEscapingMemrefs (unsigned id) {
408
- Node *node = getNode (id);
408
+ bool MemRefDependenceGraph::writesToLiveInOrEscapingMemrefs (unsigned id) const {
409
+ const Node *node = getNode (id);
409
410
for (auto *storeOpInst : node->stores ) {
410
411
auto memref = cast<AffineWriteOpInterface>(storeOpInst).getMemRef ();
411
412
auto *op = memref.getDefiningOp ();
@@ -425,14 +426,14 @@ bool MemRefDependenceGraph::writesToLiveInOrEscapingMemrefs(unsigned id) {
425
426
// is for 'value' if non-null, or for any value otherwise. Returns false
426
427
// otherwise.
427
428
bool MemRefDependenceGraph::hasEdge (unsigned srcId, unsigned dstId,
428
- Value value) {
429
- if (outEdges.count (srcId) == 0 || inEdges.count (dstId) == 0 ) {
429
+ Value value) const {
430
+ if (! outEdges.contains (srcId) || ! inEdges.contains (dstId)) {
430
431
return false ;
431
432
}
432
- bool hasOutEdge = llvm::any_of (outEdges[ srcId] , [=](Edge &edge) {
433
+ bool hasOutEdge = llvm::any_of (outEdges. lookup ( srcId) , [=](const Edge &edge) {
433
434
return edge.id == dstId && (!value || edge.value == value);
434
435
});
435
- bool hasInEdge = llvm::any_of (inEdges[ dstId] , [=](Edge &edge) {
436
+ bool hasInEdge = llvm::any_of (inEdges. lookup ( dstId) , [=](const Edge &edge) {
436
437
return edge.id == srcId && (!value || edge.value == value);
437
438
});
438
439
return hasOutEdge && hasInEdge;
@@ -477,7 +478,8 @@ void MemRefDependenceGraph::removeEdge(unsigned srcId, unsigned dstId,
477
478
// Returns true if there is a path in the dependence graph from node 'srcId'
478
479
// to node 'dstId'. Returns false otherwise. `srcId`, `dstId`, and the
479
480
// operations that the edges connected are expected to be from the same block.
480
- bool MemRefDependenceGraph::hasDependencePath (unsigned srcId, unsigned dstId) {
481
+ bool MemRefDependenceGraph::hasDependencePath (unsigned srcId,
482
+ unsigned dstId) const {
481
483
// Worklist state is: <node-id, next-output-edge-index-to-visit>
482
484
SmallVector<std::pair<unsigned , unsigned >, 4 > worklist;
483
485
worklist.push_back ({srcId, 0 });
@@ -490,13 +492,13 @@ bool MemRefDependenceGraph::hasDependencePath(unsigned srcId, unsigned dstId) {
490
492
return true ;
491
493
// Pop and continue if node has no out edges, or if all out edges have
492
494
// already been visited.
493
- if (outEdges.count (idAndIndex.first ) == 0 ||
494
- idAndIndex.second == outEdges[ idAndIndex.first ] .size ()) {
495
+ if (! outEdges.contains (idAndIndex.first ) ||
496
+ idAndIndex.second == outEdges. lookup ( idAndIndex.first ) .size ()) {
495
497
worklist.pop_back ();
496
498
continue ;
497
499
}
498
500
// Get graph edge to traverse.
499
- Edge edge = outEdges[ idAndIndex.first ] [idAndIndex.second ];
501
+ const Edge edge = outEdges. lookup ( idAndIndex.first ) [idAndIndex.second ];
500
502
// Increment next output edge index for 'idAndIndex'.
501
503
++idAndIndex.second ;
502
504
// Add node at 'edge.id' to the worklist. We don't need to consider
@@ -512,34 +514,34 @@ bool MemRefDependenceGraph::hasDependencePath(unsigned srcId, unsigned dstId) {
512
514
// Returns the input edge count for node 'id' and 'memref' from src nodes
513
515
// which access 'memref' with a store operation.
514
516
unsigned MemRefDependenceGraph::getIncomingMemRefAccesses (unsigned id,
515
- Value memref) {
517
+ Value memref) const {
516
518
unsigned inEdgeCount = 0 ;
517
- if ( inEdges.count (id) > 0 )
518
- for ( auto & inEdge : inEdges[id])
519
- if (inEdge. value == memref) {
520
- Node * srcNode = getNode (inEdge. id );
521
- // Only count in edges from 'srcNode' if ' srcNode' accesses 'memref'
522
- if (srcNode-> getStoreOpCount (memref) > 0 )
523
- ++inEdgeCount;
524
- }
519
+ for ( const Edge &inEdge : inEdges.lookup (id)) {
520
+ if ( inEdge. value == memref) {
521
+ const Node *srcNode = getNode (inEdge. id );
522
+ // Only count in edges from ' srcNode' if 'srcNode' accesses 'memref'
523
+ if ( srcNode-> getStoreOpCount (memref) > 0 )
524
+ ++inEdgeCount;
525
+ }
526
+ }
525
527
return inEdgeCount;
526
528
}
527
529
528
530
// Returns the output edge count for node 'id' and 'memref' (if non-null),
529
531
// otherwise returns the total output edge count from node 'id'.
530
- unsigned MemRefDependenceGraph::getOutEdgeCount (unsigned id, Value memref) {
532
+ unsigned MemRefDependenceGraph::getOutEdgeCount (unsigned id,
533
+ Value memref) const {
531
534
unsigned outEdgeCount = 0 ;
532
- if (outEdges.count (id) > 0 )
533
- for (auto &outEdge : outEdges[id])
534
- if (!memref || outEdge.value == memref)
535
- ++outEdgeCount;
535
+ for (const auto &outEdge : outEdges.lookup (id))
536
+ if (!memref || outEdge.value == memref)
537
+ ++outEdgeCount;
536
538
return outEdgeCount;
537
539
}
538
540
539
541
// / Return all nodes which define SSA values used in node 'id'.
540
542
void MemRefDependenceGraph::gatherDefiningNodes (
541
- unsigned id, DenseSet<unsigned > &definingNodes) {
542
- for (MemRefDependenceGraph:: Edge edge : inEdges[id] )
543
+ unsigned id, DenseSet<unsigned > &definingNodes) const {
544
+ for (const Edge & edge : inEdges. lookup (id) )
543
545
// By definition of edge, if the edge value is a non-memref value,
544
546
// then the dependence is between a graph node which defines an SSA value
545
547
// and another graph node which uses the SSA value.
@@ -552,8 +554,8 @@ void MemRefDependenceGraph::gatherDefiningNodes(
552
554
// dependences. Returns nullptr if no such insertion point is found.
553
555
Operation *
554
556
MemRefDependenceGraph::getFusedLoopNestInsertionPoint (unsigned srcId,
555
- unsigned dstId) {
556
- if (outEdges.count (srcId) == 0 )
557
+ unsigned dstId) const {
558
+ if (! outEdges.contains (srcId))
557
559
return getNode (dstId)->op ;
558
560
559
561
// Skip if there is any defining node of 'dstId' that depends on 'srcId'.
@@ -569,13 +571,13 @@ MemRefDependenceGraph::getFusedLoopNestInsertionPoint(unsigned srcId,
569
571
570
572
// Build set of insts in range (srcId, dstId) which depend on 'srcId'.
571
573
SmallPtrSet<Operation *, 2 > srcDepInsts;
572
- for (auto &outEdge : outEdges[ srcId] )
574
+ for (auto &outEdge : outEdges. lookup ( srcId) )
573
575
if (outEdge.id != dstId)
574
576
srcDepInsts.insert (getNode (outEdge.id )->op );
575
577
576
578
// Build set of insts in range (srcId, dstId) on which 'dstId' depends.
577
579
SmallPtrSet<Operation *, 2 > dstDepInsts;
578
- for (auto &inEdge : inEdges[ dstId] )
580
+ for (auto &inEdge : inEdges. lookup ( dstId) )
579
581
if (inEdge.id != srcId)
580
582
dstDepInsts.insert (getNode (inEdge.id )->op );
581
583
@@ -635,7 +637,7 @@ void MemRefDependenceGraph::updateEdges(unsigned srcId, unsigned dstId,
635
637
SmallVector<Edge, 2 > oldInEdges = inEdges[srcId];
636
638
for (auto &inEdge : oldInEdges) {
637
639
// Add edge from 'inEdge.id' to 'dstId' if it's not a private memref.
638
- if (privateMemRefs.count (inEdge.value ) == 0 )
640
+ if (! privateMemRefs.contains (inEdge.value ))
639
641
addEdge (inEdge.id , dstId, inEdge.value );
640
642
}
641
643
}
0 commit comments