Skip to content

Commit 9be3921

Browse files
committed
Templates to the rescue
1 parent 55fe7eb commit 9be3921

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

mlir/lib/Dialect/ArmSME/Transforms/TileAllocation.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -502,16 +502,11 @@ coalesceTileLiveRanges(DenseMap<Value, LiveRange> &initialLiveRanges) {
502502
}
503503

504504
/// Choose a live range to spill (via some heuristics). This picks either a live
505-
/// range from `activeRanges`, `inactiveRanges`, or the new live range
506-
/// `newRange`. Note: All live ranges in `activeRanges` and `inactiveRanges` are
507-
/// assumed to overlap with `newRange`.
508-
LiveRange *chooseSpillUsingHeuristics(ArrayRef<LiveRange *> activeRanges,
509-
ArrayRef<LiveRange *> inactiveRanges,
510-
LiveRange *newRange) {
511-
auto allOverlappingRanges =
512-
llvm::concat<LiveRange>(llvm::make_pointee_range(activeRanges),
513-
llvm::make_pointee_range(inactiveRanges));
514-
505+
/// range from `overlappingRanges`, or the new live range `newRange`.
506+
template <typename OverlappingRangesIterator>
507+
LiveRange *
508+
chooseSpillUsingHeuristics(OverlappingRangesIterator overlappingRanges,
509+
LiveRange *newRange) {
515510
// Heuristic: Spill trivially copyable operations (usually free).
516511
auto isTrivialSpill = [&](LiveRange &allocatedRange) {
517512
return isTileTypeGreaterOrEqual(allocatedRange.getTileType(),
@@ -522,18 +517,18 @@ LiveRange *chooseSpillUsingHeuristics(ArrayRef<LiveRange *> activeRanges,
522517
};
523518
if (isTrivialSpill(*newRange))
524519
return newRange;
525-
auto trivialSpill = llvm::find_if(allOverlappingRanges, isTrivialSpill);
526-
if (trivialSpill != allOverlappingRanges.end())
520+
auto trivialSpill = llvm::find_if(overlappingRanges, isTrivialSpill);
521+
if (trivialSpill != overlappingRanges.end())
527522
return &*trivialSpill;
528523

529524
// Heuristic: Spill the range that ends last (with a compatible tile type).
530525
auto isSmallerTileTypeOrEndsEarlier = [](LiveRange &a, LiveRange &b) {
531526
return !isTileTypeGreaterOrEqual(a.getTileType(), b.getTileType()) ||
532527
a.end() < b.end();
533528
};
534-
LiveRange &latestEndingLiveRange = *std::max_element(
535-
allOverlappingRanges.begin(), allOverlappingRanges.end(),
536-
isSmallerTileTypeOrEndsEarlier);
529+
LiveRange &latestEndingLiveRange =
530+
*std::max_element(overlappingRanges.begin(), overlappingRanges.end(),
531+
isSmallerTileTypeOrEndsEarlier);
537532
if (!isSmallerTileTypeOrEndsEarlier(latestEndingLiveRange, *newRange))
538533
return &latestEndingLiveRange;
539534
return newRange;
@@ -604,8 +599,13 @@ void allocateTilesToLiveRanges(
604599
if (succeeded(tileId)) {
605600
nextRange->tileId = *tileId;
606601
} else {
607-
LiveRange *rangeToSpill = chooseSpillUsingHeuristics(
608-
activeRanges.getArrayRef(), overlappingInactiveRanges, nextRange);
602+
// Create an iterator over all overlapping live ranges.
603+
auto allOverlappingRanges = llvm::concat<LiveRange>(
604+
llvm::make_pointee_range(activeRanges.getArrayRef()),
605+
llvm::make_pointee_range(overlappingInactiveRanges));
606+
// Choose an overlapping live range to spill.
607+
LiveRange *rangeToSpill =
608+
chooseSpillUsingHeuristics(allOverlappingRanges, nextRange);
609609
if (rangeToSpill != nextRange) {
610610
// Spill an (in)active live range (so release its tile ID first).
611611
tileAllocator.releaseTileId(rangeToSpill->getTileType(),

0 commit comments

Comments
 (0)