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>());
3790a8260cSergawy 
3890a8260cSergawy   OpBuilder combinedModuleBuilder(modules[0]);
3923326b9fSLei Zhang 
4023326b9fSLei Zhang   auto listener = [](spirv::ModuleOp originalModule, StringRef oldSymbol,
4123326b9fSLei Zhang                      StringRef newSymbol) {
4223326b9fSLei Zhang     llvm::outs() << "[" << originalModule.getName() << "] " << oldSymbol
4323326b9fSLei Zhang                  << " -> " << newSymbol << "\n";
4423326b9fSLei Zhang   };
4523326b9fSLei Zhang 
46*361acbb3SRiver Riddle   OwningOpRef<spirv::ModuleOp> combinedModule =
47*361acbb3SRiver Riddle       spirv::combine(modules, combinedModuleBuilder, listener);
4890a8260cSergawy 
4990a8260cSergawy   for (spirv::ModuleOp module : modules)
5090a8260cSergawy     module.erase();
51*361acbb3SRiver Riddle   combinedModule.release();
5290a8260cSergawy }
5390a8260cSergawy 
5490a8260cSergawy namespace mlir {
registerTestSpirvModuleCombinerPass()5590a8260cSergawy void registerTestSpirvModuleCombinerPass() {
56b5e22e6dSMehdi Amini   PassRegistration<TestModuleCombinerPass>();
5790a8260cSergawy }
5890a8260cSergawy } // namespace mlir
59