|
| 1 | +//===- SPIRVLowerSaddIntrinsics.cpp - Lower llvm.sadd.* -------------------===// |
| 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) 2020 Intel Corporation. All rights reserved. |
| 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 Intel Corporation, 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.* 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_intrinsics" |
| 41 | + |
| 42 | +#include "SPIRVLowerSaddIntrinsics.h" |
| 43 | +#include "LLVMSaddWithOverflow.h" |
| 44 | + |
| 45 | +#include "LLVMSPIRVLib.h" |
| 46 | +#include "SPIRVError.h" |
| 47 | +#include "libSPIRV/SPIRVDebug.h" |
| 48 | + |
| 49 | +#include "llvm/IR/IRBuilder.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 SPIRVLowerSaddIntrinsicsBase::replaceSaddOverflow(Function &F) { |
| 62 | + assert(F.getIntrinsicID() == Intrinsic::sadd_with_overflow); |
| 63 | + |
| 64 | + StringRef IntrinsicName = F.getName(); |
| 65 | + std::string FuncName = "llvm_sadd_with_overflow_i"; |
| 66 | + if (IntrinsicName.endswith(".i16")) |
| 67 | + FuncName += "16"; |
| 68 | + else if (IntrinsicName.endswith(".i32")) |
| 69 | + FuncName += "32"; |
| 70 | + else if (IntrinsicName.endswith(".i64")) |
| 71 | + FuncName += "64"; |
| 72 | + else { |
| 73 | + assert(false && |
| 74 | + "Unsupported overloading of llvm.sadd.with.overflow intrinsic"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Redirect @llvm.sadd.with.overflow.* call to the function we have in |
| 79 | + // the loaded module @llvm_sadd_with_overflow_* |
| 80 | + Function *ReplacementFunc = Mod->getFunction(FuncName); |
| 81 | + if (!ReplacementFunc) { // This function needs linking. |
| 82 | + Mod->getOrInsertFunction(FuncName, F.getFunctionType()); |
| 83 | + // Read LLVM IR with the intrinsic's implementation |
| 84 | + SMDiagnostic Err; |
| 85 | + auto MB = MemoryBuffer::getMemBuffer(LLVMSaddWithOverflow); |
| 86 | + auto SaddWithOverflowModule = |
| 87 | + parseIR(MB->getMemBufferRef(), Err, *Context, |
| 88 | + [&](StringRef) { return Mod->getDataLayoutStr(); }); |
| 89 | + if (!SaddWithOverflowModule) { |
| 90 | + std::string ErrMsg; |
| 91 | + raw_string_ostream ErrStream(ErrMsg); |
| 92 | + Err.print("", ErrStream); |
| 93 | + SPIRVErrorLog EL; |
| 94 | + EL.checkError(false, SPIRVEC_InvalidLlvmModule, ErrMsg); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Link in the intrinsic's implementation. |
| 99 | + if (!Linker::linkModules(*Mod, std::move(SaddWithOverflowModule), |
| 100 | + Linker::LinkOnlyNeeded)) |
| 101 | + TheModuleIsModified = true; |
| 102 | + |
| 103 | + ReplacementFunc = Mod->getFunction(FuncName); |
| 104 | + assert(ReplacementFunc && "How did we not link in the necessary function?"); |
| 105 | + } |
| 106 | + |
| 107 | + F.replaceAllUsesWith(ReplacementFunc); |
| 108 | +} |
| 109 | + |
| 110 | +void SPIRVLowerSaddIntrinsicsBase::replaceSaddSat(Function &F) { |
| 111 | + assert(F.getIntrinsicID() == Intrinsic::sadd_sat); |
| 112 | + |
| 113 | + SmallVector<IntrinsicInst *, 4> Intrinsics; |
| 114 | + for (User *U : F.users()) { |
| 115 | + if (auto *II = dyn_cast<IntrinsicInst>(U)) |
| 116 | + Intrinsics.push_back(II); |
| 117 | + } |
| 118 | + |
| 119 | + // Get the corresponding sadd_with_overflow intrinsic for the sadd_sat. |
| 120 | + Type *IntTy = F.getFunctionType()->getReturnType(); |
| 121 | + Function *SaddO = |
| 122 | + Intrinsic::getDeclaration(Mod, Intrinsic::sadd_with_overflow, IntTy); |
| 123 | + |
| 124 | + // Replace all uses of the intrinsic with equivalent code relying on |
| 125 | + // sadd_with_overflow |
| 126 | + IRBuilder<> Builder(F.getContext()); |
| 127 | + unsigned BitWidth = IntTy->getIntegerBitWidth(); |
| 128 | + Value *IntMin = Builder.getInt(APInt::getSignedMinValue(BitWidth)); |
| 129 | + Value *ShiftWidth = Builder.getIntN(BitWidth, BitWidth - 1); |
| 130 | + |
| 131 | + for (IntrinsicInst *II : Intrinsics) { |
| 132 | + Builder.SetInsertPoint(II); |
| 133 | + // {res, overflow} = @llvm.sadd_with_overflow(a, b) |
| 134 | + // sadd_sat(a, b) => overflow ? (res >> bitwidth) ^ intmin : res; |
| 135 | + Value *StructRes = |
| 136 | + Builder.CreateCall(SaddO, {II->getArgOperand(0), II->getArgOperand(1)}); |
| 137 | + Value *Sum = Builder.CreateExtractValue(StructRes, 0); |
| 138 | + Value *Overflow = Builder.CreateExtractValue(StructRes, 1); |
| 139 | + Value *OverflowedRes = |
| 140 | + Builder.CreateXor(Builder.CreateAShr(Sum, ShiftWidth), IntMin); |
| 141 | + Value *Result = Builder.CreateSelect(Overflow, OverflowedRes, Sum); |
| 142 | + II->replaceAllUsesWith(Result); |
| 143 | + II->eraseFromParent(); |
| 144 | + } |
| 145 | + |
| 146 | + // Now replace the sadd_with_overflow intrinsic itself. |
| 147 | + replaceSaddOverflow(*SaddO); |
| 148 | +} |
| 149 | + |
| 150 | +bool SPIRVLowerSaddIntrinsicsBase::runLowerSaddIntrinsics(Module &M) { |
| 151 | + Context = &M.getContext(); |
| 152 | + Mod = &M; |
| 153 | + for (Function &F : M) { |
| 154 | + Intrinsic::ID IntrinId = F.getIntrinsicID(); |
| 155 | + if (IntrinId == Intrinsic::sadd_with_overflow) |
| 156 | + replaceSaddOverflow(F); |
| 157 | + else if (IntrinId == Intrinsic::sadd_sat) |
| 158 | + replaceSaddSat(F); |
| 159 | + } |
| 160 | + |
| 161 | + verifyRegularizationPass(M, "SPIRVLowerSaddIntrinsics"); |
| 162 | + return TheModuleIsModified; |
| 163 | +} |
| 164 | + |
| 165 | +llvm::PreservedAnalyses |
| 166 | +SPIRVLowerSaddIntrinsicsPass::run(llvm::Module &M, |
| 167 | + llvm::ModuleAnalysisManager &MAM) { |
| 168 | + return runLowerSaddIntrinsics(M) ? llvm::PreservedAnalyses::none() |
| 169 | + : llvm::PreservedAnalyses::all(); |
| 170 | +} |
| 171 | + |
| 172 | +SPIRVLowerSaddIntrinsicsLegacy::SPIRVLowerSaddIntrinsicsLegacy() |
| 173 | + : ModulePass(ID) { |
| 174 | + initializeSPIRVLowerSaddIntrinsicsLegacyPass( |
| 175 | + *PassRegistry::getPassRegistry()); |
| 176 | +} |
| 177 | + |
| 178 | +bool SPIRVLowerSaddIntrinsicsLegacy::runOnModule(Module &M) { |
| 179 | + return runLowerSaddIntrinsics(M); |
| 180 | +} |
| 181 | + |
| 182 | +char SPIRVLowerSaddIntrinsicsLegacy::ID = 0; |
| 183 | + |
| 184 | +} // namespace SPIRV |
| 185 | + |
| 186 | +INITIALIZE_PASS(SPIRVLowerSaddIntrinsicsLegacy, |
| 187 | + "spv-lower-llvm_sadd_intrinsics", |
| 188 | + "Lower llvm.sadd.* intrinsics", false, false) |
| 189 | + |
| 190 | +ModulePass *llvm::createSPIRVLowerSaddIntrinsicsLegacy() { |
| 191 | + return new SPIRVLowerSaddIntrinsicsLegacy(); |
| 192 | +} |
0 commit comments