From 062f29c8d06ae91a05a962838e1c0a107fc01b9a Mon Sep 17 00:00:00 2001 From: wren romano <2998727+wrengr@users.noreply.github.com> Date: Mon, 17 Oct 2022 18:33:40 -0700 Subject: [PATCH] [mlir][sparse] Moving Enums.h into Dialect/SparseTensor/IR Move the SparseTensorEnums library out of the ExecutionEngine directory and into Dialect/SparseTensor/IR. Depends On D136002 Reviewed By: aartbik Differential Revision: https://reviews.llvm.org/D136005 --- .../SparseTensor/IR}/Enums.h | 39 +++++++++++++--------- .../mlir/Dialect/SparseTensor/IR/SparseTensor.h | 2 +- .../mlir/ExecutionEngine/SparseTensor/Storage.h | 2 +- .../mlir/ExecutionEngine/SparseTensorRuntime.h | 2 +- mlir/lib/Dialect/SparseTensor/IR/CMakeLists.txt | 20 +++++++++++ .../Dialect/SparseTensor/Transforms/CodegenUtils.h | 2 +- .../Transforms/SparseTensorConversion.cpp | 2 +- .../ExecutionEngine/SparseTensor/CMakeLists.txt | 20 ----------- utils/bazel/llvm-project-overlay/mlir/BUILD.bazel | 2 +- 9 files changed, 49 insertions(+), 42 deletions(-) rename mlir/include/mlir/{ExecutionEngine/SparseTensor => Dialect/SparseTensor/IR}/Enums.h (84%) diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/Enums.h b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h similarity index 84% rename from mlir/include/mlir/ExecutionEngine/SparseTensor/Enums.h rename to mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h index 716c8ab..7697f0b 100644 --- a/mlir/include/mlir/ExecutionEngine/SparseTensor/Enums.h +++ b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h @@ -1,4 +1,4 @@ -//===- Enums.h - Enums shared with the runtime ------------------*- C++ -*-===// +//===- Enums.h - Enums for the SparseTensor dialect -------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,25 +6,27 @@ // //===----------------------------------------------------------------------===// // -// Typedefs and enums for the lightweight runtime support library for -// sparse tensor manipulations. These are required to be public so that -// they can be shared with `Transforms/SparseTensorConversion.cpp`, since -// they define the arguments to the public functions declared later on. +// Typedefs and enums shared between MLIR code for manipulating the +// IR, and the lightweight runtime support library for sparse tensor +// manipulations. That is, all the enums are used to define the API +// of the runtime library and hence are also needed when generating +// calls into the runtime library. Moveover, the `DimLevelType` enum +// is also used as the internal IR encoding of dimension level types, +// to avoid code duplication (e.g., for the predicates). // // This file also defines x-macros // so that we can generate variations of the public functions for each // supported primary- and/or overhead-type. // -// This file is part of the lightweight runtime support library for sparse -// tensor manipulations. The functionality of the support library is meant -// to simplify benchmarking, testing, and debugging MLIR code operating on -// sparse tensors. However, the provided functionality is **not** part of -// core MLIR itself. +// Because this file defines a library which is a dependency of the +// runtime library itself, this file must not depend on any MLIR internals +// (e.g., operators, attributes, ArrayRefs, etc) lest the runtime library +// inherit those dependencies. // //===----------------------------------------------------------------------===// -#ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_ENUMS_H -#define MLIR_EXECUTIONENGINE_SPARSETENSOR_ENUMS_H +#ifndef MLIR_DIALECT_SPARSETENSOR_IR_ENUMS_H +#define MLIR_DIALECT_SPARSETENSOR_IR_ENUMS_H // NOTE: Client code will need to include "mlir/ExecutionEngine/Float16bits.h" // if they want to use the `MLIR_SPARSETENSOR_FOREVERY_V` macro. @@ -128,9 +130,14 @@ enum class Action : uint32_t { kToIterator = 6, }; -/// This enum mimics `SparseTensorEncodingAttr::DimLevelType` for -/// breaking dependency cycles. `SparseTensorEncodingAttr::DimLevelType` -/// is the source of truth and this enum should be kept consistent with it. +/// This enum defines all the sparse representations supportable by +/// the SparseTensor dialect. We use a lightweight encoding to encode +/// both the "format" per se (dense, compressed, singleton) as well as +/// the "properties" (ordered, unique). The encoding is chosen for +/// performance of the runtime library, and thus may change in future +/// versions; consequently, client code should use the predicate functions +/// defined below, rather than relying on knowledge about the particular +/// binary encoding. enum class DimLevelType : uint8_t { Dense = 4, // 0b001_00 Compressed = 8, // 0b010_00 @@ -218,4 +225,4 @@ static_assert((isUniqueDLT(DimLevelType::Dense) && } // namespace sparse_tensor } // namespace mlir -#endif // MLIR_EXECUTIONENGINE_SPARSETENSOR_ENUMS_H +#endif // MLIR_DIALECT_SPARSETENSOR_IR_ENUMS_H diff --git a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensor.h b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensor.h index 69a6aaa..67aaa3e 100644 --- a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensor.h +++ b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensor.h @@ -9,7 +9,7 @@ #ifndef MLIR_DIALECT_SPARSETENSOR_IR_SPARSETENSOR_H_ #define MLIR_DIALECT_SPARSETENSOR_IR_SPARSETENSOR_H_ -#include "mlir/ExecutionEngine/SparseTensor/Enums.h" +#include "mlir/Dialect/SparseTensor/IR/Enums.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/Dialect.h" #include "mlir/IR/OpDefinition.h" diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h b/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h index 0d733bd..313f13a 100644 --- a/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h +++ b/mlir/include/mlir/ExecutionEngine/SparseTensor/Storage.h @@ -33,10 +33,10 @@ #ifndef MLIR_EXECUTIONENGINE_SPARSETENSOR_STORAGE_H #define MLIR_EXECUTIONENGINE_SPARSETENSOR_STORAGE_H +#include "mlir/Dialect/SparseTensor/IR/Enums.h" #include "mlir/ExecutionEngine/Float16bits.h" #include "mlir/ExecutionEngine/SparseTensor/COO.h" #include "mlir/ExecutionEngine/SparseTensor/CheckedMul.h" -#include "mlir/ExecutionEngine/SparseTensor/Enums.h" #include "mlir/ExecutionEngine/SparseTensor/ErrorHandling.h" namespace mlir { diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensorRuntime.h b/mlir/include/mlir/ExecutionEngine/SparseTensorRuntime.h index 58d68f7..b8d4cce 100644 --- a/mlir/include/mlir/ExecutionEngine/SparseTensorRuntime.h +++ b/mlir/include/mlir/ExecutionEngine/SparseTensorRuntime.h @@ -15,9 +15,9 @@ #ifndef MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H #define MLIR_EXECUTIONENGINE_SPARSETENSORRUNTIME_H +#include "mlir/Dialect/SparseTensor/IR/Enums.h" #include "mlir/ExecutionEngine/CRunnerUtils.h" #include "mlir/ExecutionEngine/Float16bits.h" -#include "mlir/ExecutionEngine/SparseTensor/Enums.h" #include #include diff --git a/mlir/lib/Dialect/SparseTensor/IR/CMakeLists.txt b/mlir/lib/Dialect/SparseTensor/IR/CMakeLists.txt index 0e69813..14511f5 100644 --- a/mlir/lib/Dialect/SparseTensor/IR/CMakeLists.txt +++ b/mlir/lib/Dialect/SparseTensor/IR/CMakeLists.txt @@ -1,3 +1,23 @@ +# This library is shared by both MLIRSparseTensorDialect and +# MLIRSparseTensorRuntime, so it must not depend on any of the MLIR/LLVM +# internals or else mlir_c_runner_utils will inherit that dependency. +# +# Because this is a header-only (`INTERFACE`) library, we cannot use +# the `add_mlir_library` function. So we do our best to replicate the +# relevant portions below. If doing so becomes too complicated, then +# we should adjust the `add_mlir_library` function to also work for +# `INTERFACE` libraries. +set(MLIRSparseTensorEnums_srcs + ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/SparseTensor/IR/Enums.h) +add_library(MLIRSparseTensorEnums INTERFACE ${MLIRSparseTensorEnums_srcs}) +if(MSVC_IDE OR XCODE) + set_source_files_properties(${MLIRSparseTensorEnums_srcs} + PROPERTIES HEADER_FILE_ONLY ON) +endif() +add_mlir_library_install(MLIRSparseTensorEnums) +set_property(TARGET MLIRSparseTensorEnums PROPERTY CXX_STANDARD 17) + + add_mlir_dialect_library(MLIRSparseTensorDialect SparseTensorDialect.cpp diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h b/mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h index 4db8407..9476b4c 100644 --- a/mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h +++ b/mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h @@ -17,9 +17,9 @@ #include "mlir/Dialect/Complex/IR/Complex.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/Dialect/SparseTensor/IR/Enums.h" #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h" #include "mlir/Dialect/Utils/ReshapeOpsUtils.h" -#include "mlir/ExecutionEngine/SparseTensor/Enums.h" #include "mlir/IR/Builders.h" namespace mlir { diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp index a677f2b..4b9c2dd 100644 --- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp +++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp @@ -23,10 +23,10 @@ #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/Dialect/SCF/IR/SCF.h" +#include "mlir/Dialect/SparseTensor/IR/Enums.h" #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h" #include "mlir/Dialect/SparseTensor/Transforms/Passes.h" #include "mlir/Dialect/Tensor/IR/Tensor.h" -#include "mlir/ExecutionEngine/SparseTensor/Enums.h" #include "mlir/Transforms/DialectConversion.h" using namespace mlir; diff --git a/mlir/lib/ExecutionEngine/SparseTensor/CMakeLists.txt b/mlir/lib/ExecutionEngine/SparseTensor/CMakeLists.txt index 01b0793..11b696b 100644 --- a/mlir/lib/ExecutionEngine/SparseTensor/CMakeLists.txt +++ b/mlir/lib/ExecutionEngine/SparseTensor/CMakeLists.txt @@ -1,23 +1,3 @@ -# This library is shared by both MLIRSparseTensorDialect and -# MLIRSparseTensorRuntime, so it must not depend on any of the MLIR/LLVM -# internals or else mlir_c_runner_utils will inherit that dependency. -# -# Because this is a header-only (`INTERFACE`) library, we cannot use -# the `add_mlir_library` function. So we do our best to replicate the -# relevant portions below. If doing so becomes too complicated, then -# we should adjust the `add_mlir_library` function to also work for -# `INTERFACE` libraries. -set(MLIRSparseTensorEnums_srcs - ${MLIR_MAIN_INCLUDE_DIR}/mlir/ExecutionEngine/SparseTensor/Enums.h) -add_library(MLIRSparseTensorEnums INTERFACE ${MLIRSparseTensorEnums_srcs}) -if(MSVC_IDE OR XCODE) - set_source_files_properties(${MLIRSparseTensorEnums_srcs} - PROPERTIES HEADER_FILE_ONLY ON) -endif() -add_mlir_library_install(MLIRSparseTensorEnums) -set_property(TARGET MLIRSparseTensorEnums PROPERTY CXX_STANDARD 17) - - # Unlike mlir_float16_utils, mlir_c_runner_utils, etc, we do *not* make # this a shared library: because doing so causes issues when building # on Windows. In particular, various functions take/return `std::vector` diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel index e1a463b8..4401526 100644 --- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -2080,7 +2080,7 @@ gentbl_cc_library( # internals or else mlir_c_runner_utils will inherit that dependency. cc_library( name = "SparseTensorEnums", - hdrs = ["include/mlir/ExecutionEngine/SparseTensor/Enums.h"], + hdrs = ["include/mlir/Dialect/SparseTensor/IR/Enums.h"], includes = ["include"], ) -- 2.7.4