LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
ExposeChildMembersPass.cpp
Go to the documentation of this file.
1//===-- ExposeChildMembersPass.cpp ----------------------------------------===//
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
11
19#include "llzk/Util/Walk.h"
20
21#include <llvm/ADT/DenseSet.h>
22#include <llvm/ADT/STLExtras.h>
23
24namespace llzk::component {
25#define GEN_PASS_DEF_EXPOSECHILDMEMBERSPASS
27} // namespace llzk::component
28
29using namespace mlir;
30using namespace llzk;
31using namespace llzk::component;
32using namespace llzk::polymorphic;
33
34namespace {
36class ChildStorageVisitor {
38 SymbolTableCollection tables;
40 StructDefOp storageRoot;
44 llvm::DenseSet<Operation *> visited;
46 SmallVector<StructDefOp> children;
48 llvm::DenseSet<Operation *> rolledMembers;
50public:
51 ChildStorageVisitor(ModuleOp module, StructDefOp root)
52 : storageRoot(root), specializations(module) {}
53
54 /// Expose collected children only after traversal succeeds.
55 LogicalResult run() {
56 if (failed(visitStruct(storageRoot))) {
57 return failure();
58 }
59 for (auto child : children) {
60 for (auto member : child.getMemberDefs()) {
61 member.setPublicAttr();
62 }
63 }
64 for (Operation *member : rolledMembers) {
65 cast<MemberDefOp>(member).setPublicAttr();
66 }
67 return success();
68 }
69
70private:
72 LogicalResult visitStruct(StructDefOp definition) {
74 return definition.emitError("llzk-expose-child-members requires monomorphized struct types");
75 }
76 if (!visited.insert(definition).second) {
77 return success();
78 }
79 if (definition != storageRoot) {
80 children.push_back(definition);
81 }
82 for (auto member : definition.getMemberDefs()) {
83 if (failed(visitType(member.getType(), member))) {
84 return failure();
85 }
86 }
87 return success();
88 }
89
91 void collectRolledMembers(StructDefOp rolled, StructDefOp concrete) {
92 if (rolled == storageRoot) {
93 return;
94 }
95 for (auto member : concrete.getMemberDefs()) {
96 if (auto declaration = rolled.getMemberDef(member.getSymNameAttr())) {
97 rolledMembers.insert(declaration);
98 }
99 }
100 }
101
102 /// Visit the possible concrete definitions for a rolled storage type. Family
103 /// coordinates are irrelevant to visibility; repeated IDs are resolved once.
104 LogicalResult visitFamily(StructType structure, Operation *site) {
105 Attribute metadata = site->getAttr(FAMILY_ATTR);
106 if (!metadata) {
107 return site->emitError(
108 "missing concrete struct family element for llzk-expose-child-members"
109 );
110 }
111 auto records = dyn_cast<ArrayAttr>(metadata);
112 if (!records) {
113 return site->emitError("invalid poly.family metadata for llzk-expose-child-members");
114 }
115 auto rolled = structure.getDefinition(tables, site);
116 if (failed(rolled)) {
117 return failure();
118 }
119 if (!*rolled || rolled->viaInclude()) {
120 return site->emitError(
121 "cannot resolve child-member visibility family source; inline includes first"
122 );
123 }
124 llvm::DenseSet<int64_t> seenIds;
125 for (Attribute record : records) {
126 auto dict = dyn_cast<DictionaryAttr>(record);
127 auto type = dict ? dict.getAs<TypeAttr>(FAMILY_TYPE_KEY) : TypeAttr();
128 if (!type || !isa<StructType>(type.getValue())) {
129 return site->emitError("invalid poly.family entry for llzk-expose-child-members");
130 }
131 if (type.getValue() != structure) {
132 continue;
133 }
134 auto id = dict.getAs<IntegerAttr>(FAMILY_SPECIALIZATION_KEY);
135 if (!id || !id.getValue().isSignedIntN(64) || id.getInt() < 0) {
136 return site->emitError("invalid poly.family entry for llzk-expose-child-members");
137 }
138 if (!seenIds.insert(id.getInt()).second) {
139 continue;
140 }
141 auto definition = specializations.resolve(
142 tables, structure, id.getInt(), site, "llzk-expose-child-members"
143 );
144 if (failed(definition) || failed(visitStruct(*definition))) {
145 return failure();
146 }
147 collectRolledMembers(rolled->get(), *definition);
148 }
149 if (seenIds.empty()) {
150 return site->emitError(
151 "missing concrete struct family element for llzk-expose-child-members"
152 );
153 }
154 return success();
155 }
156
158 LogicalResult visitType(Type type, Operation *site) {
159 if (!walkContains<StructType>(type)) {
160 return success();
161 }
162 if (auto array = dyn_cast<array::ArrayType>(type)) {
163 if (!array.hasStaticShape()) {
164 return site->emitError("llzk-expose-child-members requires concrete array dimensions");
165 }
166 if (llvm::is_contained(array.getShape(), 0)) {
167 return success();
168 }
169 return visitType(array.getElementType(), site);
170 }
171 if (auto pod = dyn_cast<pod::PodType>(type)) {
172 for (auto record : pod.getRecords()) {
173 if (failed(visitType(record.getType(), site))) {
174 return failure();
175 }
176 }
177 return success();
178 }
179 auto structure = cast<StructType>(type);
180 if (auto params = structure.getParams(); params && !params.empty()) {
181 return visitFamily(structure, site);
182 }
183 auto definition = structure.getDefinition(tables, site);
184 if (failed(definition)) {
185 return failure();
186 }
187 if (!*definition || definition->viaInclude()) {
188 return site->emitError("cannot resolve child struct storage; inline includes first");
189 }
190 return visitStruct(definition->get());
191 }
192};
193
195class PassImpl : public llzk::component::impl::ExposeChildMembersPassBase<PassImpl> {
196 using Base = ExposeChildMembersPassBase<PassImpl>;
197 using Base::Base;
198
199 void runOnOperation() override {
200 SymbolTableCollection tables;
201 auto root = getRootStructDef(tables, getOperation(), rootStruct);
202 if (failed(root)) {
203 signalPassFailure();
204 return;
205 }
206 if (!*root) {
207 getOperation().emitError(
208 "llzk-expose-child-members requires llzk.main or an explicit root-struct"
209 );
210 signalPassFailure();
211 return;
212 }
213 if (root->viaInclude()) {
214 getOperation().emitError(
215 "llzk-expose-child-members requires a local root struct; inline includes first"
216 );
217 signalPassFailure();
218 return;
219 }
220 if (failed(exposeChildMembers(root->get()))) {
221 signalPassFailure();
222 }
223 }
224};
225} // namespace
226
228 auto module = getTopRootModule(root);
229 if (failed(module)) {
230 return failure();
231 }
232 return ChildStorageVisitor(*module, root).run();
233}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this definition
Definition LICENSE.txt:53
::std::vector< MemberDefOp > getMemberDefs()
Get all MemberDefOp in this structure.
Definition Ops.cpp:456
MemberDefOp getMemberDef(::mlir::StringAttr memberName)
Gets the MemberDefOp that defines the member in this structure with the given name,...
Definition Ops.cpp:445
::mlir::FailureOr< SymbolLookupResult< StructDefOp > > getDefinition(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op, bool reportMissing=true) const
Gets the struct op that defines this struct.
Definition Types.cpp:26
::mlir::ArrayAttr getParams() const
Resolve concrete struct specializations referenced by poly.family metadata.
mlir::LogicalResult exposeChildMembers(StructDefOp root)
Make reachable concrete child members and matching rolled family declarations public while preserving...
constexpr llvm::StringLiteral FAMILY_ATTR
Definition Ops.h:40
constexpr llvm::StringLiteral FAMILY_SPECIALIZATION_KEY
Definition Ops.h:43
bool isInTemplate(Operation *op)
Definition Ops.cpp:34
constexpr llvm::StringLiteral FAMILY_TYPE_KEY
The original affine-parameterized struct family, paired with element indices and a concrete specializ...
Definition Ops.h:46
FailureOr< SymbolLookupResult< StructDefOp > > getRootStructDef(SymbolTableCollection &tables, Operation *lookupFrom, StringRef symbolPath)