Skip to content

Commit 3fdd7ac

Browse files
committed
frontend: Add zero register constraint annotation
1 parent 10bc17e commit 3fdd7ac

File tree

5 files changed

+91
-22
lines changed

5 files changed

+91
-22
lines changed

vadl/main/vadl/ast/AnnotationTable.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import vadl.utils.WithLocation;
4141
import vadl.utils.functionInterfaces.TriConsumer;
4242
import vadl.viam.AssemblyDescription;
43+
import vadl.viam.Constant;
4344
import vadl.viam.MemoryRegion;
45+
import vadl.viam.RegisterTensor;
4446
import vadl.viam.Relocation;
4547
import vadl.viam.annotations.AsmParserCaseSensitive;
4648
import vadl.viam.annotations.AsmParserCommentString;
@@ -73,9 +75,13 @@ class AnnotationTable {
7375
// FIXME: Apply to AST
7476
.build();
7577

76-
annotationOn(RegisterFileDefinition.class, "zero", ExprAnnotation::new)
77-
// FIXME: Typecheck
78-
// FIXME: Apply to VIAM
78+
annotationOn(RegisterFileDefinition.class, "zero", ZeroConstraintAnnotation::new)
79+
.applyViam((def, annotation, lowering) -> {
80+
var viamDef = (RegisterTensor) def;
81+
var indices = annotation.indices.stream().map(ConstantValue::toViamConstant).toList();
82+
var zero = Constant.Value.of(0, viamDef.resultType(indices.size()));
83+
viamDef.addConstraint(new RegisterTensor.Constraint(indices, zero));
84+
})
7985
.build();
8086

8187
groupOn(RelocationDefinition.class)
@@ -1045,4 +1051,63 @@ void typeCheck(AnnotationDefinition definition, TypeChecker typeChecker) {
10451051
public String usageString() {
10461052
return "[ " + name + " : <expr> ]";
10471053
}
1054+
10481055
}
1056+
1057+
/**
1058+
* The {@code [ zero : <register>(<expr>) ]} annotation.
1059+
* This is its own class, as the typechecking is rather complex and determines
1060+
* new properties.
1061+
*/
1062+
class ZeroConstraintAnnotation extends ExprAnnotation {
1063+
@LazyInit
1064+
List<ConstantValue> indices;
1065+
1066+
@Override
1067+
void typeCheck(AnnotationDefinition definition, TypeChecker typeChecker) {
1068+
super.typeCheck(definition, typeChecker);
1069+
var def = definition.target;
1070+
if (!(node instanceof CallIndexExpr callExpr)) {
1071+
throw error("Invalid zero annotation", this)
1072+
.locationDescription(this, "Zero annotation must be of form %s", usageString())
1073+
.build();
1074+
}
1075+
if (!(callExpr.computedTarget == def)) {
1076+
throw error("Invalid zero annotation", callExpr.target)
1077+
.locationDescription(callExpr.target,
1078+
"Zero annotation target must be the annotated register.")
1079+
.locationNote(def, "This is the register to target.")
1080+
.build();
1081+
}
1082+
1083+
1084+
var args = callExpr.argIndicesFlatten();
1085+
// FIXME: Ones we have multi dimensional registers,
1086+
// we must loose this restriction, so that there can be multiple indices set.
1087+
if (args.size() != 1) {
1088+
throw error("Invalid zero annotation", callExpr)
1089+
.locationDescription(callExpr, "Exactly one register index was expected, but found %s.",
1090+
args.size())
1091+
.note("In the future it will be possible to have constraints on multiple dimensions.")
1092+
.build();
1093+
}
1094+
1095+
this.indices = args.stream().map(expr -> {
1096+
try {
1097+
return typeChecker.constantEvaluator.eval(expr);
1098+
} catch (EvaluationError e) {
1099+
throw error("Invalid zero annotation", expr)
1100+
.locationDescription(expr, "Index must be a constant expression.")
1101+
.locationNote(def, "%s", requireNonNull(e.getMessage()))
1102+
.build();
1103+
}
1104+
}).toList();
1105+
1106+
}
1107+
1108+
@Override
1109+
public String usageString() {
1110+
return "[" + name + " : " + "<register>( <expr> ) ]";
1111+
}
1112+
}
1113+

vadl/main/vadl/ast/Expr.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,10 @@ void replaceArgsFor(int index, List<Expr> newArgs) {
15861586
args.values.addAll(newArgs);
15871587
}
15881588

1589+
List<Expr> argIndicesFlatten() {
1590+
return argsIndices.stream().flatMap(a -> a.values.stream()).toList();
1591+
}
1592+
15891593

15901594
@Override
15911595
public IsId path() {

vadl/main/vadl/ast/ViamLowering.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,7 @@ public Optional<vadl.viam.Definition> visit(CounterDefinition definition) {
782782

783783
// FIXME: Further research for the parameters (probably don't apply to counter)
784784
var reg = new RegisterTensor(identifier,
785-
List.of(dimFromType(0, resultType)),
786-
new RegisterTensor.Constraint[] {}
785+
List.of(dimFromType(0, resultType))
787786
);
788787

789788
var counter = new Counter(identifier,
@@ -1388,8 +1387,7 @@ public Optional<vadl.viam.Definition> visit(RegisterDefinition definition) {
13881387
var resultType = (DataType) getViamType(definition.type());
13891388
var reg = new RegisterTensor(
13901389
generateIdentifier(definition.viamId, definition.identifier()),
1391-
List.of(dimFromType(0, resultType)),
1392-
new RegisterTensor.Constraint[] {}
1390+
List.of(dimFromType(0, resultType))
13931391
);
13941392
return Optional.of(reg);
13951393
}
@@ -1399,18 +1397,12 @@ public Optional<vadl.viam.Definition> visit(RegisterFileDefinition definition) {
13991397
var addrType = (DataType) getViamType(requireNonNull(definition.type).argTypes().get(0));
14001398
var resultType = (DataType) getViamType(requireNonNull(definition.type).resultType());
14011399

1402-
// FIXME: Add proper constraints. This is currently only temporarily hardcoded to
1403-
// fix the riscv iss simulation.
1404-
var zeroConstraint = new RegisterTensor.Constraint(List.of(Constant.Value.of(0, addrType)),
1405-
Constant.Value.of(0, resultType));
1406-
14071400
var regFile = new RegisterTensor(
14081401
generateIdentifier(definition.viamId, definition.identifier()),
14091402
List.of(
14101403
dimFromMappingType(0, addrType),
14111404
dimFromType(1, resultType)
1412-
),
1413-
new RegisterTensor.Constraint[] {zeroConstraint}
1405+
)
14141406
);
14151407
return Optional.of(regFile);
14161408
}

vadl/main/vadl/lcb/template/lib/Target/EmitISelLoweringCppFilePass.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.Objects;
2726
import java.util.function.Supplier;
2827
import javax.annotation.Nullable;
2928
import vadl.configuration.LcbConfiguration;
@@ -78,8 +77,10 @@ static class LlvmRegisterFile extends RegisterTensor {
7877
**/
7978
public LlvmRegisterFile(RegisterTensor registerFile) {
8079
super(registerFile.identifier,
81-
registerFile.dimensions(),
82-
registerFile.constraints());
80+
registerFile.dimensions());
81+
for (var c : registerFile.constraints()) {
82+
addConstraint(c);
83+
}
8384
}
8485

8586
public String llvmResultType() {

vadl/main/vadl/viam/RegisterTensor.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package vadl.viam;
1818

1919
import com.google.common.collect.Streams;
20+
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.List;
2223
import java.util.Optional;
@@ -75,16 +76,16 @@ public record Dimension(int index, DataType indexType, int size) {
7576
}
7677

7778
private final List<Dimension> dimensions;
78-
private final Constraint[] constraints;
79+
private final List<Constraint> constraints;
7980

8081
/**
8182
* Constructs the register tensor.
8283
*/
83-
public RegisterTensor(Identifier identifier, List<Dimension> dimensions,
84-
Constraint[] constraints) {
84+
public RegisterTensor(Identifier identifier,
85+
List<Dimension> dimensions) {
8586
super(identifier);
8687
this.dimensions = dimensions;
87-
this.constraints = constraints;
88+
this.constraints = new ArrayList<>();
8889
}
8990

9091
public int dimCount() {
@@ -103,6 +104,7 @@ public List<Dimension> indexDimensions() {
103104
return dimensions.subList(0, maxNumberOfAccessIndices());
104105
}
105106

107+
106108
public int maxNumberOfAccessIndices() {
107109
return dimCount() - 1;
108110
}
@@ -131,8 +133,13 @@ public Dimension innermostDim() {
131133
return dimensions.getLast();
132134
}
133135

136+
// TODO: Refactor this to return a list instead of an array
134137
public Constraint[] constraints() {
135-
return constraints;
138+
return constraints.toArray(new Constraint[0]);
139+
}
140+
141+
public void addConstraint(Constraint constraint) {
142+
constraints.add(constraint);
136143
}
137144

138145
@Override

0 commit comments

Comments
 (0)