1//===-- OpsBase.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/OpTraits.td"
14include "llzk/Dialect/Polymorphic/IR/Types.td"
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
22 if (mlir::succeeded(isFirst ? parser.parseOptionalColon() : parser.parseOptionalComma())) {
23 // If there is a comma, parse the `opType`
25 if (parser.parseCustomTypeWithFallback(type)) {
26 return mlir::failure();
30 // Otherwise, build the default type
32 }]#!subst("$_builder", "parser.getBuilder()", defaultTypeBuilder)#[{;
34 return mlir::success();
37 static void printInferredOrParsedType(::mlir::OpAsmPrinter &printer,
38 ::mlir::Operation *op, ::mlir::Type opType, bool isFirst
40 printer << (isFirst ? " : " : ", ");
41 printer.printStrippedAttrOrType(opType);
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">]> {
53 let arguments = (ins VarTypeOr<resultType>:$lhs, VarTypeOr<resultType>:$rhs);
54 let results = (outs resultType:$result);
56 let assemblyFormat = [{
58 `` custom<InferredOrParsedType>(type($lhs), "true")
59 `` custom<InferredOrParsedType>(type($rhs), "false")
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">]> {
70 let arguments = (ins VarTypeOr<resultType>:$operand);
71 let results = (outs resultType:$result);
73 let assemblyFormat = [{
75 `` custom<InferredOrParsedType>(type($operand), "true")
80#endif // LLZK_OPS_BASE