LLZK 2.1.1
An open-source IR for Zero Knowledge (ZK) circuits
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_CAST_OPS
11#define LLZK_CAST_OPS
12
13include "llzk/Dialect/Shared/Types.td"
14include "llzk/Dialect/Cast/IR/Dialect.td"
15include "llzk/Dialect/Cast/IR/Attrs.td"
16include "llzk/Dialect/Felt/IR/Types.td"
17include "llzk/Dialect/Shared/OpsBase.td"
18include "mlir/Interfaces/InferTypeOpInterface.td"
19include "llzk/Dialect/Function/IR/OpTraits.td"
20
21include "mlir/IR/OpBase.td"
22include "mlir/Interfaces/SideEffectInterfaces.td"
23include "mlir/IR/SymbolInterfaces.td"
24
25class CastOp<string mnemonic, list<Trait> traits = []>
26 : NaryOpBase<CastDialect, mnemonic, LLZK_FeltType.builderCall, traits>;
27
28def LLZK_IntToFeltOp
29 : CastOp<
30 "tofelt", [Pure,
31 DeclareOpInterfaceMethods<
32 InferTypeOpInterface, ["isCompatibleReturnTypes"]>]> {
33 let summary = "convert an integer into a field element";
34 let description = [{
35 This operation converts a supported integer type value into a field element value.
36
37 Example:
38
39 ```llzk
40 %0 = bool.cmp lt(%a, %b)
41 %1 = cast.tofelt %0 : i1
42 ```
43 }];
44
45 let arguments = (ins DefaultValuedAttr<LLZK_OverflowSemanticsAttr,
46 "OverflowSemantics::ASSERT">:$overflow,
47 AnyLLZKIntType:$value);
48 let results = (outs LLZK_FeltType:$result);
49 let assemblyFormat = [{
50 `` custom<OptionalOverflowSemantics>($overflow) $value
51 `:` type($value)
52 `` custom<InferredOrParsedType>(type($result), "false") attr-dict
53 }];
54
55 // Children of NaryOpBase must add their extra C++ code in the extra-extra
56 // version.
57 let extraExtraClassDeclaration = [{
58 static bool isCompatibleReturnTypes(::mlir::TypeRange lhs, ::mlir::TypeRange rhs);
59 static ::mlir::ParseResult parseOptionalOverflowSemantics(
60 ::mlir::OpAsmParser &parser, ::llzk::cast::OverflowSemanticsAttr &overflow
61 );
62
63 static void printOptionalOverflowSemantics(
64 ::mlir::OpAsmPrinter &printer, IntToFeltOp op,
65 ::llzk::cast::OverflowSemanticsAttr overflow
66 );
67 }];
68
69 let hasCanonicalizeMethod = 1;
70}
71
72def LLZK_FeltToIndexOp : CastOp<"toindex", [Pure, NotFieldNative]> {
73 let summary = "convert a field element into an index";
74 let description = [{
75 This operation converts a field element value into an index value to allow use
76 as an array index or loop bound.
77
78 Example:
79 ```llzk
80 %0 = cast.toindex %a
81 %1 = array.read %b[%0]
82 ```
83 }];
84
85 let arguments = (ins DefaultValuedAttr<LLZK_OverflowSemanticsAttr,
86 "OverflowSemantics::ASSERT">:$overflow,
87 LLZK_FeltType:$value);
88 let results = (outs Index:$result);
89 let assemblyFormat = [{
90 `` custom<OptionalOverflowSemantics>($overflow) $value
91 `` custom<InferredOrParsedType>(type($value), "true") attr-dict
92 }];
93
94 let extraExtraClassDeclaration = [{
95 static ::mlir::ParseResult parseOptionalOverflowSemantics(
96 ::mlir::OpAsmParser &parser, ::llzk::cast::OverflowSemanticsAttr &overflow
97 );
98
99 static void printOptionalOverflowSemantics(
100 ::mlir::OpAsmPrinter &printer, FeltToIndexOp op,
101 ::llzk::cast::OverflowSemanticsAttr overflow
102 );
103 }];
104
105 let hasCanonicalizeMethod = 1;
106}
107
108#endif // LLZK_CAST_OPS