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
14
15#include <mlir/IR/DialectImplementation.h>
16
17#include <llvm/ADT/APInt.h>
18#include <llvm/ADT/Hashing.h>
19
20#include <utility>
21
22template <> struct mlir::FieldParser<llvm::APInt> {
23 static mlir::FailureOr<llvm::APInt> parse(mlir::AsmParser &parser) {
24 auto loc = parser.getCurrentLocation();
25 llvm::APInt val;
26 auto result = parser.parseOptionalInteger(val);
27 if (!result.has_value() || *result) {
28 return parser.emitError(loc, "expected integer value");
29 } else {
30 return val;
31 }
32 }
33};
34
35namespace llzk {
36
44public:
45 DynamicAPIntValue(const llvm::DynamicAPInt &integer) : value(integer) {}
46
53 operator const llvm::DynamicAPInt &() const { return value; }
54
55 friend bool operator==(const DynamicAPIntValue &a, const DynamicAPIntValue &b) {
56 return a.value == b.value;
57 }
58
59 friend llvm::hash_code hash_value(const DynamicAPIntValue &key) {
60 return hashDynamicAPInt(key.value);
61 }
62
63private:
64 llvm::DynamicAPInt value;
65};
66
68inline mlir::FailureOr<DynamicAPIntValue> parseDynamicAPIntValue(mlir::AsmParser &parser) {
69 auto value = mlir::FieldParser<llvm::APInt>::parse(parser);
70 if (mlir::failed(value)) {
71 return mlir::failure();
72 }
73 return DynamicAPIntValue(llvm::DynamicAPInt(*value));
74}
75
78inline llvm::DynamicAPInt integerAttrToDynamicAPInt(mlir::IntegerAttr attr) {
79 auto type = llvm::dyn_cast<mlir::IntegerType>(attr.getType());
80 bool isUnsigned = type && (type.isUnsigned() || (type.isSignless() && type.getWidth() == 1));
81 return isUnsigned ? toDynamicAPInt(attr.getValue()) : llvm::DynamicAPInt(attr.getValue());
82}
83
85inline int64_t fromAPInt(const llvm::APInt &i) {
86 if (!i.isSignedIntN(64)) {
87 llvm::report_fatal_error("integer is not representable as an index");
88 }
89 return i.getSExtValue();
90}
91
92inline bool isNullOrEmpty(mlir::ArrayAttr a) { return !a || a.empty(); }
93inline bool isNullOrEmpty(mlir::DenseArrayAttr a) { return !a || a.empty(); }
94inline bool isNullOrEmpty(mlir::DictionaryAttr a) { return !a || a.empty(); }
95
96inline void appendWithoutType(mlir::raw_ostream &os, mlir::Attribute a) { a.print(os, true); }
97inline std::string stringWithoutType(mlir::Attribute a) {
99}
100
102 mlir::AsmPrinter &printer, mlir::ArrayRef<mlir::Attribute> attrs,
103 const mlir::StringRef &separator
104);
105
106} // namespace llzk
This file implements helpers for DynamicAPInt operations and conversions that LLVM does not provide.
Attribute storage adapter providing numeric hashing for DynamicAPInt.
DynamicAPIntValue(const llvm::DynamicAPInt &integer)
friend llvm::hash_code hash_value(const DynamicAPIntValue &key)
friend bool operator==(const DynamicAPIntValue &a, const DynamicAPIntValue &b)
mlir::FailureOr< DynamicAPIntValue > parseDynamicAPIntValue(mlir::AsmParser &parser)
Parse MLIR's signed integer literal and discard its encoding width.
hash_code hashDynamicAPInt(const DynamicAPInt &value)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
std::string stringWithoutType(mlir::Attribute a)
DynamicAPInt toDynamicAPInt(StringRef str)
bool isNullOrEmpty(mlir::ArrayAttr a)
int64_t fromAPInt(const llvm::APInt &i)
Extract a verified MLIR index value; malformed internal IR is a fatal error.
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...
llvm::DynamicAPInt integerAttrToDynamicAPInt(mlir::IntegerAttr attr)
Decode an MLIR integer using its signedness; signless i1 denotes a boolean.
static mlir::FailureOr< llvm::APInt > parse(mlir::AsmParser &parser)