LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
AttributeHelper.h
Go to the documentation of this file.
1//===-- AttributeHelper.h ---------------------------------------*- 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#pragma once
11
13
14#include <mlir/IR/DialectImplementation.h>
15
16#include <llvm/ADT/APInt.h>
17#include <llvm/ADT/Hashing.h>
18
19#include <utility>
20
21template <> struct mlir::FieldParser<llvm::APInt> {
22 static mlir::FailureOr<llvm::APInt> parse(mlir::AsmParser &parser) {
23 auto loc = parser.getCurrentLocation();
24 llvm::APInt val;
25 auto result = parser.parseOptionalInteger(val);
26 if (!result.has_value() || *result) {
27 return parser.emitError(loc, "expected integer value");
28 } else {
29 return val;
30 }
31 }
32};
33
34namespace llzk {
35
38public:
39 APIntValue(llvm::APInt apInt) : value(std::move(apInt)) {}
40
41 const llvm::APInt &getValue() const { return value; }
42 operator const llvm::APInt &() const { return value; }
43
44 friend bool operator==(const APIntValue &lhs, const APIntValue &rhs) {
45 return llvm::APInt::isSameValue(lhs.value, rhs.value);
46 }
47
48 friend llvm::hash_code hash_value(const APIntValue &key) {
49 unsigned activeBits = key.value.getActiveBits();
50 llvm::APInt canonical = key.value.trunc(activeBits == 0 ? 1 : activeBits);
51 return llvm::hash_value(canonical);
52 }
53
54private:
55 llvm::APInt value;
56};
57
58inline mlir::FailureOr<APIntValue> parseAPIntValue(mlir::AsmParser &parser) {
59 mlir::FailureOr<llvm::APInt> value = mlir::FieldParser<llvm::APInt>::parse(parser);
60 if (mlir::failed(value)) {
61 return mlir::failure();
62 }
63 return APIntValue(std::move(*value));
64}
65
66inline llvm::APInt toAPInt(int64_t i) { return llvm::APInt(64, i); }
67inline int64_t fromAPInt(const llvm::APInt &i) { return i.getSExtValue(); }
68
69inline bool isNullOrEmpty(mlir::ArrayAttr a) { return !a || a.empty(); }
70inline bool isNullOrEmpty(mlir::DenseArrayAttr a) { return !a || a.empty(); }
71inline bool isNullOrEmpty(mlir::DictionaryAttr a) { return !a || a.empty(); }
72
73inline void appendWithoutType(mlir::raw_ostream &os, mlir::Attribute a) { a.print(os, true); }
74inline std::string stringWithoutType(mlir::Attribute a) {
76}
77
79 mlir::AsmPrinter &printer, mlir::ArrayRef<mlir::Attribute> attrs,
80 const mlir::StringRef &separator
81);
82
83} // namespace llzk
Storage key for APInts whose numeric value is independent of bit width.
const llvm::APInt & getValue() const
friend bool operator==(const APIntValue &lhs, const APIntValue &rhs)
APIntValue(llvm::APInt apInt)
friend llvm::hash_code hash_value(const APIntValue &key)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
std::string stringWithoutType(mlir::Attribute a)
bool isNullOrEmpty(mlir::ArrayAttr a)
APInt toAPInt(const DynamicAPInt &val, unsigned bitWidth)
mlir::FailureOr< APIntValue > parseAPIntValue(mlir::AsmParser &parser)
int64_t fromAPInt(const llvm::APInt &i)
void appendWithoutType(mlir::raw_ostream &os, mlir::Attribute a)
std::string buildStringViaCallback(Func &&appendFn, Args &&...args)
Generate a string by calling the given appendFn with an llvm::raw_ostream & as the first argument fol...
static mlir::FailureOr< llvm::APInt > parse(mlir::AsmParser &parser)