|
42 | 42 | #include "xls/ir/source_location.h" |
43 | 43 | #include "xls/ir/value.h" |
44 | 44 | #include "xls/scheduling/schedule_graph.h" |
| 45 | +#include "xls/scheduling/scheduling_options.h" |
45 | 46 |
|
46 | 47 | namespace m = xls::op_matchers; |
47 | 48 | namespace xls { |
@@ -502,6 +503,138 @@ TEST_F(ScheduleBoundsTest, PushNodeLaterChain) { |
502 | 503 | EXPECT_THAT(sched.bounds(h.node()), Bounds(7, 7)); |
503 | 504 | } |
504 | 505 |
|
| 506 | +TEST_F(ScheduleBoundsTest, MinDelayBasic) { |
| 507 | + auto p = CreatePackage(); |
| 508 | + FunctionBuilder fb(TestName(), p.get()); |
| 509 | + auto a = fb.Param("a", p->GetBitsType(32)); |
| 510 | + auto b = fb.Param("b", p->GetBitsType(32)); |
| 511 | + fb.Add(a, b); |
| 512 | + auto tkn = fb.Literal(Value::Token()); |
| 513 | + auto min_delay = fb.MinDelay(tkn, /*delay=*/3); |
| 514 | + auto assert = fb.Assert(min_delay, fb.Literal(UBits(1, 1)), "msg"); |
| 515 | + XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); |
| 516 | + |
| 517 | + ControllableDelayEstimator delay; |
| 518 | + delay.SetDelay(m::Add(), 1); |
| 519 | + delay.SetDelay(Op::kMinDelay, 0); |
| 520 | + delay.SetDelay(Op::kAssert, 0); |
| 521 | + delay.SetDelay(FreeOperations(), 0); |
| 522 | + |
| 523 | + XLS_ASSERT_OK_AND_ASSIGN( |
| 524 | + ScheduleBounds sched, |
| 525 | + ScheduleBounds::ComputeAsapAndAlapBounds(f, |
| 526 | + /*clock_period_ps=*/3, delay)); |
| 527 | + EXPECT_THAT(sched.bounds(min_delay.node()), Bounds(3, 3)); |
| 528 | + EXPECT_THAT(sched.bounds(assert.node()), Bounds(3, 3)); |
| 529 | +} |
| 530 | + |
| 531 | +TEST_F(ScheduleBoundsTest, MinDelayChain) { |
| 532 | + auto p = CreatePackage(); |
| 533 | + FunctionBuilder fb(TestName(), p.get()); |
| 534 | + auto tkn = fb.Literal(Value::Token()); |
| 535 | + auto md1 = fb.MinDelay(tkn, /*delay=*/2); |
| 536 | + auto md2 = fb.MinDelay(md1, /*delay=*/3); |
| 537 | + fb.Assert(md2, fb.Literal(UBits(1, 1)), "msg"); |
| 538 | + XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); |
| 539 | + |
| 540 | + ControllableDelayEstimator delay; |
| 541 | + delay.SetDelay(Op::kMinDelay, 0); |
| 542 | + delay.SetDelay(Op::kAssert, 0); |
| 543 | + delay.SetDelay(FreeOperations(), 0); |
| 544 | + |
| 545 | + XLS_ASSERT_OK_AND_ASSIGN( |
| 546 | + ScheduleBounds sched, |
| 547 | + ScheduleBounds::ComputeAsapAndAlapBounds(f, |
| 548 | + /*clock_period_ps=*/3, delay)); |
| 549 | + EXPECT_THAT(sched.bounds(md1.node()), Bounds(2, 2)); |
| 550 | + EXPECT_THAT(sched.bounds(md2.node()), Bounds(5, 5)); |
| 551 | +} |
| 552 | + |
| 553 | +TEST_F(ScheduleBoundsTest, MinDelayAndIIInteraction) { |
| 554 | + auto p = CreatePackage(); |
| 555 | + ProcBuilder pb(NewStyleProc(), TestName(), p.get()); |
| 556 | + XLS_ASSERT_OK_AND_ASSIGN(ReceiveChannelInterface * ch_in, |
| 557 | + pb.AddInputChannel("in", p->GetBitsType(32))); |
| 558 | + XLS_ASSERT_OK_AND_ASSIGN(SendChannelInterface * ch_out, |
| 559 | + pb.AddOutputChannel("out", p->GetBitsType(32))); |
| 560 | + |
| 561 | + BValue state = pb.StateElement("st", Value(UBits(0, 32))); |
| 562 | + BValue tkn = pb.Literal(Value::Token()); |
| 563 | + BValue rcv = pb.Receive(ch_in, tkn); |
| 564 | + BValue rcv_tkn = pb.TupleIndex(rcv, 0); |
| 565 | + BValue rcv_data = pb.TupleIndex(rcv, 1); |
| 566 | + BValue md = pb.MinDelay(rcv_tkn, /*delay=*/3); |
| 567 | + BValue send = pb.Send(ch_out, md, rcv_data); |
| 568 | + // Make the next state depend on send token so backedge constraint covers the |
| 569 | + // whole chain. |
| 570 | + BValue next_state = pb.Add(state, pb.TupleIndex(pb.Receive(ch_in, send), 1)); |
| 571 | + pb.Next(state, next_state); |
| 572 | + XLS_ASSERT_OK_AND_ASSIGN(Proc * proc, pb.Build()); |
| 573 | + |
| 574 | + ControllableDelayEstimator delay; |
| 575 | + delay.SetDelay(m::Add(), 1); |
| 576 | + delay.SetDelay(Op::kMinDelay, 0); |
| 577 | + delay.SetDelay(FreeOperations(), 0); |
| 578 | + |
| 579 | + // Set II = 4. |
| 580 | + // Backedge constraint: Next - state <= II - 1 = 3. |
| 581 | + // MinDelay constraint: send >= md >= rcv_tkn + 3. |
| 582 | + // Since rcv_tkn is derived from tkn (state read), this forces send to be |
| 583 | + // scheduled at least 3 cycles after the receive. |
| 584 | + XLS_ASSERT_OK_AND_ASSIGN( |
| 585 | + ScheduleBounds sched, |
| 586 | + ScheduleBounds::ComputeAsapAndAlapBounds( |
| 587 | + proc, |
| 588 | + /*clock_period_ps=*/3, delay, /*ii=*/4, {BackedgeConstraint()})); |
| 589 | + EXPECT_THAT(sched.bounds(md.node()), Bounds(3, 3)); |
| 590 | + EXPECT_THAT(sched.bounds(send.node()), Bounds(3, 3)); |
| 591 | +} |
| 592 | + |
| 593 | +TEST_F(ScheduleBoundsTest, MinDelayAndIOConstraintConflict) { |
| 594 | + auto p = CreatePackage(); |
| 595 | + ProcBuilder pb(NewStyleProc(), TestName(), p.get()); |
| 596 | + XLS_ASSERT_OK_AND_ASSIGN(ReceiveChannelInterface * ch_a, |
| 597 | + pb.AddInputChannel("a", p->GetBitsType(32))); |
| 598 | + XLS_ASSERT_OK_AND_ASSIGN(SendChannelInterface * ch_b, |
| 599 | + pb.AddOutputChannel("b", p->GetBitsType(32))); |
| 600 | + XLS_ASSERT_OK_AND_ASSIGN(SendChannelInterface * ch_c, |
| 601 | + pb.AddOutputChannel("c", p->GetBitsType(32))); |
| 602 | + |
| 603 | + BValue tkn = pb.Literal(Value::Token()); |
| 604 | + BValue rcv_a = pb.Receive(ch_a, tkn); |
| 605 | + BValue rcv_a_tkn = pb.TupleIndex(rcv_a, 0); |
| 606 | + BValue rcv_a_data = pb.TupleIndex(rcv_a, 1); |
| 607 | + |
| 608 | + BValue md1 = pb.MinDelay(rcv_a_tkn, /*delay=*/2); |
| 609 | + BValue send_b = pb.Send(ch_b, md1, rcv_a_data); |
| 610 | + |
| 611 | + BValue md2 = pb.MinDelay(send_b, /*delay=*/2); |
| 612 | + pb.Send(ch_c, md2, rcv_a_data); |
| 613 | + |
| 614 | + XLS_ASSERT_OK_AND_ASSIGN(Proc * proc, pb.Build()); |
| 615 | + |
| 616 | + ControllableDelayEstimator delay; |
| 617 | + delay.SetDelay(Op::kMinDelay, 0); |
| 618 | + delay.SetDelay(FreeOperations(), 0); |
| 619 | + |
| 620 | + // IO constraint says send_c (Channel "c") must be at most 3 cycles after |
| 621 | + // rcv_a (Channel "a"). MinDelay requires: |
| 622 | + // send_b >= rcv_a + 2 |
| 623 | + // send_c >= send_b + 2 |
| 624 | + // So send_c >= rcv_a + 4. |
| 625 | + // This conflicts with maximum_latency = 3. |
| 626 | + IOConstraint io_const("a", IODirection::kReceive, "c", IODirection::kSend, |
| 627 | + /*minimum_latency=*/0, /*maximum_latency=*/3); |
| 628 | + |
| 629 | + EXPECT_THAT( |
| 630 | + ScheduleBounds::ComputeAsapAndAlapBounds(proc, |
| 631 | + /*clock_period_ps=*/3, delay, |
| 632 | + /*ii=*/std::nullopt, {io_const}), |
| 633 | + StatusIs(absl::StatusCode::kInternal, |
| 634 | + ContainsRegex(".*failed to converge.*|.*potentially " |
| 635 | + "incompatible with constraint.*"))); |
| 636 | +} |
| 637 | + |
505 | 638 | TEST_F(ScheduleBoundsTest, StringifyConstraints) { |
506 | 639 | auto p = CreatePackage(); |
507 | 640 | FunctionBuilder fb(TestName(), p.get()); |
|
0 commit comments