[llvm] 7e09239 - [CodeGen][MISched] Handle empty sized resource usage. (#75951)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 10:40:27 PST 2024


Author: Michael Maitland
Date: 2024-01-24T13:40:23-05:00
New Revision: 7e09239e24b339f45f63a670e2e831150826bf70

URL: https://github.com/llvm/llvm-project/commit/7e09239e24b339f45f63a670e2e831150826bf70
DIFF: https://github.com/llvm/llvm-project/commit/7e09239e24b339f45f63a670e2e831150826bf70.diff

LOG: [CodeGen][MISched] Handle empty sized resource usage. (#75951)

TargetSchedule.td explicitly allows the usage of a ProcResource for zero
cycles, in order to represent that the ProcResource must be available
but is not consumed by the instruction. On the other hand,
ResourceSegments explicitly does not allow for a zero sized interval. In
order to remedy this, this patch handles the special case of when there
is an empty interval usage of a resource by not adding an empty
interval.

We ran into this issue downstream, but it makes sense to have
this upstream since it is explicitly allowed by TargetSchedule.td.

Added: 
    

Modified: 
    llvm/lib/CodeGen/MachineScheduler.cpp
    llvm/unittests/CodeGen/SchedBoundary.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index f40e91819a48f1..f6d03f59f85647 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -4275,6 +4275,12 @@ unsigned ResourceSegments::getFirstAvailableAt(
   assert(std::is_sorted(std::begin(_Intervals), std::end(_Intervals),
                         sortIntervals) &&
          "Cannot execute on an un-sorted set of intervals.");
+
+  // Zero resource usage is allowed by TargetSchedule.td but we do not construct
+  // a ResourceSegment interval for that situation.
+  if (AcquireAtCycle == Cycle)
+    return CurrCycle;
+
   unsigned RetCycle = CurrCycle;
   ResourceSegments::IntervalTy NewInterval =
       IntervalBuilder(RetCycle, AcquireAtCycle, ReleaseAtCycle);
@@ -4294,8 +4300,16 @@ unsigned ResourceSegments::getFirstAvailableAt(
 
 void ResourceSegments::add(ResourceSegments::IntervalTy A,
                            const unsigned CutOff) {
-  assert(A.first < A.second && "Cannot add empty resource usage");
+  assert(A.first <= A.second && "Cannot add negative resource usage");
   assert(CutOff > 0 && "0-size interval history has no use.");
+  // Zero resource usage is allowed by TargetSchedule.td, in the case that the
+  // instruction needed the resource to be available but does not use it.
+  // However, ResourceSegment represents an interval that is closed on the left
+  // and open on the right. It is impossible to represent an empty interval when
+  // the left is closed. Do not add it to Intervals.
+  if (A.first == A.second)
+    return;
+
   assert(all_of(_Intervals,
                 [&A](const ResourceSegments::IntervalTy &Interval) -> bool {
                   return !intersects(A, Interval);

diff  --git a/llvm/unittests/CodeGen/SchedBoundary.cpp b/llvm/unittests/CodeGen/SchedBoundary.cpp
index 5ca6543f8e814b..dbcb2e024ebf64 100644
--- a/llvm/unittests/CodeGen/SchedBoundary.cpp
+++ b/llvm/unittests/CodeGen/SchedBoundary.cpp
@@ -85,7 +85,8 @@ TEST(ResourceSegments, add_02) {
 #ifndef NDEBUG
 TEST(ResourceSegmentsDeath, add_empty) {
   auto X = ResourceSegments({{10, 20}, {30, 40}});
-  EXPECT_DEATH(X.add({22, 22}), "Cannot add empty resource usage");
+  X.add({22, 22});
+  EXPECT_EQ(X, ResourceSegments({{10, 20}, {30, 40}}));
 }
 #endif
 


        


More information about the llvm-commits mailing list