1//===-- Attrs.td -------------------------------------------*- tablegen -*-===//
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
8//===----------------------------------------------------------------------===//
10#ifndef LLZK_FELT_ATTRS
11#define LLZK_FELT_ATTRS
13include "llzk/Dialect/Felt/IR/Dialect.td"
14include "llzk/Dialect/LLZK/IR/AttributeHelper.td"
16include "mlir/IR/AttrTypeBase.td"
17include "mlir/IR/BuiltinAttributeInterfaces.td"
20 : AttrDef<FeltDialect, "FeltConst", [TypedAttrInterface]> {
21 let mnemonic = "const";
22 let summary = "finite field element";
24 A felt attribute represents a finite field element.
27 let parameters = (ins APIntParameter<"The felt constant value">:$value,
28 DefaultValuedParameter<"FeltType", "FeltType::get($_ctxt)">:$type);
30 let hasCustomAssemblyFormat = 1;
33 // Like the default builder but with convenience overloads for
34 // constructing the APInt.
36 (ins "unsigned":$numBits, "::llvm::StringRef":$str, "FeltType":$ty),
37 [{ return $_get(context, ::llvm::APInt(numBits, str, 10), ty); }]>,
39 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts,
41 [{ return $_get(context, ::llvm::APInt(numBits, parts), ty); }]>,
42 // All APInt combinations but using felt type with unspecified field.
43 AttrBuilder<(ins "::llvm::APInt":$value),
44 [{ return get(context, value, FeltType::get(context)); }]>,
46 (ins "unsigned":$numBits, "::llvm::StringRef":$str),
47 [{ return get(context, numBits, str, FeltType::get(context)); }]>,
49 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts),
50 [{ return get(context, numBits, parts, FeltType::get(context)); }]>,
51 // All APInt combinations but using the field name to construct felt type.
53 (ins "::llvm::APInt":$value, "::llvm::StringRef":$fieldName),
54 [{ return get(context, value, FeltType::get(context, fieldName)); }]>,
56 (ins "unsigned":$numBits, "::llvm::StringRef":$str,
57 "::llvm::StringRef":$fieldName),
58 [{ return get(context, numBits, str, FeltType::get(context, fieldName)); }]>,
60 (ins "unsigned":$numBits, "::llvm::ArrayRef<uint64_t>":$parts,
61 "::llvm::StringRef":$fieldName),
62 [{ return get(context, numBits, parts, FeltType::get(context, fieldName)); }]>];
64 let extraClassDeclaration = [{
65 operator ::llvm::APInt() const { return getValue(); }
67 /// Returns the field name from the stored FeltType, or a null StringAttr
68 /// if no type is stored.
69 ::mlir::StringAttr getFieldName() const;
73def LLZK_FieldSpecAttr : AttrDef<FeltDialect, "FieldSpec"> {
74 let mnemonic = "field";
75 let summary = "prime field specification";
77 A specification of a prime field for use by felt types.
79 These specifications are provided in the `llzk.fields` attribute on the root
80 module as either a single element or a flat array, for example:
82 module attributes {llzk.lang, llzk.fields = field<foo, 7> { ... }
83 module attributes {llzk.lang, llzk.fields = [field<>]} { ... }
85 Specifications should not be provided for built-in fields, which include:
93 let parameters = (ins "::mlir::StringAttr":$fieldName,
94 APIntParameter<"The prime modulus">:$prime);
96 // Format is [{ `<` $fieldName `,` $prime `>` }], but with custom parsing
97 // to enable caching of the Field object definition.
98 let hasCustomAssemblyFormat = 1;
101 [AttrBuilder<(ins "::llvm::StringRef":$fieldName, "unsigned":$numBits,
102 "::llvm::StringRef":$primeStr),
104 return $_get(context, ::mlir::StringAttr::get(context, fieldName), ::llvm::APInt(numBits, primeStr, 10));
106 AttrBuilder<(ins "::llvm::StringRef":$fieldName, "unsigned":$numBits,
107 "::llvm::ArrayRef<uint64_t>":$parts),
109 return $_get(context, ::mlir::StringAttr::get(context, fieldName), ::llvm::APInt(numBits, parts));
113#endif // LLZK_FELT_ATTRS