LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
SharedImpl.h
Go to the documentation of this file.
1//===-- SharedImpl.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//===----------------------------------------------------------------------===//
13//===----------------------------------------------------------------------===//
14
15#pragma once
16
24
25#include <mlir/Dialect/Arith/IR/Arith.h>
26#include <mlir/Dialect/SCF/IR/SCF.h>
27#include <mlir/Dialect/SCF/Transforms/Patterns.h>
28#include <mlir/IR/Attributes.h>
29#include <mlir/IR/BuiltinAttributes.h>
30#include <mlir/IR/MLIRContext.h>
31#include <mlir/IR/Operation.h>
32#include <mlir/IR/PatternMatch.h>
33#include <mlir/Transforms/DialectConversion.h>
34
35#include <llvm/ADT/STLExtras.h>
36#include <llvm/ADT/SmallVector.h>
37#include <llvm/Support/Debug.h>
38
39#include <tuple>
40
41#define DEBUG_TYPE "poly-dialect-shared"
42
44
45namespace {
46
47// Default to true if the check is not for that particular operation type.
48template <typename Check> inline bool runCheck(mlir::Operation *op, Check check) {
49 if (auto specificOp =
50 llvm::dyn_cast_if_present<typename llvm::function_traits<Check>::template arg_t<0>>(op)) {
51 return check(specificOp);
52 }
53 return true;
54}
55
56} // namespace
57
59mlir::ConversionTarget newBaseTarget(mlir::MLIRContext *ctx);
60
62public:
63 virtual ~LegalityCheckCallback() = default;
64 virtual void checkStarted() = 0;
65 virtual void checkEnded(bool outcome) = 0;
66};
67
69public:
70 void checkStarted() override {}
71 void checkEnded(bool) override {}
72};
73
80template <typename... AdditionalOpClasses, typename... AdditionalChecks>
81mlir::ConversionTarget newConverterDefinedTarget(
82 mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, AdditionalChecks &&...checks
83) {
84 static EmptyLegalityCheckCallback empty;
85 return newConverterDefinedTargetWithCallback<AdditionalOpClasses...>(
86 tyConv, ctx, empty, (std::forward<AdditionalChecks>(checks))...
87 );
88}
89
96template <typename... AdditionalOpClasses, typename... AdditionalChecks>
98 mlir::TypeConverter &tyConv, mlir::MLIRContext *ctx, LegalityCheckCallback &cb,
99 AdditionalChecks &&...checks
100) {
101 mlir::ConversionTarget target = newBaseTarget(ctx);
102 auto inserter = [&](auto... opClasses) {
103 target.addDynamicallyLegalOp<decltype(opClasses)...>([&cb, &tyConv,
104 &checks...](mlir::Operation *op) {
105 LLVM_DEBUG(if (op) {
106 llvm::dbgs() << "[newConverterDefinedTarget] checking legality of ";
107 op->dump();
108 });
109 cb.checkStarted();
110 auto legality =
111 defaultLegalityCheck(tyConv, op) && (runCheck<AdditionalChecks>(op, checks) && ...);
112
113 cb.checkEnded(legality);
114 LLVM_DEBUG(if (legality) { llvm::dbgs() << "[newConverterDefinedTarget] is legal\n"; } else {
115 llvm::dbgs() << "[newConverterDefinedTarget] is not legal\n";
116 });
117 return legality;
118 });
119 };
120 std::apply(inserter, OpClassesWithStructTypes.NoGeneralBuilder);
121 std::apply(inserter, OpClassesWithStructTypes.WithGeneralBuilder);
122 applyToMoreTypes<decltype(inserter), AdditionalOpClasses...>(inserter);
123 return target;
124}
125
126} // namespace llzk::polymorphic::detail
127
128#undef DEBUG_TYPE
#define check(x)
Definition Ops.cpp:175
Reusable MLIR dialect conversion functions for LLZK StructType replacement.
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...
Definition SharedImpl.h:97
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...
Definition SharedImpl.h:81
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 ...