LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
LLZKConversionUtils.h
Go to the documentation of this file.
1//===-- LLZKConversionUtils.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// Shared utilities for dialect converting transformations.
11//
12//===----------------------------------------------------------------------===//
13
14#pragma once
15
19#include "llzk/Util/Concepts.h"
20
21#include <mlir/IR/PatternMatch.h>
22#include <mlir/IR/SymbolTable.h>
23#include <mlir/Transforms/DialectConversion.h>
24
25#include <llvm/ADT/DenseMap.h>
26#include <llvm/ADT/StringSet.h>
27#include <llvm/ADT/Twine.h>
28
29#include <optional>
30#include <string>
31
32namespace llzk {
33
36inline mlir::DictionaryAttr
37withFunctionNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef attrName, llvm::StringRef name) {
38 mlir::NamedAttrList newAttrs(attrs);
39 newAttrs.set(attrName, mlir::StringAttr::get(attrs.getContext(), name));
40 return newAttrs.getDictionary(attrs.getContext());
41}
42
45inline mlir::DictionaryAttr
46withFunctionArgNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef name) {
48}
49
51inline mlir::DictionaryAttr
52withFunctionResNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef name) {
54}
55
57inline std::string
58reserveUniqueAttrName(llvm::StringSet<> &usedNames, llvm::StringRef desiredName) {
59 if (!usedNames.contains(desiredName)) {
60 usedNames.insert(desiredName);
61 return desiredName.str();
62 }
63
64 for (unsigned suffix = 1;; ++suffix) {
65 std::string candidate = (desiredName + "#" + llvm::Twine(suffix)).str();
66 if (!usedNames.contains(candidate)) {
67 usedNames.insert(candidate);
68 return candidate;
69 }
70 }
71}
72
74inline std::optional<mlir::StringAttr>
75getAttrAtIndexWithName(mlir::ArrayAttr attrs, unsigned index, llvm::StringRef attrName) {
76 if (!attrs || index >= attrs.size()) {
77 return std::nullopt;
78 }
79 if (auto dictAttr = llvm::dyn_cast<mlir::DictionaryAttr>(attrs[index])) {
80 if (auto nameAttr = llvm::dyn_cast_if_present<mlir::StringAttr>(dictAttr.get(attrName))) {
81 return nameAttr;
82 }
83 }
84 return std::nullopt;
85}
86
89 llvm::SmallVector<std::optional<llvm::StringRef>> originalNames;
90 llvm::SmallVector<llvm::StringRef> existingNames;
91 llvm::SmallVector<llvm::SmallVector<std::string>> splitNameSuffixes;
92};
93
95template <typename GetNameAttrFn, typename GetSplitSuffixesFn>
97 mlir::ArrayRef<mlir::Type> origTypes, GetNameAttrFn &&getNameAttr,
98 GetSplitSuffixesFn &&getSplitSuffixes
99) {
101 info.originalNames.reserve(origTypes.size());
102 info.splitNameSuffixes.reserve(origTypes.size());
103 for (auto [i, type] : llvm::enumerate(origTypes)) {
104 if (std::optional<mlir::StringAttr> nameAttr = getNameAttr(i)) {
105 info.originalNames.push_back(nameAttr->getValue());
106 info.existingNames.push_back(nameAttr->getValue());
107 } else {
108 info.originalNames.push_back(std::nullopt);
109 }
110 info.splitNameSuffixes.push_back(getSplitSuffixes(type));
111 }
112 return info;
113}
114
118 mlir::ArrayAttr origAttrs, const llvm::SmallVector<size_t> &originalIdxToSize,
119 const llvm::SmallVector<mlir::Type> &newTypes, llvm::StringRef functionNameAttrName,
120 llvm::ArrayRef<std::optional<llvm::StringRef>> origNames = {},
121 llvm::ArrayRef<llvm::StringRef> existingNames = {},
122 llvm::ArrayRef<llvm::SmallVector<std::string>> splitNameSuffixes = {}
123) {
124 if (!origAttrs) {
125 return nullptr;
126 }
127 assert(originalIdxToSize.size() == origAttrs.size());
128 if (originalIdxToSize.size() == newTypes.size()) {
129 return nullptr;
130 }
131
132 llvm::SmallVector<mlir::Attribute> newAttrs;
133 llvm::StringSet<> usedNames;
134 if (!origNames.empty()) {
135 for (llvm::StringRef name : existingNames) {
136 usedNames.insert(name);
137 }
138 }
139
140 for (auto [i, s] : llvm::enumerate(originalIdxToSize)) {
141 mlir::Attribute attr = origAttrs[i];
142 if (!origNames.empty() && !splitNameSuffixes.empty() && s != 1 && origNames[i]) {
143 assert(i < splitNameSuffixes.size());
144 assert(splitNameSuffixes[i].size() == s);
145 auto dictAttr = llvm::cast<mlir::DictionaryAttr>(attr);
146 llvm::StringRef name = *origNames[i];
147 for (llvm::StringRef suffix : splitNameSuffixes[i]) {
148 std::string desiredName = (llvm::Twine(name) + suffix).str();
149 newAttrs.push_back(withFunctionNameAttr(
150 dictAttr, functionNameAttrName, reserveUniqueAttrName(usedNames, desiredName)
151 ));
152 }
153 continue;
154 }
155 newAttrs.append(s, attr);
156 }
157 return mlir::ArrayAttr::get(origAttrs.getContext(), newAttrs);
158}
159
167 mlir::Location loc, mlir::TypeRange newResultTypes, function::CallOp oldCall,
168 llvm::ArrayRef<mlir::ValueRange> mapOperands, mlir::ValueRange argOperands,
169 mlir::ConversionPatternRewriter &rewriter
170) {
171 llvm::SmallVector<mlir::Attribute> templateParams;
172 if (mlir::ArrayAttr templateParamsAttr = oldCall.getTemplateParamsAttr()) {
173 templateParams.append(templateParamsAttr.begin(), templateParamsAttr.end());
174 }
175
176 function::CallOp newCall;
177 if (oldCall.getMapOperands().empty()) {
178 newCall = rewriter.create<function::CallOp>(
179 loc, newResultTypes, oldCall.getCalleeAttr(), argOperands, templateParams
180 );
181 } else {
182 newCall = rewriter.create<function::CallOp>(
183 loc, newResultTypes, oldCall.getCalleeAttr(), mapOperands, oldCall.getNumDimsPerMapAttr(),
184 argOperands, templateParams
185 );
186 }
187
188 newCall->setDiscardableAttrs(oldCall->getDiscardableAttrDictionary());
189 return newCall;
190}
191
193inline static mlir::Type replaceAffineMapArrayDimsWithWildcards(mlir::Type type) {
194 auto arrTy = llvm::dyn_cast<array::ArrayType>(type);
195 if (!arrTy) {
196 return type;
197 }
198
199 mlir::Builder builder(arrTy.getContext());
200 llvm::SmallVector<mlir::Attribute> dims;
201 dims.reserve(arrTy.getDimensionSizes().size());
202 for (mlir::Attribute dimSize : arrTy.getDimensionSizes()) {
203 if (llvm::isa<mlir::AffineMapAttr>(dimSize)) {
204 dims.push_back(builder.getIndexAttr(mlir::ShapedType::kDynamic));
205 } else {
206 dims.push_back(dimSize);
207 }
208 }
209
210 return arrTy.cloneWith(replaceAffineMapArrayDimsWithWildcards(arrTy.getElementType()), dims);
211}
212
216
217protected:
218 virtual llvm::SmallVector<mlir::Type> convertInputs(mlir::ArrayRef<mlir::Type> origTypes) = 0;
219 virtual llvm::SmallVector<mlir::Type> convertResults(mlir::ArrayRef<mlir::Type> origTypes) = 0;
220
221 virtual mlir::ArrayAttr
222 convertInputAttrs(mlir::ArrayAttr origAttrs, llvm::SmallVector<mlir::Type> newTypes) = 0;
223 virtual mlir::ArrayAttr
224 convertResultAttrs(mlir::ArrayAttr origAttrs, llvm::SmallVector<mlir::Type> newTypes) = 0;
225
226 virtual void processBlockArgs(mlir::Block &entryBlock, mlir::RewriterBase &rewriter) = 0;
227
228public:
229 virtual ~FunctionTypeConverter() = default;
230
231 void convert(function::FuncDefOp op, mlir::RewriterBase &rewriter) {
232 // Update in/out types of the function
233 mlir::FunctionType oldTy = op.getFunctionType();
234 llvm::SmallVector<mlir::Type> newInputs = convertInputs(oldTy.getInputs());
235 llvm::SmallVector<mlir::Type> newResults = convertResults(oldTy.getResults());
236 mlir::FunctionType newTy = mlir::FunctionType::get(
237 oldTy.getContext(), mlir::TypeRange(newInputs), mlir::TypeRange(newResults)
238 );
239 if (newTy == oldTy) {
240 return; // nothing to change
241 }
242
243 // Pre-condition: arg/result count equals corresponding attribute count
244 assert(!op.getResAttrsAttr() || op.getResAttrsAttr().size() == op.getNumResults());
245 assert(!op.getArgAttrsAttr() || op.getArgAttrsAttr().size() == op.getNumArguments());
246 rewriter.modifyOpInPlace(op, [&]() {
247 op.setFunctionType(newTy);
248
249 // If any input or result types were added, ensure the attributes are updated too.
250 if (mlir::ArrayAttr newArgAttrs = convertInputAttrs(op.getArgAttrsAttr(), newInputs)) {
251 op.setArgAttrsAttr(newArgAttrs);
252 }
253 if (mlir::ArrayAttr newResAttrs = convertResultAttrs(op.getResAttrsAttr(), newResults)) {
254 op.setResAttrsAttr(newResAttrs);
255 }
256 });
257 // Post-condition: arg/result count equals corresponding attribute count
258 assert(!op.getResAttrsAttr() || op.getResAttrsAttr().size() == op.getNumResults());
259 assert(!op.getArgAttrsAttr() || op.getArgAttrsAttr().size() == op.getNumArguments());
260
261 // If the function has a body, ensure the entry block arguments match the function inputs.
262 if (mlir::Region *body = op.getCallableRegion()) {
263 mlir::Block &entryBlock = body->front();
264 bool blockArgsNeedUpdate =
265 !std::cmp_equal(entryBlock.getNumArguments(), newInputs.size()) ||
266 llvm::any_of(llvm::zip_equal(entryBlock.getArgumentTypes(), newInputs), [](auto pair) {
267 return std::get<0>(pair) != std::get<1>(pair);
268 });
269 if (blockArgsNeedUpdate) {
270 processBlockArgs(entryBlock, rewriter);
271 // Post-condition: block args must match function inputs in both arity and type.
272 assert(std::cmp_equal(entryBlock.getNumArguments(), newInputs.size()));
273 for (unsigned i = 0, e = entryBlock.getNumArguments(); i < e; ++i) {
274 assert(entryBlock.getArgument(i).getType() == newInputs[i]);
275 }
276 }
277 }
278 }
279};
280
288template <
289 typename ImplClass, HasInterface<component::MemberRefOpInterface> MemberRefOpClass,
290 typename GenHeaderType, typename IdType>
291class SplitAggregateInMemberRefOp : public mlir::OpConversionPattern<MemberRefOpClass> {
292public:
294 using MemberInfo = std::pair<mlir::StringAttr, mlir::Type>;
296 using LocalMemberReplacementMap = llvm::DenseMap<IdType, MemberInfo>;
298 using MemberReplacementMap = llvm::DenseMap<
299 component::StructDefOp, llvm::DenseMap<mlir::StringAttr, LocalMemberReplacementMap>>;
300
301private:
302 mlir::SymbolTableCollection &tables;
303 const MemberReplacementMap &repMapRef;
304
305 // Static check to ensure the methods are implemented in all subclasses.
306 inline static void ensureImplementedAtCompile() {
307 static_assert(
308 sizeof(MemberRefOpClass) == 0,
309 "SplitAggregateInMemberRefOp not implemented for requested type."
310 );
311 }
312
313protected:
314 using OpAdaptor = typename MemberRefOpClass::Adaptor;
315
318 static GenHeaderType genHeader(MemberRefOpClass, mlir::ConversionPatternRewriter &) {
319 ensureImplementedAtCompile();
320 llvm_unreachable("must have concrete instantiation");
321 }
322
325 static void forId(
326 mlir::Location, GenHeaderType &, IdType, MemberInfo, OpAdaptor,
327 mlir::ConversionPatternRewriter &
328 ) {
329 ensureImplementedAtCompile();
330 llvm_unreachable("must have concrete instantiation");
331 }
332
333public:
334 // Suppress false positive from `clang-tidy`
335 // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
337 mlir::MLIRContext *ctx, mlir::SymbolTableCollection &symTables,
338 const MemberReplacementMap &memberRepMap
339 )
340 : mlir::OpConversionPattern<MemberRefOpClass>(ctx), tables(symTables),
341 repMapRef(memberRepMap) {}
342
343 static bool legal(MemberRefOpClass) {
344 ensureImplementedAtCompile();
345 llvm_unreachable("must have concrete instantiation");
346 return false;
347 }
348
349 mlir::LogicalResult matchAndRewrite(
350 MemberRefOpClass op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter
351 ) const override {
352 if (ImplClass::legal(op)) {
353 return mlir::failure();
354 }
355 component::StructType tgtStructTy =
356 llvm::cast<component::MemberRefOpInterface>(op.getOperation()).getStructType();
357 assert(tgtStructTy);
358 auto tgtStructDef = tgtStructTy.getDefinition(tables, op);
359 assert(mlir::succeeded(tgtStructDef));
360
361 GenHeaderType prefixResult = ImplClass::genHeader(op, rewriter);
362
363 const LocalMemberReplacementMap &idToName =
364 repMapRef.at(tgtStructDef->get()).at(op.getMemberNameAttr().getAttr());
365 // Split the aggregate member into a series of scalar member ops.
366 for (const auto &[id, newMember] : idToName) {
367 ImplClass::forId(op.getLoc(), prefixResult, id, newMember, adaptor, rewriter);
368 }
369 if constexpr (requires { ImplClass::finalize(op, prefixResult, adaptor, rewriter); }) {
370 ImplClass::finalize(op, prefixResult, adaptor, rewriter);
371 }
372 rewriter.eraseOp(op);
373 return mlir::success();
374 }
375};
376
377} // namespace llzk
General helper for converting a FuncDefOp by changing its input and/or result types and the associate...
virtual void processBlockArgs(mlir::Block &entryBlock, mlir::RewriterBase &rewriter)=0
virtual llvm::SmallVector< mlir::Type > convertResults(mlir::ArrayRef< mlir::Type > origTypes)=0
virtual mlir::ArrayAttr convertResultAttrs(mlir::ArrayAttr origAttrs, llvm::SmallVector< mlir::Type > newTypes)=0
void convert(function::FuncDefOp op, mlir::RewriterBase &rewriter)
virtual ~FunctionTypeConverter()=default
virtual mlir::ArrayAttr convertInputAttrs(mlir::ArrayAttr origAttrs, llvm::SmallVector< mlir::Type > newTypes)=0
virtual llvm::SmallVector< mlir::Type > convertInputs(mlir::ArrayRef< mlir::Type > origTypes)=0
llvm::DenseMap< component::StructDefOp, llvm::DenseMap< mlir::StringAttr, LocalMemberReplacementMap > > MemberReplacementMap
Maps struct -> original aggregate-type member name -> LocalMemberReplacementMap.
static bool legal(MemberRefOpClass)
std::pair< mlir::StringAttr, mlir::Type > MemberInfo
Scalar member name and type.
SplitAggregateInMemberRefOp(mlir::MLIRContext *ctx, mlir::SymbolTableCollection &symTables, const MemberReplacementMap &memberRepMap)
typename MemberRefOpClass::Adaptor OpAdaptor
mlir::LogicalResult matchAndRewrite(MemberRefOpClass op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const override
static GenHeaderType genHeader(MemberRefOpClass, mlir::ConversionPatternRewriter &)
Executed at the start of rewrite() to (optionally) generate anything that should appear before the pe...
static void forId(mlir::Location, GenHeaderType &, IdType, MemberInfo, OpAdaptor, mlir::ConversionPatternRewriter &)
Executed for each scalar id in the aggregate type of the original member to generate the per-scalar o...
llvm::DenseMap< IdType, MemberInfo > LocalMemberReplacementMap
Maps a scalar element identifier within the aggregate to its new scalar member info.
::mlir::FailureOr< SymbolLookupResult< StructDefOp > > getDefinition(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op, bool reportMissing=true) const
Gets the struct op that defines this struct.
Definition Types.cpp:26
::mlir::SymbolRefAttr getCalleeAttr()
Definition Ops.h.inc:292
::mlir::ArrayAttr getTemplateParamsAttr()
Definition Ops.h.inc:297
::mlir::OperandRangeRange getMapOperands()
Definition Ops.h.inc:270
::mlir::DenseI32ArrayAttr getNumDimsPerMapAttr()
Definition Ops.h.inc:302
::mlir::FunctionType getFunctionType()
Definition Ops.cpp.inc:984
void setArgAttrsAttr(::mlir::ArrayAttr attr)
Definition Ops.h.inc:746
void setResAttrsAttr(::mlir::ArrayAttr attr)
Definition Ops.h.inc:750
::mlir::ArrayAttr getArgAttrsAttr()
Definition Ops.h.inc:726
void setFunctionType(::mlir::FunctionType attrValue)
Definition Ops.cpp.inc:1003
::mlir::Region * getCallableRegion()
Required by FunctionOpInterface.
Definition Ops.h.inc:879
::mlir::ArrayAttr getResAttrsAttr()
Definition Ops.h.inc:731
Restricts a template parameter to Op classes that implement the given OpInterface.
Definition Concepts.h:20
constexpr char ARG_NAME_ATTR_NAME[]
Attribute name for source-level function argument names.
Definition Ops.h:35
constexpr char RES_NAME_ATTR_NAME[]
Attribute name for source-level function result names.
Definition Ops.h:38
mlir::DictionaryAttr withFunctionResNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef name)
Return a copy of the given result attribute dictionary with function.res_name set to name.
mlir::DictionaryAttr withFunctionNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef attrName, llvm::StringRef name)
Return a copy of the given function argument/result attribute dictionary with attrName set to name.
mlir::ArrayAttr replicateFunctionNameAttrsAsNeeded(mlir::ArrayAttr origAttrs, const llvm::SmallVector< size_t > &originalIdxToSize, const llvm::SmallVector< mlir::Type > &newTypes, llvm::StringRef functionNameAttrName, llvm::ArrayRef< std::optional< llvm::StringRef > > origNames={}, llvm::ArrayRef< llvm::StringRef > existingNames={}, llvm::ArrayRef< llvm::SmallVector< std::string > > splitNameSuffixes={})
Expand function arg/result attribute arrays to match a split signature, rewriting name attrs with the...
mlir::DictionaryAttr withFunctionArgNameAttr(mlir::DictionaryAttr attrs, llvm::StringRef name)
Return a copy of the given argument attribute dictionary with function.arg_name set to name.
function::CallOp createCallPreservingInstantiationOperands(mlir::Location loc, mlir::TypeRange newResultTypes, function::CallOp oldCall, llvm::ArrayRef< mlir::ValueRange > mapOperands, mlir::ValueRange argOperands, mlir::ConversionPatternRewriter &rewriter)
Rebuild a function.call while preserving explicit instantiation state from oldCall.
SplitFunctionNameInfo collectSplitFunctionNameInfo(mlir::ArrayRef< mlir::Type > origTypes, GetNameAttrFn &&getNameAttr, GetSplitSuffixesFn &&getSplitSuffixes)
Collect function arg/result names and split suffixes from a list of original types.
std::optional< mlir::StringAttr > getAttrAtIndexWithName(mlir::ArrayAttr attrs, unsigned index, llvm::StringRef attrName)
Return the function arg/result attribute at index for the given name, if present.
std::string reserveUniqueAttrName(llvm::StringSet<> &usedNames, llvm::StringRef desiredName)
Reserve and return a unique function argument/result name based on desiredName.
Cached function arg/result names and split suffixes used while rewriting a function signature.
llvm::SmallVector< std::optional< llvm::StringRef > > originalNames
llvm::SmallVector< llvm::StringRef > existingNames
llvm::SmallVector< llvm::SmallVector< std::string > > splitNameSuffixes