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