1//===-- Ops.td ---------------------------------------------*- tablegen -*-===//
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
8//===----------------------------------------------------------------------===//
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"
20include "mlir/IR/OpBase.td"
21include "mlir/Interfaces/SideEffectInterfaces.td"
22include "mlir/IR/SymbolInterfaces.td"
24class CastOp<string mnemonic, list<Trait> traits = []>
25 : NaryOpBase<CastDialect, mnemonic, LLZK_FeltType.builderCall, traits>;
30 DeclareOpInterfaceMethods<
31 InferTypeOpInterface, ["isCompatibleReturnTypes"]>]> {
32 let summary = "convert an integer into a field element";
34 This operation converts a supported integer type value into a field element value.
39 %0 = bool.cmp lt(%a, %b)
40 %1 = cast.tofelt %0 : i1
44 let arguments = (ins AnyLLZKIntType:$value);
45 let results = (outs LLZK_FeltType:$result);
47 [{ $value `:` type($value) `` custom<InferredOrParsedType>(type($result), "false") attr-dict }];
49 // Children of NaryOpBase must add their extra C++ code in the extra-extra
51 let extraExtraClassDeclaration = [{
52 static bool isCompatibleReturnTypes(::mlir::TypeRange lhs, ::mlir::TypeRange rhs);
56def LLZK_FeltToIndexOp : CastOp<"toindex", [Pure, NotFieldNative]> {
57 let summary = "convert a field element into an index";
59 This operation converts a field element value into an index value to allow use
60 as an array index or loop bound.
65 %1 = array.read %b[%0]
69 let arguments = (ins LLZK_FeltType:$value);
70 let results = (outs Index:$result);
72 [{ $value `` custom<InferredOrParsedType>(type($value), "true") attr-dict }];
75#endif // LLZK_CAST_OPS