qe-compiler
qe-compiler copied to clipboard
Support QASM's `const` in QUIR
QUIR should support const in statements like: const int[64] x = 2.
The QASM code uint x = 1; and const uint x = 1; emit the same mlir. Furthermore, in both cases, x may be mutated.
Suggestions
The information is available in the AST via node->IsConst(). So you would discrimination when processing the variable -declaration node here
https://github.com/Qiskit/qss-compiler/blob/7180bf9a96b6109c0ff9491c15f8330f5a4ff7ef/lib/Frontend/OpenQASM3/QUIRVariableBuilder.cpp#L34-L36
by including another parameter bool isConst. And select either
https://github.com/Qiskit/qss-compiler/blob/7180bf9a96b6109c0ff9491c15f8330f5a4ff7ef/lib/Frontend/OpenQASM3/QUIRVariableBuilder.cpp#L49
as now or declareOp = builder.create<mlir::quir::DeclareConstantOp>.
Then copy/paste this https://github.com/Qiskit/qss-compiler/blob/7180bf9a96b6109c0ff9491c15f8330f5a4ff7ef/include/Dialect/QUIR/IR/QUIROps.td#L137-L138 like this:
def QUIR_DeclareConstantOp : QUIR_Op<"declare_constant", [Symbol]> {
And give the constant the same semantics except that it can't be assigned to. Even if adding an operation declare_constant is the right approach, some kind of refactoring rather than copying 50 lines verbatim should be done.
I think extending the DeclareVariableOp to indicate whether a variable is a constant is cleaner than introducing a separate op for constant variables. In addition to the operation itself, we would also introduce (mostly) duplicate code in all scenarios that reference variables (be they constant or not).
- Operations for variable assignment should have their MLIR verifier check that the target variable is not constant constant
- The AST to MLIR generation step should check as well and provide an actionable diagnostic message.