1 //===- Inliner.cpp - Pass to inline function calls ------------------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 18 #include "mlir/Dialect/StandardOps/Ops.h" 19 #include "mlir/IR/Builders.h" 20 #include "mlir/IR/Function.h" 21 #include "mlir/IR/Module.h" 22 #include "mlir/Pass/Pass.h" 23 #include "mlir/Transforms/InliningUtils.h" 24 #include "mlir/Transforms/Passes.h" 25 #include "llvm/ADT/StringSet.h" 26 27 using namespace mlir; 28 29 // TODO(riverriddle) This pass should currently only be used for basic testing 30 // of inlining functionality. 31 namespace { 32 struct Inliner : public ModulePass<Inliner> { 33 void runOnModule() override { 34 auto module = getModule(); 35 36 // Collect each of the direct function calls within the module. 37 SmallVector<CallOp, 16> callOps; 38 for (auto &f : module) 39 f.walk([&](CallOp callOp) { callOps.push_back(callOp); }); 40 41 // Build the inliner interface. 42 InlinerInterface interface(&getContext()); 43 44 // Try to inline each of the call operations. 45 for (auto &call : callOps) { 46 if (failed(inlineFunction( 47 interface, module.lookupSymbol<FuncOp>(call.getCallee()), call, 48 llvm::to_vector<8>(call.getArgOperands()), 49 llvm::to_vector<8>(call.getResults()), call.getLoc()))) 50 continue; 51 52 // If the inlining was successful then erase the call. 53 call.erase(); 54 } 55 } 56 }; 57 } // end anonymous namespace 58 59 static PassRegistration<Inliner> pass("inline", "Inline function calls"); 60