LLZK 0.1.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Matchers.h
Go to the documentation of this file.
1//===-- Matchers.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#pragma once
11
14
15#include <mlir/IR/Matchers.h>
16#include <mlir/IR/Operation.h>
17#include <mlir/IR/Value.h>
18
19namespace llzk {
20
24template <typename LhsMatcher, typename RhsMatcher, typename... OpTypes> struct CommutativeMatcher {
25
26 CommutativeMatcher(LhsMatcher lhs, RhsMatcher rhs) : lhsMatcher(lhs), rhsMatcher(rhs) {}
27
28 bool match(mlir::Operation *op) {
29 using namespace mlir::detail;
30 if (!isa<OpTypes...>(op) || op->getNumOperands() != 2) {
31 return false;
32 }
33 bool res = matchOperandOrValueAtIndex(op, 0, lhsMatcher) &&
34 matchOperandOrValueAtIndex(op, 1, rhsMatcher);
35 if (!res) {
36 res = matchOperandOrValueAtIndex(op, 1, lhsMatcher) &&
37 matchOperandOrValueAtIndex(op, 0, rhsMatcher);
38 }
39 return res;
40 }
41
42 LhsMatcher lhsMatcher;
43 RhsMatcher rhsMatcher;
44};
45
46template <typename OpType, typename LhsMatcher, typename RhsMatcher>
47auto m_CommutativeOp(LhsMatcher lhs, RhsMatcher rhs) {
49}
50
54 mlir::Value *what;
55 RefValueCapture(mlir::Value *capture) : what(capture) {}
56
57 bool match(mlir::Value v) {
58 if (isa<mlir::BlockArgument>(v) ||
59 isa_and_present<component::MemberReadOp>(v.getDefiningOp())) {
60 if (what) {
61 *what = v;
62 }
63 return true;
64 }
65 return false;
66 }
67};
68
69auto m_RefValue() { return RefValueCapture(nullptr); }
70
71auto m_RefValue(mlir::Value *capture) { return RefValueCapture(capture); }
72
77
78 bool match(mlir::Value v) {
79 if (auto match = dyn_cast_if_present<felt::FeltConstantOp>(v.getDefiningOp())) {
80 if (what) {
81 *what = match;
82 }
83 return true;
84 }
85 return false;
86 }
87};
88
89auto m_Constant() { return ConstantCapture(nullptr); }
90
91auto m_Constant(felt::FeltConstantOp *capture) { return ConstantCapture(capture); }
92
93} // namespace llzk
auto m_RefValue()
Definition Matchers.h:69
auto m_Constant()
Definition Matchers.h:89
auto m_CommutativeOp(LhsMatcher lhs, RhsMatcher rhs)
Definition Matchers.h:47
This matcher will either match on lhs op rhs or rhs op lhs.
Definition Matchers.h:24
bool match(mlir::Operation *op)
Definition Matchers.h:28
CommutativeMatcher(LhsMatcher lhs, RhsMatcher rhs)
Definition Matchers.h:26
Matches and optionally captures a felt constant.
Definition Matchers.h:74
felt::FeltConstantOp * what
Definition Matchers.h:75
bool match(mlir::Value v)
Definition Matchers.h:78
ConstantCapture(felt::FeltConstantOp *capture)
Definition Matchers.h:76
Matches and optionally captures a SourceRef base value, which is either a member read or a block argu...
Definition Matchers.h:53
RefValueCapture(mlir::Value *capture)
Definition Matchers.h:55
mlir::Value * what
Definition Matchers.h:54
bool match(mlir::Value v)
Definition Matchers.h:57