Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVRegularizeLLVM.h

raw
1 //=- SPIRVRegularizeLLVM.h - LLVM Module regularization pass -*- C++ -*-=//
2 //
3 // The LLVM/SPIR-V Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2022 The Khronos Group Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of The Khronos Group, nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34
35 #ifndef SPIRV_SPIRVREGULARIZELLVM_H
36 #define SPIRV_SPIRVREGULARIZELLVM_H
37
38 #include "SPIRVInternal.h"
39
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/PassManager.h"
42 #include "llvm/Pass.h"
43
44 namespace SPIRV {
45
46 class SPIRVRegularizeLLVMBase {
47 public:
48 SPIRVRegularizeLLVMBase() : M(nullptr), Ctx(nullptr) {}
49
50 bool runRegularizeLLVM(llvm::Module &M);
51 // Lower functions
52 bool regularize();
53
54 // SPIR-V disallows functions being entrypoints and called
55 // LLVM doesn't. This adds a wrapper around the entry point
56 // that later SPIR-V writer renames.
57 void addKernelEntryPoint(llvm::Module *M);
58
59 /// Some LLVM intrinsics that have no SPIR-V counterpart may be wrapped in
60 /// @spirv.llvm_intrinsic_* function. During reverse translation from SPIR-V
61 /// to LLVM IR we can detect this @spirv.llvm_intrinsic_* function and
62 /// replace it with @llvm.intrinsic.* back.
63 void lowerIntrinsicToFunction(llvm::IntrinsicInst *Intrinsic);
64
65 /// No SPIR-V counterpart for @llvm.fshl.*(@llvm.fshr.*) intrinsic. It will be
66 /// lowered to a newly generated @spirv.llvm_fshl_*(@spirv.llvm_fshr_*)
67 /// function.
68 ///
69 /// Conceptually, FSHL (FSHR):
70 /// 1. concatenates the ints, the first one being the more significant;
71 /// 2. performs a left (right) shift-rotate on the resulting doubled-sized
72 /// int;
73 /// 3. returns the most (least) significant bits of the shift-rotate result,
74 /// the number of bits being equal to the size of the original integers.
75 /// If FSHL (FSHR) operates on a vector type instead, the same operations are
76 /// performed for each set of corresponding vector elements.
77 ///
78 /// The actual implementation algorithm will be slightly different for
79 /// simplification purposes.
80 void lowerFunnelShift(llvm::IntrinsicInst *FSHIntrinsic);
81
82 void lowerUMulWithOverflow(llvm::IntrinsicInst *UMulIntrinsic);
83 void buildUMulWithOverflowFunc(llvm::Function *UMulFunc);
84
85 // For some cases Clang emits VectorExtractDynamic as:
86 // void @_Z28__spirv_VectorExtractDynamic(<Ty>* sret(<Ty>), jointMatrix, idx);
87 // Instead of:
88 // <Ty> @_Z28__spirv_VectorExtractDynamic(JointMatrix, Idx);
89 // And VectorInsertDynamic as:
90 // @_Z27__spirv_VectorInsertDynamic(jointMatrix, <Ty>* byval(<Ty>), idx);
91 // Instead of:
92 // @_Z27__spirv_VectorInsertDynamic(jointMatrix, <Ty>, idx)
93 // Need to add additional GEP, store and load instructions and mutate called
94 // function to avoid translation failures
95 void expandSYCLTypeUsing(llvm::Module *M);
96 void expandVEDWithSYCLTypeSRetArg(llvm::Function *F);
97 void expandVIDWithSYCLTypeByValComp(llvm::Function *F);
98
99 // According to the specification, the operands of a shift instruction must be
100 // a scalar/vector of integer. When LLVM-IR contains a shift instruction with
101 // i1 operands, they are treated as a bool. We need to extend them to i32 to
102 // comply with the specification. For example: "%shift = lshr i1 0, 1";
103 // The bit instruction should be changed to the extended version
104 // "%shift = lshr i32 0, 1" so the args are treated as int operands.
105 Value *extendBitInstBoolArg(llvm::Instruction *OldInst);
106
107 static std::string lowerLLVMIntrinsicName(llvm::IntrinsicInst *II);
108 static char ID;
109
110 private:
111 llvm::Module *M;
112 llvm::LLVMContext *Ctx;
113 };
114
115 class SPIRVRegularizeLLVMPass
116 : public llvm::PassInfoMixin<SPIRVRegularizeLLVMPass>,
117 public SPIRVRegularizeLLVMBase {
118 public:
119 llvm::PreservedAnalyses run(llvm::Module &M,
120 llvm::ModuleAnalysisManager &MAM) {
121 return runRegularizeLLVM(M) ? llvm::PreservedAnalyses::none()
122 : llvm::PreservedAnalyses::all();
123 }
124 };
125
126 class SPIRVRegularizeLLVMLegacy : public llvm::ModulePass,
127 public SPIRVRegularizeLLVMBase {
128 public:
129 SPIRVRegularizeLLVMLegacy() : ModulePass(ID) {
130 initializeSPIRVRegularizeLLVMLegacyPass(*PassRegistry::getPassRegistry());
131 }
132
133 bool runOnModule(llvm::Module &M) override;
134
135 static char ID;
136 };
137
138 } // namespace SPIRV
139
140 #endif // SPIRV_SPIRVREGULARIZELLVM_H