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 2 commits into
base: code-reflection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -827,6 +827,10 @@ public Op.Result op(Op op) {
*/
public Op.Result op(Op op, OpTransformer transformer) {
check();
if (op instanceof Op.Root) {
throw new IllegalStateException("Operation %s is root and can't be inserted in a block".formatted(op.getClass().getSimpleName()));
}

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

Op transformedOp = op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public interface BlockTerminating extends Terminating {
List<Block.Reference> successors();
}

/**
* An operation characteristic indicating the operation is a root operation
*/
public interface Root {
}

/**
* A value that is the result of an operation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected CoreOp(ExternalizedOp def) {
*/
@OpFactory.OpDeclaration(FuncOp.NAME)
public static final class FuncOp extends CoreOp
implements Op.Invokable, Op.Isolated, Op.Lowerable {
implements Op.Invokable, Op.Isolated, Op.Lowerable, Op.Root {

public static class Builder {
final Body.Builder ancestorBody;
Expand Down
31 changes: 31 additions & 0 deletions test/langtools/tools/javac/reflect/RootOpInsertionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import jdk.incubator.code.Block;
import jdk.incubator.code.Body;
import jdk.incubator.code.CodeReflection;
import jdk.incubator.code.Op;
import jdk.incubator.code.op.CoreOp;
import jdk.incubator.code.type.FunctionType;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

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

@CodeReflection
static void f() {}

@Test
void test() throws NoSuchMethodException {
Method mf = this.getClass().getDeclaredMethod("f");
CoreOp.FuncOp funcOp = Op.ofMethod(mf).orElseThrow();

Body.Builder bodyBuilder = Body.Builder.of(null, FunctionType.VOID);
Block.Builder entryBlock = bodyBuilder.entryBlock();
Assert.assertThrows(IllegalStateException.class, () -> entryBlock.op(funcOp));
}
}