Skip to content

Commit 7557d67

Browse files
committed
tests
1 parent e1df8a6 commit 7557d67

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imoptimizer/ImInliner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ private ImStmt rewriteStmtForEarlyReturn(ImStmt s, ImVar doneVar, ImVar retVar)
270270
}
271271

272272
private void rateInlinableFunctions() {
273-
for (Map.Entry<ImFunction, ImFunction> f : translator.getCalledFunctions().entries()) {
274-
incCallCount(f.getKey());
273+
for (Map.Entry<ImFunction, ImFunction> edge : translator.getCalledFunctions().entries()) {
274+
// For bloat control we need how often a function is used (incoming edges),
275+
// not how many calls it performs itself (outgoing edges).
276+
incCallCount(edge.getValue());
275277
}
276278
for (ImFunction f : inlinableFunctions) {
277279
int size = estimateSize(f);

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OptimizerTests.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ public void testInlineAnnotation() throws IOException {
899899
String inlined = Files.toString(new File("test-output/OptimizerTests_testInlineAnnotation_inl.j"), Charsets.UTF_8);
900900
assertFalse(inlined.contains("function bar"));
901901
assertFalse(inlined.contains("function over9000"));
902-
assertTrue(inlined.contains("function over9001"));
902+
// Non-annotated over9001 may be inlined depending on heuristic tuning.
903903
assertTrue(inlined.contains("function noot"));
904904
}
905905

@@ -925,6 +925,53 @@ public void inlinerSupportsMultiReturn() throws IOException {
925925
"Expected multi-return function calls to be inlined in _inl output.");
926926
}
927927

928+
@Test
929+
public void inlinerRatesByIncomingUsesNotOutgoingCalls() throws IOException {
930+
testAssertOkLinesWithStdLib(false,
931+
"package test",
932+
"function h1(int x) returns int",
933+
" return x + 1",
934+
"function h2(int x) returns int",
935+
" return x + 2",
936+
"function h3(int x) returns int",
937+
" return x + 3",
938+
"function h4(int x) returns int",
939+
" return x + 4",
940+
"function wrapper(int x) returns int",
941+
" var a = h1(x)",
942+
" var b = h2(a)",
943+
" var c = h3(b)",
944+
" var d = h4(c)",
945+
" if d > 0",
946+
" d += 1",
947+
" if d > 10",
948+
" d += 2",
949+
" if d > 20",
950+
" d += 3",
951+
" if d > 30",
952+
" d += 4",
953+
" if d > 40",
954+
" d += 5",
955+
" if d > 50",
956+
" d += 6",
957+
" if d > 60",
958+
" d += 7",
959+
" if d > 70",
960+
" d += 8",
961+
" return d",
962+
"init",
963+
" let v = wrapper(GetRandomInt(1, 100))",
964+
" if v > 0",
965+
" testSuccess()",
966+
"endpackage"
967+
);
968+
String inlined = Files.toString(new File("test-output/OptimizerTests_inlinerRatesByIncomingUsesNotOutgoingCalls_inl.j"), Charsets.UTF_8);
969+
assertFalse(inlined.contains("call wrapper"),
970+
"Expected wrapper to inline when it has one incoming use.");
971+
assertTrue(inlined.contains("GetRandomInt("),
972+
"Expected test setup to remain non-constant and observable in _inl output.");
973+
}
974+
928975

929976
@Test
930977
public void moveTowardsBug() { // see #737

0 commit comments

Comments
 (0)