LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
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
17namespace llzk {
18
19template <typename Op>
20concept OpComparable = requires(Op op) {
21 { op.getOperation() } -> std::convertible_to<mlir::Operation *>;
22};
23
24template <typename Op>
25concept NamedOpComparable = OpComparable<Op> && requires(Op op) {
26 { op.getName() } -> std::convertible_to<mlir::StringRef>;
27};
28
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
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
49template <OpComparable Op> mlir::FailureOr<bool> isLocationLess(const Op &l, const Op &r) {
50 mlir::Location lhsLoc = l->getLoc(), rhsLoc = r->getLoc();
51 // We cannot make judgments on unknown locations.
52 if (llvm::isa<mlir::UnknownLoc>(lhsLoc) || llvm::isa<mlir::UnknownLoc>(rhsLoc)) {
53 return mlir::failure();
54 }
55 // If we have full locations for both, then we can sort by file name, then line, then column.
56 auto lhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(lhsLoc);
57 auto rhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(rhsLoc);
58 if (lhsFileLoc && rhsFileLoc) {
59 return FileLineColLocComparator {}(lhsFileLoc, rhsFileLoc);
60 }
61 return mlir::failure();
62}
63
64template <OpComparable Op> struct OpLocationLess {
65 bool operator()(const Op &l, const Op &r) const { return isLocationLess(l, r).value_or(false); }
66};
67
68template <NamedOpComparable Op> struct NamedOpLocationLess {
69 bool operator()(const Op &l, const Op &r) const {
70 auto res = isLocationLess(l, r);
71 if (mlir::succeeded(res)) {
72 return res.value();
73 }
74
75 Op &lhs = const_cast<Op &>(l);
76 Op &rhs = const_cast<Op &>(r);
77 return lhs.getName().compare(rhs.getName()) < 0;
78 }
79};
80
81template <typename T, typename U> constexpr T checkedCast(U u) noexcept {
82 assert(std::in_range<T>(u) && "lossy conversion");
83 return static_cast<T>(u);
84}
85
86} // namespace llzk
constexpr T checkedCast(U u) noexcept
Definition Compare.h:81
mlir::FailureOr< bool > isLocationLess(const Op &l, const Op &r)
Definition Compare.h:49
bool operator()(const mlir::FileLineColLoc &LHS, const mlir::FileLineColLoc &RHS) const
Definition Compare.h:30
bool operator()(const mlir::Location &LHS, const mlir::Location &RHS) const
Definition Compare.h:39
bool operator()(const Op &l, const Op &r) const
Definition Compare.h:69
bool operator()(const Op &l, const Op &r) const
Definition Compare.h:65