David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <string> |
| 16 | |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/IR/Attributes.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 25 | |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 26 | #include "clspv/AddressSpace.h" |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 27 | |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame^] | 28 | #include "Passes.h" |
| 29 | |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | using std::string; |
| 32 | |
| 33 | #define DEBUG_TYPE "hideconstantloads" |
| 34 | |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 35 | namespace { |
| 36 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 37 | const char *kWrapFunctionPrefix = "clspv.wrap_constant_load."; |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 38 | |
| 39 | class HideConstantLoadsPass : public ModulePass { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 40 | public: |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 41 | static char ID; |
| 42 | HideConstantLoadsPass() : ModulePass(ID) {} |
| 43 | |
| 44 | bool runOnModule(Module &M) override; |
| 45 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 46 | private: |
| 47 | // Return the name for the wrap function for the given type. |
| 48 | string &WrapFunctionNameForType(Type *type) { |
| 49 | auto where = function_for_type_.find(type); |
| 50 | if (where == function_for_type_.end()) { |
| 51 | // Insert it. |
| 52 | auto &result = function_for_type_[type] = |
| 53 | string(kWrapFunctionPrefix) + |
| 54 | std::to_string(function_for_type_.size()); |
| 55 | return result; |
| 56 | } else { |
| 57 | return where->second; |
| 58 | } |
| 59 | } |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 60 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 61 | // Maps a loaded type to the name of the wrap function for that type. |
| 62 | DenseMap<Type *, string> function_for_type_; |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 63 | }; |
| 64 | } // namespace |
| 65 | |
| 66 | char HideConstantLoadsPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame^] | 67 | INITIALIZE_PASS(HideConstantLoadsPass, "HideConstantLoads", |
| 68 | "Hide loads from __constant memory", false, false) |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 69 | |
| 70 | namespace clspv { |
| 71 | llvm::ModulePass *createHideConstantLoadsPass() { |
| 72 | return new HideConstantLoadsPass(); |
| 73 | } |
| 74 | } // namespace clspv |
| 75 | |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 76 | bool HideConstantLoadsPass::runOnModule(Module &M) { |
| 77 | bool Changed = false; |
| 78 | |
| 79 | SmallVector<LoadInst *, 16> WorkList; |
| 80 | for (Function &F : M) { |
| 81 | for (BasicBlock &BB : F) { |
| 82 | for (Instruction &I : BB) { |
| 83 | if (LoadInst *load = dyn_cast<LoadInst>(&I)) { |
| 84 | if (clspv::AddressSpace::Constant == load->getPointerAddressSpace()) { |
| 85 | WorkList.push_back(load); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (WorkList.size() == 0) { |
| 93 | return Changed; |
| 94 | } |
| 95 | |
| 96 | for (LoadInst *load : WorkList) { |
| 97 | Changed = true; |
| 98 | |
| 99 | auto loadedTy = load->getType(); |
| 100 | |
| 101 | // The wrap function conceptually maps the loaded value to itself. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 102 | const string &fn_name = WrapFunctionNameForType(loadedTy); |
| 103 | Function *fn = M.getFunction(fn_name); |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 104 | if (!fn) { |
| 105 | // Make the function. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 106 | FunctionType *fnTy = FunctionType::get(loadedTy, {loadedTy}, false); |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 107 | auto fn_constant = M.getOrInsertFunction(fn_name, fnTy); |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 108 | fn = cast<Function>(fn_constant.getCallee()); |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 109 | fn->addFnAttr(Attribute::ReadOnly); |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Wrap the load |
| 113 | auto call = CallInst::Create(fn, {load}); |
| 114 | call->insertAfter(load); |
| 115 | |
| 116 | // Replace other uses of the load with the result of the wrap call. |
| 117 | { |
| 118 | SmallVector<User *, 16> ToReplaceIn; |
| 119 | for (auto &use : load->uses()) { |
| 120 | User *user = use.getUser(); |
| 121 | ToReplaceIn.push_back(user); |
| 122 | } |
| 123 | for (auto *user : ToReplaceIn) { |
| 124 | if (dyn_cast<CallInst>(user) != call) { |
| 125 | user->replaceUsesOfWith(load, call); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return Changed; |
| 132 | } |
| 133 | |
| 134 | namespace { |
| 135 | class UnhideConstantLoadsPass : public ModulePass { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 136 | public: |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 137 | static char ID; |
| 138 | UnhideConstantLoadsPass() : ModulePass(ID) {} |
| 139 | |
| 140 | bool runOnModule(Module &M) override; |
| 141 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 142 | private: |
| 143 | // Maps a loaded type to the name of the wrap function for that type. |
| 144 | DenseMap<Type *, string> function_for_type_; |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace |
| 148 | |
| 149 | char UnhideConstantLoadsPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame^] | 150 | INITIALIZE_PASS(UnhideConstantLoadsPass, "UnhideConstantLoads", |
| 151 | "Unhide loads from __constant memory", false, false) |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 152 | |
| 153 | namespace clspv { |
| 154 | llvm::ModulePass *createUnhideConstantLoadsPass() { |
| 155 | return new UnhideConstantLoadsPass(); |
| 156 | } |
| 157 | } // namespace clspv |
| 158 | |
| 159 | bool UnhideConstantLoadsPass::runOnModule(Module &M) { |
| 160 | bool Changed = false; |
| 161 | |
| 162 | SmallVector<Function *, 16> WorkList; |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 163 | for (auto &F : M.getFunctionList()) { |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 164 | if (F.getName().startswith(kWrapFunctionPrefix)) { |
| 165 | WorkList.push_back(&F); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (WorkList.size() == 0) |
| 170 | return Changed; |
| 171 | |
| 172 | SmallVector<CallInst *, 16> RemoveList; |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 173 | for (auto *F : WorkList) { |
| 174 | for (auto &use : F->uses()) { |
| 175 | if (auto *call = dyn_cast<CallInst>(use.getUser())) { |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 176 | assert(call->getNumArgOperands() == 1); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 177 | auto *load = call->getArgOperand(0); |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 178 | call->replaceAllUsesWith(load); |
| 179 | RemoveList.push_back(call); |
| 180 | } |
| 181 | } |
| 182 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 183 | for (auto *call : RemoveList) { |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 184 | call->eraseFromParent(); |
| 185 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 186 | for (auto *F : WorkList) { |
David Neto | 6373d82 | 2017-10-12 19:09:53 -0400 | [diff] [blame] | 187 | F->eraseFromParent(); |
| 188 | } |
| 189 | |
| 190 | return Changed; |
| 191 | } |