Skip to content

Disallow insertion of a root op in a block #425

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
wants to merge 5 commits into
base: code-reflection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ public void accept(CodeBuilder cob) {
}
cob.invokevirtual(CD_MethodHandle, "invokeExact", mtype.describeConstable().get());
cob.checkcast(funcOpClassDesc);
cob.dup();
cob.invokevirtual(CodeReflectionSupport.OP_CLASS.describeConstable().get(), "freeze", MTD_void);
cob.putstatic(lambdaClassEntry.asSymbol(), COMPILER_GENERATED_MODEL_FIELD_NAME, funcOpClassDesc);
}
cob.return_();
Expand Down Expand Up @@ -501,6 +503,7 @@ static class CodeReflectionSupport {
static final Class<?> QUOTABLE_CLASS;
static final MethodHandle QUOTED_OP_MH;
static final Class<?> FUNC_OP_CLASS;
static final Class<?> OP_CLASS;

static {
try {
Expand All @@ -511,6 +514,7 @@ static class CodeReflectionSupport {
FUNC_OP_CLASS = cl.loadClass("jdk.incubator.code.dialect.core.CoreOp$FuncOp");
QUOTED_OP_MH = Lookup.IMPL_LOOKUP.findStatic(QUOTED_CLASS, "quotedOp",
MethodType.methodType(QUOTED_CLASS, FUNC_OP_CLASS, Object[].class));
OP_CLASS = cl.loadClass("jdk.incubator.code.Op");
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ public Op.Result op(Op op) {
*/
public Op.Result op(Op op, OpTransformer transformer) {
check();

final Op.Result oprToTransform = op.result();

Op transformedOp = op;
Expand Down
21 changes: 20 additions & 1 deletion src/jdk.incubator.code/share/classes/jdk/incubator/code/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,19 @@ public interface BlockTerminating extends Terminating {
* A value that is the result of an operation.
*/
public static final class Result extends Value {

/**
* If assigned to an operation result, it indicates the operation is frozen
*/
private static final Result ROOT = new Result();

final Op op;

private Result() {
super(null, null);
this.op = null;
}

Result(Block block, Op op) {
super(block, op.resultType());

Expand Down Expand Up @@ -344,7 +355,7 @@ public List<Body> bodies() {
* @return the operation's result, or {@code null} if not assigned to a block.
*/
public final Result result() {
return result;
return result == Result.ROOT ? null : result;
}


Expand Down Expand Up @@ -551,6 +562,7 @@ private static Optional<FuncOp> createCodeModel(Method method) {
opMethod.setAccessible(true);
try {
FuncOp funcOp = (FuncOp) opMethod.invoke(null);
funcOp.freeze();
return Optional.of(funcOp);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -595,4 +607,11 @@ public static Optional<FuncOp> ofElement(ProcessingEnvironment processingEnviron
return Optional.empty();
}
}

public void freeze() {
if (result != null && result != Result.ROOT) {
throw new IllegalStateException("Can't freeze a bound operation");
}
result = Result.ROOT;
}
}
67 changes: 67 additions & 0 deletions test/jdk/java/lang/reflect/code/TestFreezeOp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import jdk.incubator.code.*;
import jdk.incubator.code.dialect.core.CoreOp;
import jdk.incubator.code.dialect.core.FunctionType;
import jdk.incubator.code.dialect.java.JavaType;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.IntSupplier;
import java.util.function.IntUnaryOperator;

/*
* @test
* @modules jdk.incubator.code
* @run testng TestFreezeOp
*/
public class TestFreezeOp {

@CodeReflection
static List<Integer> f(int i) {
return new ArrayList<>(i);
}

@Test
void test0() throws NoSuchMethodException {
Method m = this.getClass().getDeclaredMethod("f", int.class);
CoreOp.FuncOp f = Op.ofMethod(m).get();
assertOpIsCopiedWhenAddedToBlock(f);
}

@Test
void test1() {
Quotable q = (IntUnaryOperator & Quotable) i -> i / 2;
Quoted quoted = Op.ofQuotable(q).get();
CoreOp.QuotedOp quotedOp = (CoreOp.QuotedOp) quoted.op().ancestorBody().parentOp();
CoreOp.FuncOp funcOp = (CoreOp.FuncOp) quotedOp.ancestorBody().parentOp();
assertOpIsCopiedWhenAddedToBlock(funcOp);
}

@Test
void test2() {
CoreOp.ConstantOp constant = CoreOp.constant(JavaType.INT, 7);
constant.freeze();
assertOpIsCopiedWhenAddedToBlock(constant);
}

@Test
void test3() {
CoreOp.FuncOp funcOp = CoreOp.func("f", FunctionType.FUNCTION_TYPE_VOID).body(b -> {
b.op(CoreOp._return());
});
funcOp.freeze();
funcOp.freeze();
}

void assertOpIsCopiedWhenAddedToBlock(Op op) {
Body.Builder body = Body.Builder.of(null, FunctionType.FUNCTION_TYPE_VOID);
body.entryBlock().op(op);
body.entryBlock().op(CoreOp._return());
CoreOp.FuncOp funcOp = CoreOp.func("t", body);
boolean b = funcOp.body().entryBlock().ops().stream().allMatch(o -> o != op);
Assert.assertTrue(b);
}
}