[Mlir-commits] [mlir] [MLIR][Presburger] Make printing aligned to assist in debugging (PR #107648)
Amy Wang
llvmlistbot at llvm.org
Wed Sep 11 19:00:56 PDT 2024
================
@@ -292,6 +294,53 @@ std::vector<Fraction> multiplyPolynomials(ArrayRef<Fraction> a,
bool isRangeZero(ArrayRef<Fraction> arr);
+/// An example to print .12, 3.4, 56.7 where preAlign = ".", minSpacing = 1,
+/// and (`,~) indicates (PreIndent, PostIndent) respectively:
+/// ```.12```.12
+/// ``3.4~``3.4~
+/// `56.7~`56.7~
+struct PrintTableMetrics {
+ // If unknown, set to 0 and pass the struct into updatePrintMetrics.
+ unsigned maxPreIndent;
+ unsigned maxPostIndent;
+ std::string preAlign;
+};
+
+/// Iterate over each val in the table and update 'm' where
+/// .maxPreIndent and .maxPostIndent are initialized to 0.
+/// class T is any type that can be handled by llvm::raw_string_ostream.
+template <class T>
+void updatePrintMetrics(T val, PrintTableMetrics &m) {
+ std::string str;
+ llvm::raw_string_ostream(str) << val;
+ if (str.empty())
+ return;
+ unsigned preIndent = str.find(m.preAlign);
+ preIndent = (preIndent != std::string::npos) ? preIndent + 1 : 0;
+ m.maxPreIndent = std::max(m.maxPreIndent, preIndent);
+ m.maxPostIndent =
+ std::max(m.maxPostIndent, (unsigned int)(str.length() - preIndent));
+}
+
+/// Print val in the table with metrics specified in 'm'.
+template <class T>
+void printWithPrintMetrics(raw_ostream &os, T val, unsigned minSpacing,
+ const PrintTableMetrics &m) {
+ std::string str;
+ llvm::raw_string_ostream(str) << val;
+ unsigned preIndent;
+ if (str.length() != 0) {
----------------
kaitingwang wrote:
Fixed.
https://github.com/llvm/llvm-project/pull/107648
More information about the Mlir-commits
mailing list