LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
OpsBase.td
Go to the documentation of this file.
1//===-- OpsBase.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_OPS_BASE
11#define LLZK_OPS_BASE
12
13include "llzk/Dialect/Shared/OpTraits.td"
14include "llzk/Dialect/Polymorphic/IR/Types.td"
15
16class NaryOpBase<Dialect dialect, string mnemonic, string defaultTypeBuilder,
17 list<Trait> traits = []> : Op<dialect, mnemonic, traits> {
18 // Extra code added by implementations of this class.
19 code extraExtraClassDeclaration = [{}];
20
21 let extraClassDeclaration = extraExtraClassDeclaration#[{
22 private:
23 static ::mlir::ParseResult parseInferredOrParsedType(
24 ::mlir::OpAsmParser &parser, ::mlir::Type &opType, bool isFirst
25 ) {
26 if (mlir::succeeded(isFirst ? parser.parseOptionalColon() : parser.parseOptionalComma())) {
27 // If there is a comma, parse the `opType`
28 mlir::Type type;
29 if (parser.parseCustomTypeWithFallback(type)) {
30 return mlir::failure();
31 }
32 opType = type;
33 } else {
34 // Otherwise, build the default type
35 opType =
36 }]#!subst("$_builder", "parser.getBuilder()", defaultTypeBuilder)#[{;
37 }
38 return mlir::success();
39 }
40
41 static void printInferredOrParsedType(::mlir::OpAsmPrinter &printer,
42 ::mlir::Operation *op, ::mlir::Type opType, bool isFirst
43 ) {
44 printer << (isFirst ? " : " : ", ");
45 printer.printStrippedAttrOrType(opType);
46 }
47 }];
48}
49
50// Note: `resultType.builderCall` must not be empty
51class BinaryOpBase<Dialect dialect, string mnemonic, Type resultType,
52 list<Trait> traits = []>
53 : NaryOpBase<dialect, mnemonic, resultType.builderCall,
54 traits#[Pure, TypeUnifyWithResult<"lhs">,
55 TypeUnifyWithResult<"rhs">]> {
56
57 let arguments = (ins VarTypeOr<resultType>:$lhs, VarTypeOr<resultType>:$rhs);
58 let results = (outs resultType:$result);
59
60 let assemblyFormat = [{
61 $lhs `,` $rhs
62 `` custom<InferredOrParsedType>(type($lhs), "true")
63 `` custom<InferredOrParsedType>(type($rhs), "false")
64 attr-dict
65 }];
66}
67
68// Note: `resultType.builderCall` must not be empty
69class UnaryOpBase<Dialect dialect, string mnemonic, Type resultType,
70 list<Trait> traits = []>
71 : NaryOpBase<dialect, mnemonic, resultType.builderCall,
72 traits#[Pure, TypeUnifyWithResult<"operand">]> {
73
74 let arguments = (ins VarTypeOr<resultType>:$operand);
75 let results = (outs resultType:$result);
76
77 let assemblyFormat = [{
78 $operand
79 `` custom<InferredOrParsedType>(type($operand), "true")
80 attr-dict
81 }];
82}
83
84#endif // LLZK_OPS_BASE