29#include <mlir/Dialect/Arith/IR/Arith.h>
30#include <mlir/Dialect/SCF/IR/SCF.h>
31#include <mlir/Dialect/SCF/Transforms/Patterns.h>
32#include <mlir/IR/Attributes.h>
33#include <mlir/IR/BuiltinAttributes.h>
34#include <mlir/IR/MLIRContext.h>
35#include <mlir/IR/Operation.h>
36#include <mlir/IR/PatternMatch.h>
37#include <mlir/IR/SymbolTable.h>
38#include <mlir/Transforms/DialectConversion.h>
40#include <llvm/ADT/DenseMap.h>
41#include <llvm/ADT/DenseSet.h>
42#include <llvm/ADT/STLExtras.h>
43#include <llvm/ADT/SmallVector.h>
44#include <llvm/Support/Debug.h>
46#define DEBUG_TYPE "poly-dialect-shared"
53template <
typename Check>
inline bool runCheck(mlir::Operation *op, Check
check) {
55 llvm::dyn_cast_if_present<
typename llvm::function_traits<Check>::template arg_t<0>>(op)) {
56 return check(specificOp);
91 llvm::DenseSet<mlir::SymbolRefAttr> &&tryToErasePaths
96 const llvm::DenseSet<mlir::SymbolOpInterface> &
getTryToEraseSet()
const {
return tryToErase; }
100 llvm::DenseSet<mlir::SymbolOpInterface> tryToErase;
102 llvm::DenseMap<mlir::SymbolOpInterface, bool> visitedPlusSafetyResult;
104 llvm::DenseMap<const SymbolUseGraphNode *, mlir::SymbolOpInterface> lookupCache;
106 bool collectSafeToErase(mlir::SymbolOpInterface
check);
128 mlir::SymbolRefAttr nameRef, mlir::MLIRContext *ctx, mlir::ArrayRef<mlir::Attribute> params
150 TemplateOp parentTemplate, mlir::ArrayAttr callParams,
151 const llvm::DenseMap<mlir::Attribute, mlir::Attribute> ¶mNameToConcrete
160 llvm::StringRef baseName, llvm::ArrayRef<mlir::Attribute> concreteAttrs
182template <
typename... AdditionalOpClasses,
typename... AdditionalChecks>
184 mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, AdditionalChecks &&...checks
188 tyConv, ctx, empty, (std::forward<AdditionalChecks>(checks))...
198template <
typename... AdditionalOpClasses,
typename... AdditionalChecks>
201 AdditionalChecks &&...checks
204 auto inserter = [&](
auto... opClasses) {
205 target.addDynamicallyLegalOp<
decltype(opClasses)...>([&cb, &tyConv,
206 &checks...](mlir::Operation *op) {
208 llvm::dbgs() <<
"[newConverterDefinedTarget] checking legality of ";
216 LLVM_DEBUG(
if (legality) { llvm::dbgs() <<
"[newConverterDefinedTarget] is legal\n"; }
else {
217 llvm::dbgs() <<
"[newConverterDefinedTarget] is not legal\n";
222 std::apply(inserter, OpClassesWithStructTypes.NoGeneralBuilder);
223 std::apply(inserter, OpClassesWithStructTypes.WithGeneralBuilder);
224 applyToMoreTypes<
decltype(inserter), AdditionalOpClasses...>(inserter);
Reusable MLIR dialect conversion functions for LLZK StructType replacement.
Builds a tree structure representing the symbol table structure.
Builds a graph structure representing the relationships between symbols and their uses.
static StructType get(::mlir::SymbolRefAttr structName)
const SymbolDefTree & defTree
const SymbolUseGraph & useGraph
mlir::SymbolTableCollection tables
CleanupBase(mlir::ModuleOp root, const SymbolDefTree &symDefTree, const SymbolUseGraph &symUseGraph)
void checkEnded(bool) override
void checkStarted() override
mlir::LogicalResult eraseUnusedDefinitions()
const llvm::DenseSet< mlir::SymbolOpInterface > & getTryToEraseSet() const
FromEraseSet(mlir::ModuleOp root, const SymbolDefTree &symDefTree, const SymbolUseGraph &symUseGraph, llvm::DenseSet< mlir::SymbolRefAttr > &&tryToErasePaths)
Note: paths in tryToErase should be relative to root.
virtual void checkStarted()=0
virtual void checkEnded(bool outcome)=0
virtual ~LegalityCheckCallback()=default
component::StructType getStructTypeWithParams(mlir::SymbolRefAttr nameRef, mlir::ArrayAttr params)
Build a struct type while representing an empty parameter list as absent.
mlir::ConversionTarget newConverterDefinedTargetWithCallback(mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, LegalityCheckCallback &cb, AdditionalChecks &&...checks)
Return a new ConversionTarget allowing all LLZK-required dialects and defining Op legality based on t...
mlir::ConversionTarget newConverterDefinedTarget(mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, AdditionalChecks &&...checks)
Return a new ConversionTarget allowing all LLZK-required dialects and defining Op legality based on t...
FailureOr< InstantiationLayout > buildInstantiationLayout(TemplateOp parentTemplate, ArrayAttr callParams, const DenseMap< Attribute, Attribute > ¶mNameToConcrete)
array::ArrayType flattenInstantiatedArrayType(array::ArrayType inputTy, mlir::Type convertedElemTy)
Merge nested array dimensions produced by replacing an array element type.
void setInstantiationNamePattern(TemplateOp templateOp, ArrayAttr namePattern)
std::string buildOpaqueInstantiationName(StringRef baseName, ArrayRef< Attribute > concreteAttrs)
bool isErasableDefinition(mlir::Operation *op)
Return true iff op is a cleanup candidate.
mlir::ConversionTarget newBaseTarget(mlir::MLIRContext *ctx)
Return a new ConversionTarget allowing all LLZK-required dialects.
bool defaultLegalityCheck(const mlir::TypeConverter &tyConv, mlir::Operation *op)
Check whether an op is legal with respect to the given type converter, including TypeAttr attributes ...
Groups the information needed after concrete parameters have been chosen to decide how to name a new ...
mlir::ArrayAttr namePattern
The refined literal chunks; fully concrete generated templates clear this state.
std::string templateNameWithAttrs
mlir::ArrayAttr rewrittenCallParams
mlir::SmallVector< mlir::Attribute > remainingNames
mlir::ArrayAttr concreteParamKey
Ordered [parameter-name, concrete-value, ...] entries for exact partial-function reuse.