Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 1 | //===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// This pass marks all internal functions as always_inline and creates |
Alfred Huang | f9b521f | 2017-06-15 23:02:55 +0000 | [diff] [blame] | 12 | /// duplicates of all other functions and marks the duplicates as always_inline. |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "AMDGPU.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/Transforms/Utils/Cloning.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
Matt Arsenault | 1390af2 | 2017-09-21 07:00:48 +0000 | [diff] [blame] | 24 | static cl::opt<bool> StressCalls( |
| 25 | "amdgpu-stress-function-calls", |
| 26 | cl::Hidden, |
| 27 | cl::desc("Force all functions to be noinline"), |
| 28 | cl::init(false)); |
| 29 | |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 30 | class AMDGPUAlwaysInline : public ModulePass { |
Stanislav Mekhanoshin | 89653df | 2017-03-30 20:16:02 +0000 | [diff] [blame] | 31 | bool GlobalOpt; |
| 32 | |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 33 | public: |
Matt Arsenault | 746e065 | 2017-06-02 18:02:42 +0000 | [diff] [blame] | 34 | static char ID; |
| 35 | |
| 36 | AMDGPUAlwaysInline(bool GlobalOpt = false) : |
| 37 | ModulePass(ID), GlobalOpt(GlobalOpt) { } |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 38 | bool runOnModule(Module &M) override; |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 39 | StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; } |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | } // End anonymous namespace |
| 43 | |
Matt Arsenault | 746e065 | 2017-06-02 18:02:42 +0000 | [diff] [blame] | 44 | INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline", |
| 45 | "AMDGPU Inline All Functions", false, false) |
| 46 | |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 47 | char AMDGPUAlwaysInline::ID = 0; |
| 48 | |
| 49 | bool AMDGPUAlwaysInline::runOnModule(Module &M) { |
Nikolay Haustov | eba8089 | 2016-08-31 11:18:33 +0000 | [diff] [blame] | 50 | std::vector<GlobalAlias*> AliasesToRemove; |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 51 | std::vector<Function *> FuncsToClone; |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 52 | |
Nikolay Haustov | eba8089 | 2016-08-31 11:18:33 +0000 | [diff] [blame] | 53 | for (GlobalAlias &A : M.aliases()) { |
| 54 | if (Function* F = dyn_cast<Function>(A.getAliasee())) { |
| 55 | A.replaceAllUsesWith(F); |
| 56 | AliasesToRemove.push_back(&A); |
| 57 | } |
| 58 | } |
| 59 | |
Stanislav Mekhanoshin | 89653df | 2017-03-30 20:16:02 +0000 | [diff] [blame] | 60 | if (GlobalOpt) { |
| 61 | for (GlobalAlias* A : AliasesToRemove) { |
| 62 | A->eraseFromParent(); |
| 63 | } |
Nikolay Haustov | eba8089 | 2016-08-31 11:18:33 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Matt Arsenault | 1390af2 | 2017-09-21 07:00:48 +0000 | [diff] [blame] | 66 | auto NewAttr = StressCalls ? Attribute::NoInline : Attribute::AlwaysInline; |
| 67 | auto IncompatAttr |
| 68 | = StressCalls ? Attribute::AlwaysInline : Attribute::NoInline; |
| 69 | |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 70 | for (Function &F : M) { |
Matt Arsenault | deaef8e | 2015-04-22 17:10:44 +0000 | [diff] [blame] | 71 | if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() && |
Matt Arsenault | 1390af2 | 2017-09-21 07:00:48 +0000 | [diff] [blame] | 72 | !F.hasFnAttribute(IncompatAttr)) |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 73 | FuncsToClone.push_back(&F); |
| 74 | } |
| 75 | |
| 76 | for (Function *F : FuncsToClone) { |
| 77 | ValueToValueMapTy VMap; |
Peter Collingbourne | dba9956 | 2016-05-10 20:23:24 +0000 | [diff] [blame] | 78 | Function *NewFunc = CloneFunction(F, VMap); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 79 | NewFunc->setLinkage(GlobalValue::InternalLinkage); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 80 | F->replaceAllUsesWith(NewFunc); |
| 81 | } |
| 82 | |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 83 | for (Function &F : M) { |
Matt Arsenault | 1390af2 | 2017-09-21 07:00:48 +0000 | [diff] [blame] | 84 | if (F.hasLocalLinkage() && !F.hasFnAttribute(IncompatAttr)) { |
| 85 | F.addFnAttr(NewAttr); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
Stanislav Mekhanoshin | 89653df | 2017-03-30 20:16:02 +0000 | [diff] [blame] | 91 | ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) { |
| 92 | return new AMDGPUAlwaysInline(GlobalOpt); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 93 | } |