|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#include <vector> |
| 12 | +#include <set> |
| 13 | + |
| 14 | +#include "rustllvm.h" |
| 15 | + |
| 16 | +#include "llvm/IR/CallSite.h" |
| 17 | +#include "llvm/IR/Module.h" |
| 18 | +#include "llvm/ADT/STLExtras.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | + |
| 22 | +static std::vector<Function*> |
| 23 | +GetFunctionsWithSimdArgs(Module *M) { |
| 24 | + std::vector<Function*> Ret; |
| 25 | + |
| 26 | + for (auto &F : M->functions()) { |
| 27 | + // Skip all intrinsic calls as these are always tightly controlled to "work |
| 28 | + // correctly", so no need to fixup any of these. |
| 29 | + if (F.isIntrinsic()) |
| 30 | + continue; |
| 31 | + |
| 32 | + // We're only interested in rustc-defined functions, not unstably-defined |
| 33 | + // imported SIMD ffi functions. |
| 34 | + if (F.isDeclaration()) |
| 35 | + continue; |
| 36 | + |
| 37 | + // Argument promotion only happens on internal functions, so skip demoting |
| 38 | + // arguments in external functions like FFI shims and such. |
| 39 | + if (!F.hasLocalLinkage()) |
| 40 | + continue; |
| 41 | + |
| 42 | + // If any argument to this function is a by-value vector type, then that's |
| 43 | + // bad! The compiler didn't generate any functions that looked like this, |
| 44 | + // and we try to rely on LLVM to not do this! Argument promotion may, |
| 45 | + // however, promote arguments from behind references. In any case, figure |
| 46 | + // out if we're interested in demoting this argument. |
| 47 | + if (any_of(F.args(), [](Argument &arg) { return arg.getType()->isVectorTy(); })) |
| 48 | + Ret.push_back(&F); |
| 49 | + } |
| 50 | + |
| 51 | + return Ret; |
| 52 | +} |
| 53 | + |
| 54 | +extern "C" void |
| 55 | +LLVMRustDemoteSimdArguments(LLVMModuleRef Mod) { |
| 56 | + Module *M = unwrap(Mod); |
| 57 | + |
| 58 | + auto Functions = GetFunctionsWithSimdArgs(M); |
| 59 | + |
| 60 | + for (auto F : Functions) { |
| 61 | + // Build up our list of new parameters and new argument attributes. |
| 62 | + // We're only changing those arguments which are vector types. |
| 63 | + SmallVector<Type*, 8> Params; |
| 64 | + SmallVector<AttributeSet, 8> ArgAttrVec; |
| 65 | + auto PAL = F->getAttributes(); |
| 66 | + for (auto &Arg : F->args()) { |
| 67 | + auto *Ty = Arg.getType(); |
| 68 | + if (Ty->isVectorTy()) { |
| 69 | + Params.push_back(PointerType::get(Ty, 0)); |
| 70 | + ArgAttrVec.push_back(AttributeSet()); |
| 71 | + } else { |
| 72 | + Params.push_back(Ty); |
| 73 | + ArgAttrVec.push_back(PAL.getParamAttributes(Arg.getArgNo())); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Replace `F` with a new function with our new signature. I'm... not really |
| 78 | + // sure how this works, but this is all the steps `ArgumentPromotion` does |
| 79 | + // to replace a signature as well. |
| 80 | + assert(!F->isVarArg()); // ArgumentPromotion should skip these fns |
| 81 | + FunctionType *NFTy = FunctionType::get(F->getReturnType(), Params, false); |
| 82 | + Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName()); |
| 83 | + NF->copyAttributesFrom(F); |
| 84 | + NF->setSubprogram(F->getSubprogram()); |
| 85 | + F->setSubprogram(nullptr); |
| 86 | + NF->setAttributes(AttributeList::get(F->getContext(), |
| 87 | + PAL.getFnAttributes(), |
| 88 | + PAL.getRetAttributes(), |
| 89 | + ArgAttrVec)); |
| 90 | + ArgAttrVec.clear(); |
| 91 | + F->getParent()->getFunctionList().insert(F->getIterator(), NF); |
| 92 | + NF->takeName(F); |
| 93 | + |
| 94 | + // Iterate over all invocations of `F`, updating all `call` instructions to |
| 95 | + // store immediate vector types in a local `alloc` instead of a by-value |
| 96 | + // vector. |
| 97 | + // |
| 98 | + // Like before, much of this is copied from the `ArgumentPromotion` pass in |
| 99 | + // LLVM. |
| 100 | + SmallVector<Value*, 16> Args; |
| 101 | + while (!F->use_empty()) { |
| 102 | + CallSite CS(F->user_back()); |
| 103 | + assert(CS.getCalledFunction() == F); |
| 104 | + Instruction *Call = CS.getInstruction(); |
| 105 | + const AttributeList &CallPAL = CS.getAttributes(); |
| 106 | + |
| 107 | + // Loop over the operands, inserting an `alloca` and a store for any |
| 108 | + // argument we're demoting to be by reference |
| 109 | + // |
| 110 | + // FIXME: we probably want to figure out an LLVM pass to run and clean up |
| 111 | + // this function and instructions we're generating, we should in theory |
| 112 | + // only generate a maximum number of `alloca` instructions rather than |
| 113 | + // one-per-variable unconditionally. |
| 114 | + CallSite::arg_iterator AI = CS.arg_begin(); |
| 115 | + size_t ArgNo = 0; |
| 116 | + for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; |
| 117 | + ++I, ++AI, ++ArgNo) { |
| 118 | + if (I->getType()->isVectorTy()) { |
| 119 | + AllocaInst *AllocA = new AllocaInst(I->getType(), 0, nullptr, "", Call); |
| 120 | + new StoreInst(*AI, AllocA, Call); |
| 121 | + Args.push_back(AllocA); |
| 122 | + ArgAttrVec.push_back(AttributeSet()); |
| 123 | + } else { |
| 124 | + Args.push_back(*AI); |
| 125 | + ArgAttrVec.push_back(CallPAL.getParamAttributes(ArgNo)); |
| 126 | + } |
| 127 | + } |
| 128 | + assert(AI == CS.arg_end()); |
| 129 | + |
| 130 | + // Create a new call instructions which we'll use to replace the old call |
| 131 | + // instruction, copying over as many attributes and such as possible. |
| 132 | + SmallVector<OperandBundleDef, 1> OpBundles; |
| 133 | + CS.getOperandBundlesAsDefs(OpBundles); |
| 134 | + |
| 135 | + CallSite NewCS; |
| 136 | + if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
| 137 | + InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), |
| 138 | + Args, OpBundles, "", Call); |
| 139 | + } else { |
| 140 | + auto *NewCall = CallInst::Create(NF, Args, OpBundles, "", Call); |
| 141 | + NewCall->setTailCallKind(cast<CallInst>(Call)->getTailCallKind()); |
| 142 | + NewCS = NewCall; |
| 143 | + } |
| 144 | + NewCS.setCallingConv(CS.getCallingConv()); |
| 145 | + NewCS.setAttributes( |
| 146 | + AttributeList::get(F->getContext(), CallPAL.getFnAttributes(), |
| 147 | + CallPAL.getRetAttributes(), ArgAttrVec)); |
| 148 | + NewCS->setDebugLoc(Call->getDebugLoc()); |
| 149 | + Args.clear(); |
| 150 | + ArgAttrVec.clear(); |
| 151 | + Call->replaceAllUsesWith(NewCS.getInstruction()); |
| 152 | + NewCS->takeName(Call); |
| 153 | + Call->eraseFromParent(); |
| 154 | + } |
| 155 | + |
| 156 | + // Splice the body of the old function right into the new function. |
| 157 | + NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); |
| 158 | + |
| 159 | + // Update our new function to replace all uses of the by-value argument with |
| 160 | + // loads of the pointer argument we've generated. |
| 161 | + // |
| 162 | + // FIXME: we probably want to only generate one load instruction per |
| 163 | + // function? Or maybe run an LLVM pass to clean up this function? |
| 164 | + for (Function::arg_iterator I = F->arg_begin(), |
| 165 | + E = F->arg_end(), |
| 166 | + I2 = NF->arg_begin(); |
| 167 | + I != E; |
| 168 | + ++I, ++I2) { |
| 169 | + if (I->getType()->isVectorTy()) { |
| 170 | + I->replaceAllUsesWith(new LoadInst(&*I2, "", &NF->begin()->front())); |
| 171 | + } else { |
| 172 | + I->replaceAllUsesWith(&*I2); |
| 173 | + } |
| 174 | + I2->takeName(&*I); |
| 175 | + } |
| 176 | + |
| 177 | + // Delete all references to the old function, it should be entirely dead |
| 178 | + // now. |
| 179 | + M->getFunctionList().remove(F); |
| 180 | + } |
| 181 | +} |
0 commit comments