Skip to content

Commit d4f595b

Browse files
committed
Merge pull request #190 in G/truffle from ~CHRISTIAN.HUMER_ORACLE.COM/truffle:fix_implicit_cast_methods to master
* commit '9ea1c7ef5d423e757d5cd2ee85aaea871ac661cd': Rename index to mask. Generate data classes for specializations with lots of cached values. Fix implicit cast methods had wrong binary masks.
2 parents a581a71 + 9ea1c7e commit d4f595b

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/FlatNodeGenFactory.java

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,38 @@ private static String nodeFieldName(NodeExecutionData execution) {
197197

198198
/* Whether a new class should be generated for specialization instance fields. */
199199
private static boolean useSpecializationClass(SpecializationData specialization) {
200-
return specialization.hasMultipleInstances() && (specialization.getCaches().size() > 3 || specialization.getMaximumNumberOfInstances() > 1);
200+
int size = 0;
201+
for (CacheExpression expression : specialization.getCaches()) {
202+
TypeMirror type = expression.getParameter().getType();
203+
if (ElementUtils.isPrimitive(type)) {
204+
switch (type.getKind()) {
205+
case BOOLEAN:
206+
case BYTE:
207+
size++;
208+
break;
209+
case CHAR:
210+
case SHORT:
211+
size += 2;
212+
break;
213+
case INT:
214+
case FLOAT:
215+
size += 4;
216+
break;
217+
case LONG:
218+
case DOUBLE:
219+
size += 8;
220+
break;
221+
}
222+
} else {
223+
size += 4;
224+
}
225+
}
226+
// if we exceed the size of two references we generate a class
227+
if (size > 8) {
228+
return true;
229+
}
230+
// we need a data class if we need to support multiple specialization instances
231+
return specialization.getMaximumNumberOfInstances() > 1;
201232
}
202233

203234
private static boolean needsFrame(List<SpecializationData> specializations) {
@@ -392,7 +423,9 @@ private void createFields(CodeTypeElement clazz) {
392423
Class<?> annotationType;
393424
if (useNode) {
394425
annotationType = Child.class;
395-
cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
426+
if (specialization.getMaximumNumberOfInstances() > 1) {
427+
cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
428+
}
396429

397430
CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC),
398431
context.getType(NodeCost.class), "getCost");
@@ -401,7 +434,9 @@ private void createFields(CodeTypeElement clazz) {
401434
cacheType.add(getNodeCost);
402435
} else {
403436
annotationType = CompilationFinal.class;
404-
cacheType.add(createNodeField(null, cacheType.asType(), "next_", null, FINAL));
437+
if (specialization.getMaximumNumberOfInstances() > 1) {
438+
cacheType.add(createNodeField(null, cacheType.asType(), "next_", null, FINAL));
439+
}
405440
}
406441

407442
cacheType.getEnclosedElements().addAll(fields);
@@ -1220,7 +1255,7 @@ private Element createGetCostMethod() {
12201255

12211256
List<CodeTree> additionalChecks = new ArrayList<>();
12221257
for (SpecializationData specialization : reachableSpecializations) {
1223-
if (useSpecializationClass(specialization)) {
1258+
if (useSpecializationClass(specialization) && specialization.getMaximumNumberOfInstances() > 1) {
12241259
CodeTree check = builder.create().string("this.", createSpecializationFieldName(specialization), " == null || ", "this.", createSpecializationFieldName(specialization),
12251260
".next_ == null").build();
12261261
additionalChecks.add(check);
@@ -1871,19 +1906,19 @@ private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, Specialization
18711906
duplicationGuard = combineTrees(" && ", assumptionGuards);
18721907
}
18731908

1874-
String name = createSpecializationLocalName(specialization);
1909+
String specializationLocalName = createSpecializationLocalName(specialization);
18751910
if (useSpecializationClass) {
18761911
if (specialization.getMaximumNumberOfInstances() > 1) {
18771912
builder.declaration("int", countName, CodeTreeBuilder.singleString("0"));
18781913
}
1879-
builder.declaration(createSpecializationTypeName(specialization), name, CodeTreeBuilder.singleString(createSpecializationFieldName(specialization)));
1914+
builder.declaration(createSpecializationTypeName(specialization), specializationLocalName, CodeTreeBuilder.singleString(createSpecializationFieldName(specialization)));
18801915
builder.startIf().tree(stateCheck).end().startBlock();
18811916
if (specialization.getMaximumNumberOfInstances() > 1) {
18821917
builder.startWhile();
18831918
} else {
18841919
builder.startIf();
18851920
}
1886-
builder.string(name, " != null");
1921+
builder.string(specializationLocalName, " != null");
18871922
builder.end();
18881923
builder.startBlock();
18891924
} else {
@@ -1904,10 +1939,10 @@ private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, Specialization
19041939

19051940
if (useSpecializationClass) {
19061941
if (specialization.getMaximumNumberOfInstances() > 1) {
1907-
builder.startStatement().string(name, " = ", name, ".next_").end();
1942+
builder.startStatement().string(specializationLocalName, " = ", specializationLocalName, ".next_").end();
19081943
builder.statement(countName + "++");
19091944
} else {
1910-
builder.statement(name + " = null");
1945+
builder.statement(specializationLocalName + " = null");
19111946
}
19121947

19131948
builder.end();
@@ -1995,9 +2030,14 @@ private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, Specialization
19952030
}
19962031

19972032
builder.startStatement();
2033+
if (specialization.getMaximumNumberOfInstances() <= 1) {
2034+
builder.string(createSpecializationTypeName(specialization)).string(" ");
2035+
}
19982036
builder.string(createSpecializationLocalName(specialization), " = ");
19992037
builder.startNew(createSpecializationTypeName(specialization));
2000-
builder.string(createSpecializationFieldName(specialization));
2038+
if (specialization.getMaximumNumberOfInstances() > 1) {
2039+
builder.string(createSpecializationFieldName(specialization));
2040+
}
20012041
}
20022042

20032043
List<CodeTree> stateParameters = new ArrayList<>();
@@ -2149,7 +2189,7 @@ private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, Specialization
21492189
CodeTree guards = methodGuardAndAssertions[0];
21502190
CodeTree guardAssertions = methodGuardAndAssertions[1];
21512191

2152-
if (useSpecializationClass) {
2192+
if (useSpecializationClass && specialization.getMaximumNumberOfInstances() > 1) {
21532193
String name = createSpecializationLocalName(specialization);
21542194
builder.declaration(createSpecializationTypeName(specialization), name, CodeTreeBuilder.singleString(createSpecializationFieldName(specialization)));
21552195
builder.startWhile();
@@ -2185,7 +2225,7 @@ private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, Specialization
21852225
builder.end();
21862226
}
21872227

2188-
if (useSpecializationClass) {
2228+
if (useSpecializationClass && specialization.getMaximumNumberOfInstances() > 1) {
21892229
String name = createSpecializationLocalName(specialization);
21902230
builder.startStatement().string(name, " = ", name, ".next_").end();
21912231
}

truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/TypeSystemCodeGenerator.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,16 @@ private CodeExecutableElement createSpecializeImplictTypeMethodFlat(TypeMirror t
345345

346346
CodeTreeBuilder builder = method.createBuilder();
347347
boolean elseIf = false;
348-
int index = 1;
348+
int mask = 1;
349349
for (TypeMirror sourceType : sourceTypes) {
350350
elseIf = builder.startIf(elseIf);
351351
builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
352352
builder.end().startBlock();
353353
builder.startReturn();
354-
builder.string("0b", Integer.toBinaryString(index));
354+
builder.string("0b", Integer.toBinaryString(mask));
355355
builder.end();
356356
builder.end();
357-
index++;
357+
mask = mask << 1;
358358
}
359359

360360
builder.startElseBlock();
@@ -374,10 +374,10 @@ private CodeExecutableElement createExpectImplicitTypeMethodFlat(TypeMirror type
374374
CodeTreeBuilder builder = method.createBuilder();
375375
boolean elseIf = false;
376376

377-
int index = 1;
377+
int mask = 1;
378378
for (TypeMirror sourceType : sourceTypes) {
379379
elseIf = builder.startIf(elseIf);
380-
builder.string("(state & 0b").string(Integer.toBinaryString(index)).string(") != 0 && ");
380+
builder.string("(state & 0b").string(Integer.toBinaryString(mask)).string(") != 0 && ");
381381
builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
382382
builder.end().startBlock();
383383

@@ -392,7 +392,7 @@ private CodeExecutableElement createExpectImplicitTypeMethodFlat(TypeMirror type
392392
}
393393
builder.end();
394394
builder.end();
395-
index++;
395+
mask = mask << 1;
396396
}
397397

398398
builder.startElseBlock();
@@ -412,10 +412,10 @@ private CodeExecutableElement createAsImplicitTypeMethodFlat(TypeMirror type) {
412412
CodeTreeBuilder builder = method.createBuilder();
413413
boolean elseIf = false;
414414

415-
int index = 1;
415+
int mask = 1;
416416
for (TypeMirror sourceType : sourceTypes) {
417417
elseIf = builder.startIf(elseIf);
418-
builder.string("(state & 0b").string(Integer.toBinaryString(index)).string(") != 0 && ");
418+
builder.string("(state & 0b").string(Integer.toBinaryString(mask)).string(") != 0 && ");
419419
builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
420420
builder.end().startBlock();
421421

@@ -430,7 +430,7 @@ private CodeExecutableElement createAsImplicitTypeMethodFlat(TypeMirror type) {
430430
}
431431
builder.end();
432432
builder.end();
433-
index++;
433+
mask = mask << 1;
434434
}
435435

436436
builder.startElseBlock();
@@ -450,10 +450,10 @@ private CodeExecutableElement createIsImplicitTypeMethodFlat(TypeMirror type) {
450450

451451
builder.startReturn();
452452
String sep = "";
453-
int index = 1;
453+
int mask = 1;
454454
for (TypeMirror sourceType : sourceTypes) {
455455
builder.string(sep);
456-
builder.string("((state & 0b").string(Integer.toBinaryString(index)).string(") != 0 && ");
456+
builder.string("((state & 0b").string(Integer.toBinaryString(mask)).string(") != 0 && ");
457457
builder.tree(check(typeSystem, sourceType, LOCAL_VALUE));
458458
builder.string(")");
459459
if (sourceTypes.lastIndexOf(sourceType) != sourceTypes.size() - 1) {
@@ -463,7 +463,7 @@ private CodeExecutableElement createIsImplicitTypeMethodFlat(TypeMirror type) {
463463
builder.startIndention();
464464
}
465465
sep = " || ";
466-
index++;
466+
mask = mask << 1;
467467
}
468468
builder.end();
469469
builder.end();

0 commit comments

Comments
 (0)