LLZK 2.0.0
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/Felt/IR/Types.td"
16include "llzk/Dialect/Shared/OpsBase.td"
17include "mlir/Interfaces/InferTypeOpInterface.td"
18include "llzk/Dialect/Function/IR/OpTraits.td"
19
20include "mlir/IR/OpBase.td"
21include "mlir/Interfaces/SideEffectInterfaces.td"
22include "mlir/IR/SymbolInterfaces.td"
23
24class CastOp<string mnemonic, list<Trait> traits = []>
25 : NaryOpBase<CastDialect, mnemonic, LLZK_FeltType.builderCall, traits>;
26
27def LLZK_IntToFeltOp
28 : CastOp<
29 "tofelt", [Pure,
30 DeclareOpInterfaceMethods<
31 InferTypeOpInterface, ["isCompatibleReturnTypes"]>]> {
32 let summary = "convert an integer into a field element";
33 let description = [{
34 This operation converts a supported integer type value into a field element value.
35
36 Example:
37
38 ```llzk
39 %0 = bool.cmp lt(%a, %b)
40 %1 = cast.tofelt %0 : i1
41 ```
42 }];
43
44 let arguments = (ins AnyLLZKIntType:$value);
45 let results = (outs LLZK_FeltType:$result);
46 let assemblyFormat =
47 [{ $value `:` type($value) `` custom<InferredOrParsedType>(type($result), "false") attr-dict }];
48
49 // Children of NaryOpBase must add their extra C++ code in the extra-extra
50 // version.
51 let extraExtraClassDeclaration = [{
52 static bool isCompatibleReturnTypes(::mlir::TypeRange lhs, ::mlir::TypeRange rhs);
53 }];
54}
55
56def LLZK_FeltToIndexOp : CastOp<"toindex", [Pure, NotFieldNative]> {
57 let summary = "convert a field element into an index";
58 let description = [{
59 This operation converts a field element value into an index value to allow use
60 as an array index or loop bound.
61
62 Example:
63 ```llzk
64 %0 = cast.toindex %a
65 %1 = array.read %b[%0]
66 ```
67 }];
68
69 let arguments = (ins LLZK_FeltType:$value);
70 let results = (outs Index:$result);
71 let assemblyFormat =
72 [{ $value `` custom<InferredOrParsedType>(type($value), "true") attr-dict }];
73}
74
75#endif // LLZK_CAST_OPS