Skip to content

Commit c288023

Browse files
flofridayJozott00
andauthored
frontend: Add alias resource type splitting (#510)
* frontend: Add alias resource type splitting * frontend: Improve alias resource type splitting * frontend: Fixing write value --------- Co-authored-by: Johannes Zottele <official@johannes-zottele.at>
1 parent 0d77fe3 commit c288023

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

vadl/main/vadl/ast/BehaviorLowering.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,20 @@ private ExpressionNode buildConstraintDontMatchCheck(List<ExpressionNode> indice
245245
: or(constChecks);
246246
}
247247

248+
/**
249+
* The function returned represents how the read from the acutal resource will be transformed
250+
* to the value the alias read actually returns.
251+
*
252+
* @param definition of the alias definition for which the read function will be generated.
253+
* @return the mapping function.
254+
*/
248255
Function getRegisterAliasReadFunc(AliasDefinition definition) {
249256
var graph = new Graph("%s Read Behavior".formatted(definition.viamId));
250257
graph.setSourceLocation(definition.location());
251258
currentGraph = graph;
252259

253260
final var identifier =
254261
viamLowering.generateIdentifier(definition.viamId + "::read", definition.loc);
255-
final var regFileDef = (RegisterDefinition) Objects.requireNonNull(definition.computedTarget);
256-
final var zeroConst = definition.getAnnotation("zero", ZeroConstraintAnnotation.class);
257262

258263
DataType resultType;
259264
// Initially the indices are all fixed arguments specified in the alias definition.
@@ -275,21 +280,23 @@ Function getRegisterAliasReadFunc(AliasDefinition definition) {
275280
0);
276281
params.add(param);
277282
indices.add(new FuncParamNode(param));
278-
resultType = relType.resultType().asDataType();
283+
resultType = getViamType(relType.resultType()).asDataType();
279284
} else {
280-
resultType = definition.type().asDataType();
285+
resultType = getViamType(definition.type()).asDataType();
281286
}
282287

288+
final var regFileDef = (RegisterDefinition) Objects.requireNonNull(definition.computedTarget);
283289
var reg = (RegisterTensor) viamLowering.fetch(regFileDef).orElseThrow();
284290
var regReadType = regFileDef.type() instanceof ConcreteRelationType relType
285-
? relType.resultType().asDataType() : resultType.asDataType();
291+
? relType.resultType().asDataType() : resultType;
286292
ExpressionNode regAccess = new ReadRegTensorNode(
287293
reg,
288294
indices,
289295
regReadType,
290296
null
291297
);
292298

299+
final var zeroConst = definition.getAnnotation("zero", ZeroConstraintAnnotation.class);
293300
if (zeroConst != null) {
294301
// Wrap the register read in a conditional read, depending on the indices values.
295302
// Compatibility was already checked by the annotation itself during type checking.
@@ -314,7 +321,7 @@ Function getRegisterAliasReadFunc(AliasDefinition definition) {
314321
return new Function(
315322
identifier,
316323
params.toArray(vadl.viam.Parameter[]::new),
317-
getViamType(resultType),
324+
resultType,
318325
graph
319326
);
320327
}
@@ -349,16 +356,16 @@ Procedure getRegisterAliasWriteProc(AliasDefinition definition) {
349356
0);
350357
params.add(param);
351358
indices.add(new FuncParamNode(param));
352-
resultType = relType.resultType().asDataType();
359+
resultType = getViamType(relType.resultType()).asDataType();
353360
} else {
354-
resultType = definition.type().asDataType();
361+
resultType = getViamType(definition.type()).asDataType();
355362
}
356363

357364
var valueParam = new vadl.viam.Parameter(
358365
viamLowering.generateIdentifier(
359366
identifier.name() + "::value",
360367
identifier.location()),
361-
getViamType(resultType),
368+
resultType,
362369
1);
363370
params.add(valueParam);
364371

@@ -1167,7 +1174,7 @@ private ExpressionNode sliceWriteValue(ExpressionNode value,
11671174
consumed += part.size();
11681175
}
11691176

1170-
var mask = slice.mask().castTo(Type.bits(entireRead.type().bitWidth())).toNode();
1177+
var mask = slice.mask().castTo(Type.bits(entireRead.type().bitWidth())).not().toNode();
11711178
var clearedResource = BuiltInCall.of(BuiltInTable.AND, entireRead, mask);
11721179
return BuiltInCall.of(BuiltInTable.OR, clearedResource, Objects.requireNonNull(injected));
11731180
}

vadl/main/vadl/ast/Expr.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,15 @@ public TypeLiteral(Type type, SourceLocation loc) {
14051405
this.baseType = new Identifier(formatType.format.identifier().name,
14061406
SourceLocation.INVALID_SOURCE_LOCATION);
14071407
this.sizeIndices = List.of();
1408+
} else if (type instanceof TensorType tensortype) {
1409+
var innerLiteral = new TypeLiteral(tensortype.innerType(), loc);
1410+
this.baseType = innerLiteral.baseType;
1411+
var indices = new ArrayList<Expr>();
1412+
for (var i : tensortype.indexDims()) {
1413+
indices.add(new IntegerLiteral(Integer.toString(i), loc));
1414+
}
1415+
indices.addAll(innerLiteral.sizeIndices);
1416+
this.sizeIndices = indices;
14081417
} else {
14091418
throw new IllegalStateException("Unsupported type " + type.getClass().getSimpleName());
14101419
}

vadl/main/vadl/ast/TypeChecker.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,8 +1008,21 @@ public Void visit(AliasDefinition definition) {
10081008

10091009
var valType = definition.value.type();
10101010
if (definition.aliasType != null) {
1011-
definition.type = check(definition.aliasType);
1012-
definition.value = tryWrapImplicitCast(definition.value, definition.type);
1011+
var aliasType = check(definition.aliasType);
1012+
definition.type = aliasType;
1013+
1014+
// We have special casting rules here.
1015+
// They aren't as strict as implicit casting and not as lax as explicit casting.
1016+
// FIXME: Should we allow same rules as explicit casts here?
1017+
if (canImplicitCast(valType, aliasType)) {
1018+
definition.value = tryWrapImplicitCast(definition.value, definition.type);
1019+
} else if (aliasType instanceof DataType aliasDataType
1020+
&& valType instanceof DataType valDataType
1021+
&& aliasDataType.bitWidth() == valDataType.bitWidth()) {
1022+
definition.value = new CastExpr(definition.value, aliasType);
1023+
} else {
1024+
throw typeMismatchError(definition.value, aliasType, valType);
1025+
}
10131026
} else {
10141027
definition.type = valType;
10151028
}

0 commit comments

Comments
 (0)