24#include <mlir/IR/BuiltinAttributes.h>
25#include <mlir/IR/MLIRContext.h>
26#include <mlir/IR/Operation.h>
27#include <mlir/IR/SymbolTable.h>
28#include <mlir/Transforms/DialectConversion.h>
30#include <llvm/ADT/ArrayRef.h>
31#include <llvm/ADT/DenseMap.h>
32#include <llvm/ADT/DenseSet.h>
33#include <llvm/ADT/STLExtras.h>
34#include <llvm/ADT/SmallVector.h>
35#include <llvm/ADT/StringRef.h>
36#include <llvm/Support/Debug.h>
42#define DEBUG_TYPE "poly-dialect-shared"
49template <
typename Check>
inline bool runCheck(mlir::Operation *op, Check
check) {
51 llvm::dyn_cast_if_present<
typename llvm::function_traits<Check>::template arg_t<0>>(op)) {
52 return check(specificOp);
87 llvm::DenseSet<mlir::SymbolRefAttr> &&tryToErasePaths
92 const llvm::DenseSet<mlir::SymbolOpInterface> &
getTryToEraseSet()
const {
return tryToErase; }
96 llvm::DenseSet<mlir::SymbolOpInterface> tryToErase;
98 llvm::DenseMap<mlir::SymbolOpInterface, bool> visitedPlusSafetyResult;
100 llvm::DenseMap<const SymbolUseGraphNode *, mlir::SymbolOpInterface> lookupCache;
102 bool collectSafeToErase(mlir::SymbolOpInterface
check);
124 mlir::SymbolRefAttr nameRef, mlir::MLIRContext *ctx, mlir::ArrayRef<mlir::Attribute> params
146 TemplateOp parentTemplate, mlir::ArrayAttr callParams,
147 const llvm::DenseMap<mlir::Attribute, mlir::Attribute> ¶mNameToConcrete
156 llvm::StringRef baseName, llvm::ArrayRef<mlir::Attribute> concreteAttrs
177template <
typename... AdditionalOpClasses,
typename... AdditionalChecks>
179 mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, AdditionalChecks &&...checks
183 tyConv, ctx, empty, (std::forward<AdditionalChecks>(checks))...
192template <
typename... AdditionalOpClasses,
typename... AdditionalChecks>
195 AdditionalChecks &&...checks
198 auto inserter = [&](
auto... opClasses) {
199 target.addDynamicallyLegalOp<
decltype(opClasses)...>([&cb, &tyConv,
200 &checks...](mlir::Operation *op) {
202 llvm::dbgs() <<
"[newConverterDefinedTarget] checking legality of ";
210 LLVM_DEBUG(
if (legality) { llvm::dbgs() <<
"[newConverterDefinedTarget] is legal\n"; }
else {
211 llvm::dbgs() <<
"[newConverterDefinedTarget] is not legal\n";
218 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
Common interface for constant parameter-like symbol operations that can be defined within a TemplateO...
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 ...
std::tuple< function::CallOp, array::CreateArrayOp, pod::NewPodOp > NoGeneralBuilder
Ops in this subset do NOT define the general builder function (see above), so they cannot use General...
std::tuple< array::ArrayLengthOp, array::ReadArrayOp, array::WriteArrayOp, array::InsertArrayOp, array::ExtractArrayOp, constrain::EmitEqualityOp, constrain::EmitContainmentOp, component::MemberDefOp, component::MemberReadOp, component::MemberWriteOp, component::CreateStructOp, function::FuncDefOp, function::ReturnOp, global::GlobalDefOp, global::GlobalReadOp, global::GlobalWriteOp, llzk::NonDetOp, pod::ReadPodOp, pod::WritePodOp, polymorphic::UnifiableCastOp, polymorphic::ConstReadOp > WithGeneralBuilder
Ops in this subset define the general builder function: build(OpBuilder&, OperationState&,...
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.