LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
AffineHelper.h
Go to the documentation of this file.
1//===-- AffineHelper.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
12#include "llzk/Util/Compare.h"
13
14#include <mlir/IR/Builders.h>
15#include <mlir/IR/OpImplementation.h>
16#include <mlir/IR/Operation.h>
17#include <mlir/IR/ValueRange.h>
18
19#include <llvm/ADT/SmallVector.h>
20
22namespace llzk::affineMapHelpers {
23
25mlir::ParseResult parseDimAndSymbolList(
26 mlir::OpAsmParser &parser,
27 mlir::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &mapOperands,
28 mlir::IntegerAttr &numDims
29);
30
33 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRange mapOperands,
34 mlir::IntegerAttr numDims
35);
36
39 mlir::OpAsmParser &parser,
40 mlir::SmallVectorImpl<mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>>
41 &multiMapOperands,
42 mlir::DenseI32ArrayAttr &numDimsPerMap
43);
44
47 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands,
48 mlir::DenseI32ArrayAttr numDimsPerMap
49);
50
54mlir::ParseResult parseAttrDictWithWarnings(
55 mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state
56);
57
58template <typename ConcreteOp>
60 mlir::OpAsmPrinter &printer, ConcreteOp /*op*/, mlir::DictionaryAttr extraAttrs,
61 typename ConcreteOp::Properties /*state*/
62) {
63 printer.printOptionalAttrDict(extraAttrs.getValue(), ConcreteOp::getAttributeNames());
64}
65
68mlir::LogicalResult verifySizesForMultiAffineOps(
69 mlir::Operation *op, int32_t segmentSize, mlir::ArrayRef<int32_t> mapOpGroupSizes,
70 mlir::OperandRangeRange mapOperands, mlir::ArrayRef<int32_t> numDimsPerMap
71);
72
76 mlir::OperandRangeRange mapOps, mlir::ArrayRef<int32_t> numDimsPerMap,
77 mlir::ArrayRef<mlir::AffineMapAttr> mapAttrs, mlir::Operation *origin
78);
79
85template <typename OpClass>
86inline typename OpClass::Properties &buildInstantiationAttrs(
87 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState,
88 mlir::ArrayRef<mlir::ValueRange> mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap,
89 int32_t firstSegmentSize = 0
90) {
91 int32_t mapOpsSegmentSize = 0;
92 mlir::SmallVector<int32_t> rangeSegments;
93 for (mlir::ValueRange r : mapOperands) {
94 odsState.addOperands(r);
95 int32_t s = llzk::checkedCast<int32_t>(r.size());
96 rangeSegments.push_back(s);
97 mapOpsSegmentSize += s;
98 }
99 typename OpClass::Properties &props = odsState.getOrAddProperties<typename OpClass::Properties>();
100 props.setMapOpGroupSizes(odsBuilder.getDenseI32ArrayAttr(rangeSegments));
101 props.setOperandSegmentSizes({firstSegmentSize, mapOpsSegmentSize});
102 if (numDimsPerMap) {
103 props.setNumDimsPerMap(numDimsPerMap);
104 }
105 return props;
106}
107
111template <typename OpClass>
113 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState,
114 mlir::ArrayRef<mlir::ValueRange> mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap
115) {
116 mlir::SmallVector<int32_t> rangeSegments;
117 for (mlir::ValueRange r : mapOperands) {
118 odsState.addOperands(r);
119 int32_t s = llzk::checkedCast<int32_t>(r.size());
120 rangeSegments.push_back(s);
121 }
122 typename OpClass::Properties &props = odsState.getOrAddProperties<typename OpClass::Properties>();
123 props.setMapOpGroupSizes(odsBuilder.getDenseI32ArrayAttr(rangeSegments));
124 if (numDimsPerMap) {
125 props.setNumDimsPerMap(numDimsPerMap);
126 }
127}
128
132template <typename OpClass>
133inline typename OpClass::Properties &buildInstantiationAttrsEmpty(
134 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, int32_t firstSegmentSize = 0
135) {
136 typename OpClass::Properties &props = odsState.getOrAddProperties<typename OpClass::Properties>();
137 // `operandSegmentSizes` = [ firstSegmentSize, mapOperands.size ]
138 props.setOperandSegmentSizes({firstSegmentSize, 0});
139 // There are no affine map operands so initialize the related properties as empty arrays.
140 props.setMapOpGroupSizes(odsBuilder.getDenseI32ArrayAttr({}));
141 props.setNumDimsPerMap(odsBuilder.getDenseI32ArrayAttr({}));
142 return props;
143}
144
148template <typename OpClass>
149inline typename OpClass::Properties &buildInstantiationAttrsEmptyNoSegments(
150 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState
151) {
152 typename OpClass::Properties &props = odsState.getOrAddProperties<typename OpClass::Properties>();
153 // There are no affine map operands so initialize the related properties as empty arrays.
154 props.setMapOpGroupSizes(odsBuilder.getDenseI32ArrayAttr({}));
155 props.setNumDimsPerMap(odsBuilder.getDenseI32ArrayAttr({}));
156 return props;
157}
158
159} // namespace llzk::affineMapHelpers
Group together all implementation related to AffineMap type parameters.
OpClass::Properties & buildInstantiationAttrsEmptyNoSegments(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState)
Utility for build() functions that initializes the mapOpGroupSizes, and numDimsPerMap attributes for ...
OpClass::Properties & buildInstantiationAttrs(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, mlir::ArrayRef< mlir::ValueRange > mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap, int32_t firstSegmentSize=0)
Utility for build() functions that initializes the operandSegmentSizes, mapOpGroupSizes,...
void buildInstantiationAttrsNoSegments(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, mlir::ArrayRef< mlir::ValueRange > mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap)
Utility for build() functions that initializes the mapOpGroupSizes, and numDimsPerMap attributes for ...
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)
LogicalResult verifyAffineMapInstantiations(OperandRangeRange mapOps, ArrayRef< int32_t > numDimsPerMap, ArrayRef< AffineMapAttr > mapAttrs, Operation *origin)
ParseResult parseDimAndSymbolList(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &mapOperands, IntegerAttr &numDims)
OpClass::Properties & buildInstantiationAttrsEmpty(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, int32_t firstSegmentSize=0)
Utility for build() functions that initializes the operandSegmentSizes, mapOpGroupSizes,...
ParseResult parseAttrDictWithWarnings(OpAsmParser &parser, NamedAttrList &extraAttrs, OperationState &state)
void printMultiDimAndSymbolList(OpAsmPrinter &printer, Operation *, OperandRangeRange multiMapOperands, DenseI32ArrayAttr numDimsPerMap)
void printDimAndSymbolList(OpAsmPrinter &printer, Operation *, OperandRange mapOperands, IntegerAttr numDims)
void printAttrDictWithWarnings(mlir::OpAsmPrinter &printer, ConcreteOp, mlir::DictionaryAttr extraAttrs, typename ConcreteOp::Properties)
constexpr T checkedCast(U u) noexcept
Definition Compare.h:81