LLZK 0.1.0
Veridise's ZK Language IR
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
34 - SymbolRefAttr, specifying a parameter value defined by a struct parameter
35 or global constant
36 - AffineMapAttr, for an array of struct elements whose template parameters
37 vary based on some fixed pattern.
38 - TypeAttr, for specifying a type parameter.
39
40 ```llzk
41 // Type for struct `A` with no parameters.
42 !struct.type<@A>
43
44 // Type for struct `B` with IntegerAttr and SymbolRefAttr parameters.
45 !struct.type<@B<[5, @C]>>
46
47 // Type for struct `C` with TypeAttr and IntegerAttr parameters.
48 !struct.type<@C<[!felt.type, 24]>>
49 ```
50 }];
51
52 let parameters =
53 (ins TypeParameter<
54 "::mlir::SymbolRefAttr",
55 "Fully-qualified name of the struct definition.">:$nameRef,
56 OptionalParameter<"::mlir::ArrayAttr", "Struct parameters">:$params);
57
58 let assemblyFormat =
59 [{ `<` $nameRef ( `<` custom<StructParams>($params)^ `>` )? `>` }];
60
61 let genVerifyDecl = 1;
62
63 let skipDefaultBuilders = 1;
64 let builders = [TypeBuilderWithInferredContext<
65 (ins "::mlir::SymbolRefAttr":$structName), [{
66 return $_get(structName.getContext(), structName, ::mlir::ArrayAttr());
67 }]>,
68 TypeBuilderWithInferredContext<
69 (ins "::mlir::SymbolRefAttr":$structName,
70 "::mlir::ArrayAttr":$params),
71 [{
72 ::mlir::MLIRContext *ctx = structName.getContext();
73 if (params) {
74 OwningEmitErrorFn emitErrorFn = ::llzk::wrapNullableInFlightDiagnostic(emitError, ctx);
75 auto paramsRes = forceIntAttrTypes(params.getValue(), emitErrorFn);
76 if(::mlir::failed(paramsRes)) { return StructType(); }
77 params = ::mlir::ArrayAttr::get(ctx, *paramsRes);
78 }
79 return $_get(ctx, structName, params);
80 }]>,
81 TypeBuilderWithInferredContext<
82 (ins "::mlir::SymbolRefAttr":$structName,
83 "::llvm::ArrayRef<::mlir::Attribute>":$paramsRef),
84 [{
85 ::mlir::MLIRContext *ctx = structName.getContext();
86 OwningEmitErrorFn emitErrorFn = ::llzk::wrapNullableInFlightDiagnostic(emitError, ctx);
87 auto paramsRes = forceIntAttrTypes(paramsRef, emitErrorFn);
88 if(::mlir::failed(paramsRes)) { return StructType(); }
89 return $_get(ctx, structName, ::mlir::ArrayAttr::get(ctx, *paramsRes));
90 }]>];
91
92 let extraClassDeclaration = [{
93 /// Gets the `struct` op that defines this struct. Provided `op` is
94 /// used as a starting point for the lookup. Should not be assumed to
95 /// be non-`null` as we don't verify all types during verification.
96 ::mlir::FailureOr<SymbolLookupResult<StructDefOp>> getDefinition
97 (::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op) const;
98
99 // Verifies that this type references a valid struct, relative to the given `op`.
100 ::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op);
101
102 /// Returns wether the struct this type refers to has fields marked as columns.
103 /// A lookup is necessary first and will forward the failure state if it fails.
104 mlir::LogicalResult hasColumns(mlir::SymbolTableCollection &symbolTable, mlir::Operation *op) const;
105 }];
106
107 let extraClassDefinition = [{
108 namespace {
109 /// This definition of `emitError` is used by the `get()` functions generated by the
110 /// custom builders for this type. The `getChecked()` functions generated by those same
111 /// builders have a parameter with this same name that shadows this definition so the
112 /// `getChecked()` versions will use the function supplied via the parameter. Regardless,
113 /// `wrapNullableInFlightDiagnostic()` checks for `nullptr` and generates a default if
114 /// necessary. This approach, although a bit hacky, allows a legitimate error function to
115 /// be used whenever available, only reverting to a default in the `get()` function.
116 const ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError = nullptr;
117 }
118 }];
119}
120
121#endif // LLZK_STRUCT_TYPES