xref: /llvm-project/mlir/test/lib/Dialect/SPIRV/TestModuleCombiner.cpp (revision 23326b9f1723a398681def87c80e608fa94485f2)
1 //===- TestModuleCombiner.cpp - Pass to test SPIR-V module combiner lib ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
10 #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
11 #include "mlir/Dialect/SPIRV/Linking/ModuleCombiner.h"
12 #include "mlir/IR/Builders.h"
13 #include "mlir/IR/BuiltinOps.h"
14 #include "mlir/Pass/Pass.h"
15 
16 using namespace mlir;
17 
18 namespace {
19 class TestModuleCombinerPass
20     : public PassWrapper<TestModuleCombinerPass,
21                          OperationPass<mlir::ModuleOp>> {
22 public:
23   StringRef getArgument() const final { return "test-spirv-module-combiner"; }
24   StringRef getDescription() const final {
25     return "Tests SPIR-V module combiner library";
26   }
27   TestModuleCombinerPass() = default;
28   TestModuleCombinerPass(const TestModuleCombinerPass &) {}
29   void runOnOperation() override;
30 
31 private:
32   OwningOpRef<spirv::ModuleOp> combinedModule;
33 };
34 } // namespace
35 
36 void TestModuleCombinerPass::runOnOperation() {
37   auto modules = llvm::to_vector<4>(getOperation().getOps<spirv::ModuleOp>());
38 
39   OpBuilder combinedModuleBuilder(modules[0]);
40 
41   auto listener = [](spirv::ModuleOp originalModule, StringRef oldSymbol,
42                      StringRef newSymbol) {
43     llvm::outs() << "[" << originalModule.getName() << "] " << oldSymbol
44                  << " -> " << newSymbol << "\n";
45   };
46 
47   combinedModule = spirv::combine(modules, combinedModuleBuilder, listener);
48 
49   for (spirv::ModuleOp module : modules)
50     module.erase();
51 }
52 
53 namespace mlir {
54 void registerTestSpirvModuleCombinerPass() {
55   PassRegistration<TestModuleCombinerPass>();
56 }
57 } // namespace mlir
58