| 1 | //===-- AMDGPUUniformIntrinsicCombine.cpp ---------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// This pass simplifies certain intrinsic calls when the arguments are uniform. |
| 11 | /// It's true that this pass has transforms that can lead to a situation where |
| 12 | /// some instruction whose operand was previously recognized as statically |
| 13 | /// uniform is later on no longer recognized as statically uniform. However, the |
| 14 | /// semantics of how programs execute don't (and must not, for this precise |
| 15 | /// reason) care about static uniformity, they only ever care about dynamic |
| 16 | /// uniformity. And every instruction that's downstream and cares about dynamic |
| 17 | /// uniformity must be convergent (and isel will introduce v_readfirstlane for |
| 18 | /// them if their operands can't be proven statically uniform). |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "AMDGPU.h" |
| 22 | #include "llvm/Analysis/LoopInfo.h" |
| 23 | #include "llvm/Analysis/ScalarEvolution.h" |
| 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 25 | #include "llvm/Analysis/UniformityAnalysis.h" |
| 26 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 27 | #include "llvm/IR/IRBuilder.h" |
| 28 | #include "llvm/IR/InstIterator.h" |
| 29 | #include "llvm/IR/IntrinsicsAMDGPU.h" |
| 30 | #include "llvm/IR/PatternMatch.h" |
| 31 | #include "llvm/InitializePasses.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
| 33 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 34 | |
| 35 | #define DEBUG_TYPE "amdgpu-uniform-intrinsic-combine" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::AMDGPU; |
| 39 | using namespace llvm::PatternMatch; |
| 40 | |
| 41 | /// Wrapper for querying uniformity info that first checks locally tracked |
| 42 | /// instructions. |
| 43 | static bool |
| 44 | isDivergentUseWithNew(const Use &U, const UniformityInfo &UI, |
| 45 | const ValueMap<const Value *, bool> &Tracker) { |
| 46 | Value *V = U.get(); |
| 47 | if (auto It = Tracker.find(Val: V); It != Tracker.end()) |
| 48 | return !It->second; // divergent if marked false |
| 49 | return UI.isDivergentAtUse(U); |
| 50 | } |
| 51 | |
| 52 | /// Optimizes uniform intrinsics calls if their operand can be proven uniform. |
| 53 | static bool optimizeUniformIntrinsic(IntrinsicInst &II, UniformityInfo &UI, |
| 54 | ValueMap<const Value *, bool> &Tracker) { |
| 55 | llvm::Intrinsic::ID IID = II.getIntrinsicID(); |
| 56 | /// We deliberately do not simplify readfirstlane with a uniform argument, so |
| 57 | /// that frontends can use it to force a copy to SGPR and thereby prevent the |
| 58 | /// backend from generating unwanted waterfall loops. |
| 59 | switch (IID) { |
| 60 | case Intrinsic::amdgcn_permlane64: |
| 61 | case Intrinsic::amdgcn_readlane: { |
| 62 | Value *Src = II.getArgOperand(i: 0); |
| 63 | if (isDivergentUseWithNew(U: II.getOperandUse(i: 0), UI, Tracker)) |
| 64 | return false; |
| 65 | LLVM_DEBUG(dbgs() << "Replacing " << II << " with " << *Src << '\n'); |
| 66 | II.replaceAllUsesWith(V: Src); |
| 67 | UI.forgetValue(V: &II); |
| 68 | II.eraseFromParent(); |
| 69 | return true; |
| 70 | } |
| 71 | case Intrinsic::amdgcn_ballot: { |
| 72 | Value *Src = II.getArgOperand(i: 0); |
| 73 | if (isDivergentUseWithNew(U: II.getOperandUse(i: 0), UI, Tracker)) |
| 74 | return false; |
| 75 | LLVM_DEBUG(dbgs() << "Found uniform ballot intrinsic: " << II << '\n'); |
| 76 | |
| 77 | bool Changed = false; |
| 78 | for (User *U : make_early_inc_range(Range: II.users())) { |
| 79 | if (auto *ICmp = dyn_cast<ICmpInst>(Val: U)) { |
| 80 | Value *Op0 = ICmp->getOperand(i_nocapture: 0); |
| 81 | Value *Op1 = ICmp->getOperand(i_nocapture: 1); |
| 82 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
| 83 | Value *OtherOp = Op0 == &II ? Op1 : Op0; |
| 84 | |
| 85 | if (Pred == ICmpInst::ICMP_EQ && match(V: OtherOp, P: m_Zero())) { |
| 86 | // Case: (icmp eq %ballot, 0) -> xor %ballot_arg, 1 |
| 87 | Instruction *NotOp = |
| 88 | BinaryOperator::CreateNot(Op: Src, Name: "" , InsertBefore: ICmp->getIterator()); |
| 89 | Tracker[NotOp] = true; // NOT preserves uniformity |
| 90 | LLVM_DEBUG(dbgs() << "Replacing ICMP_EQ: " << *NotOp << '\n'); |
| 91 | ICmp->replaceAllUsesWith(V: NotOp); |
| 92 | Changed = true; |
| 93 | } else if (Pred == ICmpInst::ICMP_NE && match(V: OtherOp, P: m_Zero())) { |
| 94 | // Case: (icmp ne %ballot, 0) -> %ballot_arg |
| 95 | LLVM_DEBUG(dbgs() << "Replacing ICMP_NE with ballot argument: " |
| 96 | << *Src << '\n'); |
| 97 | ICmp->replaceAllUsesWith(V: Src); |
| 98 | Changed = true; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | // Erase the intrinsic if it has no remaining uses. |
| 103 | if (II.use_empty()) { |
| 104 | UI.forgetValue(V: &II); |
| 105 | II.eraseFromParent(); |
| 106 | Changed = true; |
| 107 | } |
| 108 | return Changed; |
| 109 | } |
| 110 | case Intrinsic::amdgcn_wave_shuffle: { |
| 111 | Use &Val = II.getOperandUse(i: 0); |
| 112 | Use &Idx = II.getOperandUse(i: 1); |
| 113 | |
| 114 | // Like with readlane, if Value is uniform then just propagate it |
| 115 | if (!isDivergentUseWithNew(U: Val, UI, Tracker)) { |
| 116 | II.replaceAllUsesWith(V: Val); |
| 117 | UI.forgetValue(V: &II); |
| 118 | II.eraseFromParent(); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // Otherwise, when Index is uniform, this is just a readlane operation |
| 123 | if (isDivergentUseWithNew(U: Idx, UI, Tracker)) |
| 124 | return false; |
| 125 | |
| 126 | // The readlane intrinsic we want to call has the exact same function |
| 127 | // signature, so we can quickly modify the instruction in-place |
| 128 | Module *Mod = II.getModule(); |
| 129 | II.setCalledFunction(Intrinsic::getOrInsertDeclaration( |
| 130 | M: Mod, id: Intrinsic::amdgcn_readlane, OverloadTys: II.getType())); |
| 131 | return true; |
| 132 | } |
| 133 | default: |
| 134 | return false; |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /// Iterates over intrinsic calls in the Function to optimize. |
| 140 | static bool runUniformIntrinsicCombine(Function &F, UniformityInfo &UI) { |
| 141 | bool IsChanged = false; |
| 142 | ValueMap<const Value *, bool> Tracker; |
| 143 | |
| 144 | for (Instruction &I : make_early_inc_range(Range: instructions(F))) { |
| 145 | auto *II = dyn_cast<IntrinsicInst>(Val: &I); |
| 146 | if (!II) |
| 147 | continue; |
| 148 | IsChanged |= optimizeUniformIntrinsic(II&: *II, UI, Tracker); |
| 149 | } |
| 150 | return IsChanged; |
| 151 | } |
| 152 | |
| 153 | PreservedAnalyses |
| 154 | AMDGPUUniformIntrinsicCombinePass::run(Function &F, |
| 155 | FunctionAnalysisManager &AM) { |
| 156 | UniformityInfo &UI = AM.getResult<UniformityInfoAnalysis>(IR&: F); |
| 157 | if (!runUniformIntrinsicCombine(F, UI)) |
| 158 | return PreservedAnalyses::all(); |
| 159 | |
| 160 | PreservedAnalyses PA; |
| 161 | PA.preserveSet<CFGAnalyses>(); |
| 162 | return PA; |
| 163 | } |
| 164 | |
| 165 | namespace { |
| 166 | class AMDGPUUniformIntrinsicCombineLegacy : public FunctionPass { |
| 167 | public: |
| 168 | static char ID; |
| 169 | AMDGPUUniformIntrinsicCombineLegacy() : FunctionPass(ID) {} |
| 170 | |
| 171 | private: |
| 172 | bool runOnFunction(Function &F) override; |
| 173 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 174 | AU.setPreservesCFG(); |
| 175 | AU.addRequired<UniformityInfoWrapperPass>(); |
| 176 | AU.addRequired<TargetPassConfig>(); |
| 177 | } |
| 178 | }; |
| 179 | } // namespace |
| 180 | |
| 181 | char AMDGPUUniformIntrinsicCombineLegacy::ID = 0; |
| 182 | char &llvm::AMDGPUUniformIntrinsicCombineLegacyPassID = |
| 183 | AMDGPUUniformIntrinsicCombineLegacy::ID; |
| 184 | |
| 185 | bool AMDGPUUniformIntrinsicCombineLegacy::runOnFunction(Function &F) { |
| 186 | if (skipFunction(F)) |
| 187 | return false; |
| 188 | UniformityInfo &UI = |
| 189 | getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo(); |
| 190 | return runUniformIntrinsicCombine(F, UI); |
| 191 | } |
| 192 | |
| 193 | INITIALIZE_PASS_BEGIN(AMDGPUUniformIntrinsicCombineLegacy, DEBUG_TYPE, |
| 194 | "AMDGPU Uniform Intrinsic Combine" , false, false) |
| 195 | INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass) |
| 196 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
| 197 | INITIALIZE_PASS_END(AMDGPUUniformIntrinsicCombineLegacy, DEBUG_TYPE, |
| 198 | "AMDGPU Uniform Intrinsic Combine" , false, false) |
| 199 | |
| 200 | FunctionPass *llvm::createAMDGPUUniformIntrinsicCombineLegacyPass() { |
| 201 | return new AMDGPUUniformIntrinsicCombineLegacy(); |
| 202 | } |
| 203 | |