15#include <mlir/IR/BuiltinOps.h>
16#include <mlir/Support/LLVM.h>
18#include <llvm/ADT/DenseMap.h>
19#include <llvm/ADT/DynamicAPInt.h>
20#include <llvm/ADT/SmallSet.h>
21#include <llvm/Support/LogicalResult.h>
22#include <llvm/Support/SMTAPI.h>
51 static llvm::FailureOr<std::reference_wrapper<const Field>>
71 llvm::DynamicAPInt
prime()
const {
return primeMod; }
74 llvm::DynamicAPInt
half()
const {
return halfPrime; }
77 inline llvm::DynamicAPInt
felt(
int i)
const {
return reduce(i); }
80 inline llvm::DynamicAPInt
zero()
const {
return felt(0); }
83 inline llvm::DynamicAPInt
one()
const {
return felt(1); }
89 llvm::DynamicAPInt
inv(
const llvm::DynamicAPInt &i)
const;
91 llvm::DynamicAPInt
inv(
const llvm::APInt &i)
const;
96 llvm::DynamicAPInt
reduce(
const llvm::DynamicAPInt &i)
const;
97 inline llvm::DynamicAPInt
reduce(
int i)
const {
return reduce(llvm::DynamicAPInt(i)); }
98 llvm::DynamicAPInt
reduce(
const llvm::APInt &i)
const;
104 llvm::DynamicAPInt
toSigned(
const llvm::DynamicAPInt &i)
const;
106 inline unsigned bitWidth()
const {
return bitwidth; }
108 inline llvm::StringRef
name()
const {
return primeName; }
112 return solver->mkSymbol(
name, solver->getBitvectorSort(
bitWidth()));
116 return lhs.primeMod == rhs.primeMod;
120 return std::tie(lhs.primeMod, lhs.primeName, lhs.bitwidth, lhs.halfPrime) <
121 std::tie(rhs.primeMod, rhs.primeName, rhs.bitwidth, rhs.halfPrime);
125 Field(std::string_view primeStr, llvm::StringRef
name);
126 Field(
const llvm::APInt &primeInt, llvm::StringRef
name);
129 llvm::StringRef primeName;
130 llvm::DynamicAPInt primeMod, halfPrime;
134 static void initKnownFields();
152using FieldRef = std::reference_wrapper<const Field>;
This file implements helper methods for constructing DynamicAPInts.
Information about the prime finite field used for the interval analysis.
static void addField(llvm::StringRef fieldName, llvm::StringRef primeStr, EmitErrorFn errFn)
llvm::DynamicAPInt half() const
Returns p / 2.
static llvm::FailureOr< std::reference_wrapper< const Field > > tryGetField(llvm::StringRef fieldName)
Get a Field from a given field name string, or failure if the field is not defined.
friend bool operator==(const Field &lhs, const Field &rhs)
Field(const Field &)=default
llvm::DynamicAPInt zero() const
Returns 0 at the bitwidth of the field.
llvm::DynamicAPInt toSigned(const llvm::DynamicAPInt &i) const
Converts a canonical field element to its signed integer representation: toSigned(f) = f if f < field...
llvm::DynamicAPInt prime() const
For the prime field p, returns p.
llvm::DynamicAPInt reduce(const llvm::DynamicAPInt &i) const
Returns i mod p and reduces the result into the appropriate bitwidth.
llvm::StringRef name() const
llvm::DynamicAPInt one() const
Returns 1 at the bitwidth of the field.
llvm::DynamicAPInt reduce(int i) const
llvm::DynamicAPInt felt(int i) const
Returns i as a signed field element.
llvm::DynamicAPInt reduce(const llvm::APInt &i) const
llvm::DynamicAPInt inv(const llvm::DynamicAPInt &i) const
Returns the multiplicative inverse of i in prime field p.
unsigned bitWidth() const
friend bool operator<(const Field &lhs, const Field &rhs)
llvm::SMTExprRef createSymbol(const llvm::SMTSolverRef &solver, const char *name) const
Create a SMT solver symbol with the current field's bitwidth.
static const Field & getField(llvm::StringRef fieldName)
llvm::DynamicAPInt inv(const llvm::APInt &i) const
static llvm::LogicalResult verifyFieldDefined(llvm::StringRef fieldName, EmitErrorFn errFn)
Search for a field with the given name, reporting an error if the field is not found.
static const Field & getField(llvm::StringRef fieldName, EmitErrorFn errFn)
Get a Field from a given field name string.
Field(Field &&) noexcept=default
static void addField(llvm::StringRef fieldName, const llvm::APInt &prime, EmitErrorFn errFn)
Add a new field to the set of available prime fields.
llvm::DynamicAPInt maxVal() const
Returns p - 1, which is the max value possible in a prime field described by p.
std::reference_wrapper< const Field > FieldRef
Typealias for a stable reference to a known Field.
llvm::function_ref< InFlightDiagnosticWrapper()> EmitErrorFn
Callback to produce an error diagnostic.
LogicalResult addSpecifiedFields(ModuleOp modOp)
llvm::SmallSet< FieldRef, 2 > FieldSet
Typealias for a set of Fields.
std::optional< std::reference_wrapper< const Field > > tryDetectSpecifiedField(mlir::Operation *root)
Try to detect a uniquely used field from the enclosing LLZK module.
mlir::LogicalResult collectFields(mlir::Operation *root, FieldSet &fields, bool silent=true)
Collects all the fields used in a circuit.