-
Notifications
You must be signed in to change notification settings - Fork 28
Reduce temp var creation during flatten step, re-enable rewrite test #1120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Frotty
wants to merge
1
commit into
master
Choose a base branch
from
flatten-improvement
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/FlattenTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package tests.wurstscript.tests; | ||
|
|
||
| import de.peeeq.wurstscript.RunArgs; | ||
| import de.peeeq.wurstscript.ast.Ast; | ||
| import de.peeeq.wurstscript.jassIm.*; | ||
| import de.peeeq.wurstscript.translation.imtranslation.CallType; | ||
| import de.peeeq.wurstscript.translation.imtranslation.Flatten; | ||
| import de.peeeq.wurstscript.translation.imtranslation.Flatten.MultiResult; | ||
| import de.peeeq.wurstscript.translation.imtranslation.ImTranslator; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
||
| import static org.testng.Assert.*; | ||
|
|
||
| public class FlattenTests { | ||
|
|
||
| @Test | ||
| public void avoid_extra_temp_for_var_access() throws Exception { | ||
| ImTranslator translator = new ImTranslator(Ast.WurstModel(), true, new RunArgs()); | ||
| de.peeeq.wurstscript.ast.Element trace = Ast.NoExpr(); | ||
| ImVar sourceVar = JassIm.ImVar(trace, JassIm.ImSimpleType("int"), "source", false); | ||
| ImVar targetVar = JassIm.ImVar(trace, JassIm.ImSimpleType("int"), "target", false); | ||
|
|
||
| ImExpr simpleAccess = JassIm.ImVarAccess(sourceVar); | ||
| ImExpr expressionWithStatements = JassIm.ImStatementExpr( | ||
| JassIm.ImStmts(JassIm.ImSet(trace, JassIm.ImVarAccess(targetVar), JassIm.ImIntVal(1))), | ||
| JassIm.ImVarAccess(targetVar)); | ||
|
|
||
| ImFunction function = JassIm.ImFunction(trace, "test", JassIm.ImTypeVars(), JassIm.ImVars(), JassIm.ImVoid(), | ||
| JassIm.ImVars(sourceVar, targetVar), JassIm.ImStmts(), Collections.emptyList()); | ||
|
|
||
| Method flattenExprs = Flatten.class.getDeclaredMethod("flattenExprs", ImTranslator.class, ImFunction.class, java.util.List.class); | ||
| flattenExprs.setAccessible(true); | ||
|
|
||
| MultiResult result = (MultiResult) flattenExprs.invoke(null, translator, function, Arrays.asList(simpleAccess, expressionWithStatements)); | ||
|
|
||
| assertEquals(function.getLocals().size(), 2, "no extra locals should be introduced"); | ||
| assertSame(simpleAccess, result.expr(0), "the original access should be reused"); | ||
| } | ||
|
|
||
| @Test | ||
| public void add_temp_for_impure_expr_with_followup_statements() throws Exception { | ||
| ImTranslator translator = new ImTranslator(Ast.WurstModel(), true, new RunArgs()); | ||
| de.peeeq.wurstscript.ast.Element trace = Ast.NoExpr(); | ||
| ImVar sourceVar = JassIm.ImVar(trace, JassIm.ImSimpleType("int"), "source", false); | ||
| ImVar targetVar = JassIm.ImVar(trace, JassIm.ImSimpleType("int"), "target", false); | ||
|
|
||
| ImFunction impureCallee = JassIm.ImFunction(trace, "impure", JassIm.ImTypeVars(), JassIm.ImVars(), | ||
| JassIm.ImSimpleType("int"), JassIm.ImVars(), JassIm.ImStmts(), Collections.emptyList()); | ||
| ImExpr impureCall = JassIm.ImFunctionCall(trace, impureCallee, JassIm.ImTypeArguments(), JassIm.ImExprs(), | ||
| false, CallType.NORMAL); | ||
|
|
||
| ImExpr expressionWithStatements = JassIm.ImStatementExpr( | ||
| JassIm.ImStmts(JassIm.ImSet(trace, JassIm.ImVarAccess(targetVar), JassIm.ImIntVal(1))), | ||
| JassIm.ImVarAccess(targetVar)); | ||
|
|
||
| ImFunction function = JassIm.ImFunction(trace, "test", JassIm.ImTypeVars(), JassIm.ImVars(), JassIm.ImVoid(), | ||
| JassIm.ImVars(sourceVar, targetVar), JassIm.ImStmts(), Collections.emptyList()); | ||
|
|
||
| Method flattenExprs = Flatten.class.getDeclaredMethod("flattenExprs", ImTranslator.class, ImFunction.class, java.util.List.class); | ||
| flattenExprs.setAccessible(true); | ||
|
|
||
| MultiResult result = (MultiResult) flattenExprs.invoke(null, translator, function, Arrays.asList(impureCall, expressionWithStatements)); | ||
|
|
||
| assertEquals(function.getLocals().size(), 3, "a temporary should be added for the impure expression"); | ||
| assertEquals(result.stmts.size(), 2, "flattening should produce a temp assignment followed by the statement expr body"); | ||
|
|
||
| ImExpr flattenedImpure = result.expr(0); | ||
| assertTrue(flattenedImpure instanceof ImVarAccess, "impure call should be replaced with temp access"); | ||
| ImVar tempVar = ((ImVarAccess) flattenedImpure).getVar(); | ||
| assertNotSame(tempVar, sourceVar); | ||
| assertNotSame(tempVar, targetVar); | ||
|
|
||
| ImStmt tempAssignment = result.stmts.get(0); | ||
| assertTrue(tempAssignment instanceof ImSet, "first statement should assign the impure result to the temp var"); | ||
| ImLExpr left = ((ImSet) tempAssignment).getLeft(); | ||
| assertTrue(left instanceof ImVarAccess); | ||
| assertSame(((ImVarAccess) left).getVar(), tempVar); | ||
| } | ||
|
|
||
| @Test | ||
| public void add_temp_when_followup_writes_accessed_var() throws Exception { | ||
| ImTranslator translator = new ImTranslator(Ast.WurstModel(), true, new RunArgs()); | ||
| de.peeeq.wurstscript.ast.Element trace = Ast.NoExpr(); | ||
| ImVar shared = JassIm.ImVar(trace, JassIm.ImSimpleType("int"), "shared", false); | ||
|
|
||
| ImExpr initialRead = JassIm.ImVarAccess(shared); | ||
| ImExpr statementWithWrite = JassIm.ImStatementExpr( | ||
| JassIm.ImStmts(JassIm.ImSet(trace, JassIm.ImVarAccess(shared), JassIm.ImIntVal(4))), | ||
| JassIm.ImIntVal(2)); | ||
|
|
||
| ImFunction function = JassIm.ImFunction(trace, "test", JassIm.ImTypeVars(), JassIm.ImVars(), JassIm.ImVoid(), | ||
| JassIm.ImVars(shared), JassIm.ImStmts(), Collections.emptyList()); | ||
|
|
||
| Method flattenExprs = Flatten.class.getDeclaredMethod("flattenExprs", ImTranslator.class, ImFunction.class, java.util.List.class); | ||
| flattenExprs.setAccessible(true); | ||
|
|
||
| MultiResult result = (MultiResult) flattenExprs.invoke(null, translator, function, Arrays.asList(initialRead, statementWithWrite)); | ||
|
|
||
| assertEquals(function.getLocals().size(), 2, "flattening should introduce a temp for the first argument"); | ||
| assertEquals(result.stmts.size(), 2, "flattening should store the first arg before executing later statements"); | ||
|
|
||
| ImExpr firstExpr = result.expr(0); | ||
| assertTrue(firstExpr instanceof ImVarAccess, "first expression should be rewritten to a temp access"); | ||
| ImVar tempVar = ((ImVarAccess) firstExpr).getVar(); | ||
| assertNotSame(tempVar, shared); | ||
|
|
||
| ImStmt firstStmt = result.stmts.get(0); | ||
| assertTrue(firstStmt instanceof ImSet, "first statement should capture the original value"); | ||
| assertSame(((ImVarAccess) ((ImSet) firstStmt).getLeft()).getVar(), tempVar); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
isUnchangedVarAccessguard inflattenExprsnow reuses anImVarAccesswhen no directImSetto the same var is found, causing the first argument to be evaluated after later statements. However,ImVarAccessis classified asReadsGlobalsrather thanPure(PurityLevels.java:32‑34), so its value can change across any side effect. The write check only looks forImSettargets (writesVar at lines 515‑524) and ignores other impure statements such as function-call statements (ImStmtincludesImExpr, parserspec/jass_im.parseq lines 74‑83), so a later statement expression that calls a mutating function will bypass the temp creation and the read will occur after the mutation (e.g.,foo(g, (mutate(); ...))now reads the updatedg). This reorders observable effects compared to the original left-to-right evaluation.Useful? React with 👍 / 👎.