LLZK 0.1.0
Veridise's ZK Language IR
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) || isa_and_present<component::FieldReadOp>(v.getDefiningOp())) {
59 if (what) {
60 *what = v;
61 }
62 return true;
63 }
64 return false;
65 }
66};
67
68auto m_RefValue() { return RefValueCapture(nullptr); }
69
70auto m_RefValue(mlir::Value *capture) { return RefValueCapture(capture); }
71
76
77 bool match(mlir::Value v) {
78 if (auto match = dyn_cast_if_present<felt::FeltConstantOp>(v.getDefiningOp())) {
79 if (what) {
80 *what = match;
81 }
82 return true;
83 }
84 return false;
85 }
86};
87
88auto m_Constant() { return ConstantCapture(nullptr); }
89
90auto m_Constant(felt::FeltConstantOp *capture) { return ConstantCapture(capture); }
91
92} // namespace llzk
auto m_RefValue()
Definition Matchers.h:68
auto m_Constant()
Definition Matchers.h:88
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:73
felt::FeltConstantOp * what
Definition Matchers.h:74
bool match(mlir::Value v)
Definition Matchers.h:77
ConstantCapture(felt::FeltConstantOp *capture)
Definition Matchers.h:75
Matches and optionally captures a constrainref base value, which is either a field read or a block ar...
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