LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
TemplateInstantiation.h
Go to the documentation of this file.
1//===-- TemplateInstantiation.h ---------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2026 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
14//===----------------------------------------------------------------------===//
15
16#pragma once
17
22
23#include <mlir/IR/Diagnostics.h>
24#include <mlir/Transforms/DialectConversion.h>
25
26#include <llvm/ADT/DenseMap.h>
27#include <llvm/ADT/SmallVector.h>
28
29#include <optional>
30
32
36class TemplateTypeConverter : public mlir::TypeConverter {
37 llvm::DenseMap<mlir::Attribute, mlir::Attribute> paramNameToValue;
38 mlir::Attribute convertIfPossible(mlir::Attribute attr) const;
39
40public:
41 explicit TemplateTypeConverter(llvm::DenseMap<mlir::Attribute, mlir::Attribute> bindings);
42
44 mlir::Attribute convertAttr(mlir::Attribute attr) const;
45
47 bool containsParam(mlir::Attribute name) const { return paramNameToValue.contains(name); }
48
50 const llvm::DenseMap<mlir::Attribute, mlir::Attribute> &getParamMap() const {
51 return paramNameToValue;
52 }
53};
54
60mlir::FailureOr<mlir::ArrayAttr> rebaseTemplateParams(
61 mlir::SymbolTableCollection &tables, mlir::ArrayAttr templateParams,
62 mlir::Operation *lookupFrom, mlir::ModuleOp destinationRoot, mlir::Operation *requestSite
63);
64
68 mlir::Operation *site, llvm::SmallVector<mlir::Diagnostic> &&diagnostics
69);
70
74 mlir::Operation *op, const llvm::DenseMap<mlir::Attribute, mlir::Attribute> &bindings
75);
76
80 TemplateOp templateOp, llvm::DenseMap<mlir::Attribute, mlir::Attribute> &bindings
81);
82
84std::optional<mlir::Attribute>
85inferUnifiedParam(const UnificationMap &unifyResult, mlir::SymbolRefAttr paramName);
86
90mlir::LogicalResult substituteStructBody(
92 const llvm::DenseMap<mlir::Attribute, mlir::Attribute> &bindings,
93 llvm::SmallVector<mlir::Diagnostic> &diagnostics
94);
95
100mlir::LogicalResult substituteFunctionBody(
101 function::FuncDefOp clone, const llvm::DenseMap<mlir::Attribute, mlir::Attribute> &bindings,
102 llvm::SmallVector<mlir::Diagnostic> &diagnostics
103);
104
105} // namespace llzk::polymorphic::detail
const llvm::DenseMap< mlir::Attribute, mlir::Attribute > & getParamMap() const
Return the bindings used by body substitution patterns.
bool containsParam(mlir::Attribute name) const
Return whether the parameter has a supplied binding.
mlir::Attribute convertAttr(mlir::Attribute attr) const
Substitute a bound attribute or the type contained in a TypeAttr.
TemplateTypeConverter(llvm::DenseMap< mlir::Attribute, mlir::Attribute > bindings)
Common interface for constant parameter-like symbol operations that can be defined within a TemplateO...
FailureOr< ArrayAttr > rebaseTemplateParams(SymbolTableCollection &tables, ArrayAttr templateParams, Operation *lookupFrom, ModuleOp destinationRoot, Operation *requestSite)
std::optional< Attribute > inferUnifiedParam(const UnificationMap &unifyResult, SymbolRefAttr paramName)
Return the callee-side unification-derived value for a template parameter, if any.
LogicalResult substituteStructBody(StructDefOp newStruct, StructType typeAtDef, const DenseMap< Attribute, Attribute > &paramNameToConcrete, SmallVector< Diagnostic > &diagnostics)
LogicalResult substituteFunctionBody(FuncDefOp newFunc, const DenseMap< Attribute, Attribute > &paramNameToConcrete, SmallVector< Diagnostic > &delayedDiagnostics)
void evaluateTemplateExprs(TemplateOp templateOp, DenseMap< Attribute, Attribute > &paramNameToConcrete)
Evaluate all TemplateExprOps in templateOp that can be computed from the currently-known concrete par...
void convertCalleesInPlace(Operation *op, const DenseMap< Attribute, Attribute > &paramNameToValue)
void reportDelayedDiagnostics(Operation *site, SmallVector< Diagnostic > &&diagnostics)
mlir::DenseMap< std::pair< mlir::SymbolRefAttr, Side >, mlir::Attribute > UnificationMap
Optional result from type unifications.
Definition TypeHelper.h:208