LLVM 23.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 "SPIRVRegularizer.h"
15#include "SPIRV.h"
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/PassManager.h"
21
22#include <list>
23
24#define DEBUG_TYPE "spirv-regularizer"
25
26using namespace llvm;
27
28static bool runImpl(Function &F);
29
30namespace {
31struct SPIRVRegularizerLegacy : public FunctionPass {
32public:
33 static char ID;
34 SPIRVRegularizerLegacy() : FunctionPass(ID) {}
35 bool runOnFunction(Function &F) override { return runImpl(F); }
36 StringRef getPassName() const override { return "SPIR-V Regularizer"; }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 FunctionPass::getAnalysisUsage(AU);
40 }
41};
42} // namespace
43
44char SPIRVRegularizerLegacy::ID = 0;
45
46INITIALIZE_PASS(SPIRVRegularizerLegacy, DEBUG_TYPE, "SPIR-V Regularizer", false,
47 false)
48
49// Since SPIR-V cannot represent constant expression, constant expressions
50// in LLVM IR need to be lowered to instructions. For each function,
51// the constant expressions used by instructions of the function are replaced
52// by instructions placed in the entry block since it dominates all other BBs.
53// Each constant expression only needs to be lowered once in each function
54// and all uses of it by instructions in that function are replaced by
55// one instruction.
56// TODO: remove redundant instructions for common subexpression.
57static void runLowerConstExpr(Function &F) {
58 LLVMContext &Ctx = F.getContext();
59 std::list<Instruction *> WorkList;
60 for (auto &II : instructions(F))
61 WorkList.push_back(&II);
62
63 auto FBegin = F.begin();
64 while (!WorkList.empty()) {
65 Instruction *II = WorkList.front();
66
67 auto LowerOp = [&II, &FBegin, &F](Value *V) -> Value * {
68 if (isa<Function>(V))
69 return V;
70 auto *CE = cast<ConstantExpr>(V);
71 LLVM_DEBUG(dbgs() << "[lowerConstantExpressions] " << *CE);
72 auto ReplInst = CE->getAsInstruction();
73 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
74 ReplInst->insertBefore(InsPoint->getIterator());
75 LLVM_DEBUG(dbgs() << " -> " << *ReplInst << '\n');
76 std::vector<Instruction *> Users;
77 // Do not replace use during iteration of use. Do it in another loop.
78 for (auto U : CE->users()) {
79 LLVM_DEBUG(dbgs() << "[lowerConstantExpressions] Use: " << *U << '\n');
80 auto InstUser = dyn_cast<Instruction>(U);
81 // Only replace users in scope of current function.
82 if (InstUser && InstUser->getParent()->getParent() == &F)
83 Users.push_back(InstUser);
84 }
85 for (auto &User : Users) {
86 if (ReplInst->getParent() == User->getParent() &&
87 User->comesBefore(ReplInst))
88 ReplInst->moveBefore(User->getIterator());
89 User->replaceUsesOfWith(CE, ReplInst);
90 }
91 return ReplInst;
92 };
93
94 WorkList.pop_front();
95 auto LowerConstantVec = [&II, &LowerOp, &WorkList,
96 &Ctx](ConstantVector *Vec,
97 unsigned NumOfOp) -> Value * {
98 if (std::all_of(Vec->op_begin(), Vec->op_end(), [](Value *V) {
99 return isa<ConstantExpr>(V) || isa<Function>(V);
100 })) {
101 // Expand a vector of constexprs and construct it back with
102 // series of insertelement instructions.
103 std::list<Value *> OpList;
104 std::transform(Vec->op_begin(), Vec->op_end(),
105 std::back_inserter(OpList),
106 [LowerOp](Value *V) { return LowerOp(V); });
107 Value *Repl = nullptr;
108 unsigned Idx = 0;
109 auto *PhiII = dyn_cast<PHINode>(II);
110 Instruction *InsPoint =
111 PhiII ? &PhiII->getIncomingBlock(NumOfOp)->back() : II;
112 std::list<Instruction *> ReplList;
113 for (auto V : OpList) {
114 if (auto *Inst = dyn_cast<Instruction>(V))
115 ReplList.push_back(Inst);
117 (Repl ? Repl : PoisonValue::get(Vec->getType())), V,
118 ConstantInt::get(Type::getInt32Ty(Ctx), Idx++), "",
119 InsPoint->getIterator());
120 }
121 WorkList.splice(WorkList.begin(), 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(OI);
128 if (auto *Vec = dyn_cast<ConstantVector>(Op)) {
129 Value *ReplInst = LowerConstantVec(Vec, OI);
130 if (ReplInst)
131 II->replaceUsesOfWith(Op, ReplInst);
132 } else if (auto CE = dyn_cast<ConstantExpr>(Op)) {
133 WorkList.push_front(cast<Instruction>(LowerOp(CE)));
134 } else if (auto MDAsVal = dyn_cast<MetadataAsValue>(Op)) {
135 auto ConstMD = dyn_cast<ConstantAsMetadata>(MDAsVal->getMetadata());
136 if (!ConstMD)
137 continue;
138 Constant *C = ConstMD->getValue();
139 Value *ReplInst = nullptr;
140 if (auto *Vec = dyn_cast<ConstantVector>(C))
141 ReplInst = LowerConstantVec(Vec, OI);
142 if (auto *CE = dyn_cast<ConstantExpr>(C))
143 ReplInst = LowerOp(CE);
144 if (!ReplInst)
145 continue;
146 Metadata *RepMD = ValueAsMetadata::get(ReplInst);
147 Value *RepMDVal = MetadataAsValue::get(Ctx, RepMD);
148 II->setOperand(OI, RepMDVal);
149 WorkList.push_front(cast<Instruction>(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.
160 for (auto &I : make_early_inc_range(instructions(F))) {
161 auto *Cmp = dyn_cast<ICmpInst>(&I);
162 if (!Cmp)
163 continue;
164
165 bool IsI1 = Cmp->getOperand(0)->getType()->isIntegerTy(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(0);
176 Value *Q = Cmp->getOperand(1);
177
178 IRBuilder<> Builder(Cmp);
179 Value *Result = nullptr;
180 switch (Pred) {
183 // Result = p & !q
184 Result = Builder.CreateAnd(P, Builder.CreateNot(Q));
185 break;
188 // Result = q & !p
189 Result = Builder.CreateAnd(Q, Builder.CreateNot(P));
190 break;
193 // Result = q | !p
194 Result = Builder.CreateOr(Q, Builder.CreateNot(P));
195 break;
198 // Result = p | !q
199 Result = Builder.CreateOr(P, Builder.CreateNot(Q));
200 break;
201 default:
202 llvm_unreachable("Unexpected predicate");
203 }
204
205 Result->takeName(Cmp);
206 Cmp->replaceAllUsesWith(Result);
207 Cmp->eraseFromParent();
208 }
209}
210
211static bool runImpl(Function &F) {
213 runLowerConstExpr(F);
214 return true;
215}
216
221
223 return new SPIRVRegularizerLegacy();
224}
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)
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
#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
static void runLowerI1Comparisons(Function &F)
static bool runImpl(Function &F)
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
Constant Vector Declarations.
Definition Constants.h:660
FixedVectorType * getType() const
Specialize the getType() method to always return a FixedVectorType, which reduces the amount of casti...
Definition Constants.h:683
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
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2847
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:110
Root of the metadata hierarchy.
Definition Metadata.h:64
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
op_iterator op_begin()
Definition User.h:259
op_iterator op_end()
Definition User.h:261
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:509
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.
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:634
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
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.