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 - POD types implementations -------------------*- 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
11
12#include <mlir/IR/Builders.h>
13#include <mlir/IR/Diagnostics.h>
14#include <mlir/Support/LLVM.h>
15
16#include <llvm/ADT/STLExtras.h>
17#include <llvm/ADT/StringSet.h>
18
19using namespace mlir;
20
21namespace llzk::pod {
22
23//===----------------------------------------------------------------------===//
24// PodType
25//===----------------------------------------------------------------------===//
26
27LogicalResult
28PodType::verify(llvm::function_ref<InFlightDiagnostic()> emitError, ArrayRef<RecordAttr> records) {
29 llvm::StringSet<> seenNames;
30 bool failed = false;
31 for (auto record : records) {
32 auto recordName = record.getName();
33 if (seenNames.contains(recordName)) {
34 emitError() << "found duplicated record name '" << recordName.getValue() << '\'';
35 failed = true;
36 }
37 seenNames.insert(recordName);
38 }
39 return mlir::failure(failed);
40}
41
43 auto records = llvm::map_to_vector(init, [ctx](auto record) {
44 return RecordAttr::get(ctx, StringAttr::get(ctx, record.name), record.value.getType());
45 });
46 return get(ctx, records);
47}
48
49FailureOr<Type>
50PodType::getRecord(StringRef recordName, function_ref<InFlightDiagnostic()> emitError) const {
51 for (RecordAttr record : getRecords()) {
52 if (record.getName() == recordName) {
53 return record.getType();
54 }
55 }
56 return emitError() << "record '" << recordName << "' was not found in plain-old-data type";
57}
58
59llvm::StringMap<Type> PodType::getRecordMap() const {
60 llvm::StringMap<Type> map;
61 for (RecordAttr record : getRecords()) {
62 map.insert({record.getName(), record.getType()});
63 }
64 return map;
65}
66
67ParseResult parsePodType(AsmParser &parser, SmallVector<RecordAttr> &records) {
68 return parser.parseCommaSeparatedList(AsmParser::Delimiter::Square, [&records, &parser]() {
69 StringAttr name;
70 Type type;
71 auto result = parseRecord(parser, name, type);
72 if (mlir::succeeded(result)) {
73 records.push_back(RecordAttr::get(parser.getContext(), name, type));
74 }
75 return result;
76 });
77}
78
79void printPodType(AsmPrinter &printer, ArrayRef<RecordAttr> records) {
80 auto &os = printer.getStream();
81 os << '[';
82 printer.printStrippedAttrOrType(records);
83 os << ']';
84}
85
86} // namespace llzk::pod
::llvm::FailureOr<::mlir::Type > getRecord(::llvm::StringRef name, ::llvm::function_ref<::mlir::InFlightDiagnostic()>) const
Searches a record by name.
Definition Types.cpp:50
static PodType fromInitialValues(::mlir::MLIRContext *ctx, InitializedRecords init)
Creates a new type from a set of initialized records.
Definition Types.cpp:42
static PodType get(::mlir::MLIRContext *context, ::llvm::ArrayRef<::llzk::pod::RecordAttr > records)
Definition Types.cpp.inc:68
::llvm::StringMap<::mlir::Type > getRecordMap() const
Returns the records in map form.
Definition Types.cpp:59
::llvm::ArrayRef<::llzk::pod::RecordAttr > getRecords() const
::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::llvm::ArrayRef<::llzk::pod::RecordAttr > records)
Definition Types.cpp:28
mlir::ArrayRef< RecordValue > InitializedRecords
Definition Types.h:23
ParseResult parsePodType(AsmParser &parser, SmallVector< RecordAttr > &records)
Definition Types.cpp:67
ParseResult parseRecord(AsmParser &parser, StringAttr &name, Type &type)
Definition Attrs.cpp:27
void printPodType(AsmPrinter &printer, ArrayRef< RecordAttr > records)
Definition Types.cpp:79