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/DynamicAPIntHelper.h
"
13
#include "
llzk/Util/StreamHelper.h
"
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
22
template
<>
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
35
namespace
llzk
{
36
43
class
DynamicAPIntValue
{
44
public
:
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
63
private
:
64
llvm::DynamicAPInt value;
65
};
66
68
inline
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
78
inline
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
85
inline
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
92
inline
bool
isNullOrEmpty
(mlir::ArrayAttr a) {
return
!a || a.empty(); }
93
inline
bool
isNullOrEmpty
(mlir::DenseArrayAttr a) {
return
!a || a.empty(); }
94
inline
bool
isNullOrEmpty
(mlir::DictionaryAttr a) {
return
!a || a.empty(); }
95
96
inline
void
appendWithoutType
(mlir::raw_ostream &os, mlir::Attribute a) { a.print(os,
true
); }
97
inline
std::string
stringWithoutType
(mlir::Attribute a) {
98
return
buildStringViaCallback
(
appendWithoutType
, a);
99
}
100
101
void
printAttrs
(
102
mlir::AsmPrinter &printer, mlir::ArrayRef<mlir::Attribute> attrs,
103
const
mlir::StringRef &separator
104
);
105
106
}
// namespace llzk
DynamicAPIntHelper.h
This file implements helpers for DynamicAPInt operations and conversions that LLVM does not provide.
StreamHelper.h
llzk::DynamicAPIntValue
Attribute storage adapter providing numeric hashing for DynamicAPInt.
Definition
AttributeHelper.h:43
llzk::DynamicAPIntValue::DynamicAPIntValue
DynamicAPIntValue(const llvm::DynamicAPInt &integer)
Definition
AttributeHelper.h:45
llzk::DynamicAPIntValue::hash_value
friend llvm::hash_code hash_value(const DynamicAPIntValue &key)
Definition
AttributeHelper.h:59
llzk::DynamicAPIntValue::operator==
friend bool operator==(const DynamicAPIntValue &a, const DynamicAPIntValue &b)
Definition
AttributeHelper.h:55
llvm
Definition
SourceRefLattice.cpp:411
llzk
Definition
AnalysisPassEnums.cpp:19
llzk::parseDynamicAPIntValue
mlir::FailureOr< DynamicAPIntValue > parseDynamicAPIntValue(mlir::AsmParser &parser)
Parse MLIR's signed integer literal and discard its encoding width.
Definition
AttributeHelper.h:68
llzk::hashDynamicAPInt
hash_code hashDynamicAPInt(const DynamicAPInt &value)
Definition
DynamicAPIntHelper.cpp:59
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:97
llzk::toDynamicAPInt
DynamicAPInt toDynamicAPInt(StringRef str)
Definition
DynamicAPIntHelper.cpp:166
llzk::isNullOrEmpty
bool isNullOrEmpty(mlir::ArrayAttr a)
Definition
AttributeHelper.h:92
llzk::fromAPInt
int64_t fromAPInt(const llvm::APInt &i)
Extract a verified MLIR index value; malformed internal IR is a fatal error.
Definition
AttributeHelper.h:85
llzk::appendWithoutType
void appendWithoutType(mlir::raw_ostream &os, mlir::Attribute a)
Definition
AttributeHelper.h:96
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
llzk::integerAttrToDynamicAPInt
llvm::DynamicAPInt integerAttrToDynamicAPInt(mlir::IntegerAttr attr)
Decode an MLIR integer using its signedness; signless i1 denotes a boolean.
Definition
AttributeHelper.h:78
mlir::FieldParser< llvm::APInt >::parse
static mlir::FailureOr< llvm::APInt > parse(mlir::AsmParser &parser)
Definition
AttributeHelper.h:23
mlir::FieldParser
Definition
AnalysisPassEnums.h.inc:43
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.