[llvm-branch-commits] [mlir] ecab638 - [MLIR][SPIRV] Refactoring serialization and deserialization

Lei Zhang via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 14 09:32:57 PST 2020


Author: ergawy
Date: 2020-12-14T12:28:16-05:00
New Revision: ecab63894bb5aebcbbe694839779f346e6fbe9e2

URL: https://github.com/llvm/llvm-project/commit/ecab63894bb5aebcbbe694839779f346e6fbe9e2
DIFF: https://github.com/llvm/llvm-project/commit/ecab63894bb5aebcbbe694839779f346e6fbe9e2.diff

LOG: [MLIR][SPIRV] Refactoring serialization and deserialization

This commit splits SPIR-V's serialization and deserialization code
into separate libraries. The motiviation being that the serializer
is used more often the deserializer and therefore lumping them
together unnecessarily increases binary size for the most common
case.

This commit also moves these libraries into the Target/ directory
to follow MLIR convention.

Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D91548

Added: 
    mlir/include/mlir/Target/SPIRV/Deserialization.h
    mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
    mlir/include/mlir/Target/SPIRV/Serialization.h
    mlir/lib/Target/SPIRV/Deserialization.cpp
    mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
    mlir/lib/Target/SPIRV/Serialization.cpp
    mlir/lib/Target/SPIRV/TranslateRegistration.cpp

Modified: 
    mlir/lib/Conversion/GPUToVulkan/ConvertGPULaunchFuncToVulkanLaunchFunc.cpp
    mlir/lib/Dialect/SPIRV/CMakeLists.txt
    mlir/lib/Target/CMakeLists.txt
    mlir/unittests/Dialect/SPIRV/CMakeLists.txt
    mlir/unittests/Dialect/SPIRV/DeserializationTest.cpp
    mlir/unittests/Dialect/SPIRV/SerializationTest.cpp

Removed: 
    mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
    mlir/include/mlir/Dialect/SPIRV/Serialization.h
    mlir/lib/Dialect/SPIRV/Serialization/CMakeLists.txt
    mlir/lib/Dialect/SPIRV/Serialization/Deserializer.cpp
    mlir/lib/Dialect/SPIRV/Serialization/SPIRVBinaryUtils.cpp
    mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
    mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp


################################################################################
diff  --git a/mlir/include/mlir/Dialect/SPIRV/Serialization.h b/mlir/include/mlir/Target/SPIRV/Deserialization.h
similarity index 65%
rename from mlir/include/mlir/Dialect/SPIRV/Serialization.h
rename to mlir/include/mlir/Target/SPIRV/Deserialization.h
index 2c91286ca158..207f506b17ad 100644
--- a/mlir/include/mlir/Dialect/SPIRV/Serialization.h
+++ b/mlir/include/mlir/Target/SPIRV/Deserialization.h
@@ -6,13 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file declares the entry points for serialize and deserialize SPIR-V
-// binary modules.
+// This file declares the entry points for deserializing SPIR-V binary modules.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef MLIR_DIALECT_SPIRV_SERIALIZATION_H_
-#define MLIR_DIALECT_SPIRV_SERIALIZATION_H_
+#ifndef MLIR_TARGET_SPIRV_DESERIALIZATION_H
+#define MLIR_TARGET_SPIRV_DESERIALIZATION_H
 
 #include "mlir/Support/LLVM.h"
 
@@ -21,15 +20,8 @@ struct LogicalResult;
 class MLIRContext;
 
 namespace spirv {
-class ModuleOp;
 class OwningSPIRVModuleRef;
 
-/// Serializes the given SPIR-V `module` and writes to `binary`. On failure,
-/// reports errors to the error handler registered with the MLIR context for
-/// `module`.
-LogicalResult serialize(ModuleOp module, SmallVectorImpl<uint32_t> &binary,
-                        bool emitDebugInfo = false);
-
 /// Deserializes the given SPIR-V `binary` module and creates a MLIR ModuleOp
 /// in the given `context`. Returns the ModuleOp on success; otherwise, reports
 /// errors to the error handler registered with `context` and returns a null
@@ -40,4 +32,4 @@ OwningSPIRVModuleRef deserialize(ArrayRef<uint32_t> binary,
 } // end namespace spirv
 } // end namespace mlir
 
-#endif // MLIR_DIALECT_SPIRV_SERIALIZATION_H_
+#endif // MLIR_TARGET_SPIRV_DESERIALIZATION_H

