Skip to content

Commit

Permalink
perf: use batched codegen for Max/MinShortNode and MulNode
Browse files Browse the repository at this point in the history
Cherry-picked from 3aa5e54
  • Loading branch information
ishland committed Nov 17, 2024
1 parent 888b087 commit 39bdddb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,53 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
context.delegateToSingle(m, localVarConsumer, this);
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Label minLabel = new Label();
Label end = new Label();

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);

m.dup2();
m.dconst(this.rightMax);
m.cmpl(Type.DOUBLE_TYPE);
m.iflt(minLabel);
m.goTo(end);

m.visitLabel(minLabel);

m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, rightMethodSingle, BytecodeGen.Context.SINGLE_DESC, false);

m.invokestatic(
Type.getInternalName(Math.class),
"max",
Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE),
false
);

m.visitLabel(end);
m.astore(Type.DOUBLE_TYPE);
});

m.areturn(Type.VOID_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,51 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
context.delegateToSingle(m, localVarConsumer, this);
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Label minLabel = new Label();
Label end = new Label();

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);

m.dup2();
m.dconst(this.rightMin);
m.cmpg(Type.DOUBLE_TYPE);
m.ifgt(minLabel);
m.goTo(end);

m.visitLabel(minLabel);
m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, rightMethodSingle, BytecodeGen.Context.SINGLE_DESC, false);
m.invokestatic(
Type.getInternalName(Math.class),
"min",
Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE),
false
);

m.visitLabel(end);
m.astore(Type.DOUBLE_TYPE);
});

m.areturn(Type.VOID_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,48 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
context.delegateToSingle(m, localVarConsumer, this);
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Label minLabel = new Label();
Label end = new Label();

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);

m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);

m.dup2();
m.dconst(0.0);
m.cmpl(Type.DOUBLE_TYPE);
m.ifne(minLabel);
m.pop2();
m.dconst(0.0);
m.goTo(end);

m.visitLabel(minLabel);
m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.INT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, rightMethodSingle, BytecodeGen.Context.SINGLE_DESC, false);
m.mul(Type.DOUBLE_TYPE);

m.visitLabel(end);
m.astore(Type.DOUBLE_TYPE);
});

m.areturn(Type.VOID_TYPE);
}

Expand Down

0 comments on commit 39bdddb

Please sign in to comment.