LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
InlineIncludesPass.cpp
Go to the documentation of this file.
1//===-- LLZKInlineIncludesPass.cpp - -llzk-inline-includes pass -*- 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
16
19
20#include <mlir/IR/BuiltinOps.h>
21
22#include <llvm/Support/LogicalResult.h>
23
24// Include the generated base pass class definitions.
25namespace llzk::include {
26#define GEN_PASS_DEF_INLINEINCLUDESPASS
28} // namespace llzk::include
30using namespace mlir;
31using namespace llzk::include;
33namespace {
34using IncludeStack = std::vector<std::pair<StringRef, Location>>;
36inline bool contains(IncludeStack &stack, StringRef &&loc) {
37 auto path_match = [loc](std::pair<StringRef, Location> &p) { return p.first == loc; };
38 return std::find_if(stack.begin(), stack.end(), path_match) != stack.end();
39}
41class InlineIncludesPass : public llzk::include::impl::InlineIncludesPassBase<InlineIncludesPass> {
42 void runOnOperation() override {
43 std::vector<std::pair<ModuleOp, IncludeStack>> currLevel = {
44 std::make_pair(getOperation(), IncludeStack())
45 };
46 do {
47 std::vector<std::pair<ModuleOp, IncludeStack>> nextLevel = {};
48 for (std::pair<ModuleOp, IncludeStack> &curr : currLevel) {
49 curr.first.walk([includeStack = std::move(curr.second),
50 &nextLevel](IncludeOp incOp) mutable {
51 // Check for cyclic includes
52 if (contains(includeStack, incOp.getPath())) {
53 auto err = incOp.emitError().append("found cyclic include");
54 for (auto it = includeStack.rbegin(); it != includeStack.rend(); ++it) {
55 err.attachNote(it->second).append("included from here");
56 }
57 err.report();
58 } else {
59 includeStack.push_back(std::make_pair(incOp.getPath(), incOp.getLoc()));
60 FailureOr<ModuleOp> result = incOp.inlineAndErase();
61 if (succeeded(result)) {
62 ModuleOp newMod = result.value();
63 assert(succeeded(newMod.verify()) && "newMod must pass verification");
64 nextLevel.push_back(make_pair(newMod, includeStack));
65 }
66 }
67 // Advance in either case so as many errors as possible are found in a single run.
68 return WalkResult::advance();
69 });
70 }
71 currLevel = nextLevel;
72 } while (!currLevel.empty());
73
74 if (failed(getOperation().verify())) {
75 signalPassFailure();
76 return;
77 }
78 markAllAnalysesPreserved();
79 }
80};
81
82} // namespace
83
84std::unique_ptr<mlir::Pass> llzk::include::createInlineIncludesPass() {
85 return std::make_unique<InlineIncludesPass>();
86};
std::unique_ptr< mlir::Pass > createInlineIncludesPass()