LLZK 0.1.0
Veridise's ZK Language IR
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 let extraClassDeclaration = [{
19 static ::mlir::ParseResult parseInferredOrParsedType(
20 ::mlir::OpAsmParser &parser, ::mlir::Type &opType, bool isFirst
21 ) {
22 if (mlir::succeeded(isFirst ? parser.parseOptionalColon() : parser.parseOptionalComma())) {
23 // If there is a comma, parse the `opType`
24 mlir::Type type;
25 if (parser.parseCustomTypeWithFallback(type)) {
26 return mlir::failure();
27 }
28 opType = type;
29 } else {
30 // Otherwise, build the default type
31 opType =
32 }]#!subst("$_builder", "parser.getBuilder()", defaultTypeBuilder)#[{;
33 }
34 return mlir::success();
35 }
36
37 static void printInferredOrParsedType(::mlir::OpAsmPrinter &printer,
38 ::mlir::Operation *op, ::mlir::Type opType, bool isFirst
39 ) {
40 printer << (isFirst ? " : " : ", ");
41 printer.printStrippedAttrOrType(opType);
42 }
43 }];
44}
45
46// Note: `resultType.builderCall` must not be empty
47class BinaryOpBase<Dialect dialect, string mnemonic, Type resultType,
48 list<Trait> traits = []>
49 : NaryOpBase<dialect, mnemonic, resultType.builderCall,
50 traits#[Pure, TypeUnifyWithResult<"lhs">,
51 TypeUnifyWithResult<"rhs">]> {
52
53 let arguments = (ins VarTypeOr<resultType>:$lhs, VarTypeOr<resultType>:$rhs);
54 let results = (outs resultType:$result);
55
56 let assemblyFormat = [{
57 $lhs `,` $rhs
58 `` custom<InferredOrParsedType>(type($lhs), "true")
59 `` custom<InferredOrParsedType>(type($rhs), "false")
60 attr-dict
61 }];
62}
63
64// Note: `resultType.builderCall` must not be empty
65class UnaryOpBase<Dialect dialect, string mnemonic, Type resultType,
66 list<Trait> traits = []>
67 : NaryOpBase<dialect, mnemonic, resultType.builderCall,
68 traits#[Pure, TypeUnifyWithResult<"operand">]> {
69
70 let arguments = (ins VarTypeOr<resultType>:$operand);
71 let results = (outs resultType:$result);
72
73 let assemblyFormat = [{
74 $operand
75 `` custom<InferredOrParsedType>(type($operand), "true")
76 attr-dict
77 }];
78}
79
80#endif // LLZK_OPS_BASE