Skip to content

Commit fefa490

Browse files
committed
Change Pass::Run to take Program by value
Program is a move-only type, so passing by value enables callers to transfer ownership via std::move and allows passes to return early without cloning (e.g. the null-topology fast path just returns the input directly). Update PassManager to std::move programs between passes and adjust test fixtures to use factory lambdas for repeated program creation.
1 parent 80a1fa0 commit fefa490

5 files changed

Lines changed: 15 additions & 12 deletions

File tree

csrc/setu/planner/passes/Pass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using setu::planner::hints::HintStore;
3131
class Pass : public setu::commons::NonCopyableNonMovable {
3232
public:
3333
virtual ~Pass() = default;
34-
[[nodiscard]] virtual cir::Program Run(const cir::Program& program,
34+
[[nodiscard]] virtual cir::Program Run(cir::Program program,
3535
const HintStore& hints) = 0;
3636
[[nodiscard]] virtual std::string Name() const = 0;
3737
};

csrc/setu/planner/passes/PassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void PassManager::AddPass(PassPtr pass) {
2828
cir::Program PassManager::Run(cir::Program program,
2929
const HintStore& hints) const {
3030
for (const auto& pass : passes_) {
31-
program = pass->Run(program, hints);
31+
program = pass->Run(std::move(program), hints);
3232
LOG_DEBUG("After pass '{}': {}", pass->Name(), program.Dump());
3333
}
3434
return program;

csrc/setu/planner/passes/ShortestPathRouting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using setu::planner::hints::RoutingHint;
1010
using setu::planner::topo::Link;
1111
using setu::planner::topo::Path;
1212

13-
cir::Program ShortestPathRouting::Run(const cir::Program& program,
13+
cir::Program ShortestPathRouting::Run(cir::Program program,
1414
const HintStore& hints) {
1515
// Calculate override map from routing hints
1616
std::map<std::pair<Participant, Participant>,

csrc/setu/planner/passes/ShortestPathRouting.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using setu::planner::topo::TopologyPtr;
2929
class ShortestPathRouting : public Pass {
3030
public:
3131
explicit ShortestPathRouting(TopologyPtr topo) : topo_(std::move(topo)) {}
32-
[[nodiscard]] cir::Program Run(const cir::Program& program,
32+
[[nodiscard]] cir::Program Run(cir::Program program,
3333
const HintStore& hints) override;
3434
[[nodiscard]] std::string Name() const override {
3535
return "ShortestPathRouting";

csrc/test/native/cir/ShortestPathRoutingTest.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ TEST_F(CIRShortestPathRoutingTest, Scratch) {
9595

9696
ShortestPathRouting pass(topo);
9797
HintStore hints;
98-
auto program_t = pass.Run(program, hints);
98+
auto program_t = pass.Run(std::move(program), hints);
9999

100100
std::cout << "AFTER" << std::endl;
101101
std::cout << program_t.Dump() << std::endl;
@@ -104,7 +104,6 @@ TEST_F(CIRShortestPathRoutingTest, Scratch) {
104104
}
105105

106106
TEST_F(CIRShortestPathRoutingTest, RoutingHintOverridesPath) {
107-
Program program;
108107
auto dev0 = MakeDevice(n0, 0);
109108
auto dev1 = MakeDevice(n0, 1);
110109
auto dev2 = MakeDevice(n1, 0);
@@ -114,10 +113,14 @@ TEST_F(CIRShortestPathRoutingTest, RoutingHintOverridesPath) {
114113
topo->AddBidirectionalLink(dev0, dev2, Link(10, 100));
115114
topo->AddBidirectionalLink(dev1, dev2, Link(10, 50));
116115

117-
// Direct path dev0->dev2 exists, but provide a hint to route via dev1
118-
auto v0 = program.EmitView(dev0, MakeEmptyShardRef(), Slice{0, 256}, dt);
119-
auto v2 = program.EmitView(dev2, MakeEmptyShardRef(), Slice{0, 256}, dt);
120-
(void)program.EmitCopy(v0, v2);
116+
// Helper — builds the same one-copy program each time (Program is move-only)
117+
auto make_program = [&]() {
118+
Program p;
119+
auto v0 = p.EmitView(dev0, MakeEmptyShardRef(), Slice{0, 256}, dt);
120+
auto v2 = p.EmitView(dev2, MakeEmptyShardRef(), Slice{0, 256}, dt);
121+
(void)p.EmitCopy(v0, v2);
122+
return p;
123+
};
121124

122125
// Create a routing hint: dev0 -> dev1 -> dev2
123126
Path forced_path({Participant(n0, dev0.device), Participant(n0, dev1.device),
@@ -132,12 +135,12 @@ TEST_F(CIRShortestPathRoutingTest, RoutingHintOverridesPath) {
132135

133136
// Without hints — should produce direct copy (2 hops, no intermediates)
134137
HintStore empty_hints;
135-
auto program_no_hint = pass.Run(program, empty_hints);
138+
auto program_no_hint = pass.Run(make_program(), empty_hints);
136139
std::cout << "WITHOUT HINT" << std::endl;
137140
std::cout << program_no_hint.Dump() << std::endl;
138141

139142
// With hints — should produce multi-hop copy (3 hops, 1 intermediate)
140-
auto program_with_hint = pass.Run(program, hints);
143+
auto program_with_hint = pass.Run(make_program(), hints);
141144
std::cout << "WITH HINT" << std::endl;
142145
std::cout << program_with_hint.Dump() << std::endl;
143146

0 commit comments

Comments
 (0)