LLZK
3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Toggle main menu visibility
Loading...
Searching...
No Matches
Compare.h
Go to the documentation of this file.
1
//===-- Compare.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 <mlir/IR/Operation.h>
13
14
#include <concepts>
15
#include <utility>
16
17
namespace
llzk
{
18
19
template
<
typename
Op>
20
concept
OpComparable
=
requires
(Op op) {
21
{ op.getOperation() } -> std::convertible_to<mlir::Operation *>;
22
};
23
24
template
<
typename
Op>
25
concept
NamedOpComparable
=
OpComparable<Op>
&&
requires
(Op op) {
26
{ op.getName() } -> std::convertible_to<mlir::StringRef>;
27
};
28
29
struct
FileLineColLocComparator
{
30
bool
operator()
(
const
mlir::FileLineColLoc &
LHS
,
const
mlir::FileLineColLoc &
RHS
)
const
{
31
auto
filenameCmp =
LHS
.getFilename().compare(
RHS
.getFilename());
32
return
filenameCmp < 0 || (filenameCmp == 0 &&
LHS
.getLine() <
RHS
.getLine()) ||
33
(filenameCmp == 0 &&
LHS
.getLine() ==
RHS
.getLine() &&
34
LHS
.getColumn() <
RHS
.getColumn());
35
}
36
};
37
38
struct
LocationComparator
{
39
bool
operator()
(
const
mlir::Location &
LHS
,
const
mlir::Location &
RHS
)
const
{
40
auto
lhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(
LHS
);
41
auto
rhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(
RHS
);
42
if
(lhsFileLoc && rhsFileLoc) {
43
return
FileLineColLocComparator
{}(lhsFileLoc, rhsFileLoc);
44
}
45
return
mlir::hash_value(
LHS
) < mlir::hash_value(
RHS
);
46
}
47
};
48
54
template
<OpComparable Op> mlir::FailureOr<bool>
isLocationLess
(
const
Op &l,
const
Op &r) {
55
mlir::Location lhsLoc = l->getLoc(), rhsLoc = r->getLoc();
56
// We cannot make judgments on unknown locations.
57
if
(llvm::isa<mlir::UnknownLoc>(lhsLoc) || llvm::isa<mlir::UnknownLoc>(rhsLoc)) {
58
return
mlir::failure();
59
}
60
// If we have full locations for both, then we can sort by file name, then line, then column.
61
auto
lhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(lhsLoc);
62
auto
rhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(rhsLoc);
63
if
(lhsFileLoc && rhsFileLoc) {
64
return
FileLineColLocComparator
{}(lhsFileLoc, rhsFileLoc);
65
}
66
return
mlir::failure();
67
}
68
69
template
<OpComparable Op>
struct
OpLocationLess
{
70
bool
operator()
(
const
Op &l,
const
Op &r)
const
{
return
isLocationLess
(l, r).value_or(
false
); }
71
};
72
75
template
<NamedOpComparable Op>
struct
NamedOpLocationLess
{
76
bool
operator()
(
const
Op &l,
const
Op &r)
const
{
77
auto
res =
isLocationLess
(l, r);
78
auto
reverseRes =
isLocationLess
(r, l);
79
// A successful false result may mean either equal locations or that the operands are reversed.
80
// Check both directions before using the symbol name as the deterministic tie-breaker.
81
if
(mlir::succeeded(res) && res.value()) {
82
return
true
;
83
}
84
if
(mlir::succeeded(reverseRes) && reverseRes.value()) {
85
return
false
;
86
}
87
88
Op &lhs =
const_cast<
Op &
>
(l);
89
Op &rhs =
const_cast<
Op &
>
(r);
90
return
lhs.getName().compare(rhs.getName()) < 0;
91
}
92
};
93
94
template
<
typename
T,
typename
U>
constexpr
T
checkedCast
(U u)
noexcept
{
95
assert(std::in_range<T>(u) &&
"lossy conversion"
);
96
return
static_cast<
T
>
(u);
97
}
98
99
}
// namespace llzk
llzk::NamedOpComparable
Definition
Compare.h:25
llzk::OpComparable
Definition
Compare.h:20
llzk
Definition
AnalysisPassEnums.cpp:19
llzk::checkedCast
constexpr T checkedCast(U u) noexcept
Definition
Compare.h:94
llzk::isLocationLess
mlir::FailureOr< bool > isLocationLess(const Op &l, const Op &r)
Compare the source locations of two operations.
Definition
Compare.h:54
llzk::Side::LHS
@ LHS
Definition
TypeHelper.h:148
llzk::Side::RHS
@ RHS
Definition
TypeHelper.h:148
llzk::FileLineColLocComparator
Definition
Compare.h:29
llzk::FileLineColLocComparator::operator()
bool operator()(const mlir::FileLineColLoc &LHS, const mlir::FileLineColLoc &RHS) const
Definition
Compare.h:30
llzk::LocationComparator
Definition
Compare.h:38
llzk::LocationComparator::operator()
bool operator()(const mlir::Location &LHS, const mlir::Location &RHS) const
Definition
Compare.h:39
llzk::NamedOpLocationLess
Order named operations by source location, using the symbol name to break ties or when source locatio...
Definition
Compare.h:75
llzk::NamedOpLocationLess::operator()
bool operator()(const Op &l, const Op &r) const
Definition
Compare.h:76
llzk::OpLocationLess
Definition
Compare.h:69
llzk::OpLocationLess::operator()
bool operator()(const Op &l, const Op &r) const
Definition
Compare.h:70
include
llzk
Util
Compare.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.