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 #pragma once
10 
11 #include "Compiler/MetaDataUtilsWrapper.h"
12 #include "Compiler/CodeGenContextWrapper.hpp"
13 
14 #include "common/LLVMWarningsPush.hpp"
15 #include <llvm/Pass.h>
16 #include <llvm/IR/DataLayout.h>
17 #include <llvm/IR/Function.h>
18 #include <llvm/IR/InstVisitor.h>
19 #include <llvm/IR/IRBuilder.h>
20 #include "llvm/ADT/SmallVector.h"
21 #include "common/LLVMWarningsPop.hpp"
22 #include "GenISAIntrinsics/GenIntrinsicInst.h"
23 #include "Compiler/CISACodeGen/helper.h"
24 #include "common/IGCIRBuilder.h"
25 
26 namespace IGC
27 {
28     class CodeGenContext;
29 }
30 
31 namespace IGC
32 {
33     class LowPrecisionOpt : public llvm::FunctionPass, public llvm::InstVisitor<LowPrecisionOpt>
34     {
35     private:
36         llvm::IGCIRBuilder<>* m_builder;
37         bool m_changed;
38         llvm::Function* m_func_llvm_GenISA_DCL_inputVec_f16;
39         llvm::Function* m_func_llvm_GenISA_DCL_inputVec_f32;
40         llvm::Function* m_currFunction;
41         llvm::Function* func_llvm_floor_f32;
42         ShaderType shdrType;
43         typedef struct _moveBundle
44         {
45             uint index;
46             llvm::GenIntrinsicInst* cInst;
47             llvm::FPTruncInst* fpTrunc;
48         }moveBundle;
49         struct _cmpOperator
50         {
operator ()IGC::LowPrecisionOpt::_cmpOperator51             bool operator()(const moveBundle& obj1, const moveBundle& obj2) { return obj1.index > obj2.index; };
52         }cmpOperator;
53         llvm::SmallVector<moveBundle, 11> bundles;
54         bool m_changeSample = false;
55         bool m_simplifyAlu = false;
56     public:
57 
58         static char ID;
59 
60         LowPrecisionOpt();
61 
~LowPrecisionOpt()62         ~LowPrecisionOpt() {}
63 
64         virtual bool runOnFunction(llvm::Function& F) override;
65 
getAnalysisUsage(llvm::AnalysisUsage & AU) const66         virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override
67         {
68             AU.setPreservesCFG();
69             AU.addRequired<MetaDataUtilsWrapper>();
70             AU.addRequired<CodeGenContextWrapper>();
71         }
72 
getPassName() const73         virtual llvm::StringRef getPassName() const override
74         {
75             return "LowPrecisionOpt Pass";
76         }
77 
78         void visitFPExtInst(llvm::FPExtInst& I);
79         void visitFPTruncInst(llvm::FPTruncInst& I);
80         void visitIntrinsicInst(llvm::IntrinsicInst& I);
81         void visitCallInst(llvm::CallInst& I);
82         bool propagateSamplerType(llvm::GenIntrinsicInst& I);
83     };
84 } // namespace IGC
85