xref: /llvm-project/mlir/test/lib/Dialect/SPIRV/TestModuleCombiner.cpp (revision 4f4cd963a6e820b50514706a1a3faed3a05779a2)
190a8260cSergawy //===- TestModuleCombiner.cpp - Pass to test SPIR-V module combiner lib ---===//
290a8260cSergawy //
390a8260cSergawy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
490a8260cSergawy // See https://llvm.org/LICENSE.txt for license information.
590a8260cSergawy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
690a8260cSergawy //
790a8260cSergawy //===----------------------------------------------------------------------===//
890a8260cSergawy 
901178654SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
1001178654SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
1101178654SLei Zhang #include "mlir/Dialect/SPIRV/Linking/ModuleCombiner.h"
1290a8260cSergawy #include "mlir/IR/Builders.h"
1365fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h"
1490a8260cSergawy #include "mlir/Pass/Pass.h"
1590a8260cSergawy 
1690a8260cSergawy using namespace mlir;
1790a8260cSergawy 
1890a8260cSergawy namespace {
1990a8260cSergawy class TestModuleCombinerPass
2090a8260cSergawy     : public PassWrapper<TestModuleCombinerPass,
2190a8260cSergawy                          OperationPass<mlir::ModuleOp>> {
2290a8260cSergawy public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestModuleCombinerPass)235e50dd04SRiver Riddle   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestModuleCombinerPass)
245e50dd04SRiver Riddle 
25b5e22e6dSMehdi Amini   StringRef getArgument() const final { return "test-spirv-module-combiner"; }
getDescription() const26b5e22e6dSMehdi Amini   StringRef getDescription() const final {
27b5e22e6dSMehdi Amini     return "Tests SPIR-V module combiner library";
28b5e22e6dSMehdi Amini   }
2990a8260cSergawy   TestModuleCombinerPass() = default;
TestModuleCombinerPass(const TestModuleCombinerPass &)3090a8260cSergawy   TestModuleCombinerPass(const TestModuleCombinerPass &) {}
3190a8260cSergawy   void runOnOperation() override;
3290a8260cSergawy };
3390a8260cSergawy } // namespace
3490a8260cSergawy 
runOnOperation()3590a8260cSergawy void TestModuleCombinerPass::runOnOperation() {
3690a8260cSergawy   auto modules = llvm::to_vector<4>(getOperation().getOps<spirv::ModuleOp>());
37*4f4cd963SJakub Kuderski   if (modules.empty())
38*4f4cd963SJakub Kuderski     return;
3990a8260cSergawy 
4090a8260cSergawy   OpBuilder combinedModuleBuilder(modules[0]);
4123326b9fSLei Zhang 
4223326b9fSLei Zhang   auto listener = [](spirv::ModuleOp originalModule, StringRef oldSymbol,
4323326b9fSLei Zhang                      StringRef newSymbol) {
4423326b9fSLei Zhang     llvm::outs() << "[" << originalModule.getName() << "] " << oldSymbol
4523326b9fSLei Zhang                  << " -> " << newSymbol << "\n";
4623326b9fSLei Zhang   };
4723326b9fSLei Zhang 
48361acbb3SRiver Riddle   OwningOpRef<spirv::ModuleOp> combinedModule =
49361acbb3SRiver Riddle       spirv::combine(modules, combinedModuleBuilder, listener);
5090a8260cSergawy 
5190a8260cSergawy   for (spirv::ModuleOp module : modules)
5290a8260cSergawy     module.erase();
53361acbb3SRiver Riddle   combinedModule.release();
5490a8260cSergawy }
5590a8260cSergawy 
5690a8260cSergawy namespace mlir {
registerTestSpirvModuleCombinerPass()5790a8260cSergawy void registerTestSpirvModuleCombinerPass() {
58b5e22e6dSMehdi Amini   PassRegistration<TestModuleCombinerPass>();
5990a8260cSergawy }
6090a8260cSergawy } // namespace mlir
61