LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Types.cpp
Go to the documentation of this file.
1//===-- Types.cpp - Struct type implementations -----------------*- 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
11
14
15using namespace mlir;
16using namespace llzk::polymorphic;
17
18namespace llzk::component {
19
20LogicalResult StructType::verify(
21 function_ref<InFlightDiagnostic()> emitError, SymbolRefAttr /*nameRef*/, ArrayAttr params
22) {
24}
25
26FailureOr<SymbolLookupResult<StructDefOp>> StructType::getDefinition(
27 SymbolTableCollection &symbolTable, Operation *op, bool reportMissing
28) const {
29 // First ensure this StructType passes verification
30 ArrayAttr typeParams = this->getParams();
31 if (failed(StructType::verify([op] { return op->emitError(); }, getNameRef(), typeParams))) {
32 return failure();
33 }
34 // Perform lookup and ensure the symbol references a StructDefOp
35 auto res = lookupTopLevelSymbol<StructDefOp>(symbolTable, getNameRef(), op, reportMissing);
36 if (failed(res) || !res.value()) {
37 if (reportMissing) {
38 return op->emitError() << "could not find '" << StructDefOp::getOperationName()
39 << "' named \"" << getNameRef() << '"';
40 } else {
41 return failure();
42 }
43 }
44 // If this StructType contains parameters, make sure the StructDefOp is within a TemplateOp with
45 // the same number of params.
46 if (typeParams) {
47 size_t numExpected = 0;
48 if (TemplateOp parent = getParentOfType<TemplateOp>(*res.value())) {
49 numExpected = parent.numConstOps<TemplateParamOp>();
50 }
51 if (typeParams.size() != numExpected) {
52 return op->emitError() << '\'' << StructType::name << "' type has " << typeParams.size()
53 << " parameters but \"" << res.value().get().getSymName()
54 << "\" expects " << numExpected;
55 }
56 }
57 return res;
58}
59
60LogicalResult StructType::verifySymbolRef(SymbolTableCollection &symbolTable, Operation *op) {
61 return getDefinition(symbolTable, op);
62}
63
64LogicalResult StructType::hasColumns(SymbolTableCollection &symbolTable, Operation *op) const {
65 auto lookup = getDefinition(symbolTable, op);
66 if (failed(lookup)) {
67 return lookup;
68 }
69 return lookup->get().hasColumns();
70}
71
72} // namespace llzk::component
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:1165
::mlir::SymbolRefAttr getNameRef() const
mlir::LogicalResult hasColumns(mlir::SymbolTableCollection &symbolTable, mlir::Operation *op) const
Returns wether the struct this type refers to has members marked as columns.
Definition Types.cpp:64
::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
::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::SymbolRefAttr nameRef, ::mlir::ArrayAttr params)
Definition Types.cpp:20
::mlir::ArrayAttr getParams() const
::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op)
Definition Types.cpp:60
static constexpr ::llvm::StringLiteral name
Definition Types.h.inc:38
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
OpClass getParentOfType(mlir::Operation *op)
Return the closest surrounding parent operation that is of type 'OpClass'.
Definition OpHelpers.h:69
OwningEmitErrorFn wrapNonNullableInFlightDiagnostic(llvm::function_ref< mlir::InFlightDiagnostic()> emitError)
LogicalResult verifyStructTypeParams(EmitErrorFn emitError, ArrayAttr params)