LLVM 20.0.0git
SPIRVPrepareFunctions.cpp
Go to the documentation of this file.
1//===-- SPIRVPrepareFunctions.cpp - modify function signatures --*- 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 modifies function signatures containing aggregate arguments
10// and/or return value before IRTranslator. Information about the original
11// signatures is stored in metadata. It is used during call lowering to
12// restore correct SPIR-V types of function arguments and return values.
13// This pass also substitutes some llvm intrinsic calls with calls to newly
14// generated functions (as the Khronos LLVM/SPIR-V Translator does).
15//
16// NOTE: this pass is a module-level one due to the necessity to modify
17// GVs/functions.
18//
19//===----------------------------------------------------------------------===//
20
21#include "SPIRV.h"
22#include "SPIRVSubtarget.h"
23#include "SPIRVTargetMachine.h"
24#include "SPIRVUtils.h"
27#include "llvm/IR/IRBuilder.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/IntrinsicsSPIRV.h"
33#include <charconv>
34#include <regex>
35
36using namespace llvm;
37
38namespace llvm {
40}
41
42namespace {
43
44class SPIRVPrepareFunctions : public ModulePass {
45 const SPIRVTargetMachine &TM;
46 bool substituteIntrinsicCalls(Function *F);
47 Function *removeAggregateTypesFromSignature(Function *F);
48
49public:
50 static char ID;
51 SPIRVPrepareFunctions(const SPIRVTargetMachine &TM) : ModulePass(ID), TM(TM) {
53 }
54
55 bool runOnModule(Module &M) override;
56
57 StringRef getPassName() const override { return "SPIRV prepare functions"; }
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 }
62};
63
64} // namespace
65
66char SPIRVPrepareFunctions::ID = 0;
67
68INITIALIZE_PASS(SPIRVPrepareFunctions, "prepare-functions",
69 "SPIRV prepare functions", false, false)
70
71std::string lowerLLVMIntrinsicName(IntrinsicInst *II) {
72 Function *IntrinsicFunc = II->getCalledFunction();
73 assert(IntrinsicFunc && "Missing function");
74 std::string FuncName = IntrinsicFunc->getName().str();
75 std::replace(FuncName.begin(), FuncName.end(), '.', '_');
76 FuncName = "spirv." + FuncName;
77 return FuncName;
78}
79
81 ArrayRef<Type *> ArgTypes,
83 FunctionType *FT = FunctionType::get(RetTy, ArgTypes, false);
84 Function *F = M->getFunction(Name);
85 if (F && F->getFunctionType() == FT)
86 return F;
88 if (F)
89 NewF->setDSOLocal(F->isDSOLocal());
91 return NewF;
92}
93
94static bool lowerIntrinsicToFunction(IntrinsicInst *Intrinsic) {
95 // For @llvm.memset.* intrinsic cases with constant value and length arguments
96 // are emulated via "storing" a constant array to the destination. For other
97 // cases we wrap the intrinsic in @spirv.llvm_memset_* function and expand the
98 // intrinsic to a loop via expandMemSetAsLoop().
99 if (auto *MSI = dyn_cast<MemSetInst>(Intrinsic))
100 if (isa<Constant>(MSI->getValue()) && isa<ConstantInt>(MSI->getLength()))
101 return false; // It is handled later using OpCopyMemorySized.
102
103 Module *M = Intrinsic->getModule();
104 std::string FuncName = lowerLLVMIntrinsicName(Intrinsic);
105 if (Intrinsic->isVolatile())
106 FuncName += ".volatile";
107 // Redirect @llvm.intrinsic.* call to @spirv.llvm_intrinsic_*
108 Function *F = M->getFunction(FuncName);
109 if (F) {
110 Intrinsic->setCalledFunction(F);
111 return true;
112 }
113 // TODO copy arguments attributes: nocapture writeonly.
114 FunctionCallee FC =
115 M->getOrInsertFunction(FuncName, Intrinsic->getFunctionType());
116 auto IntrinsicID = Intrinsic->getIntrinsicID();
117 Intrinsic->setCalledFunction(FC);
118
119 F = dyn_cast<Function>(FC.getCallee());
120 assert(F && "Callee must be a function");
121
122 switch (IntrinsicID) {
123 case Intrinsic::memset: {
124 auto *MSI = static_cast<MemSetInst *>(Intrinsic);
125 Argument *Dest = F->getArg(0);
126 Argument *Val = F->getArg(1);
127 Argument *Len = F->getArg(2);
128 Argument *IsVolatile = F->getArg(3);
129 Dest->setName("dest");
130 Val->setName("val");
131 Len->setName("len");
132 IsVolatile->setName("isvolatile");
133 BasicBlock *EntryBB = BasicBlock::Create(M->getContext(), "entry", F);
134 IRBuilder<> IRB(EntryBB);
135 auto *MemSet = IRB.CreateMemSet(Dest, Val, Len, MSI->getDestAlign(),
136 MSI->isVolatile());
137 IRB.CreateRetVoid();
138 expandMemSetAsLoop(cast<MemSetInst>(MemSet));
139 MemSet->eraseFromParent();
140 break;
141 }
142 case Intrinsic::bswap: {
143 BasicBlock *EntryBB = BasicBlock::Create(M->getContext(), "entry", F);
144 IRBuilder<> IRB(EntryBB);
145 auto *BSwap = IRB.CreateIntrinsic(Intrinsic::bswap, Intrinsic->getType(),
146 F->getArg(0));
147 IRB.CreateRet(BSwap);
148 IntrinsicLowering IL(M->getDataLayout());
149 IL.LowerIntrinsicCall(BSwap);
150 break;
151 }
152 default:
153 break;
154 }
155 return true;
156}
157
158static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal) {
159 if (auto *Ref = dyn_cast_or_null<GetElementPtrInst>(AnnoVal))
160 AnnoVal = Ref->getOperand(0);
161 if (auto *Ref = dyn_cast_or_null<BitCastInst>(OptAnnoVal))
162 OptAnnoVal = Ref->getOperand(0);
163
164 std::string Anno;
165 if (auto *C = dyn_cast_or_null<Constant>(AnnoVal)) {
166 StringRef Str;
167 if (getConstantStringInfo(C, Str))
168 Anno = Str;
169 }
170 // handle optional annotation parameter in a way that Khronos Translator do
171 // (collect integers wrapped in a struct)
172 if (auto *C = dyn_cast_or_null<Constant>(OptAnnoVal);
173 C && C->getNumOperands()) {
174 Value *MaybeStruct = C->getOperand(0);
175 if (auto *Struct = dyn_cast<ConstantStruct>(MaybeStruct)) {
176 for (unsigned I = 0, E = Struct->getNumOperands(); I != E; ++I) {
177 if (auto *CInt = dyn_cast<ConstantInt>(Struct->getOperand(I)))
178 Anno += (I == 0 ? ": " : ", ") +
179 std::to_string(CInt->getType()->getIntegerBitWidth() == 1
180 ? CInt->getZExtValue()
181 : CInt->getSExtValue());
182 }
183 } else if (auto *Struct = dyn_cast<ConstantAggregateZero>(MaybeStruct)) {
184 // { i32 i32 ... } zeroinitializer
185 for (unsigned I = 0, E = Struct->getType()->getStructNumElements();
186 I != E; ++I)
187 Anno += I == 0 ? ": 0" : ", 0";
188 }
189 }
190 return Anno;
191}
192
194 const std::string &Anno,
195 LLVMContext &Ctx,
196 Type *Int32Ty) {
197 // Try to parse the annotation string according to the following rules:
198 // annotation := ({kind} | {kind:value,value,...})+
199 // kind := number
200 // value := number | string
201 static const std::regex R(
202 "\\{(\\d+)(?:[:,](\\d+|\"[^\"]*\")(?:,(\\d+|\"[^\"]*\"))*)?\\}");
204 int Pos = 0;
205 for (std::sregex_iterator
206 It = std::sregex_iterator(Anno.begin(), Anno.end(), R),
207 ItEnd = std::sregex_iterator();
208 It != ItEnd; ++It) {
209 if (It->position() != Pos)
211 Pos = It->position() + It->length();
212 std::smatch Match = *It;
214 for (std::size_t i = 1; i < Match.size(); ++i) {
215 std::ssub_match SMatch = Match[i];
216 std::string Item = SMatch.str();
217 if (Item.length() == 0)
218 break;
219 if (Item[0] == '"') {
220 Item = Item.substr(1, Item.length() - 2);
221 // Acceptable format of the string snippet is:
222 static const std::regex RStr("^(\\d+)(?:,(\\d+))*$");
223 if (std::smatch MatchStr; std::regex_match(Item, MatchStr, RStr)) {
224 for (std::size_t SubIdx = 1; SubIdx < MatchStr.size(); ++SubIdx)
225 if (std::string SubStr = MatchStr[SubIdx].str(); SubStr.length())
227 ConstantInt::get(Int32Ty, std::stoi(SubStr))));
228 } else {
229 MDsItem.push_back(MDString::get(Ctx, Item));
230 }
231 } else if (int32_t Num;
232 std::from_chars(Item.data(), Item.data() + Item.size(), Num)
233 .ec == std::errc{}) {
234 MDsItem.push_back(
235 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Num)));
236 } else {
237 MDsItem.push_back(MDString::get(Ctx, Item));
238 }
239 }
240 if (MDsItem.size() == 0)
242 MDs.push_back(MDNode::get(Ctx, MDsItem));
243 }
244 return Pos == static_cast<int>(Anno.length()) ? MDs
246}
247
249 LLVMContext &Ctx = II->getContext();
250 Type *Int32Ty = Type::getInt32Ty(Ctx);
251
252 // Retrieve an annotation string from arguments.
253 Value *PtrArg = nullptr;
254 if (auto *BI = dyn_cast<BitCastInst>(II->getArgOperand(0)))
255 PtrArg = BI->getOperand(0);
256 else
257 PtrArg = II->getOperand(0);
258 std::string Anno =
259 getAnnotation(II->getArgOperand(1),
260 4 < II->arg_size() ? II->getArgOperand(4) : nullptr);
261
262 // Parse the annotation.
263 SmallVector<Metadata *> MDs = parseAnnotation(II, Anno, Ctx, Int32Ty);
264
265 // If the annotation string is not parsed successfully we don't know the
266 // format used and output it as a general UserSemantic decoration.
267 // Otherwise MDs is a Metadata tuple (a decoration list) in the format
268 // expected by `spirv.Decorations`.
269 if (MDs.size() == 0) {
270 auto UserSemantic = ConstantAsMetadata::get(ConstantInt::get(
271 Int32Ty, static_cast<uint32_t>(SPIRV::Decoration::UserSemantic)));
272 MDs.push_back(MDNode::get(Ctx, {UserSemantic, MDString::get(Ctx, Anno)}));
273 }
274
275 // Build the internal intrinsic function.
276 IRBuilder<> IRB(II->getParent());
277 IRB.SetInsertPoint(II);
278 IRB.CreateIntrinsic(
279 Intrinsic::spv_assign_decoration, {PtrArg->getType()},
280 {PtrArg, MetadataAsValue::get(Ctx, MDNode::get(Ctx, MDs))});
281 II->replaceAllUsesWith(II->getOperand(0));
282}
283
284static void lowerFunnelShifts(IntrinsicInst *FSHIntrinsic) {
285 // Get a separate function - otherwise, we'd have to rework the CFG of the
286 // current one. Then simply replace the intrinsic uses with a call to the new
287 // function.
288 // Generate LLVM IR for i* @spirv.llvm_fsh?_i* (i* %a, i* %b, i* %c)
289 Module *M = FSHIntrinsic->getModule();
290 FunctionType *FSHFuncTy = FSHIntrinsic->getFunctionType();
291 Type *FSHRetTy = FSHFuncTy->getReturnType();
292 const std::string FuncName = lowerLLVMIntrinsicName(FSHIntrinsic);
293 Function *FSHFunc =
294 getOrCreateFunction(M, FSHRetTy, FSHFuncTy->params(), FuncName);
295
296 if (!FSHFunc->empty()) {
297 FSHIntrinsic->setCalledFunction(FSHFunc);
298 return;
299 }
300 BasicBlock *RotateBB = BasicBlock::Create(M->getContext(), "rotate", FSHFunc);
301 IRBuilder<> IRB(RotateBB);
302 Type *Ty = FSHFunc->getReturnType();
303 // Build the actual funnel shift rotate logic.
304 // In the comments, "int" is used interchangeably with "vector of int
305 // elements".
306 FixedVectorType *VectorTy = dyn_cast<FixedVectorType>(Ty);
307 Type *IntTy = VectorTy ? VectorTy->getElementType() : Ty;
308 unsigned BitWidth = IntTy->getIntegerBitWidth();
309 ConstantInt *BitWidthConstant = IRB.getInt({BitWidth, BitWidth});
310 Value *BitWidthForInsts =
311 VectorTy
312 ? IRB.CreateVectorSplat(VectorTy->getNumElements(), BitWidthConstant)
313 : BitWidthConstant;
314 Value *RotateModVal =
315 IRB.CreateURem(/*Rotate*/ FSHFunc->getArg(2), BitWidthForInsts);
316 Value *FirstShift = nullptr, *SecShift = nullptr;
317 if (FSHIntrinsic->getIntrinsicID() == Intrinsic::fshr) {
318 // Shift the less significant number right, the "rotate" number of bits
319 // will be 0-filled on the left as a result of this regular shift.
320 FirstShift = IRB.CreateLShr(FSHFunc->getArg(1), RotateModVal);
321 } else {
322 // Shift the more significant number left, the "rotate" number of bits
323 // will be 0-filled on the right as a result of this regular shift.
324 FirstShift = IRB.CreateShl(FSHFunc->getArg(0), RotateModVal);
325 }
326 // We want the "rotate" number of the more significant int's LSBs (MSBs) to
327 // occupy the leftmost (rightmost) "0 space" left by the previous operation.
328 // Therefore, subtract the "rotate" number from the integer bitsize...
329 Value *SubRotateVal = IRB.CreateSub(BitWidthForInsts, RotateModVal);
330 if (FSHIntrinsic->getIntrinsicID() == Intrinsic::fshr) {
331 // ...and left-shift the more significant int by this number, zero-filling
332 // the LSBs.
333 SecShift = IRB.CreateShl(FSHFunc->getArg(0), SubRotateVal);
334 } else {
335 // ...and right-shift the less significant int by this number, zero-filling
336 // the MSBs.
337 SecShift = IRB.CreateLShr(FSHFunc->getArg(1), SubRotateVal);
338 }
339 // A simple binary addition of the shifted ints yields the final result.
340 IRB.CreateRet(IRB.CreateOr(FirstShift, SecShift));
341
342 FSHIntrinsic->setCalledFunction(FSHFunc);
343}
344
346 // If we cannot use the SPV_KHR_expect_assume extension, then we need to
347 // ignore the intrinsic and move on. It should be removed later on by LLVM.
348 // Otherwise we should lower the intrinsic to the corresponding SPIR-V
349 // instruction.
350 // For @llvm.assume we have OpAssumeTrueKHR.
351 // For @llvm.expect we have OpExpectKHR.
352 //
353 // We need to lower this into a builtin and then the builtin into a SPIR-V
354 // instruction.
355 if (II->getIntrinsicID() == Intrinsic::assume) {
357 II->getModule(), Intrinsic::SPVIntrinsics::spv_assume);
358 II->setCalledFunction(F);
359 } else if (II->getIntrinsicID() == Intrinsic::expect) {
361 II->getModule(), Intrinsic::SPVIntrinsics::spv_expect,
362 {II->getOperand(0)->getType()});
363 II->setCalledFunction(F);
364 } else {
365 llvm_unreachable("Unknown intrinsic");
366 }
367
368 return;
369}
370
372 ArrayRef<unsigned> OpNos) {
373 Function *F = nullptr;
374 if (OpNos.empty()) {
375 F = Intrinsic::getOrInsertDeclaration(II->getModule(), NewID);
376 } else {
378 for (unsigned OpNo : OpNos)
379 Tys.push_back(II->getOperand(OpNo)->getType());
380 F = Intrinsic::getOrInsertDeclaration(II->getModule(), NewID, Tys);
381 }
382 II->setCalledFunction(F);
383 return true;
384}
385
386// Substitutes calls to LLVM intrinsics with either calls to SPIR-V intrinsics
387// or calls to proper generated functions. Returns True if F was modified.
388bool SPIRVPrepareFunctions::substituteIntrinsicCalls(Function *F) {
389 bool Changed = false;
390 for (BasicBlock &BB : *F) {
391 for (Instruction &I : BB) {
392 auto Call = dyn_cast<CallInst>(&I);
393 if (!Call)
394 continue;
395 Function *CF = Call->getCalledFunction();
396 if (!CF || !CF->isIntrinsic())
397 continue;
398 auto *II = cast<IntrinsicInst>(Call);
399 switch (II->getIntrinsicID()) {
400 case Intrinsic::memset:
401 case Intrinsic::bswap:
402 Changed |= lowerIntrinsicToFunction(II);
403 break;
404 case Intrinsic::fshl:
405 case Intrinsic::fshr:
407 Changed = true;
408 break;
409 case Intrinsic::assume:
410 case Intrinsic::expect: {
411 const SPIRVSubtarget &STI = TM.getSubtarget<SPIRVSubtarget>(*F);
412 if (STI.canUseExtension(SPIRV::Extension::SPV_KHR_expect_assume))
414 Changed = true;
415 } break;
416 case Intrinsic::lifetime_start:
417 Changed |= toSpvOverloadedIntrinsic(
418 II, Intrinsic::SPVIntrinsics::spv_lifetime_start, {1});
419 break;
420 case Intrinsic::lifetime_end:
421 Changed |= toSpvOverloadedIntrinsic(
422 II, Intrinsic::SPVIntrinsics::spv_lifetime_end, {1});
423 break;
424 case Intrinsic::ptr_annotation:
426 Changed = true;
427 break;
428 }
429 }
430 }
431 return Changed;
432}
433
434// Returns F if aggregate argument/return types are not present or cloned F
435// function with the types replaced by i32 types. The change in types is
436// noted in 'spv.cloned_funcs' metadata for later restoration.
437Function *
438SPIRVPrepareFunctions::removeAggregateTypesFromSignature(Function *F) {
439 bool IsRetAggr = F->getReturnType()->isAggregateType();
440 // Allow intrinsics with aggregate return type to reach GlobalISel
441 if (F->isIntrinsic() && IsRetAggr)
442 return F;
443
444 IRBuilder<> B(F->getContext());
445
446 bool HasAggrArg =
447 std::any_of(F->arg_begin(), F->arg_end(), [](Argument &Arg) {
448 return Arg.getType()->isAggregateType();
449 });
450 bool DoClone = IsRetAggr || HasAggrArg;
451 if (!DoClone)
452 return F;
453 SmallVector<std::pair<int, Type *>, 4> ChangedTypes;
454 Type *RetType = IsRetAggr ? B.getInt32Ty() : F->getReturnType();
455 if (IsRetAggr)
456 ChangedTypes.push_back(std::pair<int, Type *>(-1, F->getReturnType()));
457 SmallVector<Type *, 4> ArgTypes;
458 for (const auto &Arg : F->args()) {
459 if (Arg.getType()->isAggregateType()) {
460 ArgTypes.push_back(B.getInt32Ty());
461 ChangedTypes.push_back(
462 std::pair<int, Type *>(Arg.getArgNo(), Arg.getType()));
463 } else
464 ArgTypes.push_back(Arg.getType());
465 }
466 FunctionType *NewFTy =
467 FunctionType::get(RetType, ArgTypes, F->getFunctionType()->isVarArg());
468 Function *NewF =
469 Function::Create(NewFTy, F->getLinkage(), F->getName(), *F->getParent());
470
472 auto NewFArgIt = NewF->arg_begin();
473 for (auto &Arg : F->args()) {
474 StringRef ArgName = Arg.getName();
475 NewFArgIt->setName(ArgName);
476 VMap[&Arg] = &(*NewFArgIt++);
477 }
479
480 CloneFunctionInto(NewF, F, VMap, CloneFunctionChangeType::LocalChangesOnly,
481 Returns);
482 NewF->takeName(F);
483
484 NamedMDNode *FuncMD =
485 F->getParent()->getOrInsertNamedMetadata("spv.cloned_funcs");
487 MDArgs.push_back(MDString::get(B.getContext(), NewF->getName()));
488 for (auto &ChangedTyP : ChangedTypes)
489 MDArgs.push_back(MDNode::get(
490 B.getContext(),
491 {ConstantAsMetadata::get(B.getInt32(ChangedTyP.first)),
492 ValueAsMetadata::get(Constant::getNullValue(ChangedTyP.second))}));
493 MDNode *ThisFuncMD = MDNode::get(B.getContext(), MDArgs);
494 FuncMD->addOperand(ThisFuncMD);
495
496 for (auto *U : make_early_inc_range(F->users())) {
497 if (auto *CI = dyn_cast<CallInst>(U))
498 CI->mutateFunctionType(NewF->getFunctionType());
499 U->replaceUsesOfWith(F, NewF);
500 }
501
502 // register the mutation
503 if (RetType != F->getReturnType())
505 NewF, F->getReturnType());
506 return NewF;
507}
508
509bool SPIRVPrepareFunctions::runOnModule(Module &M) {
510 bool Changed = false;
511 for (Function &F : M) {
512 Changed |= substituteIntrinsicCalls(&F);
513 Changed |= sortBlocks(F);
514 }
515
516 std::vector<Function *> FuncsWorklist;
517 for (auto &F : M)
518 FuncsWorklist.push_back(&F);
519
520 for (auto *F : FuncsWorklist) {
521 Function *NewF = removeAggregateTypesFromSignature(F);
522
523 if (NewF != F) {
524 F->eraseFromParent();
525 Changed = true;
526 }
527 }
528 return Changed;
529}
530
533 return new SPIRVPrepareFunctions(TM);
534}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
return RetTy
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void lowerFunnelShifts(IntrinsicInst *FSHIntrinsic)
static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal)
static bool toSpvOverloadedIntrinsic(IntrinsicInst *II, Intrinsic::ID NewID, ArrayRef< unsigned > OpNos)
static bool lowerIntrinsicToFunction(IntrinsicInst *Intrinsic)
static void lowerPtrAnnotation(IntrinsicInst *II)
static SmallVector< Metadata * > parseAnnotation(Value *I, const std::string &Anno, LLVMContext &Ctx, Type *Int32Ty)
static void lowerExpectAssume(IntrinsicInst *II)
static Function * getOrCreateFunction(Module *M, Type *RetTy, ArrayRef< Type * > ArgTypes, StringRef Name)
@ Struct
Represent the analysis usage information of a pass.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1207
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1388
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:563
unsigned getNumElements() const
Definition: DerivedTypes.h:606
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:173
bool empty() const
Definition: Function.h:859
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:216
arg_iterator arg_begin()
Definition: Function.h:868
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition: Function.h:256
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:221
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:281
Argument * getArg(unsigned i) const
Definition: Function.h:886
void setDSOLocal(bool Local)
Definition: GlobalValue.h:303
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1152
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:890
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:592
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1460
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
Definition: IRBuilder.h:1119
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1367
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1439
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1114
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1520
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:177
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:499
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1427
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:66
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:55
void LowerIntrinsicCall(CallInst *CI)
Replace a call to the specified intrinsic function.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1543
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1731
void addOperand(MDNode *M)
Definition: Metadata.cpp:1431
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
void addMutated(Value *Val, Type *Ty)
SPIRVGlobalRegistry * getSPIRVGlobalRegistry() const
bool canUseExtension(SPIRV::Extension::Extension E) const
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
Type * getElementType() const
Definition: DerivedTypes.h:460
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SPIR_FUNC
Used for SPIR non-kernel device functions.
Definition: CallingConv.h:138
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:731
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeSPIRVPrepareFunctionsPass(PassRegistry &)
bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
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:657
bool sortBlocks(Function &F)
Definition: SPIRVUtils.cpp:679
@ Ref
The access may reference the value stored in memory.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
ModulePass * createSPIRVPrepareFunctionsPass(const SPIRVTargetMachine &TM)
void expandMemSetAsLoop(MemSetInst *MemSet)
Expand MemSet as a loop. MemSet is not deleted.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858