Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 9 | // The kernel divergence analysis is an LLVM pass which can be used to find out |
| 10 | // if a branch instruction in a GPU program (kernel) is divergent or not. It can help |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 11 | // branch optimizations such as jump threading and loop unswitching to make |
| 12 | // better decisions. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 15 | #ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |
| 16 | #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | |
| 18 | #include "llvm/ADT/DenseSet.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 20 | #include <memory> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 23 | class Function; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 24 | class GPUDivergenceAnalysis; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 25 | class Module; |
| 26 | class raw_ostream; |
| 27 | class TargetTransformInfo; |
| 28 | class Use; |
| 29 | class Value; |
| 30 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 31 | class LegacyDivergenceAnalysis : public FunctionPass { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | public: |
| 33 | static char ID; |
| 34 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 35 | LegacyDivergenceAnalysis(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
| 37 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 38 | |
| 39 | bool runOnFunction(Function &F) override; |
| 40 | |
| 41 | // Print all divergent branches in the function. |
| 42 | void print(raw_ostream &OS, const Module *) const override; |
| 43 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 44 | // Returns true if V is divergent at its definition. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 45 | bool isDivergent(const Value *V) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 47 | // Returns true if U is divergent. Uses of a uniform value can be divergent. |
| 48 | bool isDivergentUse(const Use *U) const; |
| 49 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | // Returns true if V is uniform/non-divergent. |
| 51 | bool isUniform(const Value *V) const { return !isDivergent(V); } |
| 52 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 53 | // Returns true if U is uniform/non-divergent. Uses of a uniform value can be |
| 54 | // divergent. |
| 55 | bool isUniformUse(const Use *U) const { return !isDivergentUse(U); } |
| 56 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 57 | // Keep the analysis results uptodate by removing an erased value. |
| 58 | void removeValue(const Value *V) { DivergentValues.erase(V); } |
| 59 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 60 | private: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 61 | // Whether analysis should be performed by GPUDivergenceAnalysis. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 62 | bool shouldUseGPUDivergenceAnalysis(const Function &F, |
| 63 | const TargetTransformInfo &TTI) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 64 | |
| 65 | // (optional) handle to new DivergenceAnalysis |
| 66 | std::unique_ptr<GPUDivergenceAnalysis> gpuDA; |
| 67 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | // Stores all divergent values. |
| 69 | DenseSet<const Value *> DivergentValues; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 70 | |
| 71 | // Stores divergent uses of possibly uniform values. |
| 72 | DenseSet<const Use *> DivergentUses; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | }; |
| 74 | } // End llvm namespace |
| 75 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 76 | #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |