frontend: Group Array Type#976
Conversation
b57ba75 to
1e86643
Compare
|
I'll wait with the review until #918 got merged, so it becomes easier to see this PR's changes. |
1e86643 to
19f1b2b
Compare
Jozott00
left a comment
There was a problem hiding this comment.
Since this is unrelated to VIAM or ISS, I’ll leave the approval to @flofriday.
|
It will need to be added to the VIAM as well, but I didn't get to that yet. I switched it to a Draft PR, and amend it later. |
flofriday
left a comment
There was a problem hiding this comment.
Looks pretty good.
I obviously don't get why there is a need for the ArrayType 😅
Or how the groups work and I also couldn't find a lot in our documentation.
Maybe you can explain it in a comment and then disregard all the other comments.
| // error: Invalid annotation expression | ||
| // ╭── test/resources/frontend-snapshots/typechecker/invalidGroupReference.vadl:27:13 | ||
| // │ | ||
| // 27 │ [assert : VLIW.a = 0b0] | ||
| // │ ^^^^^^^^^^^^ Unable to determine type | ||
| // │ | ||
| // help: Check additional errors which may have caused the type to be missing |
There was a problem hiding this comment.
nitpick: I don't like this error, I think we can completely remove it as it doesn't add any useful information.
| // 27 │ [assert : VLIW.a = 0b0] | ||
| // │ ^^^^^^ | ||
| // │ | ||
| // Arrays only have the fields `length` or `bitLength` |
There was a problem hiding this comment.
question: Will this also be called an "Array" in the docs? Or is it something more specific like a Group Array?
I'm mostly asking because it could be confusing for users to name them Array when they only exist in a subsection of the language when the more useful analog from other languages is a Tensor.
| // Intersection format `(AType ∩ BType)` doesn't have any field with this name | ||
| // help: Did you maybe mean one of: a, b |
There was a problem hiding this comment.
**praise This is very readable, and a great error message. 🎉
| // 27 │ [assert : VLIW(1)(2).a = 0b0] | ||
| // │ ^^^^^^^ | ||
| // │ | ||
| // Type `a: Bits<20>, b: Bits<5>` cannot be indexed or sliced |
There was a problem hiding this comment.
minor: I don't love the Type description, maybe GroupType<a: Bits<20>, b: Bits<5>> makes it clearer?
I think we have something similar for FormatTypes.
| public static ArrayType array(Type elementType, UIntType lengthType, UIntType bitLengthType) { | ||
| var hash = Objects.hash(elementType, lengthType, bitLengthType); | ||
| return arrayTypes | ||
| .computeIfAbsent(hash, k -> new ArrayType(elementType, lengthType, bitLengthType)); |
There was a problem hiding this comment.
issue: This has hash-collisions. You need to wrap the contents in a datastructure, like a Triple.
|
|
||
| private final ConstantEvaluator constantEvaluator; | ||
|
|
||
| public GroupExprLengthCollector(ConstantEvaluator constantEvaluator) { |
There was a problem hiding this comment.
nitpick: Maybe this too should be a private constructor.
|
|
||
| private final ConstantEvaluator constantEvaluator; | ||
|
|
||
| public GroupExprBitLengthCollector(ConstantEvaluator constantEvaluator) { |
There was a problem hiding this comment.
nitpick: Same private constructor here.
| } | ||
|
|
||
| Type currType = typeBeforeSlice; | ||
| SourceLocation targetLoc = expr.location(); |
There was a problem hiding this comment.
nitpick: Calculating expression can be expensive in hot loops so let's add it to the only branch where we actually need it when there is an issue we need to report.
In general we optimize for the case where no errors occur.
|
|
||
| if (!(currType instanceof BitsType) && !(currType instanceof TensorType) | ||
| && !(currType instanceof ArrayType)) { | ||
| var loc = expr.target.location().join(targetLoc.location()); |
There was a problem hiding this comment.
minor: The .location() call is redundant on targetLoc.location() as it's already a SourceLocation.
| }; | ||
| } else { | ||
| addErrorAndStopChecking(error("Cannot resolve `%s`".formatted(fieldName), expr) | ||
| throw addErrorAndStopChecking(error("Cannot resolve `%s`".formatted(fieldName), expr) |
There was a problem hiding this comment.
question: Is there a reason you added the throw?
The group definition's name can be used in expressions (within group annotations) to access the matched instructions / operations at runtime. In addition it exposes a
lengthandbitLengthfield.I introduced a more general 'ArrayType' for such group references. In this case we're able to upper-bound the
lengthandbitLength, so we can also give these properties aUIntTypetype with some maximal width.