| 1 | //===-- SPIRVStripConvergentIntrinsics.cpp ----------------------*- C++ -*-===// |
|---|---|
| 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 | // This pass trims convergence intrinsics as those were only useful when |
| 10 | // modifying the CFG during IR passes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SPIRV.h" |
| 15 | #include "SPIRVSubtarget.h" |
| 16 | #include "SPIRVUtils.h" |
| 17 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| 18 | #include "llvm/IR/IntrinsicInst.h" |
| 19 | #include "llvm/IR/Intrinsics.h" |
| 20 | #include "llvm/Transforms/Utils/Cloning.h" |
| 21 | #include "llvm/Transforms/Utils/LowerMemIntrinsics.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | class SPIRVStripConvergentIntrinsics : public FunctionPass { |
| 27 | public: |
| 28 | static char ID; |
| 29 | |
| 30 | SPIRVStripConvergentIntrinsics() : FunctionPass(ID) {} |
| 31 | |
| 32 | virtual bool runOnFunction(Function &F) override { |
| 33 | DenseSet<Instruction *> ToRemove; |
| 34 | |
| 35 | // Is the instruction is a convergent intrinsic, add it to kill-list and |
| 36 | // returns true. Returns false otherwise. |
| 37 | auto CleanupIntrinsic = [&](IntrinsicInst *II) { |
| 38 | if (II->getIntrinsicID() != Intrinsic::experimental_convergence_entry && |
| 39 | II->getIntrinsicID() != Intrinsic::experimental_convergence_loop && |
| 40 | II->getIntrinsicID() != Intrinsic::experimental_convergence_anchor) |
| 41 | return false; |
| 42 | |
| 43 | II->replaceAllUsesWith(V: UndefValue::get(T: II->getType())); |
| 44 | ToRemove.insert(V: II); |
| 45 | return true; |
| 46 | }; |
| 47 | |
| 48 | // Replace the given CallInst by a similar CallInst with no convergencectrl |
| 49 | // attribute. |
| 50 | auto CleanupCall = [&](CallInst *CI) { |
| 51 | auto OB = CI->getOperandBundle(ID: LLVMContext::OB_convergencectrl); |
| 52 | if (!OB.has_value()) |
| 53 | return; |
| 54 | |
| 55 | auto *NewCall = CallBase::removeOperandBundle( |
| 56 | CB: CI, ID: LLVMContext::OB_convergencectrl, InsertPt: CI->getIterator()); |
| 57 | NewCall->copyMetadata(SrcInst: *CI); |
| 58 | CI->replaceAllUsesWith(V: NewCall); |
| 59 | ToRemove.insert(V: CI); |
| 60 | }; |
| 61 | |
| 62 | for (BasicBlock &BB : F) { |
| 63 | for (Instruction &I : BB) { |
| 64 | if (auto *II = dyn_cast<IntrinsicInst>(Val: &I)) |
| 65 | if (CleanupIntrinsic(II)) |
| 66 | continue; |
| 67 | if (auto *CI = dyn_cast<CallInst>(Val: &I)) |
| 68 | CleanupCall(CI); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // All usages must be removed before their definition is removed. |
| 73 | for (Instruction *I : ToRemove) |
| 74 | I->eraseFromParent(); |
| 75 | |
| 76 | return ToRemove.size() != 0; |
| 77 | } |
| 78 | }; |
| 79 | } // namespace |
| 80 | |
| 81 | char SPIRVStripConvergentIntrinsics::ID = 0; |
| 82 | INITIALIZE_PASS(SPIRVStripConvergentIntrinsics, "strip-convergent-intrinsics", |
| 83 | "SPIRV strip convergent intrinsics", false, false) |
| 84 | |
| 85 | FunctionPass *llvm::createSPIRVStripConvergenceIntrinsicsPass() { |
| 86 | return new SPIRVStripConvergentIntrinsics(); |
| 87 | } |
| 88 |