Skip to content

Commit e3531e1

Browse files
committed
Fix materialized local reads
1 parent c08b90f commit e3531e1

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/basic_interpreter/BasicInterpreter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ public static long bla(long a1, @Variadic Object[] a2) {
198198
static final class ThrowOperation {
199199
@Specialization
200200
public static Object perform(long value,
201-
// TODO passing the actual bci breaks compiler tests because of how we
202-
// instantiate a location node from the bci
203-
@SuppressWarnings("unused") @Bind("$location") BytecodeLocation bci,
204-
@Bind("$root") Node node) {
205-
throw new TestException("fail", node, -1, value);
201+
@Bind("$root") Node node,
202+
@Bind("$bci") int bci) {
203+
throw new TestException("fail", node, bci, value);
206204
}
207205
}
208206

@@ -308,6 +306,14 @@ protected static boolean callTargetMatches(CallTarget left, CallTarget right) {
308306
}
309307
}
310308

309+
@Operation
310+
public static final class MaterializeFrame {
311+
@Specialization
312+
public static MaterializedFrame materialize(VirtualFrame frame) {
313+
return frame.materialize();
314+
}
315+
}
316+
311317
@Operation
312318
public static final class CreateClosure {
313319
@Specialization

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/basic_interpreter/BasicInterpreterTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,50 @@ public void testNestedFunctions() {
510510
assertEquals(1L, root.call());
511511
}
512512

513+
@Test
514+
public void testMaterializedFrameAccesses() {
515+
// x = 41
516+
// f = materialize()
517+
// f.x = f.x + 1
518+
// return x
519+
520+
RootCallTarget root = parse("materializedFrameAccesses", b -> {
521+
b.beginRoot(LANGUAGE);
522+
523+
BytecodeLocal x = b.createLocal();
524+
BytecodeLocal f = b.createLocal();
525+
526+
b.beginStoreLocal(x);
527+
b.emitLoadConstant(41L);
528+
b.endStoreLocal();
529+
530+
b.beginStoreLocal(f);
531+
b.emitMaterializeFrame();
532+
b.endStoreLocal();
533+
534+
b.beginStoreLocalMaterialized(x);
535+
536+
b.emitLoadLocal(f);
537+
538+
b.beginAddOperation();
539+
b.beginLoadLocalMaterialized(x);
540+
b.emitLoadLocal(f);
541+
b.endLoadLocalMaterialized();
542+
b.emitLoadConstant(1L);
543+
b.endAddOperation();
544+
545+
b.endStoreLocalMaterialized();
546+
547+
b.beginReturn();
548+
b.emitLoadLocal(x);
549+
b.endReturn();
550+
551+
b.endRoot();
552+
});
553+
554+
assertEquals(42L, root.call());
555+
}
556+
513557
@Test
514558
@Ignore
515559
public void testLocalsNonlocalRead() {

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/generator/BytecodeDSLNodeFactory.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7964,7 +7964,7 @@ private List<CodeExecutableElement> createContinueAt() {
79647964
b.statement("sp += 1");
79657965
break;
79667966
case LOAD_LOCAL_MATERIALIZED:
7967-
String materializedFrame = "((VirtualFrame) " + getFrameObject("sp - 2)");
7967+
String materializedFrame = "((VirtualFrame) " + getFrameObject("sp - 1)");
79687968
if (instr.isQuickening() || tier.isUncached() || !model.usesBoxingElimination()) {
79697969
b.startStatement();
79707970
b.startCall(lookupDoLoadLocal(instr).getSimpleName().toString());
@@ -9005,7 +9005,12 @@ private CodeExecutableElement lookupDoLoadLocal(InstructionModel instr) {
90059005
}
90069006

90079007
b.startStatement();
9008-
startSetFrame(b, inputType).string(needsStackFrame ? "stackFrame" : "frame").string("sp");
9008+
startSetFrame(b, inputType).string(needsStackFrame ? "stackFrame" : "frame");
9009+
if (instr.kind == InstructionKind.LOAD_LOCAL_MATERIALIZED) {
9010+
b.string("sp - 1"); // overwrite the materialized frame
9011+
} else {
9012+
b.string("sp");
9013+
}
90099014
if (generic) {
90109015
startRequireFrame(b, slotType).string("frame").tree(readSlot).end();
90119016
} else {
@@ -9125,7 +9130,13 @@ private CodeExecutableElement lookupDoSpecializeLoadLocal(InstructionModel instr
91259130

91269131
emitQuickening(b, "$this", "bc", "bci", null, "newInstruction");
91279132
b.startStatement();
9128-
startSetFrame(b, type(Object.class)).string(stackFrame).string("sp").string("value").end();
9133+
startSetFrame(b, type(Object.class)).string(stackFrame);
9134+
if (instr.kind == InstructionKind.LOAD_LOCAL_MATERIALIZED) {
9135+
b.string("sp - 1"); // overwrite the materialized frame
9136+
} else {
9137+
b.string("sp");
9138+
}
9139+
b.string("value").end();
91299140
b.end();
91309141

91319142
doInstructionMethods.put(instr, method);

0 commit comments

Comments
 (0)