diff  --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h b/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
similarity index 91%
rename from mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
rename to mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
index 6baafdb9d9e9..cdfadb02fe5c 100644
--- a/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
+++ b/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
@@ -10,8 +10,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef MLIR_DIALECT_SPIRV_SPIRV_BINARY_UTILS_H_
-#define MLIR_DIALECT_SPIRV_SPIRV_BINARY_UTILS_H_
+#ifndef MLIR_TARGET_SPIRV_BINARY_UTILS_H_
+#define MLIR_TARGET_SPIRV_BINARY_UTILS_H_
 
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
 #include "mlir/Support/LogicalResult.h"
@@ -44,4 +44,4 @@ LogicalResult encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary,
 } // end namespace spirv
 } // end namespace mlir
 
-#endif // MLIR_DIALECT_SPIRV_SPIRV_BINARY_UTILS_H_
+#endif // MLIR_TARGET_SPIRV_BINARY_UTILS_H_

diff  --git a/mlir/include/mlir/Target/SPIRV/Serialization.h b/mlir/include/mlir/Target/SPIRV/Serialization.h
new file mode 100644
index 000000000000..d8431dc7dc2a
--- /dev/null
+++ b/mlir/include/mlir/Target/SPIRV/Serialization.h
@@ -0,0 +1,34 @@
+//===- Serialization.h - MLIR SPIR-V (De)serialization ----------*- 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 declares the entry point for serializing SPIR-V binary modules.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TARGET_SPIRV_SERIALIZATION_H
+#define MLIR_TARGET_SPIRV_SERIALIZATION_H
+
+#include "mlir/Support/LLVM.h"
+
+namespace mlir {
+struct LogicalResult;
+class MLIRContext;
+
+namespace spirv {
+class ModuleOp;
+
+/// Serializes the given SPIR-V `module` and writes to `binary`. On failure,
+/// reports errors to the error handler registered with the MLIR context for
+/// `module`.
+LogicalResult serialize(ModuleOp module, SmallVectorImpl<uint32_t> &binary,
+                        bool emitDebugInfo = false);
+
+} // end namespace spirv
+} // end namespace mlir
+
+#endif // MLIR_TARGET_SPIRV_SERIALIZATION_H

diff  --git a/mlir/lib/Conversion/GPUToVulkan/ConvertGPULaunchFuncToVulkanLaunchFunc.cpp b/mlir/lib/Conversion/GPUToVulkan/ConvertGPULaunchFuncToVulkanLaunchFunc.cpp
index fbb860f9e2ea..3808e3df024c 100644
--- a/mlir/lib/Conversion/GPUToVulkan/ConvertGPULaunchFuncToVulkanLaunchFunc.cpp
+++ b/mlir/lib/Conversion/GPUToVulkan/ConvertGPULaunchFuncToVulkanLaunchFunc.cpp
@@ -18,12 +18,12 @@
 #include "mlir/Dialect/GPU/GPUDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
-#include "mlir/Dialect/SPIRV/Serialization.h"
 #include "mlir/Dialect/StandardOps/IR/Ops.h"
 #include "mlir/IR/Attributes.h"
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/BuiltinTypes.h"
+#include "mlir/Target/SPIRV/Serialization.h"
 
 using namespace mlir;
 

diff  --git a/mlir/lib/Dialect/SPIRV/CMakeLists.txt b/mlir/lib/Dialect/SPIRV/CMakeLists.txt
index f37182121fed..8362a980cc5e 100644
--- a/mlir/lib/Dialect/SPIRV/CMakeLists.txt
+++ b/mlir/lib/Dialect/SPIRV/CMakeLists.txt
@@ -35,5 +35,4 @@ add_mlir_dialect_library(MLIRSPIRV
   )
 
 add_subdirectory(Linking)
-add_subdirectory(Serialization)
 add_subdirectory(Transforms)

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/CMakeLists.txt b/mlir/lib/Dialect/SPIRV/Serialization/CMakeLists.txt
deleted file mode 100644
index c04f801321ea..000000000000
--- a/mlir/lib/Dialect/SPIRV/Serialization/CMakeLists.txt
+++ /dev/null
@@ -1,18 +0,0 @@
-add_mlir_dialect_library(MLIRSPIRVSerialization
-  Deserializer.cpp
-  Serializer.cpp
-  SPIRVBinaryUtils.cpp
-  TranslateRegistration.cpp
-
-  ADDITIONAL_HEADER_DIRS
-  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/SPIRV
-
-  DEPENDS
-  MLIRSPIRVSerializationGen
-
-  LINK_LIBS PUBLIC
-  MLIRIR
-  MLIRSPIRV
-  MLIRSupport
-  MLIRTranslation
-  )

