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 | |
| 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> |
| 25 | |
| 26 | #include <clspv/AddressSpace.h> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using std::string; |
| 30 | |
| 31 | #define DEBUG_TYPE "hideconstantloads" |
| 32 | |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | const char* kWrapFunctionPrefix = "clspv.wrap_constant_load."; |
| 37 | |
| 38 | class HideConstantLoadsPass : public ModulePass { |
| 39 | public: |
| 40 | static char ID; |
| 41 | HideConstantLoadsPass() : ModulePass(ID) {} |
| 42 | |
| 43 | bool runOnModule(Module &M) override; |
| 44 | |
| 45 | private: |
| 46 | // Return the name for the wrap function for the given type. |
| 47 | string &WrapFunctionNameForType(Type *type) { |
| 48 | auto where = function_for_type_.find(type); |
| 49 | if (where == function_for_type_.end()) { |
| 50 | // Insert it. |
| 51 | auto &result = function_for_type_[type] = |
| 52 | string(kWrapFunctionPrefix) + |
| 53 | std::to_string(function_for_type_.size()); |
| 54 | return result; |
| 55 | } else { |
| 56 | return where->second; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Maps a loaded type to the name of the wrap function for that type. |
| 61 | DenseMap<Type *, string> function_for_type_; |
| 62 | }; |
| 63 | } // namespace |
| 64 | |
| 65 | char HideConstantLoadsPass::ID = 0; |
| 66 | static RegisterPass<HideConstantLoadsPass> |
| 67 | X("HideConstantLoads", "Hide loads from __constant memory"); |
| 68 | |
| 69 | namespace clspv { |
| 70 | llvm::ModulePass *createHideConstantLoadsPass() { |
| 71 | return new HideConstantLoadsPass(); |
| 72 | } |
| 73 | } // namespace clspv |
| 74 | |
| 75 | |
| 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. |
| 102 | const string& fn_name = WrapFunctionNameForType(loadedTy); |
| 103 | Function* fn = M.getFunction(fn_name); |
| 104 | if (!fn) { |
| 105 | // Make the function. |
| 106 | FunctionType* fnTy = FunctionType::get(loadedTy, {loadedTy}, false); |
| 107 | auto fn_constant = M.getOrInsertFunction(fn_name, fnTy); |
| 108 | fn = cast<Function>(fn_constant); |
| 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 { |
| 136 | public: |
| 137 | static char ID; |
| 138 | UnhideConstantLoadsPass() : ModulePass(ID) {} |
| 139 | |
| 140 | bool runOnModule(Module &M) override; |
| 141 | |
| 142 | private: |
| 143 | |
| 144 | // Maps a loaded type to the name of the wrap function for that type. |
| 145 | DenseMap<Type *, string> function_for_type_; |
| 146 | }; |
| 147 | |
| 148 | } // namespace |
| 149 | |
| 150 | char UnhideConstantLoadsPass::ID = 0; |
| 151 | static RegisterPass<UnhideConstantLoadsPass> |
| 152 | X2("UnhideConstantLoads", "Unhide loads from __constant memory"); |
| 153 | |
| 154 | namespace clspv { |
| 155 | llvm::ModulePass *createUnhideConstantLoadsPass() { |
| 156 | return new UnhideConstantLoadsPass(); |
| 157 | } |
| 158 | } // namespace clspv |
| 159 | |
| 160 | bool UnhideConstantLoadsPass::runOnModule(Module &M) { |
| 161 | bool Changed = false; |
| 162 | |
| 163 | SmallVector<Function *, 16> WorkList; |
| 164 | for (auto& F : M.getFunctionList()) { |
| 165 | if (F.getName().startswith(kWrapFunctionPrefix)) { |
| 166 | WorkList.push_back(&F); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (WorkList.size() == 0) |
| 171 | return Changed; |
| 172 | |
| 173 | SmallVector<CallInst *, 16> RemoveList; |
| 174 | for (auto* F : WorkList) { |
| 175 | for (auto& use : F->uses()) { |
| 176 | if (auto* call = dyn_cast<CallInst>(use.getUser())) { |
| 177 | assert(call->getNumArgOperands() == 1); |
| 178 | auto* load = call->getArgOperand(0); |
| 179 | call->replaceAllUsesWith(load); |
| 180 | RemoveList.push_back(call); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | for (auto* call : RemoveList) { |
| 185 | call->eraseFromParent(); |
| 186 | } |
| 187 | for (auto* F : WorkList) { |
| 188 | F->eraseFromParent(); |
| 189 | } |
| 190 | |
| 191 | return Changed; |
| 192 | } |