LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Walk.h
Go to the documentation of this file.
1//===-- Walk.h --------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2026 Project LLZK
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
12#include <mlir/IR/Visitors.h>
13
14#include <llvm/ADT/STLFunctionalExtras.h>
15#include <llvm/ADT/SmallVector.h>
16
20template <typename MatchType, typename R> inline static bool walkContains(R &root) {
21 return root.walk([](MatchType) { return mlir::WalkResult::interrupt(); }).wasInterrupted();
22}
23
28template <typename MatchType, typename R>
29inline static bool walkContains(R &root, llvm::function_ref<bool(MatchType)> pred) {
30 return root
31 .walk([&pred](MatchType t) {
32 return pred(t) ? mlir::WalkResult::interrupt() : mlir::WalkResult::advance();
33 }).wasInterrupted();
34}
35
37template <typename MatchType, typename R>
38inline static llvm::SmallVector<MatchType> walkCollect(R &root) {
39 llvm::SmallVector<MatchType> collected;
40 root.walk([&collected](MatchType op) { collected.push_back(op); });
41 return collected;
42}
43
46template <typename MatchType, typename R>
47inline static llvm::SmallVector<MatchType>
48walkCollect(R &root, llvm::function_ref<bool(MatchType)> pred) {
49 llvm::SmallVector<MatchType> collected;
50 root.walk([&collected, &pred](MatchType op) {
51 if (pred(op)) {
52 collected.push_back(op);
53 }
54 });
55 return collected;
56}
57
60template <typename MatchType, typename R, typename Map>
61inline static auto walkCollectMapped(R &root, Map &&map) {
62 using MappedType = std::invoke_result_t<Map &, MatchType &>;
63
64 llvm::SmallVector<MappedType> collected;
65 root.walk([&collected, &map](MatchType op) { collected.push_back(map(op)); });
66 return collected;
67}