|
| 1 | +/*========================== begin_copyright_notice ============================ |
| 2 | +
|
| 3 | +Copyright (C) 2023 Intel Corporation |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT |
| 6 | +
|
| 7 | +============================= end_copyright_notice ===========================*/ |
| 8 | + |
| 9 | +#include "common/LLVMWarningsPush.hpp" |
| 10 | +#include <llvm/IR/Instructions.h> |
| 11 | +#include <llvm/IR/InstVisitor.h> |
| 12 | +#include <llvm/IR/Operator.h> |
| 13 | +#include "common/LLVMWarningsPop.hpp" |
| 14 | +#include "FastMathConstantHandling.h" |
| 15 | +#include "Compiler/IGCPassSupport.h" |
| 16 | +#include "common/igc_regkeys.hpp" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | +namespace IGC |
| 20 | +{ |
| 21 | + |
| 22 | +class FastMathConstantHandling : public FunctionPass, public InstVisitor<FastMathConstantHandling> |
| 23 | +{ |
| 24 | +public: |
| 25 | + FastMathConstantHandling(); |
| 26 | + ~FastMathConstantHandling() {} |
| 27 | + static char ID; |
| 28 | + bool runOnFunction(Function& F) override; |
| 29 | + void visitInstruction(Instruction& I); |
| 30 | + virtual llvm::StringRef getPassName() const override { return "Fast Math Constant Handling"; } |
| 31 | + |
| 32 | + void getAnalysisUsage(AnalysisUsage& AU) const override |
| 33 | + { |
| 34 | + AU.setPreservesCFG(); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +#define PASS_FLAG "FastMathConstantHandling" |
| 39 | +#define PASS_DESC "Fast Math Constant Handling" |
| 40 | +#define PASS_CFG_ONLY false |
| 41 | +#define PASS_ANALYSIS false |
| 42 | +IGC_INITIALIZE_PASS_BEGIN(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 43 | +IGC_INITIALIZE_PASS_END(FastMathConstantHandling, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 44 | + |
| 45 | +char FastMathConstantHandling::ID = 0; |
| 46 | + |
| 47 | +FastMathConstantHandling::FastMathConstantHandling() |
| 48 | + :FunctionPass(ID) |
| 49 | +{ |
| 50 | + initializeFastMathConstantHandlingPass(*PassRegistry::getPassRegistry()); |
| 51 | +} |
| 52 | + |
| 53 | +// This purpose of this pass is to catch bad fast flag management where we are seeing |
| 54 | +// constants that are not matching the fast flags that are set on the instructions |
| 55 | + |
| 56 | +void FastMathConstantHandling::visitInstruction(Instruction& I) |
| 57 | +{ |
| 58 | + if (isa<FPMathOperator>(I)) |
| 59 | + { |
| 60 | + struct BoolSpecialConstants { |
| 61 | + bool hasInf = false; |
| 62 | + bool hasNan = false; |
| 63 | + bool hasNegZero = false; |
| 64 | + } BSC; |
| 65 | + |
| 66 | + for (auto &Op : I.operands()) |
| 67 | + { |
| 68 | + if (auto* fp_val = dyn_cast<llvm::ConstantFP>(Op)) |
| 69 | + { |
| 70 | + auto APF = fp_val->getValueAPF(); |
| 71 | + BSC.hasInf |= APF.isInfinity(); |
| 72 | + BSC.hasNan |= APF.isNaN(); |
| 73 | + BSC.hasNegZero |= APF.isNegZero(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (BSC.hasInf) |
| 78 | + I.setHasNoInfs(false); |
| 79 | + |
| 80 | + if (BSC.hasNan) |
| 81 | + I.setHasNoNaNs(false); |
| 82 | + |
| 83 | + if (BSC.hasNegZero) |
| 84 | + I.setHasNoSignedZeros(false); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +bool FastMathConstantHandling::runOnFunction(Function& F) |
| 89 | +{ |
| 90 | + if (IGC_IS_FLAG_DISABLED(DisableFastMathConstantHandling)) |
| 91 | + { |
| 92 | + visit(F); |
| 93 | + } |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +FunctionPass* createFastMathConstantHandling() |
| 99 | +{ |
| 100 | + return new FastMathConstantHandling(); |
| 101 | +} |
| 102 | + |
| 103 | +}//namespace IGC |
0 commit comments