Skip to content

Commit 55e0c1b

Browse files
committed
Infer dynamic operand names for custom operations from specializations; misc javadoc improvements and parser refactors as well
1 parent a3df206 commit 55e0c1b

File tree

11 files changed

+330
-221
lines changed

11 files changed

+330
-221
lines changed

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/ConstantOperandTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public static final class DifferentDynamicArgumentCount {
445445
public static void doOperation(VirtualFrame frame, int const1, Object dynamic1, int const2) {
446446
}
447447

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

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

Lines changed: 94 additions & 48 deletions
Large diffs are not rendered by default.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public boolean canBoxingEliminateType(NodeExecutionData currentExecution, TypeMi
130130
private boolean buildChildExecution(CodeTreeBuilder b, FrameState frameState, String frame, int idx) {
131131
int index = idx;
132132

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

144-
index -= instruction.signature.getConstantOperandsBeforeCount();
144+
index -= instruction.signature.constantOperandsBeforeCount;
145145
if (index < instruction.signature.dynamicOperandCount) {
146146
TypeMirror targetType = instruction.signature.getSpecializedType(index);
147147
TypeMirror genericType = instruction.signature.getGenericType(index);
@@ -181,18 +181,18 @@ private boolean buildChildExecution(CodeTreeBuilder b, FrameState frameState, St
181181
}
182182

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

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

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/BytecodeDSLBuiltins.java

Lines changed: 50 additions & 45 deletions
Large diffs are not rendered by default.

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/BytecodeDSLModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public CustomOperationModel customRegularOperation(OperationKind kind, String na
300300
} else if (ElementUtils.typeEquals(mirror.getAnnotationType(), types.EpilogReturn)) {
301301
op.setInternal();
302302
op.setTransparent(true);
303-
op.setDynamicOperands(new DynamicOperandModel("value", true, false));
303+
op.setDynamicOperands(new DynamicOperandModel(List.of("value"), true, false));
304304
if (epilogReturn != null) {
305305
addError(typeElement, "%s is already annotated with @%s. A Bytecode DSL class can only declare one return epilog.", getSimpleName(epilogReturn.getTemplateType()),
306306
getSimpleName(types.EpilogReturn));

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/DynamicOperandModel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.truffle.dsl.processor.bytecode.model;
4242

43-
public record DynamicOperandModel(String name, boolean voidAllowed, boolean isVariadic) {
43+
import java.util.List;
44+
45+
public record DynamicOperandModel(List<String> names, boolean voidAllowed, boolean isVariadic) {
4446

4547
}

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/OperationModel.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public enum Encoding {
117117
* Models the constant operand data statically declared on the operation using ConstantOperand
118118
* annotations.
119119
*/
120-
public record ConstantOperands(List<ConstantOperandModel> before, List<ConstantOperandModel> after) {
121-
public static final ConstantOperands NONE = new ConstantOperands(List.of(), List.of());
120+
public record ConstantOperandsModel(List<ConstantOperandModel> before, List<ConstantOperandModel> after) {
121+
public static final ConstantOperandsModel NONE = new ConstantOperandsModel(List.of(), List.of());
122122

123123
public boolean hasConstantOperands() {
124124
return this != NONE;
@@ -149,14 +149,23 @@ public boolean hasConstantOperands() {
149149
public boolean isInternal;
150150

151151
public InstructionModel instruction;
152-
public ConstantOperands constantOperands = null;
152+
public CustomOperationModel customModel;
153+
154+
// The constant operands parsed from {@code @ConstantOperand} annotations.
155+
public ConstantOperandsModel constantOperands = null;
156+
157+
// Dynamic operand data supplied by builtin specs / parsed from operation specializations.
153158
public DynamicOperandModel[] dynamicOperands = new DynamicOperandModel[0];
159+
160+
// Operand names parsed from operation specializations.
161+
public List<String> constantOperandBeforeNames;
162+
public List<String> constantOperandAfterNames;
163+
154164
public OperationArgument[] operationBeginArguments = EMPTY_ARGUMENTS;
155165
public OperationArgument[] operationEndArguments = EMPTY_ARGUMENTS;
156166
public boolean operationBeginArgumentVarArgs = false;
157167

158-
public CustomOperationModel customModel;
159-
168+
// A unique identifier for instrumentation instructions.
160169
public int instrumentationIndex;
161170

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

170-
public int numChildren() {
179+
public int numConstantOperandsBefore() {
180+
if (constantOperands == null) {
181+
return 0;
182+
}
183+
return constantOperands.before.size();
184+
}
185+
186+
public int numDynamicOperands() {
171187
return dynamicOperands.length;
172188
}
173189

190+
public int numConstantOperandsAfter() {
191+
if (constantOperands == null) {
192+
return 0;
193+
}
194+
return constantOperands.after.size();
195+
}
196+
174197
public boolean hasChildren() {
175-
return isVariadic || numChildren() > 0;
198+
return isVariadic || numDynamicOperands() > 0;
176199
}
177200

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

224+
public String getConstantOperandBeforeName(int i) {
225+
return constantOperandBeforeNames.get(i);
226+
}
227+
228+
public String getConstantOperandAfterName(int i) {
229+
return constantOperandAfterNames.get(i);
230+
}
231+
201232
public OperationModel setDynamicOperands(DynamicOperandModel... dynamicOperands) {
202233
this.dynamicOperands = dynamicOperands;
203234
return this;

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/model/Signature.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,22 @@ public final class Signature {
5656
public final List<TypeMirror> operandTypes;
5757
public final boolean isVariadic;
5858
public final boolean isVoid;
59-
60-
public final List<String> constantOperandsBefore;
61-
public final List<String> constantOperandsAfter;
59+
public final int constantOperandsBeforeCount;
6260
public final int dynamicOperandCount;
61+
public final int constantOperandsAfterCount;
6362

6463
public Signature(TypeMirror returnType, List<TypeMirror> types) {
65-
this(returnType, types, false, List.of(), List.of());
64+
this(returnType, types, false, 0, 0);
6665
}
6766

68-
public Signature(TypeMirror returnType, List<TypeMirror> types, boolean isVariadic, List<String> constantOperandsBefore, List<String> constantOperandsAfter) {
67+
public Signature(TypeMirror returnType, List<TypeMirror> types, boolean isVariadic, int constantOperandsBeforeCount, int constantOperandsAfterCount) {
6968
this.returnType = returnType;
7069
this.operandTypes = Collections.unmodifiableList(types);
7170
this.isVariadic = isVariadic;
7271
this.isVoid = ElementUtils.isVoid(returnType);
73-
74-
this.constantOperandsBefore = Collections.unmodifiableList(constantOperandsBefore);
75-
this.constantOperandsAfter = Collections.unmodifiableList(constantOperandsAfter);
76-
this.dynamicOperandCount = types.size() - constantOperandsBefore.size() - constantOperandsAfter.size();
72+
this.constantOperandsBeforeCount = constantOperandsBeforeCount;
73+
this.dynamicOperandCount = operandTypes.size() - constantOperandsBeforeCount - constantOperandsAfterCount;
74+
this.constantOperandsAfterCount = constantOperandsAfterCount;
7775
}
7876

7977
public TypeMirror getGenericType(int i) {
@@ -97,7 +95,7 @@ public TypeMirror getSpecializedType(int i) {
9795
if (isVariadicParameter(i)) {
9896
return context.getType(Object[].class);
9997
}
100-
return operandTypes.get(constantOperandsBefore.size() + i);
98+
return operandTypes.get(constantOperandsBeforeCount + i);
10199
}
102100

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

114-
for (int i = 0; i < getConstantOperandsBeforeCount(); i++) {
112+
for (int i = 0; i < constantOperandsBeforeCount; i++) {
115113
sb.append(ElementUtils.getSimpleName(operandTypes.get(i)));
116114
sb.append(", ");
117115
}
118116

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

128126
offset += dynamicOperandCount;
129-
for (int i = 0; i < getConstantOperandsAfterCount(); i++) {
127+
for (int i = 0; i < constantOperandsAfterCount; i++) {
130128
sb.append(ElementUtils.getSimpleName(operandTypes.get(offset + i)));
131129
sb.append(", ");
132130
}
@@ -139,12 +137,4 @@ public String toString() {
139137

140138
return sb.toString();
141139
}
142-
143-
public int getConstantOperandsBeforeCount() {
144-
return constantOperandsBefore.size();
145-
}
146-
147-
public int getConstantOperandsAfterCount() {
148-
return constantOperandsAfter.size();
149-
}
150140
}

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/parser/BytecodeDSLParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import com.oracle.truffle.dsl.processor.bytecode.model.OptimizationDecisionsModel.QuickenDecision;
9393
import com.oracle.truffle.dsl.processor.bytecode.model.OptimizationDecisionsModel.SuperInstructionDecision;
9494
import com.oracle.truffle.dsl.processor.bytecode.model.Signature;
95+
import com.oracle.truffle.dsl.processor.bytecode.parser.SpecializationSignatureParser.SpecializationSignature;
9596
import com.oracle.truffle.dsl.processor.java.ElementUtils;
9697
import com.oracle.truffle.dsl.processor.java.compiler.CompilerFactory;
9798
import com.oracle.truffle.dsl.processor.library.ExportsData;
@@ -689,9 +690,9 @@ private void parseBytecodeDSLModel(TypeElement typeElement, BytecodeDSLModel mod
689690
name = String.join("#", includedSpecializations.stream().map((s) -> s.getId()).toList());
690691
}
691692
List<ExecutableElement> includedSpecializationElements = includedSpecializations.stream().map(s -> s.getMethod()).toList();
692-
List<Signature> includedSpecializationSignatures = CustomOperationParser.parseSignatures(includedSpecializationElements, customOperation, operation.constantOperands);
693+
List<SpecializationSignature> includedSpecializationSignatures = CustomOperationParser.parseSignatures(includedSpecializationElements, customOperation, operation.constantOperands);
693694
assert !customOperation.hasErrors();
694-
Signature signature = SignatureParser.createPolymorphicSignature(includedSpecializationSignatures, includedSpecializationElements, customOperation);
695+
Signature signature = SpecializationSignatureParser.createPolymorphicSignature(includedSpecializationSignatures, includedSpecializationElements, customOperation);
695696
InstructionModel baseInstruction = operation.instruction;
696697
InstructionModel quickenedInstruction = model.quickenInstruction(baseInstruction, signature, ElementUtils.firstLetterUpperCase(name));
697698
quickenedInstruction.filteredSpecializations = includedSpecializations;

0 commit comments

Comments
 (0)