Skip to content

Commit

Permalink
Infer dynamic operand names for custom operations from specialization…
Browse files Browse the repository at this point in the history
…s; misc javadoc improvements and parser refactors as well
  • Loading branch information
DSouzaM committed May 24, 2024
1 parent a3df206 commit 55e0c1b
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public static final class DifferentDynamicArgumentCount {
public static void doOperation(VirtualFrame frame, int const1, Object dynamic1, int const2) {
}

@ExpectError("Error calculating operation signature: all specializations must have the same number of value arguments.")
@ExpectError("Error calculating operation signature: all specializations must have the same number of operands.")
@Specialization
public static void doOperation2(VirtualFrame frame, int const1, Object dynamic1, Object dynamic2, int const2) {
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public boolean canBoxingEliminateType(NodeExecutionData currentExecution, TypeMi
private boolean buildChildExecution(CodeTreeBuilder b, FrameState frameState, String frame, int idx) {
int index = idx;

if (index < instruction.signature.getConstantOperandsBeforeCount()) {
if (index < instruction.signature.constantOperandsBeforeCount) {
TypeMirror constantOperandType = instruction.operation.constantOperands.before().get(index).type();
if (!ElementUtils.isObject(constantOperandType)) {
b.cast(constantOperandType);
Expand All @@ -141,7 +141,7 @@ private boolean buildChildExecution(CodeTreeBuilder b, FrameState frameState, St
return false;
}

index -= instruction.signature.getConstantOperandsBeforeCount();
index -= instruction.signature.constantOperandsBeforeCount;
if (index < instruction.signature.dynamicOperandCount) {
TypeMirror targetType = instruction.signature.getSpecializedType(index);
TypeMirror genericType = instruction.signature.getGenericType(index);
Expand Down Expand Up @@ -181,18 +181,18 @@ private boolean buildChildExecution(CodeTreeBuilder b, FrameState frameState, St
}

index -= instruction.signature.dynamicOperandCount;
if (index < instruction.signature.getConstantOperandsAfterCount()) {
if (index < instruction.signature.constantOperandsAfterCount) {
TypeMirror constantOperandType = instruction.operation.constantOperands.after().get(index).type();
if (!ElementUtils.isObject(constantOperandType)) {
b.cast(constantOperandType);
}
List<InstructionImmediate> imms = instruction.getImmediates(ImmediateKind.CONSTANT);
InstructionImmediate imm = imms.get(instruction.signature.getConstantOperandsBeforeCount() + index);
InstructionImmediate imm = imms.get(instruction.signature.constantOperandsBeforeCount + index);
b.string(readConst(readBc("$bc", "$bci + " + imm.offset()), "$bytecode.constants"));
return false;
}

index -= instruction.signature.getConstantOperandsAfterCount();
index -= instruction.signature.constantOperandsAfterCount;
throw new AssertionError("index=" + index + ", signature=" + instruction.signature);
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public CustomOperationModel customRegularOperation(OperationKind kind, String na
} else if (ElementUtils.typeEquals(mirror.getAnnotationType(), types.EpilogReturn)) {
op.setInternal();
op.setTransparent(true);
op.setDynamicOperands(new DynamicOperandModel("value", true, false));
op.setDynamicOperands(new DynamicOperandModel(List.of("value"), true, false));
if (epilogReturn != null) {
addError(typeElement, "%s is already annotated with @%s. A Bytecode DSL class can only declare one return epilog.", getSimpleName(epilogReturn.getTemplateType()),
getSimpleName(types.EpilogReturn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
package com.oracle.truffle.dsl.processor.bytecode.model;

public record DynamicOperandModel(String name, boolean voidAllowed, boolean isVariadic) {
import java.util.List;

public record DynamicOperandModel(List<String> names, boolean voidAllowed, boolean isVariadic) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public enum Encoding {
* Models the constant operand data statically declared on the operation using ConstantOperand
* annotations.
*/
public record ConstantOperands(List<ConstantOperandModel> before, List<ConstantOperandModel> after) {
public static final ConstantOperands NONE = new ConstantOperands(List.of(), List.of());
public record ConstantOperandsModel(List<ConstantOperandModel> before, List<ConstantOperandModel> after) {
public static final ConstantOperandsModel NONE = new ConstantOperandsModel(List.of(), List.of());

public boolean hasConstantOperands() {
return this != NONE;
Expand Down Expand Up @@ -149,14 +149,23 @@ public boolean hasConstantOperands() {
public boolean isInternal;

public InstructionModel instruction;
public ConstantOperands constantOperands = null;
public CustomOperationModel customModel;

// The constant operands parsed from {@code @ConstantOperand} annotations.
public ConstantOperandsModel constantOperands = null;

// Dynamic operand data supplied by builtin specs / parsed from operation specializations.
public DynamicOperandModel[] dynamicOperands = new DynamicOperandModel[0];

// Operand names parsed from operation specializations.
public List<String> constantOperandBeforeNames;
public List<String> constantOperandAfterNames;

public OperationArgument[] operationBeginArguments = EMPTY_ARGUMENTS;
public OperationArgument[] operationEndArguments = EMPTY_ARGUMENTS;
public boolean operationBeginArgumentVarArgs = false;

public CustomOperationModel customModel;

// A unique identifier for instrumentation instructions.
public int instrumentationIndex;

public OperationModel(BytecodeDSLModel parent, int id, OperationKind kind, String name, String javadoc) {
Expand All @@ -167,12 +176,26 @@ public OperationModel(BytecodeDSLModel parent, int id, OperationKind kind, Strin
this.javadoc = javadoc;
}

public int numChildren() {
public int numConstantOperandsBefore() {
if (constantOperands == null) {
return 0;
}
return constantOperands.before.size();
}

public int numDynamicOperands() {
return dynamicOperands.length;
}

public int numConstantOperandsAfter() {
if (constantOperands == null) {
return 0;
}
return constantOperands.after.size();
}

public boolean hasChildren() {
return isVariadic || numChildren() > 0;
return isVariadic || numDynamicOperands() > 0;
}

public void setInstrumentationIndex(int instrumentationIndex) {
Expand All @@ -198,6 +221,14 @@ public OperationModel setVoid(boolean isVoid) {
return this;
}

public String getConstantOperandBeforeName(int i) {
return constantOperandBeforeNames.get(i);
}

public String getConstantOperandAfterName(int i) {
return constantOperandAfterNames.get(i);
}

public OperationModel setDynamicOperands(DynamicOperandModel... dynamicOperands) {
this.dynamicOperands = dynamicOperands;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,22 @@ public final class Signature {
public final List<TypeMirror> operandTypes;
public final boolean isVariadic;
public final boolean isVoid;

public final List<String> constantOperandsBefore;
public final List<String> constantOperandsAfter;
public final int constantOperandsBeforeCount;
public final int dynamicOperandCount;
public final int constantOperandsAfterCount;

public Signature(TypeMirror returnType, List<TypeMirror> types) {
this(returnType, types, false, List.of(), List.of());
this(returnType, types, false, 0, 0);
}

public Signature(TypeMirror returnType, List<TypeMirror> types, boolean isVariadic, List<String> constantOperandsBefore, List<String> constantOperandsAfter) {
public Signature(TypeMirror returnType, List<TypeMirror> types, boolean isVariadic, int constantOperandsBeforeCount, int constantOperandsAfterCount) {
this.returnType = returnType;
this.operandTypes = Collections.unmodifiableList(types);
this.isVariadic = isVariadic;
this.isVoid = ElementUtils.isVoid(returnType);

this.constantOperandsBefore = Collections.unmodifiableList(constantOperandsBefore);
this.constantOperandsAfter = Collections.unmodifiableList(constantOperandsAfter);
this.dynamicOperandCount = types.size() - constantOperandsBefore.size() - constantOperandsAfter.size();
this.constantOperandsBeforeCount = constantOperandsBeforeCount;
this.dynamicOperandCount = operandTypes.size() - constantOperandsBeforeCount - constantOperandsAfterCount;
this.constantOperandsAfterCount = constantOperandsAfterCount;
}

public TypeMirror getGenericType(int i) {
Expand All @@ -97,7 +95,7 @@ public TypeMirror getSpecializedType(int i) {
if (isVariadicParameter(i)) {
return context.getType(Object[].class);
}
return operandTypes.get(constantOperandsBefore.size() + i);
return operandTypes.get(constantOperandsBeforeCount + i);
}

public boolean isVariadicParameter(int i) {
Expand All @@ -111,12 +109,12 @@ public String toString() {
sb.append(ElementUtils.getSimpleName(returnType)).append(" ");
sb.append("(");

for (int i = 0; i < getConstantOperandsBeforeCount(); i++) {
for (int i = 0; i < constantOperandsBeforeCount; i++) {
sb.append(ElementUtils.getSimpleName(operandTypes.get(i)));
sb.append(", ");
}

int offset = getConstantOperandsBeforeCount();
int offset = constantOperandsBeforeCount;
for (int i = 0; i < dynamicOperandCount; i++) {
sb.append(ElementUtils.getSimpleName(operandTypes.get(offset + i)));
if (isVariadic && i == dynamicOperandCount - 1) {
Expand All @@ -126,7 +124,7 @@ public String toString() {
}

offset += dynamicOperandCount;
for (int i = 0; i < getConstantOperandsAfterCount(); i++) {
for (int i = 0; i < constantOperandsAfterCount; i++) {
sb.append(ElementUtils.getSimpleName(operandTypes.get(offset + i)));
sb.append(", ");
}
Expand All @@ -139,12 +137,4 @@ public String toString() {

return sb.toString();
}

public int getConstantOperandsBeforeCount() {
return constantOperandsBefore.size();
}

public int getConstantOperandsAfterCount() {
return constantOperandsAfter.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import com.oracle.truffle.dsl.processor.bytecode.model.OptimizationDecisionsModel.QuickenDecision;
import com.oracle.truffle.dsl.processor.bytecode.model.OptimizationDecisionsModel.SuperInstructionDecision;
import com.oracle.truffle.dsl.processor.bytecode.model.Signature;
import com.oracle.truffle.dsl.processor.bytecode.parser.SpecializationSignatureParser.SpecializationSignature;
import com.oracle.truffle.dsl.processor.java.ElementUtils;
import com.oracle.truffle.dsl.processor.java.compiler.CompilerFactory;
import com.oracle.truffle.dsl.processor.library.ExportsData;
Expand Down Expand Up @@ -689,9 +690,9 @@ private void parseBytecodeDSLModel(TypeElement typeElement, BytecodeDSLModel mod
name = String.join("#", includedSpecializations.stream().map((s) -> s.getId()).toList());
}
List<ExecutableElement> includedSpecializationElements = includedSpecializations.stream().map(s -> s.getMethod()).toList();
List<Signature> includedSpecializationSignatures = CustomOperationParser.parseSignatures(includedSpecializationElements, customOperation, operation.constantOperands);
List<SpecializationSignature> includedSpecializationSignatures = CustomOperationParser.parseSignatures(includedSpecializationElements, customOperation, operation.constantOperands);
assert !customOperation.hasErrors();
Signature signature = SignatureParser.createPolymorphicSignature(includedSpecializationSignatures, includedSpecializationElements, customOperation);
Signature signature = SpecializationSignatureParser.createPolymorphicSignature(includedSpecializationSignatures, includedSpecializationElements, customOperation);
InstructionModel baseInstruction = operation.instruction;
InstructionModel quickenedInstruction = model.quickenInstruction(baseInstruction, signature, ElementUtils.firstLetterUpperCase(name));
quickenedInstruction.filteredSpecializations = includedSpecializations;
Expand Down
Loading

0 comments on commit 55e0c1b

Please sign in to comment.