LLZK 0.1.0
Veridise's ZK Language IR
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 <mlir/IR/Builders.h>
13#include <mlir/IR/OpImplementation.h>
14#include <mlir/IR/Operation.h>
15#include <mlir/IR/ValueRange.h>
16
17#include <llvm/ADT/SmallVector.h>
18
20namespace llzk::affineMapHelpers {
21
23mlir::ParseResult parseDimAndSymbolList(
24 mlir::OpAsmParser &parser,
25 mlir::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &mapOperands,
26 mlir::IntegerAttr &numDims
27);
28
31 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRange mapOperands,
32 mlir::IntegerAttr numDims
33);
34
37 mlir::OpAsmParser &parser,
38 mlir::SmallVectorImpl<mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>>
39 &multiMapOperands,
40 mlir::DenseI32ArrayAttr &numDimsPerMap
41);
42
45 mlir::OpAsmPrinter &printer, mlir::Operation *op, mlir::OperandRangeRange multiMapOperands,
46 mlir::DenseI32ArrayAttr numDimsPerMap
47);
48
52mlir::ParseResult parseAttrDictWithWarnings(
53 mlir::OpAsmParser &parser, mlir::NamedAttrList &extraAttrs, mlir::OperationState &state
54);
55
56template <typename ConcreteOp>
58 mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs,
59 typename ConcreteOp::Properties state
60) {
61 printer.printOptionalAttrDict(extraAttrs.getValue(), ConcreteOp::getAttributeNames());
62}
63
66mlir::LogicalResult verifySizesForMultiAffineOps(
67 mlir::Operation *op, int32_t segmentSize, mlir::ArrayRef<int32_t> mapOpGroupSizes,
68 mlir::OperandRangeRange mapOperands, mlir::ArrayRef<int32_t> numDimsPerMap
69);
70
74 mlir::OperandRangeRange mapOps, mlir::ArrayRef<int32_t> numDimsPerMap,
75 mlir::ArrayRef<mlir::AffineMapAttr> mapAttrs, mlir::Operation *origin
76);
77
83template <typename OpClass>
84inline typename OpClass::Properties &buildInstantiationAttrs(
85 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState,
86 mlir::ArrayRef<mlir::ValueRange> mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap,
87 int32_t firstSegmentSize = 0
88) {
89 int32_t mapOpsSegmentSize = 0;
90 mlir::SmallVector<int32_t> rangeSegments;
91 for (mlir::ValueRange r : mapOperands) {
92 odsState.addOperands(r);
93 assert(std::cmp_less_equal(r.size(), std::numeric_limits<int32_t>::max()));
94 int32_t s = static_cast<int32_t>(r.size());
95 rangeSegments.push_back(s);
96 mapOpsSegmentSize += s;
97 }
98 typename OpClass::Properties &props = odsState.getOrAddProperties<typename OpClass::Properties>();
99 props.setMapOpGroupSizes(odsBuilder.getDenseI32ArrayAttr(rangeSegments));
100 props.setOperandSegmentSizes({firstSegmentSize, mapOpsSegmentSize});
101 if (numDimsPerMap) {
102 props.setNumDimsPerMap(numDimsPerMap);
103 }
104 return props;
105}
106
110template <typename OpClass>
112 mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState,
113 mlir::ArrayRef<mlir::ValueRange> mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap
114) {
115 mlir::SmallVector<int32_t> rangeSegments;
116 for (mlir::ValueRange r : mapOperands) {
117 odsState.addOperands(r);
118 assert(std::cmp_less_equal(r.size(), std::numeric_limits<int32_t>::max()));
119 int32_t s = static_cast<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 printAttrDictWithWarnings(mlir::OpAsmPrinter &printer, ConcreteOp op, mlir::DictionaryAttr extraAttrs, typename ConcreteOp::Properties state)
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)
void printDimAndSymbolList(OpAsmPrinter &printer, Operation *op, OperandRange mapOperands, IntegerAttr numDims)
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 *op, OperandRangeRange multiMapOperands, DenseI32ArrayAttr numDimsPerMap)