1 //===- TestInlining.cpp - Pass to inline calls in the test dialect --------===// 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 // TODO: This pass is only necessary because the main inlining pass 10 // has no abstracted away the call+callee relationship. When the inlining 11 // interface has this support, this pass should be removed. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "TestDialect.h" 16 #include "mlir/Dialect/StandardOps/IR/Ops.h" 17 #include "mlir/IR/BlockAndValueMapping.h" 18 #include "mlir/IR/BuiltinOps.h" 19 #include "mlir/Pass/Pass.h" 20 #include "mlir/Transforms/InliningUtils.h" 21 #include "llvm/ADT/StringSet.h" 22 23 using namespace mlir; 24 using namespace test; 25 26 namespace { 27 struct Inliner : public PassWrapper<Inliner, OperationPass<FuncOp>> { 28 StringRef getArgument() const final { return "test-inline"; } 29 StringRef getDescription() const final { 30 return "Test inlining region calls"; 31 } 32 33 void runOnOperation() override { 34 auto function = getOperation(); 35 36 // Collect each of the direct function calls within the module. 37 SmallVector<CallIndirectOp, 16> callers; 38 function.walk([&](CallIndirectOp caller) { callers.push_back(caller); }); 39 40 // Build the inliner interface. 41 InlinerInterface interface(&getContext()); 42 43 // Try to inline each of the call operations. 44 for (auto caller : callers) { 45 auto callee = dyn_cast_or_null<FunctionalRegionOp>( 46 caller.getCallee().getDefiningOp()); 47 if (!callee) 48 continue; 49 50 // Inline the functional region operation, but only clone the internal 51 // region if there is more than one use. 52 if (failed(inlineRegion( 53 interface, &callee.getBody(), caller, caller.getArgOperands(), 54 caller.getResults(), caller.getLoc(), 55 /*shouldCloneInlinedRegion=*/!callee.getResult().hasOneUse()))) 56 continue; 57 58 // If the inlining was successful then erase the call and callee if 59 // possible. 60 caller.erase(); 61 if (callee.use_empty()) 62 callee.erase(); 63 } 64 } 65 }; 66 } // namespace 67 68 namespace mlir { 69 namespace test { 70 void registerInliner() { PassRegistration<Inliner>(); } 71 } // namespace test 72 } // namespace mlir 73