Skip to content

Commit

Permalink
[mlir][sparse] remove very thin header file from sparse runtime suppo…
Browse files Browse the repository at this point in the history
…rt (llvm#82820)
  • Loading branch information
aartbik authored Feb 23, 2024
1 parent 9966008 commit 1c2456d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 82 deletions.
34 changes: 0 additions & 34 deletions mlir/include/mlir/ExecutionEngine/SparseTensor/ErrorHandling.h

This file was deleted.

10 changes: 6 additions & 4 deletions mlir/include/mlir/ExecutionEngine/SparseTensor/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
84 changes: 52 additions & 32 deletions mlir/lib/ExecutionEngine/SparseTensor/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -116,33 +124,38 @@ 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);
toLower(format);
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();
Expand All @@ -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
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion mlir/lib/ExecutionEngine/SparseTensor/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down
19 changes: 10 additions & 9 deletions mlir/lib/ExecutionEngine/SparseTensorRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1c2456d

Please sign in to comment.