|
| 1 | +/*===================== begin_copyright_notice ================================== |
| 2 | +
|
| 3 | +Copyright (c) 2017 Intel Corporation |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | +copy of this software and associated documentation files (the |
| 7 | +"Software"), to deal in the Software without restriction, including |
| 8 | +without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +permit persons to whom the Software is furnished to do so, subject to |
| 11 | +the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included |
| 14 | +in all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +
|
| 24 | +
|
| 25 | +======================= end_copyright_notice ==================================*/ |
| 26 | + |
| 27 | +#include "Compiler/CISACodeGen/CrossPhaseConstProp.hpp" |
| 28 | +#include "LLVMWarningsPush.hpp" |
| 29 | +#include "llvm/Pass.h" |
| 30 | +#include "LLVMWarningsPop.hpp" |
| 31 | +#include "Compiler/CISACodeGen/PixelShaderCodeGen.hpp" |
| 32 | +#include "Compiler/CISACodeGen/helper.h" |
| 33 | +#include "Compiler/MetaDataUtilsWrapper.h" |
| 34 | +using namespace llvm; |
| 35 | +namespace |
| 36 | +{ |
| 37 | + class CrossPhaseConstProp : public FunctionPass |
| 38 | + { |
| 39 | + public: |
| 40 | + static char ID; // Pass identification, replacement for typeid |
| 41 | + |
| 42 | + CrossPhaseConstProp(IGC::PSSignature* signature=nullptr); |
| 43 | + |
| 44 | + bool runOnFunction(Function& F) override; |
| 45 | + |
| 46 | + void getAnalysisUsage(AnalysisUsage& AU) const override |
| 47 | + { |
| 48 | + AU.addRequired<IGC::MetaDataUtilsWrapper>(); |
| 49 | + AU.setPreservesCFG(); |
| 50 | + } |
| 51 | + private: |
| 52 | + IGC::PSSignature* m_signature; |
| 53 | + }; |
| 54 | +} // namespace |
| 55 | + |
| 56 | + |
| 57 | +#define PASS_FLAG "igc-crossphaseconstprop" |
| 58 | +#define PASS_DESCRIPTION "Special const prop for multirate shading" |
| 59 | +#define PASS_CFG_ONLY false |
| 60 | +#define PASS_ANALYSIS false |
| 61 | +IGC_INITIALIZE_PASS_BEGIN(CrossPhaseConstProp, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 62 | +IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper) |
| 63 | +IGC_INITIALIZE_PASS_END(CrossPhaseConstProp, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS) |
| 64 | + |
| 65 | +CrossPhaseConstProp::CrossPhaseConstProp(IGC::PSSignature* signature) : FunctionPass(ID), m_signature(signature) |
| 66 | +{ |
| 67 | + initializeCrossPhaseConstPropPass(*PassRegistry::getPassRegistry()); |
| 68 | +} |
| 69 | + |
| 70 | +char CrossPhaseConstProp::ID = 0; |
| 71 | + |
| 72 | +FunctionPass* IGC::createCrossPhaseConstPropPass(IGC::PSSignature* signature) |
| 73 | +{ |
| 74 | + return new CrossPhaseConstProp(signature); |
| 75 | +} |
| 76 | + |
| 77 | +bool CrossPhaseConstProp::runOnFunction(Function& F) |
| 78 | +{ |
| 79 | + IGC::IGCMD::MetaDataUtils* pMdUtils = getAnalysis<IGC::MetaDataUtilsWrapper>().getMetaDataUtils(); |
| 80 | + if (!IGC::isEntryFunc(pMdUtils, &F) || m_signature == nullptr) |
| 81 | + { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + std::vector<Instruction*> instructionsToRemove; |
| 86 | + bool isCoarse = IGC::isCoarsePhaseFunction(&F); |
| 87 | + bool isPerPixel = IGC::isPixelPhaseFunction(&F); |
| 88 | + if (isCoarse || isPerPixel) |
| 89 | + { |
| 90 | + for (auto& BB : F) |
| 91 | + { |
| 92 | + for (auto& I : BB) |
| 93 | + { |
| 94 | + if (GenIntrinsicInst * inst = dyn_cast<GenIntrinsicInst>(&I)) |
| 95 | + { |
| 96 | + GenISAIntrinsic::ID IID = inst->getIntrinsicID(); |
| 97 | + if (isPerPixel && IID == GenISAIntrinsic::GenISA_PHASE_INPUT) |
| 98 | + { |
| 99 | + unsigned int index = (unsigned int)cast<llvm::ConstantInt>(inst->getOperand(0))->getZExtValue(); |
| 100 | + auto constantOutput = m_signature->PSConstantOutput.find(index); |
| 101 | + if (constantOutput != m_signature->PSConstantOutput.end()) |
| 102 | + { |
| 103 | + inst->replaceAllUsesWith( |
| 104 | + ConstantFP::get(inst->getType(), |
| 105 | + APFloat(inst->getType()->getFltSemantics(), APInt(inst->getType()->getScalarSizeInBits(), constantOutput->second)))); |
| 106 | + instructionsToRemove.push_back(inst); |
| 107 | + } |
| 108 | + } |
| 109 | + else if (isCoarse && IID == GenISAIntrinsic::GenISA_PHASE_OUTPUT) |
| 110 | + { |
| 111 | + if (auto fpValue = dyn_cast<ConstantFP>(inst->getArgOperand(0))) |
| 112 | + { |
| 113 | + unsigned int index = (unsigned int)cast<llvm::ConstantInt>(inst->getArgOperand(1))->getZExtValue(); |
| 114 | + m_signature->PSConstantOutput[index] = fpValue->getValueAPF().bitcastToAPInt().getLimitedValue(); |
| 115 | + instructionsToRemove.push_back(inst); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + for (auto inst : instructionsToRemove) |
| 123 | + { |
| 124 | + inst->eraseFromParent(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return instructionsToRemove.size() > 0; |
| 129 | +} |
0 commit comments