diff  --git a/mlir/lib/Target/CMakeLists.txt b/mlir/lib/Target/CMakeLists.txt
index 96568438a0a3..275c1a0f78fc 100644
--- a/mlir/lib/Target/CMakeLists.txt
+++ b/mlir/lib/Target/CMakeLists.txt
@@ -113,3 +113,52 @@ add_mlir_translation_library(MLIRTargetROCDLIR
   MLIRROCDLIR
   MLIRTargetLLVMIRModuleTranslation
   )
+
+add_mlir_translation_library(MLIRSPIRVBinaryUtils
+  SPIRV/SPIRVBinaryUtils.cpp
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  MLIRSPIRV
+  MLIRSupport
+  )
+
+add_mlir_translation_library(MLIRSPIRVSerialization
+  SPIRV/Serialization.cpp
+
+  DEPENDS
+  MLIRSPIRVSerializationGen
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  MLIRSPIRV
+  MLIRSPIRVBinaryUtils
+  MLIRSupport
+  MLIRTranslation
+  )
+
+add_mlir_translation_library(MLIRSPIRVDeserialization
+  SPIRV/Deserialization.cpp
+
+  DEPENDS
+  MLIRSPIRVSerializationGen
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  MLIRSPIRV
+  MLIRSPIRVBinaryUtils
+  MLIRSupport
+  MLIRTranslation
+  )
+
+add_mlir_translation_library(MLIRSPIRVTranslateRegistration
+  SPIRV/TranslateRegistration.cpp
+
+  LINK_LIBS PUBLIC
+  MLIRIR
+  MLIRSPIRV
+  MLIRSPIRVSerialization
+  MLIRSPIRVDeserialization
+  MLIRSupport
+  MLIRTranslation
+  )

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/Deserializer.cpp b/mlir/lib/Target/SPIRV/Deserialization.cpp
similarity index 99%
rename from mlir/lib/Dialect/SPIRV/Serialization/Deserializer.cpp
rename to mlir/lib/Target/SPIRV/Deserialization.cpp
index 97d1b6e534b0..6512b1a0f111 100644
--- a/mlir/lib/Dialect/SPIRV/Serialization/Deserializer.cpp
+++ b/mlir/lib/Target/SPIRV/Deserialization.cpp
@@ -10,10 +10,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/Dialect/SPIRV/Serialization.h"
+#include "mlir/Target/SPIRV/Deserialization.h"
 
 #include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
-#include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
 #include "mlir/Dialect/SPIRV/SPIRVModule.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
 #include "mlir/Dialect/SPIRV/SPIRVTypes.h"
@@ -21,6 +20,7 @@
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/Location.h"
 #include "mlir/Support/LogicalResult.h"
+#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SetVector.h"
@@ -2781,6 +2781,7 @@ Deserializer::processOp<spirv::CopyMemoryOp>(ArrayRef<uint32_t> words) {
 #include "mlir/Dialect/SPIRV/SPIRVSerialization.inc"
 } // namespace
 
