LLZK 2.1.1
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
67Type PodType::getSingleRecordType(StringAttr recordName) const {
68 for (RecordAttr record : getRecords()) {
69 if (record.getName() == recordName.getValue()) {
70 return PodType::get(getContext(), {record});
71 }
72 }
73 return nullptr;
74}
75
77std::optional<DenseMap<Attribute, Type>> PodType::getSubelementIndexMap() const {
78 DenseMap<Attribute, Type> ret;
79 for (RecordAttr record : getRecords()) {
80 ret[record.getName()] = PodType::get(getContext(), {record});
81 }
82 return ret;
83}
84
86Type PodType::getTypeAtIndex(Attribute index) const {
87 auto recordName = llvm::dyn_cast<StringAttr>(index);
88 return recordName ? getSingleRecordType(recordName) : nullptr;
89}
90
91ParseResult parsePodType(AsmParser &parser, SmallVector<RecordAttr> &records) {
92 return parser.parseCommaSeparatedList(AsmParser::Delimiter::Square, [&records, &parser]() {
93 StringAttr name;
94 Type type;
95 auto result = parseRecord(parser, name, type);
96 if (mlir::succeeded(result)) {
97 records.push_back(RecordAttr::get(parser.getContext(), name, type));
98 }
99 return result;
100 });
101}
102
103void printPodType(AsmPrinter &printer, ArrayRef<RecordAttr> records) {
104 auto &os = printer.getStream();
105 os << '[';
106 printer.printStrippedAttrOrType(records);
107 os << ']';
108}
109
110} // 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
::mlir::Type getTypeAtIndex(::mlir::Attribute index) const
Required by DestructurableTypeInterface / SROA pass.
Definition Types.cpp:86
::std::optional<::llvm::DenseMap<::mlir::Attribute, ::mlir::Type > > getSubelementIndexMap() const
Required by DestructurableTypeInterface / SROA pass.
Definition Types.cpp:77
::llvm::ArrayRef<::llzk::pod::RecordAttr > getRecords() const
::mlir::Type getSingleRecordType(::mlir::StringAttr name) const
Creates a PodType containing only the requested record.
Definition Types.cpp:67
::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:25
ParseResult parsePodType(AsmParser &parser, SmallVector< RecordAttr > &records)
Definition Types.cpp:91
ParseResult parseRecord(AsmParser &parser, StringAttr &name, Type &type)
Definition Attrs.cpp:27
void printPodType(AsmPrinter &printer, ArrayRef< RecordAttr > records)
Definition Types.cpp:103