1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #include "Compiler/IGCPassSupport.h"
10 #include "common/LLVMWarningsPush.hpp"
11 #include "llvm/IR/InstIterator.h"
12 #include "common/LLVMWarningsPop.hpp"
13 #include "GenISAIntrinsics/GenIntrinsicInst.h"
14 #include "Compiler/CodeGenPublicEnums.h"
15 #include "Compiler/CISACodeGen/helper.h"
16 
17 #include <llvm/IR/PassManager.h>
18 
19 using namespace IGC;
20 using namespace llvm;
21 
22 class RemoveNonPositionOutput : public llvm::FunctionPass
23 {
24 public:
25     static char ID;
26 
27     RemoveNonPositionOutput();
28 
~RemoveNonPositionOutput()29     ~RemoveNonPositionOutput() {}
30 
getAnalysisUsage(llvm::AnalysisUsage & AU) const31     virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override
32     {
33         AU.setPreservesCFG();
34     }
35 
36     virtual bool runOnFunction(llvm::Function& F) override;
37 
getPassName() const38     virtual llvm::StringRef getPassName() const override
39     {
40         return "remove non-position output in vertex shader";
41     }
42 };
43 
createRemoveNonPositionOutputPass()44 llvm::FunctionPass* createRemoveNonPositionOutputPass()
45 {
46     return new RemoveNonPositionOutput();
47 }
48 
49 // Register pass to igc-opt
50 #define PASS_FLAG_POSH "igc-remove-nonposition-output"
51 #define PASS_DESCRIPTION_POSH "Custom Pass for Position-Only Shader"
52 #define PASS_CFG_ONLY_POSH false
53 #define PASS_ANALYSIS_POSH false
54 IGC_INITIALIZE_PASS_BEGIN(RemoveNonPositionOutput, PASS_FLAG_POSH, PASS_DESCRIPTION_POSH, PASS_CFG_ONLY_POSH, PASS_ANALYSIS_POSH)
55 IGC_INITIALIZE_PASS_END(RemoveNonPositionOutput, PASS_FLAG_POSH, PASS_DESCRIPTION_POSH, PASS_CFG_ONLY_POSH, PASS_ANALYSIS_POSH)
56 
57 char RemoveNonPositionOutput::ID = 0;
58 
RemoveNonPositionOutput()59 RemoveNonPositionOutput::RemoveNonPositionOutput() : FunctionPass(ID)
60 {
61     initializeRemoveNonPositionOutputPass(*PassRegistry::getPassRegistry());
62 }
63 
runOnFunction(Function & F)64 bool RemoveNonPositionOutput::runOnFunction(Function& F)
65 {
66     // Initialize the worklist to all of the instructions ready to process...
67     SmallVector<Instruction*, 10> instructionToRemove;
68     for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II)
69     {
70         if (GenIntrinsicInst * inst = dyn_cast<GenIntrinsicInst>(&*II))
71         {
72             if (inst->getIntrinsicID() == GenISAIntrinsic::GenISA_OUTPUT)
73             {
74                 const ShaderOutputType usage = static_cast<ShaderOutputType>(
75                     llvm::cast<llvm::ConstantInt>(inst->getOperand(4))->getZExtValue());
76                 if (usage != SHADER_OUTPUT_TYPE_POSITION &&
77                     usage != SHADER_OUTPUT_TYPE_POINTWIDTH &&
78                     usage != SHADER_OUTPUT_TYPE_VIEWPORT_ARRAY_INDEX &&
79                     usage != SHADER_OUTPUT_TYPE_CLIPDISTANCE_LO &&
80                     usage != SHADER_OUTPUT_TYPE_CLIPDISTANCE_HI)
81                 {
82                     instructionToRemove.push_back(inst);
83                 }
84             }
85         }
86     }
87     bool changed = false;
88     uint num = instructionToRemove.size();
89     for (uint i = 0; i < num; ++i)
90     {
91         instructionToRemove[i]->eraseFromParent();
92         changed = true;
93     }
94     return changed;
95 }
96