+namespace mlir {
 spirv::OwningSPIRVModuleRef spirv::deserialize(ArrayRef<uint32_t> binary,
                                                MLIRContext *context) {
   Deserializer deserializer(binary, context);
@@ -2790,3 +2791,4 @@ spirv::OwningSPIRVModuleRef spirv::deserialize(ArrayRef<uint32_t> binary,
 
   return deserializer.collect();
 }
+} // namespace mlir

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/SPIRVBinaryUtils.cpp b/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
similarity index 98%
rename from mlir/lib/Dialect/SPIRV/Serialization/SPIRVBinaryUtils.cpp
rename to mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
index eabc410fb972..e1795021b335 100644
--- a/mlir/lib/Dialect/SPIRV/Serialization/SPIRVBinaryUtils.cpp
+++ b/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
+#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
 #include "mlir/Dialect/SPIRV/SPIRVTypes.h"
 
 using namespace mlir;

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp b/mlir/lib/Target/SPIRV/Serialization.cpp
similarity index 99%
rename from mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
rename to mlir/lib/Target/SPIRV/Serialization.cpp
index 38f9e5c53a13..5d05197e2659 100644
--- a/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
+++ b/mlir/lib/Target/SPIRV/Serialization.cpp
@@ -10,16 +10,16 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/Dialect/SPIRV/Serialization.h"
+#include "mlir/Target/SPIRV/Serialization.h"
 
 #include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
-#include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
 #include "mlir/Dialect/SPIRV/SPIRVDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
 #include "mlir/Dialect/SPIRV/SPIRVTypes.h"
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/RegionGraphTraits.h"
 #include "mlir/Support/LogicalResult.h"
+#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SetVector.h"
@@ -2139,6 +2139,7 @@ LogicalResult Serializer::emitDebugLine(SmallVectorImpl<uint32_t> &binary,
   return success();
 }
 
+namespace mlir {
 LogicalResult spirv::serialize(spirv::ModuleOp module,
                                SmallVectorImpl<uint32_t> &binary,
                                bool emitDebugInfo) {
@@ -2156,3 +2157,4 @@ LogicalResult spirv::serialize(spirv::ModuleOp module,
   serializer.collect(binary);
   return success();
 }
+} // namespace mlir

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp b/mlir/lib/Target/SPIRV/TranslateRegistration.cpp
similarity index 98%
rename from mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp
rename to mlir/lib/Target/SPIRV/TranslateRegistration.cpp
index 2685fb661860..f4cf45ae1956 100644
--- a/mlir/lib/Dialect/SPIRV/Serialization/TranslateRegistration.cpp
+++ b/mlir/lib/Target/SPIRV/TranslateRegistration.cpp
@@ -14,12 +14,13 @@
 #include "mlir/Dialect/SPIRV/SPIRVDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVModule.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
-#include "mlir/Dialect/SPIRV/Serialization.h"
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/Dialect.h"
 #include "mlir/Parser.h"
 #include "mlir/Support/FileUtilities.h"
+#include "mlir/Target/SPIRV/Deserialization.h"
+#include "mlir/Target/SPIRV/Serialization.h"
 #include "mlir/Translation.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/MemoryBuffer.h"

diff  --git a/mlir/unittests/Dialect/SPIRV/CMakeLists.txt b/mlir/unittests/Dialect/SPIRV/CMakeLists.txt
index 2d9b60addbe7..530896a22ea6 100644
--- a/mlir/unittests/Dialect/SPIRV/CMakeLists.txt
+++ b/mlir/unittests/Dialect/SPIRV/CMakeLists.txt
@@ -1,8 +1,11 @@
-add_mlir_unittest(MLIRSPIRVTests
+add_mlir_unittest(MLIRSPIRVImportExportTests
   DeserializationTest.cpp
   SerializationTest.cpp
 )
-target_link_libraries(MLIRSPIRVTests
+target_link_libraries(MLIRSPIRVImportExportTests
   PRIVATE
+  MLIRIR
   MLIRSPIRV
-  MLIRSPIRVSerialization)
+  MLIRSPIRVDeserialization
+  MLIRSPIRVSerialization
+)

diff  --git a/mlir/unittests/Dialect/SPIRV/DeserializationTest.cpp b/mlir/unittests/Dialect/SPIRV/DeserializationTest.cpp
index ccec33951870..3823c7b8b544 100644
--- a/mlir/unittests/Dialect/SPIRV/DeserializationTest.cpp
+++ b/mlir/unittests/Dialect/SPIRV/DeserializationTest.cpp
@@ -12,13 +12,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
+#include "mlir/Target/SPIRV/Deserialization.h"
 #include "mlir/Dialect/SPIRV/SPIRVDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVModule.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
-#include "mlir/Dialect/SPIRV/Serialization.h"
 #include "mlir/IR/Diagnostics.h"
 #include "mlir/IR/MLIRContext.h"
+#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
 #include "gmock/gmock.h"
 
 #include <memory>

diff  --git a/mlir/unittests/Dialect/SPIRV/SerializationTest.cpp b/mlir/unittests/Dialect/SPIRV/SerializationTest.cpp
index 6bd16b964c29..bfeffa3fb055 100644
--- a/mlir/unittests/Dialect/SPIRV/SerializationTest.cpp
+++ b/mlir/unittests/Dialect/SPIRV/SerializationTest.cpp
@@ -11,9 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/Dialect/SPIRV/Serialization.h"
+#include "mlir/Target/SPIRV/Serialization.h"
 #include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
-#include "mlir/Dialect/SPIRV/SPIRVBinaryUtils.h"
 #include "mlir/Dialect/SPIRV/SPIRVDialect.h"
 #include "mlir/Dialect/SPIRV/SPIRVModule.h"
 #include "mlir/Dialect/SPIRV/SPIRVOps.h"
@@ -21,6 +20,7 @@
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/Location.h"
 #include "mlir/IR/MLIRContext.h"
+#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Sequence.h"


        


More information about the llvm-branch-commits mailing list