1//===-- SPIRVRegularizer.cpp - regularize IR for SPIR-V ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass implements regularization of LLVM IR for SPIR-V. The prototype of
10// the pass was taken from SPIRV-LLVM translator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRVRegularizer.h"
15#include "SPIRV.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/InstIterator.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/PassManager.h"
22
23#include <list>
24
25#define DEBUG_TYPE "spirv-regularizer"
26
27using namespace llvm;
28
29static bool runImpl(Function &F);
30
31namespace {
32struct SPIRVRegularizerLegacy : public FunctionPass {
33public:
34 static char ID;
35 SPIRVRegularizerLegacy() : FunctionPass(ID) {}
36 bool runOnFunction(Function &F) override { return runImpl(F); }
37 StringRef getPassName() const override { return "SPIR-V Regularizer"; }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 FunctionPass::getAnalysisUsage(AU);
41 }
42};
43} // namespace
44
45char SPIRVRegularizerLegacy::ID = 0;
46
47INITIALIZE_PASS(SPIRVRegularizerLegacy, DEBUG_TYPE, "SPIR-V Regularizer", false,
48 false)
49
50// Since SPIR-V cannot represent constant expression, constant expressions
51// in LLVM IR need to be lowered to instructions. For each function,
52// the constant expressions used by instructions of the function are replaced
53// by instructions placed in the entry block since it dominates all other BBs.
54// Each constant expression only needs to be lowered once in each function
55// and all uses of it by instructions in that function are replaced by
56// one instruction.
57// TODO: remove redundant instructions for common subexpression.
58static void runLowerConstExpr(Function &F) {
59 LLVMContext &Ctx = F.getContext();
60 std::list<Instruction *> WorkList;
61 for (auto &II : instructions(F))
62 WorkList.push_back(x: &II);
63
64 auto FBegin = F.begin();
65 while (!WorkList.empty()) {
66 Instruction *II = WorkList.front();
67
68 auto LowerOp = [&II, &FBegin, &F](Value *V) -> Value * {
69 if (isa<Function>(Val: V))
70 return V;
71 auto *CE = cast<ConstantExpr>(Val: V);
72 LLVM_DEBUG(dbgs() << "[lowerConstantExpressions] " << *CE);
73 auto ReplInst = CE->getAsInstruction();
74 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
75 ReplInst->insertBefore(InsertPos: InsPoint->getIterator());
76 LLVM_DEBUG(dbgs() << " -> " << *ReplInst << '\n');
77 std::vector<Instruction *> Users;
78 // Do not replace use during iteration of use. Do it in another loop.
79 for (auto U : CE->users()) {
80 LLVM_DEBUG(dbgs() << "[lowerConstantExpressions] Use: " << *U << '\n');
81 auto InstUser = dyn_cast<Instruction>(Val: U);
82 // Only replace users in scope of current function.
83 if (InstUser && InstUser->getParent()->getParent() == &F)
84 Users.push_back(x: InstUser);
85 }
86 for (auto &User : Users) {
87 if (ReplInst->getParent() == User->getParent() &&
88 User->comesBefore(Other: ReplInst))
89 ReplInst->moveBefore(InsertPos: User->getIterator());
90 User->replaceUsesOfWith(From: CE, To: ReplInst);
91 }
92 return ReplInst;
93 };
94
95 WorkList.pop_front();
96 auto LowerConstantVec = [&II, &LowerOp, &WorkList,
97 &Ctx](ConstantVector *Vec,
98 unsigned NumOfOp) -> Value * {
99 if (llvm::all_of(Range: Vec->operands(), P: [](Value *V) {
100 return isa<ConstantExpr>(Val: V) || isa<Function>(Val: V);
101 })) {
102 // Expand a vector of constexprs and construct it back with
103 // series of insertelement instructions.
104 std::list<Value *> OpList;
105 llvm::transform(Range: Vec->operands(), d_first: std::back_inserter(x&: OpList),
106 F: [LowerOp](Value *V) { return LowerOp(V); });
107 Value *Repl = nullptr;
108 unsigned Idx = 0;
109 auto *PhiII = dyn_cast<PHINode>(Val: II);
110 Instruction *InsPoint =
111 PhiII ? &PhiII->getIncomingBlock(i: NumOfOp)->back() : II;
112 std::list<Instruction *> ReplList;
113 for (auto V : OpList) {
114 if (auto *Inst = dyn_cast<Instruction>(Val: V))
115 ReplList.push_back(x: Inst);
116 Repl = InsertElementInst::Create(
117 Vec: (Repl ? Repl : PoisonValue::get(T: Vec->getType())), NewElt: V,
118 Idx: ConstantInt::get(Ty: Type::getInt32Ty(C&: Ctx), V: Idx++), NameStr: "",
119 InsertBefore: InsPoint->getIterator());
120 }
121 WorkList.splice(position: WorkList.begin(), x&: ReplList);
122 return Repl;
123 }
124 return nullptr;
125 };
126 for (unsigned OI = 0, OE = II->getNumOperands(); OI != OE; ++OI) {
127 auto *Op = II->getOperand(i: OI);
128 if (auto *Vec = dyn_cast<ConstantVector>(Val: Op)) {
129 Value *ReplInst = LowerConstantVec(Vec, OI);
130 if (ReplInst)
131 II->replaceUsesOfWith(From: Op, To: ReplInst);
132 } else if (auto CE = dyn_cast<ConstantExpr>(Val: Op)) {
133 WorkList.push_front(x: cast<Instruction>(Val: LowerOp(CE)));
134 } else if (auto MDAsVal = dyn_cast<MetadataAsValue>(Val: Op)) {
135 auto ConstMD = dyn_cast<ConstantAsMetadata>(Val: MDAsVal->getMetadata());
136 if (!ConstMD)
137 continue;
138 Constant *C = ConstMD->getValue();
139 Value *ReplInst = nullptr;
140 if (auto *Vec = dyn_cast<ConstantVector>(Val: C))
141 ReplInst = LowerConstantVec(Vec, OI);
142 if (auto *CE = dyn_cast<ConstantExpr>(Val: C))
143 ReplInst = LowerOp(CE);
144 if (!ReplInst)
145 continue;
146 Metadata *RepMD = ValueAsMetadata::get(V: ReplInst);
147 Value *RepMDVal = MetadataAsValue::get(Context&: Ctx, MD: RepMD);
148 II->setOperand(i: OI, Val: RepMDVal);
149 WorkList.push_front(x: cast<Instruction>(Val: ReplInst));
150 }
151 }
152 }
153}
154
155// Lower i1 comparisons with certain predicates to logical operations.
156// The backend treats i1 as boolean values, and SPIR-V only allows logical
157// operations for boolean values. This function lowers i1 comparisons with
158// certain predicates to logical operations to generate valid SPIR-V.
159static void runLowerI1Comparisons(Function &F) {
160 for (auto &I : make_early_inc_range(Range: instructions(F))) {
161 auto *Cmp = dyn_cast<ICmpInst>(Val: &I);
162 if (!Cmp)
163 continue;
164
165 bool IsI1 = Cmp->getOperand(i_nocapture: 0)->getType()->getScalarType()->isIntegerTy(BitWidth: 1);
166 if (!IsI1)
167 continue;
168
169 auto Pred = Cmp->getPredicate();
170 bool IsTargetPred =
171 Pred >= ICmpInst::ICMP_UGT && Pred <= ICmpInst::ICMP_SLE;
172 if (!IsTargetPred)
173 continue;
174
175 Value *P = Cmp->getOperand(i_nocapture: 0);
176 Value *Q = Cmp->getOperand(i_nocapture: 1);
177
178 IRBuilder<> Builder(Cmp);
179 Value *Result = nullptr;
180 switch (Pred) {
181 case ICmpInst::ICMP_UGT:
182 case ICmpInst::ICMP_SLT:
183 // Result = p & !q
184 Result = Builder.CreateAnd(LHS: P, RHS: Builder.CreateNot(V: Q));
185 break;
186 case ICmpInst::ICMP_ULT:
187 case ICmpInst::ICMP_SGT:
188 // Result = q & !p
189 Result = Builder.CreateAnd(LHS: Q, RHS: Builder.CreateNot(V: P));
190 break;
191 case ICmpInst::ICMP_ULE:
192 case ICmpInst::ICMP_SGE:
193 // Result = q | !p
194 Result = Builder.CreateOr(LHS: Q, RHS: Builder.CreateNot(V: P));
195 break;
196 case ICmpInst::ICMP_UGE:
197 case ICmpInst::ICMP_SLE:
198 // Result = p | !q
199 Result = Builder.CreateOr(LHS: P, RHS: Builder.CreateNot(V: Q));
200 break;
201 default:
202 llvm_unreachable("Unexpected predicate");
203 }
204
205 Result->takeName(V: Cmp);
206 Cmp->replaceAllUsesWith(V: Result);
207 Cmp->eraseFromParent();
208 }
209}
210
211static bool runImpl(Function &F) {
212 runLowerI1Comparisons(F);
213 runLowerConstExpr(F);
214 return true;
215}
216
217PreservedAnalyses SPIRVRegularizer::run(Function &F,
218 FunctionAnalysisManager &AM) {
219 return runImpl(F) ? PreservedAnalyses::none() : PreservedAnalyses::all();
220}
221
222FunctionPass *llvm::createSPIRVRegularizerPass() {
223 return new SPIRVRegularizerLegacy();
224}
225