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