LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Felt.cpp
Go to the documentation of this file.
1//===-- Felt.cpp - Felt dialect C API implementation ------------*- C++ -*-===//
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#include "llzk-c/Dialect/Felt.h"
11
12#include "llzk-c/Dialect/LLZK.h"
13
14#include "llzk/CAPI/Support.h"
21
22#include <mlir/CAPI/Registration.h>
23#include <mlir/CAPI/Wrap.h>
24
25using namespace mlir;
26using namespace llzk;
27using namespace llzk::felt;
28
29// Include the generated CAPI
33
34MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Felt, llzk__felt, FeltDialect)
35
36MlirAttribute llzkFelt_FeltConstAttrGetFromInt64(MlirContext ctx, int64_t value, MlirType type) {
37 return wrap(
38 FeltConstAttr::get(unwrap(ctx), llvm::DynamicAPInt(value), unwrap_cast<FeltType>(type))
39 );
40}
41
42MlirAttribute
43llzkFelt_FeltConstAttrGetFromInt64InField(MlirContext ctx, int64_t value, MlirStringRef fieldName) {
44 MlirType type = llzkFelt_FeltTypeGetFromRef(ctx, fieldName);
45 if (mlirTypeIsNull(type)) {
46 return MlirAttribute {nullptr};
47 }
48 return llzkFelt_FeltConstAttrGetFromInt64(ctx, value, type);
49}
50
51MlirAttribute llzkFelt_FeltConstAttrGetFromInt64Unspecified(MlirContext ctx, int64_t value) {
52 return wrap(FeltConstAttr::get(unwrap(ctx), llvm::DynamicAPInt(value)));
53}
54
55MlirAttribute
56llzkFelt_FeltConstAttrGetFromString(MlirContext ctx, MlirStringRef str, MlirType type) {
57 return llzkFelt_FeltConstAttrGet(ctx, str, type);
58}
59
61 MlirContext ctx, MlirStringRef str, MlirStringRef fieldName
62) {
63 MlirType type = llzkFelt_FeltTypeGetFromRef(ctx, fieldName);
64 if (mlirTypeIsNull(type)) {
65 return MlirAttribute {nullptr};
66 }
67 return llzkFelt_FeltConstAttrGetFromString(ctx, str, type);
68}
69
70MlirAttribute llzkFelt_FeltConstAttrGetFromStringUnspecified(MlirContext ctx, MlirStringRef str) {
72}
73
75 MlirContext ctx, const uint64_t *parts, intptr_t nParts, MlirType type
76) {
77 assert(nParts >= 0 && "part count must be non-negative");
78 assert((parts || nParts == 0) && "non-empty parts must not be null");
79 return wrap(
80 FeltConstAttr::get(unwrap(ctx), llvm::ArrayRef(parts, nParts), unwrap_cast<FeltType>(type))
81 );
82}
83
85 MlirContext ctx, const uint64_t *parts, intptr_t nParts, MlirStringRef fieldName
86) {
87 MlirType type = llzkFelt_FeltTypeGetFromRef(ctx, fieldName);
88 if (mlirTypeIsNull(type)) {
89 return MlirAttribute {nullptr};
90 }
91 return llzkFelt_FeltConstAttrGetFromParts(ctx, parts, nParts, type);
92}
93
95 MlirContext ctx, const uint64_t *parts, intptr_t nParts
96) {
98 ctx, parts, nParts, llzkFelt_FeltTypeGetUnspecified(ctx)
99 );
100}
101
103 MlirContext ctx, MlirIdentifier fieldName, MlirStringRef primeStr
104) {
105 return llzkFelt_FieldSpecAttrGet(ctx, fieldName, primeStr);
106}
107
109 MlirContext ctx, MlirIdentifier fieldName, const uint64_t *parts, intptr_t nParts
110) {
111 assert(nParts >= 0 && "part count must be non-negative");
112 assert((parts || nParts == 0) && "non-empty parts must not be null");
113 return wrap(
114 FieldSpecAttr::getChecked(
115 UnknownLoc::get(unwrap(ctx)), unwrap(ctx), unwrap(fieldName),
116 llvm::ArrayRef(parts, nParts)
117 )
118 );
119}
120
121MlirType llzkFelt_FeltTypeGetUnspecified(MlirContext ctx) {
122 return wrap(FeltType::get(unwrap(ctx)));
123}
124
125MlirType llzkFelt_FeltTypeGetFromRef(MlirContext ctx, MlirStringRef fieldName) {
126 return wrap(
128 UnknownLoc::get(unwrap(ctx)), unwrap(ctx), StringAttr::get(unwrap(ctx), unwrap(fieldName))
129 )
130 );
131}
This file implements helpers for DynamicAPInt operations and conversions that LLVM does not provide.
MlirAttribute llzkFelt_FieldSpecAttrGet(MlirContext ctx, MlirIdentifier fieldName, MlirStringRef prime)
Create a llzk::felt::FieldSpecAttr Attribute with the given parameters.
MlirAttribute llzkFelt_FeltConstAttrGet(MlirContext ctx, MlirStringRef rawValue, MlirType type)
Create a llzk::felt::FeltConstAttr Attribute with the given parameters.
MlirAttribute llzkFelt_FeltConstAttrGetFromPartsInField(MlirContext ctx, const uint64_t *parts, intptr_t nParts, MlirStringRef fieldName)
Creates a llzk::felt::FeltConstAttr from 64-bit parts in LSB order.
Definition Felt.cpp:84
MlirType llzkFelt_FeltTypeGetUnspecified(MlirContext ctx)
Gets the llzk::felt::FeltType without the field specified.
Definition Felt.cpp:121
MlirAttribute llzkFelt_FeltConstAttrGetFromString(MlirContext ctx, MlirStringRef str, MlirType type)
Creates a llzk::felt::FeltConstAttr from a base-10 representation of a signed integer in the specifie...
Definition Felt.cpp:56
MlirAttribute llzkFelt_FeltConstAttrGetFromInt64InField(MlirContext ctx, int64_t value, MlirStringRef fieldName)
Creates a llzk::felt::FeltConstAttr from a signed 64-bit integer in the specified field.
Definition Felt.cpp:43
MlirAttribute llzkFelt_FieldSpecAttrGetFromString(MlirContext ctx, MlirIdentifier fieldName, MlirStringRef primeStr)
Creates a llzk::felt::FieldSpecAttr from a base-10 representation of the prime.
Definition Felt.cpp:102
MlirAttribute llzkFelt_FeltConstAttrGetFromPartsUnspecified(MlirContext ctx, const uint64_t *parts, intptr_t nParts)
Creates a llzk::felt::FeltConstAttr from 64-bit parts in LSB order.
Definition Felt.cpp:94
MlirAttribute llzkFelt_FeltConstAttrGetFromInt64Unspecified(MlirContext ctx, int64_t value)
Creates a llzk::felt::FeltConstAttr from a signed 64-bit integer in an unspecified field.
Definition Felt.cpp:51
MlirAttribute llzkFelt_FieldSpecAttrGetFromParts(MlirContext ctx, MlirIdentifier fieldName, const uint64_t *parts, intptr_t nParts)
Creates a llzk::felt::FieldSpecAttr from 64-bit parts in LSB order.
Definition Felt.cpp:108
MlirAttribute llzkFelt_FeltConstAttrGetFromStringUnspecified(MlirContext ctx, MlirStringRef str)
Creates a llzk::felt::FeltConstAttr from a base-10 representation of a signed integer in an unspecifi...
Definition Felt.cpp:70
MlirType llzkFelt_FeltTypeGetFromRef(MlirContext ctx, MlirStringRef fieldName)
Gets a llzk::felt::FeltType referring to an already defined field.
Definition Felt.cpp:125
MlirAttribute llzkFelt_FeltConstAttrGetFromParts(MlirContext ctx, const uint64_t *parts, intptr_t nParts, MlirType type)
Creates a llzk::felt::FeltConstAttr from 64-bit parts in LSB order.
Definition Felt.cpp:74
MlirAttribute llzkFelt_FeltConstAttrGetFromInt64(MlirContext ctx, int64_t value, MlirType type)
Creates a llzk::felt::FeltConstAttr from a signed 64-bit integer in the specified field.
Definition Felt.cpp:36
MlirAttribute llzkFelt_FeltConstAttrGetFromStringInField(MlirContext ctx, MlirStringRef str, MlirStringRef fieldName)
Creates a llzk::felt::FeltConstAttr from a base-10 representation of a signed integer in the specifie...
Definition Felt.cpp:60
LLZK felt (Field ELemenT) type and operations.
Definition Dialect.h.inc:13
static FeltType getChecked(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, ::mlir::StringAttr fieldName)
Definition Types.cpp.inc:73
static FeltType get(::mlir::MLIRContext *context, ::mlir::StringAttr fieldName)
Definition Types.cpp.inc:69
auto unwrap_cast(auto &from)
Definition Support.h:47