LLVM 22.0.0git
SPIRVRegularizer.cpp
Go to the documentation of this file.
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 "SPIRV.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/PassManager.h"
20
21#include <list>
22
23#define DEBUG_TYPE "spirv-regularizer"
24
25using namespace llvm;
26
27namespace {
28struct SPIRVRegularizer : public FunctionPass {
29public:
30 static char ID;
31 SPIRVRegularizer() : FunctionPass(ID) {}
32 bool runOnFunction(Function &F) override;
33 StringRef getPassName() const override { return "SPIR-V Regularizer"; }
34
35 void getAnalysisUsage(AnalysisUsage &AU) const override {
37 }
38
39private:
40 void runLowerConstExpr(Function &F);
41 void runLowerI1Comparisons(Function &F);
42};
43} // namespace
44
45char SPIRVRegularizer::ID = 0;
46
47INITIALIZE_PASS(SPIRVRegularizer, 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.
58void SPIRVRegularizer::runLowerConstExpr(Function &F) {
59 LLVMContext &Ctx = F.getContext();
60 std::list<Instruction *> WorkList;
61 for (auto &II : instructions(F))
62 WorkList.push_back(&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>(V))
70 return V;
71 auto *CE = cast<ConstantExpr>(V);
72 LLVM_DEBUG(dbgs() << "[lowerConstantExpressions] " << *CE);
73 auto ReplInst = CE->getAsInstruction();
74 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
75 ReplInst->insertBefore(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>(U);
82 // Only replace users in scope of current function.
83 if (InstUser && InstUser->getParent()->getParent() == &F)
84 Users.push_back(InstUser);
85 }
86 for (auto &User : Users) {
87 if (ReplInst->getParent() == User->getParent() &&
88 User->comesBefore(ReplInst))
89 ReplInst->moveBefore(User->getIterator());
90 User->replaceUsesOfWith(CE, 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 (std::all_of(Vec->op_begin(), Vec->op_end(), [](Value *V) {
100 return isa<ConstantExpr>(V) || isa<Function>(V);
101 })) {
102 // Expand a vector of constexprs and construct it back with
103 // series of insertelement instructions.
104 std::list<Value *> OpList;
105 std::transform(Vec->op_begin(), Vec->op_end(),
106 std::back_inserter(OpList),
107 [LowerOp](Value *V) { return LowerOp(V); });
108 Value *Repl = nullptr;
109 unsigned Idx = 0;
110 auto *PhiII = dyn_cast<PHINode>(II);
111 Instruction *InsPoint =
112 PhiII ? &PhiII->getIncomingBlock(NumOfOp)->back() : II;
113 std::list<Instruction *> ReplList;
114 for (auto V : OpList) {
115 if (auto *Inst = dyn_cast<Instruction>(V))
116 ReplList.push_back(Inst);
118 (Repl ? Repl : PoisonValue::get(Vec->getType())), V,
119 ConstantInt::get(Type::getInt32Ty(Ctx), Idx++), "",
120 InsPoint->getIterator());
121 }
122 WorkList.splice(WorkList.begin(), ReplList);
123 return Repl;
124 }
125 return nullptr;
126 };
127 for (unsigned OI = 0, OE = II->getNumOperands(); OI != OE; ++OI) {
128 auto *Op = II->getOperand(OI);
129 if (auto *Vec = dyn_cast<ConstantVector>(Op)) {
130 Value *ReplInst = LowerConstantVec(Vec, OI);
131 if (ReplInst)
132 II->replaceUsesOfWith(Op, ReplInst);
133 } else if (auto CE = dyn_cast<ConstantExpr>(Op)) {
134 WorkList.push_front(cast<Instruction>(LowerOp(CE)));
135 } else if (auto MDAsVal = dyn_cast<MetadataAsValue>(Op)) {
136 auto ConstMD = dyn_cast<ConstantAsMetadata>(MDAsVal->getMetadata());
137 if (!ConstMD)
138 continue;
139 Constant *C = ConstMD->getValue();
140 Value *ReplInst = nullptr;
141 if (auto *Vec = dyn_cast<ConstantVector>(C))
142 ReplInst = LowerConstantVec(Vec, OI);
143 if (auto *CE = dyn_cast<ConstantExpr>(C))
144 ReplInst = LowerOp(CE);
145 if (!ReplInst)
146 continue;
147 Metadata *RepMD = ValueAsMetadata::get(ReplInst);
148 Value *RepMDVal = MetadataAsValue::get(Ctx, RepMD);
149 II->setOperand(OI, RepMDVal);
150 WorkList.push_front(cast<Instruction>(ReplInst));
151 }
152 }
153 }
154}
155
156// Lower i1 comparisons with certain predicates to logical operations.
157// The backend treats i1 as boolean values, and SPIR-V only allows logical
158// operations for boolean values. This function lowers i1 comparisons with
159// certain predicates to logical operations to generate valid SPIR-V.
160void SPIRVRegularizer::runLowerI1Comparisons(Function &F) {
161 for (auto &I : make_early_inc_range(instructions(F))) {
162 auto *Cmp = dyn_cast<ICmpInst>(&I);
163 if (!Cmp)
164 continue;
165
166 bool IsI1 = Cmp->getOperand(0)->getType()->isIntegerTy(1);
167 if (!IsI1)
168 continue;
169
170 auto Pred = Cmp->getPredicate();
171 bool IsTargetPred =
172 Pred >= ICmpInst::ICMP_UGT && Pred <= ICmpInst::ICMP_SLE;
173 if (!IsTargetPred)
174 continue;
175
176 Value *P = Cmp->getOperand(0);
177 Value *Q = Cmp->getOperand(1);
178
179 IRBuilder<> Builder(Cmp);
180 Value *Result = nullptr;
181 switch (Pred) {
182 case ICmpInst::ICMP_UGT:
183 case ICmpInst::ICMP_SLT:
184 // Result = p & !q
185 Result = Builder.CreateAnd(P, Builder.CreateNot(Q));
186 break;
187 case ICmpInst::ICMP_ULT:
188 case ICmpInst::ICMP_SGT:
189 // Result = q & !p
190 Result = Builder.CreateAnd(Q, Builder.CreateNot(P));
191 break;
192 case ICmpInst::ICMP_ULE:
193 case ICmpInst::ICMP_SGE:
194 // Result = q | !p
195 Result = Builder.CreateOr(Q, Builder.CreateNot(P));
196 break;
197 case ICmpInst::ICMP_UGE:
198 case ICmpInst::ICMP_SLE:
199 // Result = p | !q
200 Result = Builder.CreateOr(P, Builder.CreateNot(Q));
201 break;
202 default:
203 llvm_unreachable("Unexpected predicate");
204 }
205
206 Result->takeName(Cmp);
207 Cmp->replaceAllUsesWith(Result);
208 Cmp->eraseFromParent();
209 }
210}
211
212bool SPIRVRegularizer::runOnFunction(Function &F) {
213 runLowerI1Comparisons(F);
214 runLowerConstExpr(F);
215 return true;
216}
217
219 return new SPIRVRegularizer();
220}
Expand Atomic instructions
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Represent the analysis usage information of a pass.
Constant Vector Declarations.
Definition Constants.h:522
FixedVectorType * getType() const
Specialize the getType() method to always return a FixedVectorType, which reduces the amount of casti...
Definition Constants.h:545
This is an important base class in LLVM.
Definition Constant.h:43
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:104
Root of the metadata hierarchy.
Definition Metadata.h:64
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:112
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
op_iterator op_begin()
Definition User.h:285
op_iterator op_end()
Definition User.h:287
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:503
LLVM Value Representation.
Definition Value.h:75
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
FunctionPass * createSPIRVRegularizerPass()
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559