LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
OpHelpers.h
Go to the documentation of this file.
1//===-- OpHelpers.h ---------------------------------------------*- C++ -*-===//
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#pragma once
11
13#include "llzk/Util/Constants.h"
14
15#include <mlir/IR/OpImplementation.h>
16#include <mlir/IR/Operation.h>
17#include <mlir/Support/LogicalResult.h>
18
19#include <llvm/ADT/SmallString.h>
20#include <llvm/ADT/StringRef.h>
21
22namespace llzk {
23
27template <typename OpClass> inline llvm::StringLiteral getOperationName() {
28 return OpClass::getOperationName();
29}
30
32template <typename OpClass> inline OpClass getSelfOrParentOfType(mlir::Operation *op) {
33 if (op) {
34 if (OpClass self = llvm::dyn_cast<OpClass>(op)) {
35 return self;
36 }
37 if (OpClass parent = op->getParentOfType<OpClass>()) {
38 return parent;
39 }
40 }
41 return {};
42}
43
45template <typename OpClass> inline mlir::FailureOr<OpClass> getParentOfType(mlir::Operation *op) {
46 if (OpClass p = op->getParentOfType<OpClass>()) {
47 return p;
48 } else {
49 return mlir::failure();
50 }
51}
52
55template <int OperandSegmentIndex> struct VerifySizesForMultiAffineOps {
56 template <typename TypeClass> class Impl : public mlir::OpTrait::TraitBase<TypeClass, Impl> {
57 inline static mlir::LogicalResult verifyHelper(mlir::Operation *op, int32_t segmentSize) {
58 TypeClass c = llvm::cast<TypeClass>(op);
60 op, segmentSize, c.getMapOpGroupSizesAttr(), c.getMapOperands(), c.getNumDimsPerMapAttr()
61 );
62 }
63
64 public:
65 static mlir::LogicalResult verifyTrait(mlir::Operation *op) {
66 if (TypeClass::template hasTrait<mlir::OpTrait::AttrSizedOperandSegments>()) {
67 // If the AttrSizedOperandSegments trait is present, must have `OperandSegmentIndex`.
68 static_assert(
69 OperandSegmentIndex >= 0,
70 "When the `AttrSizedOperandSegments` trait is present, the index of `$mapOperands` "
71 "within the `operandSegmentSizes` attribute must be specified."
72 );
73 mlir::DenseI32ArrayAttr segmentSizes = op->getAttrOfType<mlir::DenseI32ArrayAttr>(
74 mlir::OpTrait::AttrSizedOperandSegments<TypeClass>::getOperandSegmentSizeAttr()
75 );
76 assert(
77 OperandSegmentIndex < segmentSizes.size() &&
78 "Parameter of `VerifySizesForMultiAffineOps` exceeds the number of ODS-declared "
79 "operands"
80 );
81 return verifyHelper(op, segmentSizes[OperandSegmentIndex]);
82 } else {
83 // If the trait is not present, the `OperandSegmentIndex` is ignored. Pass `-1` to indicate
84 // that the checks against `operandSegmentSizes` should be skipped.
85 return verifyHelper(op, -1);
86 }
87 }
88 };
89};
90
91template <unsigned N>
92inline mlir::ParseResult parseDimAndSymbolList(
93 mlir::OpAsmParser &parser,
94 mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand, N> &mapOperands,
95 mlir::IntegerAttr &numDims
96) {
97 return affineMapHelpers::parseDimAndSymbolList(parser, mapOperands, numDims);
98}
99
101 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRange mapOperands,
102 mlir::IntegerAttr numDims
103) {
104 return affineMapHelpers::printDimAndSymbolList(printer, op, mapOperands, numDims);
105}
106
107inline mlir::ParseResult parseMultiDimAndSymbolList(
108 mlir::OpAsmParser &parser,
109 mlir::SmallVector<mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>> &multiMapOperands,
110 mlir::DenseI32ArrayAttr &numDimsPerMap
111) {
112 return affineMapHelpers::parseMultiDimAndSymbolList(parser, multiMapOperands, numDimsPerMap);
113}
114
116 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands,
117 mlir::DenseI32ArrayAttr numDimsPerMap
118) {
119 return affineMapHelpers::printMultiDimAndSymbolList(printer, op, multiMapOperands, numDimsPerMap);
120}
121
122inline mlir::ParseResult parseAttrDictWithWarnings(
123 mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state
124) {
125 return affineMapHelpers::parseAttrDictWithWarnings(parser, extraAttrs, state);
126}
127
128template <typename ConcreteOp>
130 mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs,
131 typename mlir::PropertiesSelector<ConcreteOp>::type state
132) {
133 return affineMapHelpers::printAttrDictWithWarnings(printer, op, extraAttrs, state);
134}
135
136} // namespace llzk
static mlir::LogicalResult verifyTrait(mlir::Operation *op)
Definition OpHelpers.h:65
void printAttrDictWithWarnings(mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs, typename ConcreteOp::Properties state)
LogicalResult verifySizesForMultiAffineOps(Operation *op, int32_t segmentSize, ArrayRef< int32_t > mapOpGroupSizes, OperandRangeRange mapOperands, ArrayRef< int32_t > numDimsPerMap)
ParseResult parseMultiDimAndSymbolList(OpAsmParser &parser, SmallVectorImpl< SmallVector< OpAsmParser::UnresolvedOperand > > &multiMapOperands, DenseI32ArrayAttr &numDimsPerMap)
void printDimAndSymbolList(OpAsmPrinter &printer, Operation *op, OperandRange mapOperands, IntegerAttr numDims)
ParseResult parseDimAndSymbolList(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &mapOperands, IntegerAttr &numDims)
ParseResult parseAttrDictWithWarnings(OpAsmParser &parser, NamedAttrList &extraAttrs, OperationState &state)
void printMultiDimAndSymbolList(OpAsmPrinter &printer, Operation *op, OperandRangeRange multiMapOperands, DenseI32ArrayAttr numDimsPerMap)
void printDimAndSymbolList(mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRange mapOperands, mlir::IntegerAttr numDims)
Definition OpHelpers.h:100
mlir::ParseResult parseAttrDictWithWarnings(mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state)
Definition OpHelpers.h:122
void printMultiDimAndSymbolList(mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands, mlir::DenseI32ArrayAttr numDimsPerMap)
Definition OpHelpers.h:115
void printAttrDictWithWarnings(mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs, typename mlir::PropertiesSelector< ConcreteOp >::type state)
Definition OpHelpers.h:129
mlir::FailureOr< OpClass > getParentOfType(mlir::Operation *op)
Return the closest surrounding parent operation that is of type 'OpClass'.
Definition OpHelpers.h:45
mlir::ParseResult parseMultiDimAndSymbolList(mlir::OpAsmParser &parser, mlir::SmallVector< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &multiMapOperands, mlir::DenseI32ArrayAttr &numDimsPerMap)
Definition OpHelpers.h:107
llvm::StringLiteral getOperationName()
Get the operation name, like "constrain.eq" for the given OpClass.
Definition OpHelpers.h:27
OpClass getSelfOrParentOfType(mlir::Operation *op)
Return the closest operation that is of type 'OpClass', either the op itself or an ancestor.
Definition OpHelpers.h:32
mlir::ParseResult parseDimAndSymbolList(mlir::OpAsmParser &parser, mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand, N > &mapOperands, mlir::IntegerAttr &numDims)
Definition OpHelpers.h:92
Produces errors if there is an inconsistency in the various attributes/values that are used to suppor...
Definition OpHelpers.h:55