LLZK
3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Toggle main menu visibility
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
12
#include "
llzk/Dialect/Felt/IR/Ops.h
"
13
#include "
llzk/Dialect/Struct/IR/Ops.h
"
14
15
#include <mlir/IR/Matchers.h>
16
#include <mlir/IR/Operation.h>
17
#include <mlir/IR/Value.h>
18
19
namespace
llzk
{
20
24
template
<
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
46
template
<
typename
OpType,
typename
LhsMatcher,
typename
RhsMatcher>
47
auto
m_CommutativeOp
(LhsMatcher lhs, RhsMatcher rhs) {
48
return
CommutativeMatcher<LhsMatcher, RhsMatcher, OpType>
(lhs, rhs);
49
}
50
53
struct
RefValueCapture
{
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
69
inline
RefValueCapture
m_RefValue
() {
return
RefValueCapture
(
nullptr
); }
70
71
inline
RefValueCapture
m_RefValue
(mlir::Value *capture) {
return
RefValueCapture
(capture); }
72
74
struct
ConstantCapture
{
75
felt::FeltConstantOp
*
what
;
76
ConstantCapture
(
felt::FeltConstantOp
*capture) :
what
(capture) {}
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
89
inline
ConstantCapture
m_Constant
() {
return
ConstantCapture
(
nullptr
); }
90
91
inline
ConstantCapture
m_Constant
(
felt::FeltConstantOp
*capture) {
92
return
ConstantCapture
(capture);
93
}
94
95
}
// namespace llzk
Ops.h
Ops.h
llzk::felt::FeltConstantOp
Definition
Ops.h.inc:766
llzk
Definition
AnalysisPassEnums.cpp:19
llzk::m_RefValue
RefValueCapture m_RefValue()
Definition
Matchers.h:69
llzk::m_Constant
ConstantCapture m_Constant()
Definition
Matchers.h:89
llzk::m_CommutativeOp
auto m_CommutativeOp(LhsMatcher lhs, RhsMatcher rhs)
Definition
Matchers.h:47
llzk::CommutativeMatcher
This matcher will either match on lhs op rhs or rhs op lhs.
Definition
Matchers.h:24
llzk::CommutativeMatcher::rhsMatcher
RhsMatcher rhsMatcher
Definition
Matchers.h:43
llzk::CommutativeMatcher::match
bool match(mlir::Operation *op)
Definition
Matchers.h:28
llzk::CommutativeMatcher::lhsMatcher
LhsMatcher lhsMatcher
Definition
Matchers.h:42
llzk::CommutativeMatcher::CommutativeMatcher
CommutativeMatcher(LhsMatcher lhs, RhsMatcher rhs)
Definition
Matchers.h:26
llzk::ConstantCapture
Matches and optionally captures a felt constant.
Definition
Matchers.h:74
llzk::ConstantCapture::what
felt::FeltConstantOp * what
Definition
Matchers.h:75
llzk::ConstantCapture::match
bool match(mlir::Value v)
Definition
Matchers.h:78
llzk::ConstantCapture::ConstantCapture
ConstantCapture(felt::FeltConstantOp *capture)
Definition
Matchers.h:76
llzk::RefValueCapture
Matches and optionally captures a SourceRef base value, which is either a member read or a block argu...
Definition
Matchers.h:53
llzk::RefValueCapture::RefValueCapture
RefValueCapture(mlir::Value *capture)
Definition
Matchers.h:55
llzk::RefValueCapture::what
mlir::Value * what
Definition
Matchers.h:54
llzk::RefValueCapture::match
bool match(mlir::Value v)
Definition
Matchers.h:57
include
llzk
Analysis
Matchers.h
Generated by
1.17.0
Copyright 2025 Veridise Inc. under the Apache License v2.0. Copyright 2026 Project LLZK under the Apache License v2.0.