LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Types.td
Go to the documentation of this file.
1//===-- Types.td -------------------------------------------*- tablegen -*-===//
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
10#ifndef LLZK_STRUCT_TYPES
11#define LLZK_STRUCT_TYPES
12
13include "llzk/Dialect/Shared/Types.td"
14include "llzk/Dialect/Struct/IR/Dialect.td"
15
16include "mlir/IR/AttrTypeBase.td"
17include "mlir/IR/BuiltinTypes.td"
18include "mlir/Interfaces/MemorySlotInterfaces.td"
19include "mlir/IR/BuiltinTypeInterfaces.td"
20
21class StructDialectType<string name, string typeMnemonic,
22 list<Trait> traits = []>
23 : TypeDef<StructDialect, name, traits> {
24 let mnemonic = typeMnemonic;
25}
26
27def LLZK_StructType : StructDialectType<"Struct", "type"> {
28 let summary = "circuit component";
29 let description = [{
30 Type of a `struct` op instance. For structs that contain template parameters,
31 the type must contain a list of attributes that instantiate the template
32 parameters, one per parameter. Each attribute must be one of the following:
33 - IntegerAttr (with IndexType), specifying a fixed parameter value; the
34 wildcard (`?`) is not allowed
35 - SymbolRefAttr, specifying a parameter value defined by a struct parameter
36 or global constant
37 - AffineMapAttr, for an array of struct elements whose template parameters
38 vary based on some fixed pattern.
39 - TypeAttr, for specifying a type parameter.
40
41 ```llzk
42 // Type for struct `A` with no parameters.
43 !struct.type<@A>
44
45 // Type for struct `B` with IntegerAttr and SymbolRefAttr parameters.
46 !struct.type<@B<[5, @C]>>
47
48 // Type for struct `C` with TypeAttr and IntegerAttr parameters.
49 !struct.type<@C<[!felt.type, 24]>>
50 ```
51 }];
52
53 let parameters =
54 (ins TypeParameter<
55 "::mlir::SymbolRefAttr",
56 "Fully-qualified name of the struct definition.">:$nameRef,
57 OptionalParameter<"::mlir::ArrayAttr", "Struct parameters">:$params);
58
59 let assemblyFormat =
60 [{ `<` $nameRef ( `<` custom<TemplateParams>($params)^ `>` )? `>` }];
61
62 let genVerifyDecl = 1;
63
64 let skipDefaultBuilders = 1;
65 let builders = [TypeBuilderWithInferredContext<
66 (ins "::mlir::SymbolRefAttr":$structName), [{
67 return $_get(structName.getContext(), structName, ::mlir::ArrayAttr());
68 }]>,
69 TypeBuilderWithInferredContext<
70 (ins "::mlir::SymbolRefAttr":$structName,
71 "::mlir::ArrayAttr":$params),
72 [{
73 ::mlir::MLIRContext *ctx = structName.getContext();
74 if (params) {
75 OwningEmitErrorFn emitErrorFn = ::llzk::wrapNullableInFlightDiagnostic(emitError, ctx);
76 auto paramsRes = forceIntAttrTypes(params.getValue(), emitErrorFn);
77 if(::mlir::failed(paramsRes)) { return StructType(); }
78 params = ::mlir::ArrayAttr::get(ctx, *paramsRes);
79 }
80 return $_get(ctx, structName, params);
81 }]>,
82 TypeBuilderWithInferredContext<
83 (ins "::mlir::SymbolRefAttr":$structName,
84 "::llvm::ArrayRef<::mlir::Attribute>":$paramsRef),
85 [{
86 ::mlir::MLIRContext *ctx = structName.getContext();
87 OwningEmitErrorFn emitErrorFn = ::llzk::wrapNullableInFlightDiagnostic(emitError, ctx);
88 auto paramsRes = forceIntAttrTypes(paramsRef, emitErrorFn);
89 if(::mlir::failed(paramsRes)) { return StructType(); }
90 return $_get(ctx, structName, ::mlir::ArrayAttr::get(ctx, *paramsRes));
91 }]>];
92
93 let extraClassDeclaration = [{
94 /// Gets the `struct` op that defines this struct. Provided `op` is
95 /// used as a starting point for the lookup. Should not be assumed to
96 /// be non-`null` as we don't verify all types during verification.
97 ::mlir::FailureOr<SymbolLookupResult<StructDefOp>> getDefinition
98 (::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op, bool reportMissing = true) const;
99
100 // Verifies that this type references a valid struct, relative to the given `op`.
101 ::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op);
102
103 /// Returns wether the struct this type refers to has members marked as columns.
104 /// A lookup is necessary first and will forward the failure state if it fails.
105 mlir::LogicalResult hasColumns(mlir::SymbolTableCollection &symbolTable, mlir::Operation *op) const;
106 }];
107
108 let extraClassDefinition = [{
109 namespace {
110 /// This definition of `emitError` is used by the `get()` functions generated by the
111 /// custom builders for this type. The `getChecked()` functions generated by those same
112 /// builders have a parameter with this same name that shadows this definition so the
113 /// `getChecked()` versions will use the function supplied via the parameter. Regardless,
114 /// `wrapNullableInFlightDiagnostic()` checks for `nullptr` and generates a default if
115 /// necessary. This approach, although a bit hacky, allows a legitimate error function to
116 /// be used whenever available, only reverting to a default in the `get()` function.
117 const ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError = nullptr;
118 }
119 }];
120}
121
122#endif // LLZK_STRUCT_TYPES