Skip to content

Commit d98c0d3

Browse files
committed
improve program folding
1 parent 0699730 commit d98c0d3

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ To install or update LODA, please follow the [installation instructions](https:/
44

55
### Enhancements
66

7-
* Improve internal maintenance command
7+
* Improve program unfolding and maintenance
88

99
## v24.12.16
1010

src/cmd/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void Test::unfold() {
752752
for (const auto& t : tests) {
753753
Log::get().info("Testing unfold " + std::to_string(i));
754754
auto p = t.first;
755-
if (!Subprogram::unfold(p)) {
755+
if (!Subprogram::autoUnfold(p)) {
756756
Log::get().error("Unfolding not supported", true);
757757
}
758758
if (p != t.second) {

src/lang/subprogram.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,25 @@ bool prepareEmbedding(int64_t id, Program &sub, Operation::Type embeddingType) {
153153
return true;
154154
}
155155

156-
bool Subprogram::unfold(Program &main) {
156+
bool Subprogram::canUnfold(Operation::Type type) {
157+
return type == Operation::Type::SEQ || type == Operation::Type::PRG;
158+
}
159+
160+
bool Subprogram::unfold(Program &main, int64_t pos) {
157161
if (ProgramUtil::hasIndirectOperand(main)) {
158162
return false;
159163
}
160-
// find first seq or prg operation
161-
int64_t pos = -1;
162-
for (size_t i = 0; i < main.ops.size(); i++) {
163-
if (main.ops[i].type == Operation::Type::SEQ ||
164-
main.ops[i].type == Operation::Type::PRG) {
165-
pos = i;
166-
break;
164+
if (pos < 0) {
165+
// find first operation that can be unfolded
166+
for (size_t i = 0; i < main.ops.size(); i++) {
167+
if (canUnfold(main.ops[i].type)) {
168+
pos = i;
169+
break;
170+
}
167171
}
168172
}
169-
if (pos < 0) {
173+
if (pos < 0 || static_cast<size_t>(pos) >= main.ops.size() ||
174+
!canUnfold(main.ops[pos].type)) {
170175
return false;
171176
}
172177
const auto &emb_op = main.ops[pos];
@@ -187,7 +192,7 @@ bool Subprogram::unfold(Program &main) {
187192
updateOperand(op.target, start, shared_region_length, largest_used);
188193
updateOperand(op.source, start, shared_region_length, largest_used);
189194
}
190-
// delete seq operation
195+
// delete old operation
191196
main.ops.erase(main.ops.begin() + pos);
192197
// embed program
193198
main.ops.insert(main.ops.begin() + pos, sub.ops.begin(), sub.ops.end());
@@ -197,18 +202,28 @@ bool Subprogram::unfold(Program &main) {
197202
bool Subprogram::autoUnfold(Program &main) {
198203
bool changed = false;
199204
while (true) {
200-
// try to unfold
201205
auto copy = main;
202-
if (!unfold(copy)) {
203-
break;
206+
bool unfolded = false;
207+
for (size_t i = 0; i < copy.ops.size(); i++) {
208+
// try to unfold
209+
if (!unfold(copy, i)) {
210+
continue;
211+
}
212+
// revert if unfolded program is too complex
213+
if (shouldFold(copy)) {
214+
copy = main;
215+
} else {
216+
unfolded = true;
217+
break;
218+
}
204219
}
205-
// abort if unfolded program is too complex
206-
if (shouldFold(copy)) {
220+
// update main program if unfolding was successful
221+
if (unfolded) {
222+
main = copy;
223+
changed = true;
224+
} else {
207225
break;
208226
}
209-
// ok, update program!
210-
main = copy;
211-
changed = true;
212227
}
213228
return changed;
214229
}

src/lang/subprogram.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class Subprogram {
1212
static int64_t search(const Program &main, const Program &sub,
1313
std::map<int64_t, int64_t> &cell_map);
1414

15-
static bool unfold(Program &main);
15+
static bool canUnfold(Operation::Type type);
16+
17+
static bool unfold(Program &main, int64_t pos = -1);
1618

1719
static bool autoUnfold(Program &main);
1820

tests/unfold/U005.asm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; in
2+
mov $1,$0
3+
mul $1,5
4+
seq $1,30
5+
add $1,$0
6+
seq $1,34
7+
div $1,4
8+
; out
9+
mov $1,$0
10+
mul $1,5
11+
mov $2,$1
12+
lpb $2
13+
div $1,10
14+
sub $2,$1
15+
lpe
16+
add $1,$0
17+
mod $1,2
18+
add $1,1
19+
div $1,4

0 commit comments

Comments
 (0)