LLZK
3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Toggle main menu visibility
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
12
#include "
llzk/Util/StreamHelper.h
"
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
21
template
<>
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
34
namespace
llzk
{
35
37
class
APIntValue
{
38
public
:
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
54
private
:
55
llvm::APInt value;
56
};
57
58
inline
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
66
inline
llvm::APInt
toAPInt
(int64_t i) {
return
llvm::APInt(64, i); }
67
inline
int64_t
fromAPInt
(
const
llvm::APInt &i) {
return
i.getSExtValue(); }
68
69
inline
bool
isNullOrEmpty
(mlir::ArrayAttr a) {
return
!a || a.empty(); }
70
inline
bool
isNullOrEmpty
(mlir::DenseArrayAttr a) {
return
!a || a.empty(); }
71
inline
bool
isNullOrEmpty
(mlir::DictionaryAttr a) {
return
!a || a.empty(); }
72
73
inline
void
appendWithoutType
(mlir::raw_ostream &os, mlir::Attribute a) { a.print(os,
true
); }
74
inline
std::string
stringWithoutType
(mlir::Attribute a) {
75
return
buildStringViaCallback
(
appendWithoutType
, a);
76
}
77
78
void
printAttrs
(
79
mlir::AsmPrinter &printer, mlir::ArrayRef<mlir::Attribute> attrs,
80
const
mlir::StringRef &separator
81
);
82
83
}
// namespace llzk
StreamHelper.h
llzk::APIntValue
Storage key for APInts whose numeric value is independent of bit width.
Definition
AttributeHelper.h:37
llzk::APIntValue::getValue
const llvm::APInt & getValue() const
Definition
AttributeHelper.h:41
llzk::APIntValue::operator==
friend bool operator==(const APIntValue &lhs, const APIntValue &rhs)
Definition
AttributeHelper.h:44
llzk::APIntValue::APIntValue
APIntValue(llvm::APInt apInt)
Definition
AttributeHelper.h:39
llzk::APIntValue::hash_value
friend llvm::hash_code hash_value(const APIntValue &key)
Definition
AttributeHelper.h:48
llvm
Definition
SourceRefLattice.cpp:411
llzk
Definition
AnalysisPassEnums.cpp:19
llzk::printAttrs
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
Definition
AttributeHelper.cpp:19
llzk::stringWithoutType
std::string stringWithoutType(mlir::Attribute a)
Definition
AttributeHelper.h:74
llzk::isNullOrEmpty
bool isNullOrEmpty(mlir::ArrayAttr a)
Definition
AttributeHelper.h:69
llzk::toAPInt
APInt toAPInt(const DynamicAPInt &val, unsigned bitWidth)
Definition
DynamicAPIntHelper.cpp:143
llzk::parseAPIntValue
mlir::FailureOr< APIntValue > parseAPIntValue(mlir::AsmParser &parser)
Definition
AttributeHelper.h:58
llzk::fromAPInt
int64_t fromAPInt(const llvm::APInt &i)
Definition
AttributeHelper.h:67
llzk::appendWithoutType
void appendWithoutType(mlir::raw_ostream &os, mlir::Attribute a)
Definition
AttributeHelper.h:73
llzk::buildStringViaCallback
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...
Definition
StreamHelper.h:42
mlir::FieldParser< llvm::APInt >::parse
static mlir::FailureOr< llvm::APInt > parse(mlir::AsmParser &parser)
Definition
AttributeHelper.h:22
mlir::FieldParser
Definition
AnalysisPassEnums.h.inc:41
include
llzk
Dialect
LLZK
IR
AttributeHelper.h
Generated by
1.17.0
Copyright 2025 Veridise Inc. under the Apache License v2.0. Copyright 2026 Project LLZK under the Apache License v2.0.