LLZK 0.1.0
Veridise's ZK Language IR
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
16namespace llzk {
17
18template <typename Op>
19concept OpComparable = requires(Op op) {
20 { op.getOperation() } -> std::convertible_to<mlir::Operation *>;
21};
22
23template <typename Op>
24concept NamedOpComparable = OpComparable<Op> && requires(Op op) {
25 { op.getName() } -> std::convertible_to<mlir::StringRef>;
26};
27
29 bool operator()(const mlir::FileLineColLoc &LHS, const mlir::FileLineColLoc &RHS) const {
30 auto filenameCmp = LHS.getFilename().compare(RHS.getFilename());
31 return filenameCmp < 0 || (filenameCmp == 0 && LHS.getLine() < RHS.getLine()) ||
32 (filenameCmp == 0 && LHS.getLine() == RHS.getLine() && LHS.getColumn() < RHS.getColumn()
33 );
34 }
35};
36
38 bool operator()(const mlir::Location &LHS, const mlir::Location &RHS) const {
39 auto lhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(LHS);
40 auto rhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(RHS);
41 if (lhsFileLoc && rhsFileLoc) {
42 return FileLineColLocComparator {}(lhsFileLoc, rhsFileLoc);
43 }
44 return mlir::hash_value(LHS) < mlir::hash_value(RHS);
45 }
46};
47
48template <OpComparable Op> mlir::FailureOr<bool> isLocationLess(const Op &l, const Op &r) {
49 mlir::Location lhsLoc = l->getLoc(), rhsLoc = r->getLoc();
50 // We cannot make judgments on unknown locations.
51 if (llvm::isa<mlir::UnknownLoc>(lhsLoc) || llvm::isa<mlir::UnknownLoc>(rhsLoc)) {
52 return mlir::failure();
53 }
54 // If we have full locations for both, then we can sort by file name, then line, then column.
55 auto lhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(lhsLoc);
56 auto rhsFileLoc = llvm::dyn_cast<mlir::FileLineColLoc>(rhsLoc);
57 if (lhsFileLoc && rhsFileLoc) {
58 return FileLineColLocComparator {}(lhsFileLoc, rhsFileLoc);
59 }
60 return mlir::failure();
61}
62
63template <OpComparable Op> struct OpLocationLess {
64 bool operator()(const Op &l, const Op &r) const { return isLocationLess(l, r).value_or(false); }
65};
66
67template <NamedOpComparable Op> struct NamedOpLocationLess {
68 bool operator()(const Op &l, const Op &r) const {
69 auto res = isLocationLess(l, r);
70 if (mlir::succeeded(res)) {
71 return res.value();
72 }
73
74 Op &lhs = const_cast<Op &>(l);
75 Op &rhs = const_cast<Op &>(r);
76 return lhs.getName().compare(rhs.getName()) < 0;
77 }
78};
79
80} // namespace llzk
mlir::FailureOr< bool > isLocationLess(const Op &l, const Op &r)
Definition Compare.h:48
bool operator()(const mlir::FileLineColLoc &LHS, const mlir::FileLineColLoc &RHS) const
Definition Compare.h:29
bool operator()(const mlir::Location &LHS, const mlir::Location &RHS) const
Definition Compare.h:38
bool operator()(const Op &l, const Op &r) const
Definition Compare.h:68
bool operator()(const Op &l, const Op &r) const
Definition Compare.h:64