LLZK 0.1.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
13
14using namespace mlir;
15
16namespace llzk::component {
17
18ParseResult parseStructParams(AsmParser &parser, ArrayAttr &value) {
19 auto parseResult = FieldParser<ArrayAttr>::parse(parser);
20 if (failed(parseResult)) {
21 return parser.emitError(parser.getCurrentLocation(), "failed to parse struct parameters");
22 }
23 auto emitError = [&parser] {
24 return InFlightDiagnosticWrapper(parser.emitError(parser.getCurrentLocation()));
25 };
26 FailureOr<SmallVector<Attribute>> res = forceIntAttrTypes(parseResult->getValue(), emitError);
27 if (failed(res)) {
28 return failure();
29 }
30 value = parser.getBuilder().getArrayAttr(*res);
31 return success();
32}
33
34void printStructParams(AsmPrinter &printer, ArrayAttr value) {
35 printer << '[';
36 printAttrs(printer, value.getValue(), ", ");
37 printer << ']';
38}
39
40LogicalResult StructType::verify(
41 function_ref<InFlightDiagnostic()> emitError, SymbolRefAttr nameRef, ArrayAttr params
42) {
44}
45
46FailureOr<SymbolLookupResult<StructDefOp>> StructType::getDefinition(
47 SymbolTableCollection &symbolTable, Operation *op, bool reportMissing
48) const {
49 // First ensure this StructType passes verification
50 ArrayAttr typeParams = this->getParams();
51 if (failed(StructType::verify([op] { return op->emitError(); }, getNameRef(), typeParams))) {
52 return failure();
53 }
54 // Perform lookup and ensure the symbol references a StructDefOp
55 auto res = lookupTopLevelSymbol<StructDefOp>(symbolTable, getNameRef(), op, reportMissing);
56 if (failed(res) || !res.value()) {
57 if (reportMissing) {
58 return op->emitError() << "could not find '" << StructDefOp::getOperationName()
59 << "' named \"" << getNameRef() << '"';
60 } else {
61 return failure();
62 }
63 }
64 // If this StructType contains parameters, make sure they match the number from the StructDefOp.
65 if (typeParams) {
66 auto defParams = res.value().get().getConstParams();
67 size_t numExpected = defParams ? defParams->size() : 0;
68 if (typeParams.size() != numExpected) {
69 return op->emitError() << '\'' << StructType::name << "' type has " << typeParams.size()
70 << " parameters but \"" << res.value().get().getSymName()
71 << "\" expects " << numExpected;
72 }
73 }
74 return res;
75}
76
77LogicalResult StructType::verifySymbolRef(SymbolTableCollection &symbolTable, Operation *op) {
78 return getDefinition(symbolTable, op);
79}
80
81LogicalResult StructType::hasColumns(SymbolTableCollection &symbolTable, Operation *op) const {
82 auto lookup = getDefinition(symbolTable, op);
83 if (failed(lookup)) {
84 return lookup;
85 }
86 return lookup->get().hasColumns();
87}
88
89} // namespace llzk::component
Wrapper around InFlightDiagnostic that can either be a regular InFlightDiagnostic or a special versio...
Definition ErrorHelper.h:26
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:1189
::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:81
::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:46
::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::SymbolRefAttr nameRef, ::mlir::ArrayAttr params)
Definition Types.cpp:40
::mlir::ArrayAttr getParams() const
::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op)
Definition Types.cpp:77
static constexpr ::llvm::StringLiteral name
Definition Types.h.inc:38
void printStructParams(AsmPrinter &printer, ArrayAttr value)
Definition Types.cpp:34
ParseResult parseStructParams(AsmParser &parser, ArrayAttr &value)
Definition Types.cpp:18
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
FailureOr< SmallVector< Attribute > > forceIntAttrTypes(ArrayRef< Attribute > attrList, EmitErrorFn emitError)
OwningEmitErrorFn wrapNonNullableInFlightDiagnostic(llvm::function_ref< mlir::InFlightDiagnostic()> emitError)
LogicalResult verifyStructTypeParams(EmitErrorFn emitError, ArrayAttr params)