| 1 |
//===- SPIRVLowerConstExpr.cpp - Regularize LLVM for SPIR-V ------- C++ -*-===// |
| 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) 2014 Advanced Micro Devices, Inc. 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 Advanced Micro Devices, Inc., 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 regularization of LLVM module for SPIR-V. |
| 36 |
// |
| 37 |
//===----------------------------------------------------------------------===// |
| 38 |
#define DEBUG_TYPE "spv-lower-const-expr" |
| 39 |
|
| 40 |
#include "SPIRVLowerConstExpr.h" |
| 41 |
#include "OCLUtil.h" |
| 42 |
#include "SPIRVInternal.h" |
| 43 |
#include "SPIRVMDBuilder.h" |
| 44 |
#include "SPIRVMDWalker.h" |
| 45 |
#include "libSPIRV/SPIRVDebug.h" |
| 46 |
|
| 47 |
#include "llvm/ADT/StringSwitch.h" |
| 48 |
#include "llvm/ADT/Triple.h" |
| 49 |
#include "llvm/IR/IRBuilder.h" |
| 50 |
#include "llvm/IR/InstVisitor.h" |
| 51 |
#include "llvm/IR/Instructions.h" |
| 52 |
#include "llvm/IR/PassManager.h" |
| 53 |
#include "llvm/Pass.h" |
| 54 |
#include "llvm/Support/CommandLine.h" |
| 55 |
|
| 56 |
#include <list> |
| 57 |
#include <set> |
| 58 |
|
| 59 |
using namespace llvm; |
| 60 |
using namespace SPIRV; |
| 61 |
using namespace OCLUtil; |
| 62 |
|
| 63 |
namespace SPIRV { |
| 64 |
|
| 65 |
cl::opt<bool> SPIRVLowerConst( |
| 66 |
"spirv-lower-const-expr", cl::init(true), |
| 67 |
cl::desc("LLVM/SPIR-V translation enable lowering constant expression")); |
| 68 |
|
| 69 |
class SPIRVLowerConstExprLegacy : public ModulePass, |
| 70 |
public SPIRVLowerConstExprBase { |
| 71 |
public: |
| 72 |
SPIRVLowerConstExprLegacy() : ModulePass(ID) { |
| 73 |
initializeSPIRVLowerConstExprLegacyPass(*PassRegistry::getPassRegistry()); |
| 74 |
} |
| 75 |
|
| 76 |
bool runOnModule(Module &M) override { return runLowerConstExpr(M); } |
| 77 |
|
| 78 |
static char ID; |
| 79 |
}; |
| 80 |
|
| 81 |
char SPIRVLowerConstExprLegacy::ID = 0; |
| 82 |
|
| 83 |
bool SPIRVLowerConstExprBase::runLowerConstExpr(Module &Module) { |
| 84 |
if (!SPIRVLowerConst) |
| 85 |
return false; |
| 86 |
|
| 87 |
M = &Module; |
| 88 |
Ctx = &M->getContext(); |
| 89 |
|
| 90 |
LLVM_DEBUG(dbgs() << "Enter SPIRVLowerConstExpr:\n"); |
| 91 |
bool Changed = visit(M); |
| 92 |
|
| 93 |
verifyRegularizationPass(*M, "SPIRVLowerConstExpr"); |
| 94 |
|
| 95 |
return Changed; |
| 96 |
} |
| 97 |
|
| 98 |
/// Since SPIR-V cannot represent constant expression, constant expressions |
| 99 |
/// in LLVM needs to be lowered to instructions. |
| 100 |
/// For each function, the constant expressions used by instructions of the |
| 101 |
/// function are replaced by instructions placed in the entry block since it |
| 102 |
/// dominates all other BB's. Each constant expression only needs to be lowered |
| 103 |
/// once in each function and all uses of it by instructions in that function |
| 104 |
/// is replaced by one instruction. |
| 105 |
/// ToDo: remove redundant instructions for common subexpression |
| 106 |
|
| 107 |
bool SPIRVLowerConstExprBase::visit(Module *M) { |
| 108 |
bool Changed = false; |
| 109 |
for (auto &I : M->functions()) { |
| 110 |
std::list<Instruction *> WorkList; |
| 111 |
for (auto &BI : I) { |
| 112 |
for (auto &II : BI) { |
| 113 |
WorkList.push_back(&II); |
| 114 |
} |
| 115 |
} |
| 116 |
auto FBegin = I.begin(); |
| 117 |
while (!WorkList.empty()) { |
| 118 |
auto II = WorkList.front(); |
| 119 |
|
| 120 |
auto LowerOp = [&II, &FBegin, &I, &Changed](Value *V) -> Value * { |
| 121 |
if (isa<Function>(V)) |
| 122 |
return V; |
| 123 |
auto *CE = cast<ConstantExpr>(V); |
| 124 |
SPIRVDBG(dbgs() << "[lowerConstantExpressions] " << *CE;) |
| 125 |
auto ReplInst = CE->getAsInstruction(); |
| 126 |
auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back(); |
| 127 |
ReplInst->insertBefore(InsPoint); |
| 128 |
SPIRVDBG(dbgs() << " -> " << *ReplInst << '\n';) |
| 129 |
std::vector<Instruction *> Users; |
| 130 |
// Do not replace use during iteration of use. Do it in another loop |
| 131 |
for (auto U : CE->users()) { |
| 132 |
SPIRVDBG(dbgs() << "[lowerConstantExpressions] Use: " << *U << '\n';) |
| 133 |
if (auto InstUser = dyn_cast<Instruction>(U)) { |
| 134 |
// Only replace users in scope of current function |
| 135 |
if (InstUser->getParent()->getParent() == &I) |
| 136 |
Users.push_back(InstUser); |
| 137 |
} |
| 138 |
} |
| 139 |
for (auto &User : Users) { |
| 140 |
if (ReplInst->getParent() == User->getParent()) |
| 141 |
if (User->comesBefore(ReplInst)) |
| 142 |
ReplInst->moveBefore(User); |
| 143 |
User->replaceUsesOfWith(CE, ReplInst); |
| 144 |
} |
| 145 |
Changed = true; |
| 146 |
return ReplInst; |
| 147 |
}; |
| 148 |
|
| 149 |
WorkList.pop_front(); |
| 150 |
|
| 151 |
for (unsigned OI = 0, OE = II->getNumOperands(); OI != OE; ++OI) { |
| 152 |
auto *Op = II->getOperand(OI); |
| 153 |
if (auto *CE = dyn_cast<ConstantExpr>(Op)) { |
| 154 |
WorkList.push_front(cast<Instruction>(LowerOp(CE))); |
| 155 |
} else if (auto MDAsVal = dyn_cast<MetadataAsValue>(Op)) { |
| 156 |
Metadata *MD = MDAsVal->getMetadata(); |
| 157 |
if (auto ConstMD = dyn_cast<ConstantAsMetadata>(MD)) { |
| 158 |
Constant *C = ConstMD->getValue(); |
| 159 |
Value *ReplInst = nullptr; |
| 160 |
if (auto *CE = dyn_cast<ConstantExpr>(C)) |
| 161 |
ReplInst = LowerOp(CE); |
| 162 |
if (ReplInst) { |
| 163 |
Metadata *RepMD = ValueAsMetadata::get(ReplInst); |
| 164 |
Value *RepMDVal = MetadataAsValue::get(M->getContext(), RepMD); |
| 165 |
II->setOperand(OI, RepMDVal); |
| 166 |
WorkList.push_front(cast<Instruction>(ReplInst)); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
return Changed; |
| 174 |
} |
| 175 |
|
| 176 |
} // namespace SPIRV |
| 177 |
|
| 178 |
INITIALIZE_PASS(SPIRVLowerConstExprLegacy, "spv-lower-const-expr", |
| 179 |
"Regularize LLVM for SPIR-V", false, false) |
| 180 |
|
| 181 |
ModulePass *llvm::createSPIRVLowerConstExprLegacy() { |
| 182 |
return new SPIRVLowerConstExprLegacy(); |
| 183 |
} |