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 mlir::FailureOr<OpClass> getParentOfType(mlir::Operation *op) {
33 if (OpClass p = op->getParentOfType<OpClass>()) {
34 return p;
35 } else {
36 return mlir::failure();
37 }
38}
39
42template <int OperandSegmentIndex> struct VerifySizesForMultiAffineOps {
43 template <typename TypeClass> class Impl : public mlir::OpTrait::TraitBase<TypeClass, Impl> {
44 inline static mlir::LogicalResult verifyHelper(mlir::Operation *op, int32_t segmentSize) {
45 TypeClass c = llvm::cast<TypeClass>(op);
47 op, segmentSize, c.getMapOpGroupSizesAttr(), c.getMapOperands(), c.getNumDimsPerMapAttr()
48 );
49 }
50
51 public:
52 static mlir::LogicalResult verifyTrait(mlir::Operation *op) {
53 if (TypeClass::template hasTrait<mlir::OpTrait::AttrSizedOperandSegments>()) {
54 // If the AttrSizedOperandSegments trait is present, must have `OperandSegmentIndex`.
55 static_assert(
56 OperandSegmentIndex >= 0,
57 "When the `AttrSizedOperandSegments` trait is present, the index of `$mapOperands` "
58 "within the `operandSegmentSizes` attribute must be specified."
59 );
60 mlir::DenseI32ArrayAttr segmentSizes = op->getAttrOfType<mlir::DenseI32ArrayAttr>(
61 mlir::OpTrait::AttrSizedOperandSegments<TypeClass>::getOperandSegmentSizeAttr()
62 );
63 assert(
64 OperandSegmentIndex < segmentSizes.size() &&
65 "Parameter of `VerifySizesForMultiAffineOps` exceeds the number of ODS-declared "
66 "operands"
67 );
68 return verifyHelper(op, segmentSizes[OperandSegmentIndex]);
69 } else {
70 // If the trait is not present, the `OperandSegmentIndex` is ignored. Pass `-1` to indicate
71 // that the checks against `operandSegmentSizes` should be skipped.
72 return verifyHelper(op, -1);
73 }
74 }
75 };
76};
77
78template <unsigned N>
79inline mlir::ParseResult parseDimAndSymbolList(
80 mlir::OpAsmParser &parser,
81 mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand, N> &mapOperands,
82 mlir::IntegerAttr &numDims
83) {
84 return affineMapHelpers::parseDimAndSymbolList(parser, mapOperands, numDims);
85}
86
88 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRange mapOperands,
89 mlir::IntegerAttr numDims
90) {
91 return affineMapHelpers::printDimAndSymbolList(printer, op, mapOperands, numDims);
92}
93
94inline mlir::ParseResult parseMultiDimAndSymbolList(
95 mlir::OpAsmParser &parser,
96 mlir::SmallVector<mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>> &multiMapOperands,
97 mlir::DenseI32ArrayAttr &numDimsPerMap
98) {
99 return affineMapHelpers::parseMultiDimAndSymbolList(parser, multiMapOperands, numDimsPerMap);
100}
101
103 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands,
104 mlir::DenseI32ArrayAttr numDimsPerMap
105) {
106 return affineMapHelpers::printMultiDimAndSymbolList(printer, op, multiMapOperands, numDimsPerMap);
107}
108
109inline mlir::ParseResult parseAttrDictWithWarnings(
110 mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state
111) {
112 return affineMapHelpers::parseAttrDictWithWarnings(parser, extraAttrs, state);
113}
114
115template <typename ConcreteOp>
117 mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs,
118 typename mlir::PropertiesSelector<ConcreteOp>::type state
119) {
120 return affineMapHelpers::printAttrDictWithWarnings(printer, op, extraAttrs, state);
121}
122
123} // namespace llzk
static mlir::LogicalResult verifyTrait(mlir::Operation *op)
Definition OpHelpers.h:52
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:87
mlir::ParseResult parseAttrDictWithWarnings(mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state)
Definition OpHelpers.h:109
void printMultiDimAndSymbolList(mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands, mlir::DenseI32ArrayAttr numDimsPerMap)
Definition OpHelpers.h:102
void printAttrDictWithWarnings(mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs, typename mlir::PropertiesSelector< ConcreteOp >::type state)
Definition OpHelpers.h:116
mlir::FailureOr< OpClass > getParentOfType(mlir::Operation *op)
Return the closest surrounding parent operation that is of type 'OpClass'.
Definition OpHelpers.h:32
mlir::ParseResult parseMultiDimAndSymbolList(mlir::OpAsmParser &parser, mlir::SmallVector< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &multiMapOperands, mlir::DenseI32ArrayAttr &numDimsPerMap)
Definition OpHelpers.h:94
llvm::StringLiteral getOperationName()
Get the operation name, like "constrain.eq" for the given OpClass.
Definition OpHelpers.h:27
mlir::ParseResult parseDimAndSymbolList(mlir::OpAsmParser &parser, mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand, N > &mapOperands, mlir::IntegerAttr &numDims)
Definition OpHelpers.h:79
Produces errors if there is an inconsistency in the various attributes/values that are used to suppor...
Definition OpHelpers.h:42