[mlir][sparse] remove very thin header file from sparse runtime support (#82820)

This commit is contained in:
Aart Bik 2024-02-23 12:37:36 -08:00 committed by GitHub
parent 99660082cb
commit 1c2456d659
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 82 deletions

View File

@ -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

View File

@ -115,10 +115,12 @@ public:
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;
}

View File

@ -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 {

View File

@ -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;

View File

@ -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) { \

View File

@ -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;
}

View File

@ -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",