18#include <mlir/IR/Operation.h>
20#include <llvm/ADT/TypeSwitch.h>
28 if (
const auto *boolValue = std::get_if<bool>(&value)) {
31 if (std::holds_alternative<std::monostate>(value)) {
32 return makeError(
"read of uninitialized i1 value");
39 if (
const auto *indexValue = std::get_if<int64_t>(&value)) {
42 if (std::holds_alternative<std::monostate>(value)) {
43 return makeError(
"read of uninitialized index value");
50 if (
const auto *feltValue = std::get_if<llvm::DynamicAPInt>(&value)) {
53 if (std::holds_alternative<std::monostate>(value)) {
54 return makeError(
"read of uninitialized felt value");
61 if (
const auto *arrayValue = std::get_if<ArrayValueRef>(&value)) {
64 if (std::holds_alternative<std::monostate>(value)) {
65 return makeError(
"read of uninitialized array value");
72 if (
const auto *podValue = std::get_if<PodValueRef>(&value)) {
75 if (std::holds_alternative<std::monostate>(value)) {
76 return makeError(
"read of uninitialized POD value");
83 if (
const auto *structValue = std::get_if<StructValueRef>(&value)) {
86 if (std::holds_alternative<std::monostate>(value)) {
87 return makeError(
"read of uninitialized struct value");
89 return makeError(
"expected struct value");
94 Type type, SymbolTableCollection &tables, Operation *origin,
const Field &field,
97 return llvm::TypeSwitch<Type, llvm::Expected<WitnessVal>>(type)
104 return makeError(
"missing RNG for random witgen initialization");
110 .Case([&](IndexType) -> llvm::Expected<WitnessVal> {
116 return makeError(
"missing RNG for random witgen initialization");
122 .Case([&](IntegerType intType) -> llvm::Expected<WitnessVal> {
123 if (intType.getWidth() == 1) {
129 return makeError(
"missing RNG for random witgen initialization");
135 return makeError(
"only i1 integer values are supported in llzk-witgen");
138 auto arrayValue = std::make_shared<ArrayValue>();
139 arrayValue->type = arrayType;
140 arrayValue->elements.reserve(arrayType.getNumElements());
141 for (int64_t i = 0; i < arrayType.getNumElements(); ++i) {
144 return elem.takeError();
146 arrayValue->elements.push_back(*elem);
150 .Case([&](
pod::PodType podType) -> llvm::Expected<WitnessVal> {
151 auto podValue = std::make_shared<PodValue>();
152 podValue->type = podType;
153 for (pod::RecordAttr record : podType.
getRecords()) {
154 auto recordValue =
defaultValue(record.getType(), tables, origin, field, behavior, rng);
156 return recordValue.takeError();
158 podValue->records[record.getName().getValue()] = *recordValue;
164 if (failed(defLookup)) {
165 return makeError(
"could not resolve struct type");
167 auto structValue = std::make_shared<StructValue>();
168 structValue->type = structType;
170 auto memberValue =
defaultValue(member.getType(), tables, origin, field, behavior, rng);
172 return memberValue.takeError();
174 structValue->members[member.getSymName()] = *memberValue;
177 }).Default([&](Type) -> llvm::Expected<WitnessVal> {
178 return makeError(
"unsupported type in llzk-witgen");
Information about the prime finite field used for the interval analysis.
llvm::DynamicAPInt zero() const
Returns 0 at the bitwidth of the field.
::mlir::Type getElementType() const
::mlir::FailureOr< SymbolLookupResult< StructDefOp > > getDefinition(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op, bool reportMissing=true) const
Gets the struct op that defines this struct.
::llvm::ArrayRef<::llzk::pod::RecordAttr > getRecords() const
llvm::Expected< PodValueRef > asPod(const WitnessVal &value)
Require a POD value from the runtime variant.
llvm::Expected< bool > asBool(const WitnessVal &value)
Require a boolean value from the runtime variant.
UninitializedBehavior
Control how witgen materializes uninitialized/default values.
llvm::Expected< WitnessVal > defaultValue(Type type, SymbolTableCollection &tables, Operation *origin, const Field &field, UninitializedBehavior behavior, std::mt19937_64 *rng)
Build a default value for a supported LLZK type.
llvm::DynamicAPInt randomFieldElement(std::mt19937_64 &rng, const Field &field)
Draw a uniformly distributed field element in [0, prime).
bool randomBoolValue(std::mt19937_64 &rng)
Draw a uniformly distributed boolean value.
llvm::Expected< int64_t > asIndex(const WitnessVal &value)
Require an index value from the runtime variant.
int64_t randomIndexValue(std::mt19937_64 &rng)
Draw a uniformly distributed signed index value.
std::variant< std::monostate, bool, int64_t, llvm::DynamicAPInt, ArrayValueRef, PodValueRef, StructValueRef > WitnessVal
Runtime value representation used by the tool-local interpreter.
llvm::Expected< llvm::DynamicAPInt > asFelt(const WitnessVal &value)
Require a felt value from the runtime variant.
llvm::Error makeError(const llvm::Twine &msg)
Build a string-backed error for user-facing witgen failures.
llvm::Expected< StructValueRef > asStruct(const WitnessVal &value)
Require a struct value from the runtime variant.
llvm::Expected< ArrayValueRef > asArray(const WitnessVal &value)
Require an array value from the runtime variant.