Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVLowerSaddWithOverflow.cpp

raw
1 //===- SPIRVLowerSaddWithOverflow.cpp - Lower llvm.sadd.with.overflow -----===//
2 //
3 // The LLVM/SPIRV 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 // This file implements lowering of llvm.sadd.with.overflow.* into basic LLVM
36 // operations. Probably, in the future this pass can be generalized for other
37 // function calls
38 //
39 //===----------------------------------------------------------------------===//
40 #define DEBUG_TYPE "spv-lower-llvm_sadd_with_overflow"
41
42 #include "SPIRVLowerSaddWithOverflow.h"
43 #include "LLVMSaddWithOverflow.h"
44
45 #include "LLVMSPIRVLib.h"
46 #include "SPIRVError.h"
47 #include "libSPIRV/SPIRVDebug.h"
48
49 #include "llvm/IR/InstVisitor.h"
50 #include "llvm/IR/IntrinsicInst.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/IRReader/IRReader.h"
53 #include "llvm/Linker/Linker.h"
54 #include "llvm/Support/SourceMgr.h"
55
56 using namespace llvm;
57 using namespace SPIRV;
58
59 namespace SPIRV {
60
61 void SPIRVLowerSaddWithOverflowBase::visitIntrinsicInst(CallInst &I) {
62 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
63 if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow)
64 return;
65
66 Function *IntrinsicFunc = I.getCalledFunction();
67 assert(IntrinsicFunc && "Missing function");
68 StringRef IntrinsicName = IntrinsicFunc->getName();
69 std::string FuncName = "llvm_sadd_with_overflow_i";
70 if (IntrinsicName.endswith(".i16"))
71 FuncName += "16";
72 else if (IntrinsicName.endswith(".i32"))
73 FuncName += "32";
74 else if (IntrinsicName.endswith(".i64"))
75 FuncName += "64";
76 else {
77 assert(false &&
78 "Unsupported overloading of llvm.sadd.with.overflow intrinsic");
79 return;
80 }
81
82 // Redirect @llvm.sadd.with.overflow.* call to the function we have in
83 // the loaded module @llvm_sadd_with_overflow_*
84 Function *F = Mod->getFunction(FuncName);
85 if (F) { // This function is already linked in.
86 I.setCalledFunction(F);
87 return;
88 }
89 FunctionCallee FC = Mod->getOrInsertFunction(FuncName, I.getFunctionType());
90 I.setCalledFunction(FC);
91
92 // Read LLVM IR with the intrinsic's implementation
93 SMDiagnostic Err;
94 auto MB = MemoryBuffer::getMemBuffer(LLVMSaddWithOverflow);
95 auto SaddWithOverflowModule =
96 parseIR(MB->getMemBufferRef(), Err, *Context,
97 ParserCallbacks([&](StringRef, StringRef) {
98 return Mod->getDataLayoutStr();
99 }));
100 if (!SaddWithOverflowModule) {
101 std::string ErrMsg;
102 raw_string_ostream ErrStream(ErrMsg);
103 Err.print("", ErrStream);
104 SPIRVErrorLog EL;
105 EL.checkError(false, SPIRVEC_InvalidLlvmModule, ErrMsg);
106 return;
107 }
108
109 // Link in the intrinsic's implementation.
110 if (!Linker::linkModules(*Mod, std::move(SaddWithOverflowModule),
111 Linker::LinkOnlyNeeded))
112 TheModuleIsModified = true;
113 }
114
115 bool SPIRVLowerSaddWithOverflowBase::runLowerSaddWithOverflow(Module &M) {
116 Context = &M.getContext();
117 Mod = &M;
118 visit(M);
119
120 verifyRegularizationPass(M, "SPIRVLowerSaddWithOverflow");
121 return TheModuleIsModified;
122 }
123
124 llvm::PreservedAnalyses
125 SPIRVLowerSaddWithOverflowPass::run(llvm::Module &M,
126 llvm::ModuleAnalysisManager &MAM) {
127 return runLowerSaddWithOverflow(M) ? llvm::PreservedAnalyses::none()
128 : llvm::PreservedAnalyses::all();
129 }
130
131 SPIRVLowerSaddWithOverflowLegacy::SPIRVLowerSaddWithOverflowLegacy()
132 : ModulePass(ID) {
133 initializeSPIRVLowerSaddWithOverflowLegacyPass(
134 *PassRegistry::getPassRegistry());
135 }
136
137 bool SPIRVLowerSaddWithOverflowLegacy::runOnModule(Module &M) {
138 return runLowerSaddWithOverflow(M);
139 }
140
141 char SPIRVLowerSaddWithOverflowLegacy::ID = 0;
142
143 } // namespace SPIRV
144
145 INITIALIZE_PASS(SPIRVLowerSaddWithOverflowLegacy,
146 "spv-lower-llvm_sadd_with_overflow",
147 "Lower llvm.sadd.with.overflow.* intrinsics", false, false)
148
149 ModulePass *llvm::createSPIRVLowerSaddWithOverflowLegacy() {
150 return new SPIRVLowerSaddWithOverflowLegacy();
151 }