LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Ops.td
Go to the documentation of this file.
1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLZK_CONSTRAIN_OPS
11#define LLZK_CONSTRAIN_OPS
12
13include "llzk/Dialect/Array/IR/Types.td"
14include "llzk/Dialect/Constrain/IR/Dialect.td"
15include "llzk/Dialect/Constrain/IR/OpInterfaces.td"
16include "llzk/Dialect/Function/IR/OpTraits.td"
17include "llzk/Dialect/Shared/Types.td"
18include "llzk/Dialect/Shared/OpTraits.td"
19
20include "mlir/Interfaces/SideEffectInterfaces.td"
21include "mlir/IR/OpBase.td"
22include "mlir/IR/SymbolInterfaces.td"
23
24//===------------------------------------------------------------------===//
25// Constraint emit operations
26//===------------------------------------------------------------------===//
27
28class EmitOp<string mnemonic, list<Trait> traits = []>
29 : Op<ConstrainDialect, mnemonic,
30 traits#[ConstraintOpInterface, ConstraintGen,
31 DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {}
32
33def EmitEqualityOp
34 : EmitOp<"eq", [Commutative, ElementwiseMappable, TypesUnify<"lhs", "rhs">,
35]> {
36 let summary = "emit an equality constraint";
37 let description = [{
38 Emits an equality constraint between lhs and rhs. For the built-in Signal
39 struct, the constraint is performed on the Signal's inner `@reg` felt field.
40 }];
41
42 let arguments = (ins EmitEqType:$lhs, EmitEqType:$rhs);
43
44 let assemblyFormat = [{
45 $lhs `,` $rhs `:` type($lhs)
46 `` custom<InferredOrParsedType>(type($rhs), ref(type($lhs)))
47 attr-dict
48 }];
49
50 let extraClassDeclaration = [{
51 static ::mlir::Type inferRHS(::mlir::Type lhsType);
52
53 static ::mlir::ParseResult parseInferredOrParsedType(::mlir::OpAsmParser &parser,
54 ::mlir::Type &rhsType, ::mlir::Type lhsType
55 ) {
56 if (mlir::succeeded(parser.parseOptionalComma())) {
57 // If there is a comma, parse the `rhsType`
58 mlir::Type type;
59 if (parser.parseCustomTypeWithFallback(type)) {
60 return mlir::failure();
61 }
62 rhsType = type;
63 } else {
64 // Otherwise, infer the `rhsType` from `lhsType`
65 rhsType = inferRHS(lhsType);
66 }
67 return mlir::success();
68 }
69
70 static void printInferredOrParsedType(::mlir::OpAsmPrinter &printer,
71 ::mlir::Operation *op, ::mlir::Type rhsType, ::mlir::Type lhsType
72 ) {
73 printer << ", ";
74 printer.printStrippedAttrOrType(rhsType);
75 }
76 }];
77}
78
79def EmitContainmentOp : EmitOp<"in"> {
80 let summary = "emit a containment constraint";
81 let description = [{
82 Emits a containment constraint between lhs and rhs (rhs must be contained
83 within lhs). If lhs is an N-dimensional array (N >= 1), rhs must be an
84 M-dimensional array, where N >= M (M=0 means that rhs is a scalar element
85 of type accepted by the `constrain.eq` operation).
86 }];
87
88 let arguments = (ins LLZK_ArrayType:$lhs, AnyLLZKType:$rhs);
89
90 let assemblyFormat = [{
91 $lhs `,` $rhs `:` type($lhs) `,` type($rhs) attr-dict
92 }];
93
94 let hasVerifier = true;
95}
96
97#endif // LLZK_CONSTRAIN_OPS