[Mlir-commits] [llvm] [mlir] [mlir][sparse] remove very thin header file from sparse runtime support (PR #82820)

Aart Bik llvmlistbot at llvm.org
Fri Feb 23 11:41:05 PST 2024


https://github.com/aartbik created https://github.com/llvm/llvm-project/pull/82820

None

>From d2274d72b693040526ab0b14dfa6022349e616d5 Mon Sep 17 00:00:00 2001
From: Aart Bik <ajcbik at google.com>
Date: Fri, 23 Feb 2024 11:00:22 -0800
Subject: [PATCH] [mlir][sparse] remove very thin header file from sparse
 runtime support

---
 .../SparseTensor/ErrorHandling.h              | 34 --------
 .../mlir/ExecutionEngine/SparseTensor/File.h  | 10 ++-
 .../ExecutionEngine/SparseTensor/Storage.h    |  1 -
 .../lib/ExecutionEngine/SparseTensor/File.cpp | 84 ++++++++++++-------
 .../ExecutionEngine/SparseTensor/Storage.cpp  |  3 +-
 .../ExecutionEngine/SparseTensorRuntime.cpp   | 19 +++--
 .../llvm-project-overlay/mlir/BUILD.bazel     |  1 -
 7 files changed, 70 insertions(+), 82 deletions(-)
 delete mode 100644 mlir/include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h

diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h b/mlir/include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h
deleted file mode 100644
index 6b39526211c77d..00000000000000
--- a/mlir/include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h
+++ /dev/null
@@ -1,34 +0,0 @@
-//===- ErrorHandling.h - Helpers for errors ---------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file defines an extremely lightweight API for fatal errors (not
-// arising from assertions).  The API does not attempt to be sophisticated
-// in any way, it's just the usual "I give up" style of error reporting.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_ERRORHANDLING_H
-#define MLIR_EXECUTIONENGINE_SPARSETENSOR_ERRORHANDLING_H
-
-#include <cstdio>
-#include <cstdlib>
-
-/// This macro helps minimize repetition of the printf-and-exit idiom,
-/// as well as ensuring that we print some additional output indicating
-/// where the error is coming from--- to make it easier to determine
-/// whether some particular error is coming from the runtime library's
-/// code vs from somewhere else in the MLIR stack.  (Since that can be
-/// hard to determine without the stacktraces provided by assertion failures.)
-#define MLIR_SPARSETENSOR_FATAL(...)                                           \
-  do {                                                                         \
-    fprintf(stderr, "SparseTensorUtils: " __VA_ARGS__);                        \
-    fprintf(stderr, "SparseTensorUtils: at %s:%d\n", __FILE__, __LINE__);      \
-    exit(1);                                                                   \
-  } while (0)
-
-#endif // MLIR_EXECUTIONENGINE_SPARSETENSOR_ERRORHANDLING_H
diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/File.h b/mlir/include/mlir/ExecutionEngine/SparseTensor/File.h
index ccdc605d756433..f927b82628b1a6 100644
--- a/mlir/include/mlir/ExecutionEngine/SparseTensor/File.h
+++ b/mlir/include/mlir/ExecutionEngine/SparseTensor/File.h
@@ -115,10 +115,12 @@ class SparseTensorReader final {
     SparseTensorReader *reader = new SparseTensorReader(filename);
     reader->openFile();
     reader->readHeader();
-    if (!reader->canReadAs(valTp))
-      MLIR_SPARSETENSOR_FATAL(
-          "Tensor element type %d not compatible with values in file %s\n",
-          static_cast<int>(valTp), filename);
+    if (!reader->canReadAs(valTp)) {
+      fprintf(stderr,
+              "Tensor element type %d not compatible with values in file %s\n",
+              static_cast<int>(valTp), filename);
+      exit(1);
+    }
     reader->assertMatchesShape(dimRank, dimShape);
     return reader;
   }
diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h b/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h
index fe0e08b5c84036..468782ebef4e34 100644
--- a/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h
+++ b/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h
@@ -20,7 +20,6 @@
 #include "mlir/ExecutionEngine/Float16bits.h"
 #include "mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h"
 #include "mlir/ExecutionEngine/SparseTensor/COO.h"
-#include "mlir/ExecutionEngine/SparseTensor/ErrorHandling.h"
 #include "mlir/ExecutionEngine/SparseTensor/MapRef.h"
 
 namespace mlir {
diff --git a/mlir/lib/ExecutionEngine/SparseTensor/File.cpp b/mlir/lib/ExecutionEngine/SparseTensor/File.cpp
index c49ec0998fb444..63b29ba34ba02c 100644
--- a/mlir/lib/ExecutionEngine/SparseTensor/File.cpp
+++ b/mlir/lib/ExecutionEngine/SparseTensor/File.cpp
@@ -19,11 +19,15 @@ using namespace mlir::sparse_tensor;
 
 /// Opens the file for reading.
 void SparseTensorReader::openFile() {
-  if (file)
-    MLIR_SPARSETENSOR_FATAL("Already opened file %s\n", filename);
+  if (file) {
+    fprintf(stderr, "Already opened file %s\n", filename);
+    exit(1);
+  }
   file = fopen(filename, "r");
-  if (!file)
-    MLIR_SPARSETENSOR_FATAL("Cannot find file %s\n", filename);
+  if (!file) {
+    fprintf(stderr, "Cannot find file %s\n", filename);
+    exit(1);
+  }
 }
 
 /// Closes the file.
@@ -36,19 +40,23 @@ void SparseTensorReader::closeFile() {
 
 /// Attempts to read a line from the file.
 void SparseTensorReader::readLine() {
-  if (!fgets(line, kColWidth, file))
-    MLIR_SPARSETENSOR_FATAL("Cannot read next line of %s\n", filename);
+  if (!fgets(line, kColWidth, file)) {
+    fprintf(stderr, "Cannot read next line of %s\n", filename);
+    exit(1);
+  }
 }
 
 /// Reads and parses the file's header.
 void SparseTensorReader::readHeader() {
   assert(file && "Attempt to readHeader() before openFile()");
-  if (strstr(filename, ".mtx"))
+  if (strstr(filename, ".mtx")) {
     readMMEHeader();
-  else if (strstr(filename, ".tns"))
+  } else if (strstr(filename, ".tns")) {
     readExtFROSTTHeader();
-  else
-    MLIR_SPARSETENSOR_FATAL("Unknown format %s\n", filename);
+  } else {
+    fprintf(stderr, "Unknown format %s\n", filename);
+    exit(1);
+  }
   assert(isValid() && "Failed to read the header");
 }
 
@@ -57,7 +65,7 @@ void SparseTensorReader::readHeader() {
 void SparseTensorReader::assertMatchesShape(uint64_t rank,
                                             const uint64_t *shape) const {
   assert(rank == getRank() && "Rank mismatch");
-  for (uint64_t r = 0; r < rank; ++r)
+  for (uint64_t r = 0; r < rank; r++)
     assert((shape[r] == 0 || shape[r] == idata[2 + r]) &&
            "Dimension size mismatch");
 }
@@ -87,13 +95,13 @@ bool SparseTensorReader::canReadAs(PrimaryType valTy) const {
     // integer and floating primary-types.
     return isRealPrimaryType(valTy);
   }
-  MLIR_SPARSETENSOR_FATAL("Unknown ValueKind: %d\n",
-                          static_cast<uint8_t>(valueKind_));
+  fprintf(stderr, "Unknown ValueKind: %d\n", static_cast<uint8_t>(valueKind_));
+  return false;
 }
 
 /// Helper to convert C-style strings (i.e., '\0' terminated) to lower case.
 static inline void toLower(char *token) {
-  for (char *c = token; *c; ++c)
+  for (char *c = token; *c; c++)
     *c = tolower(*c);
 }
 
@@ -116,8 +124,10 @@ void SparseTensorReader::readMMEHeader() {
   char symmetry[64];
   // Read header line.
   if (fscanf(file, "%63s %63s %63s %63s %63s\n", header, object, format, field,
-             symmetry) != 5)
-    MLIR_SPARSETENSOR_FATAL("Corrupt header in %s\n", filename);
+             symmetry) != 5) {
+    fprintf(stderr, "Corrupt header in %s\n", filename);
+    exit(1);
+  }
   // Convert all to lowercase up front (to avoid accidental redundancy).
   toLower(header);
   toLower(object);
@@ -125,24 +135,27 @@ void SparseTensorReader::readMMEHeader() {
   toLower(field);
   toLower(symmetry);
   // Process `field`, which specify pattern or the data type of the values.
-  if (streq(field, "pattern"))
+  if (streq(field, "pattern")) {
     valueKind_ = ValueKind::kPattern;
-  else if (streq(field, "real"))
+  } else if (streq(field, "real")) {
     valueKind_ = ValueKind::kReal;
-  else if (streq(field, "integer"))
+  } else if (streq(field, "integer")) {
     valueKind_ = ValueKind::kInteger;
-  else if (streq(field, "complex"))
+  } else if (streq(field, "complex")) {
     valueKind_ = ValueKind::kComplex;
-  else
-    MLIR_SPARSETENSOR_FATAL("Unexpected header field value in %s\n", filename);
+  } else {
+    fprintf(stderr, "Unexpected header field value in %s\n", filename);
+    exit(1);
+  }
   // Set properties.
   isSymmetric_ = streq(symmetry, "symmetric");
   // Make sure this is a general sparse matrix.
   if (strne(header, "%%matrixmarket") || strne(object, "matrix") ||
       strne(format, "coordinate") ||
-      (strne(symmetry, "general") && !isSymmetric_))
-    MLIR_SPARSETENSOR_FATAL("Cannot find a general sparse matrix in %s\n",
-                            filename);
+      (strne(symmetry, "general") && !isSymmetric_)) {
+    fprintf(stderr, "Cannot find a general sparse matrix in %s\n", filename);
+    exit(1);
+  }
   // Skip comments.
   while (true) {
     readLine();
@@ -152,8 +165,10 @@ void SparseTensorReader::readMMEHeader() {
   // Next line contains M N NNZ.
   idata[0] = 2; // rank
   if (sscanf(line, "%" PRIu64 "%" PRIu64 "%" PRIu64 "\n", idata + 2, idata + 3,
-             idata + 1) != 3)
-    MLIR_SPARSETENSOR_FATAL("Cannot find size in %s\n", filename);
+             idata + 1) != 3) {
+    fprintf(stderr, "Cannot find size in %s\n", filename);
+    exit(1);
+  }
 }
 
 /// Read the "extended" FROSTT header. Although not part of the documented
@@ -168,12 +183,17 @@ void SparseTensorReader::readExtFROSTTHeader() {
       break;
   }
   // Next line contains RANK and NNZ.
-  if (sscanf(line, "%" PRIu64 "%" PRIu64 "\n", idata, idata + 1) != 2)
-    MLIR_SPARSETENSOR_FATAL("Cannot find metadata in %s\n", filename);
+  if (sscanf(line, "%" PRIu64 "%" PRIu64 "\n", idata, idata + 1) != 2) {
+    fprintf(stderr, "Cannot find metadata in %s\n", filename);
+    exit(1);
+  }
   // Followed by a line with the dimension sizes (one per rank).
-  for (uint64_t r = 0; r < idata[0]; ++r)
-    if (fscanf(file, "%" PRIu64, idata + 2 + r) != 1)
-      MLIR_SPARSETENSOR_FATAL("Cannot find dimension size %s\n", filename);
+  for (uint64_t r = 0; r < idata[0]; r++) {
+    if (fscanf(file, "%" PRIu64, idata + 2 + r) != 1) {
+      fprintf(stderr, "Cannot find dimension size %s\n", filename);
+      exit(1);
+    }
+  }
   readLine(); // end of line
   // The FROSTT format does not define the data type of the nonzero elements.
   valueKind_ = ValueKind::kUndefined;
diff --git a/mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp b/mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp
index bbe10b0dcdd465..aaa42a7e3a31bf 100644
--- a/mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp
+++ b/mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp
@@ -51,7 +51,8 @@ SparseTensorStorageBase::SparseTensorStorageBase( // NOLINT
 
 // Helper macro for wrong "partial method specialization" errors.
 #define FATAL_PIV(NAME)                                                        \
-  MLIR_SPARSETENSOR_FATAL("<P,I,V> type mismatch for: " #NAME);
+  fprintf(stderr, "<P,I,V> type mismatch for: " #NAME);                        \
+  exit(1);
 
 #define IMPL_GETPOSITIONS(PNAME, P)                                            \
   void SparseTensorStorageBase::getPositions(std::vector<P> **, uint64_t) {    \
diff --git a/mlir/lib/ExecutionEngine/SparseTensorRuntime.cpp b/mlir/lib/ExecutionEngine/SparseTensorRuntime.cpp
index 0bc90b281b7ced..731abcbbf1f39e 100644
--- a/mlir/lib/ExecutionEngine/SparseTensorRuntime.cpp
+++ b/mlir/lib/ExecutionEngine/SparseTensorRuntime.cpp
@@ -52,7 +52,6 @@
 
 #include "mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h"
 #include "mlir/ExecutionEngine/SparseTensor/COO.h"
-#include "mlir/ExecutionEngine/SparseTensor/ErrorHandling.h"
 #include "mlir/ExecutionEngine/SparseTensor/File.h"
 #include "mlir/ExecutionEngine/SparseTensor/Storage.h"
 
@@ -139,8 +138,8 @@ extern "C" {
       return ptr;                                                              \
     }                                                                          \
     }                                                                          \
-    MLIR_SPARSETENSOR_FATAL("unknown action: %d\n",                            \
-                            static_cast<uint32_t>(action));                    \
+    fprintf(stderr, "unknown action %d\n", static_cast<uint32_t>(action));     \
+    exit(1);                                                                   \
   }
 
 #define CASE_SECSAME(p, v, P, V) CASE(p, p, v, P, P, V)
@@ -283,10 +282,10 @@ void *_mlir_ciface_newSparseTensor( // NOLINT
   CASE_SECSAME(OverheadType::kU64, PrimaryType::kC32, uint64_t, complex32);
 
   // Unsupported case (add above if needed).
-  MLIR_SPARSETENSOR_FATAL(
-      "unsupported combination of types: <P=%d, C=%d, V=%d>\n",
-      static_cast<int>(posTp), static_cast<int>(crdTp),
-      static_cast<int>(valTp));
+  fprintf(stderr, "unsupported combination of types: <P=%d, C=%d, V=%d>\n",
+          static_cast<int>(posTp), static_cast<int>(crdTp),
+          static_cast<int>(valTp));
+  exit(1);
 }
 #undef CASE
 #undef CASE_SECSAME
@@ -468,8 +467,10 @@ char *getTensorFilename(index_type id) {
   char var[bufSize];
   snprintf(var, bufSize, "TENSOR%" PRIu64, id);
   char *env = getenv(var);
-  if (!env)
-    MLIR_SPARSETENSOR_FATAL("Environment variable %s is not set\n", var);
+  if (!env) {
+    fprintf(stderr, "Environment variable %s is not set\n", var);
+    exit(1);
+  }
   return env;
 }
 
diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 853d136d9478f4..59ee03d9a3213f 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -9300,7 +9300,6 @@ cc_library(
     hdrs = [
         "include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h",
         "include/mlir/ExecutionEngine/SparseTensor/COO.h",
-        "include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h",
         "include/mlir/ExecutionEngine/SparseTensor/File.h",
         "include/mlir/ExecutionEngine/SparseTensor/MapRef.h",
         "include/mlir/ExecutionEngine/SparseTensor/Storage.h",



More information about the Mlir-commits mailing list