LLVM 18.0.0git
SPIRVUtils.h
Go to the documentation of this file.
1//===--- SPIRVUtils.h ---- SPIR-V Utility Functions -------------*- 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 file contains miscellaneous utility functions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
14#define LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
15
17#include "llvm/IR/IRBuilder.h"
18#include <string>
19
20namespace llvm {
21class MCInst;
22class MachineFunction;
23class MachineInstr;
24class MachineInstrBuilder;
25class MachineIRBuilder;
26class MachineRegisterInfo;
27class Register;
28class StringRef;
29class SPIRVInstrInfo;
30
31// Add the given string as a series of integer operand, inserting null
32// terminators and padding to make sure the operands all have 32-bit
33// little-endian words.
34void addStringImm(const StringRef &Str, MCInst &Inst);
35void addStringImm(const StringRef &Str, MachineInstrBuilder &MIB);
36void addStringImm(const StringRef &Str, IRBuilder<> &B,
37 std::vector<Value *> &Args);
38
39// Read the series of integer operands back as a null-terminated string using
40// the reverse of the logic in addStringImm.
41std::string getStringImm(const MachineInstr &MI, unsigned StartIndex);
42
43// Add the given numerical immediate to MIB.
44void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB);
45
46// Add an OpName instruction for the given target register.
47void buildOpName(Register Target, const StringRef &Name,
48 MachineIRBuilder &MIRBuilder);
49
50// Add an OpDecorate instruction for the given Reg.
51void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder,
52 SPIRV::Decoration::Decoration Dec,
53 const std::vector<uint32_t> &DecArgs,
54 StringRef StrImm = "");
55void buildOpDecorate(Register Reg, MachineInstr &I, const SPIRVInstrInfo &TII,
56 SPIRV::Decoration::Decoration Dec,
57 const std::vector<uint32_t> &DecArgs,
58 StringRef StrImm = "");
59
60// Convert a SPIR-V storage class to the corresponding LLVM IR address space.
61unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC);
62
63// Convert an LLVM IR address space to a SPIR-V storage class.
64SPIRV::StorageClass::StorageClass
65addressSpaceToStorageClass(unsigned AddrSpace);
66
67SPIRV::MemorySemantics::MemorySemantics
68getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC);
69
70SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord);
71
72// Find def instruction for the given ConstReg, walking through
73// spv_track_constant and ASSIGN_TYPE instructions. Updates ConstReg by def
74// of OpConstant instruction.
75MachineInstr *getDefInstrMaybeConstant(Register &ConstReg,
76 const MachineRegisterInfo *MRI);
77
78// Get constant integer value of the given ConstReg.
79uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI);
80
81// Check if MI is a SPIR-V specific intrinsic call.
82bool isSpvIntrinsic(MachineInstr &MI, Intrinsic::ID IntrinsicID);
83
84// Get type of i-th operand of the metadata node.
85Type *getMDOperandAsType(const MDNode *N, unsigned I);
86
87// If OpenCL or SPIR-V builtin function name is recognized, return a demangled
88// name, otherwise return an empty string.
89std::string getOclOrSpirvBuiltinDemangledName(StringRef Name);
90
91// If Type is a pointer type and it is not opaque pointer, return its
92// element type, otherwise return Type.
93const Type *getTypedPtrEltType(const Type *Type);
94
95// Check if a string contains a builtin prefix.
96bool hasBuiltinTypePrefix(StringRef Name);
97
98// Check if given LLVM type is a special opaque builtin type.
99bool isSpecialOpaqueType(const Type *Ty);
100} // namespace llvm
101#endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
RelocType Type
Definition: COFFYAML.cpp:391
std::string Name
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned Reg
Promote Memory to Register
Definition: Mem2Reg.cpp:114
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
Definition: SPIRVUtils.cpp:96
unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition: SPIRVUtils.cpp:134
std::string getStringImm(const MachineInstr &MI, unsigned StartIndex)
Definition: SPIRVUtils.cpp:75
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
Definition: SPIRVUtils.cpp:79
uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI)
Definition: SPIRVUtils.cpp:225
SPIRV::MemorySemantics::MemorySemantics getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC)
Definition: SPIRVUtils.cpp:174
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
Definition: SPIRVUtils.cpp:290
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Definition: SPIRVUtils.cpp:113
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace)
Definition: SPIRVUtils.cpp:154
bool isSpecialOpaqueType(const Type *Ty)
Definition: SPIRVUtils.cpp:341
bool isSpvIntrinsic(MachineInstr &MI, Intrinsic::ID IntrinsicID)
Definition: SPIRVUtils.cpp:231
AtomicOrdering
Atomic ordering for LLVM's memory model.
const Type * getTypedPtrEltType(const Type *Ty)
Definition: SPIRVUtils.cpp:328
MachineInstr * getDefInstrMaybeConstant(Register &ConstReg, const MachineRegisterInfo *MRI)
Definition: SPIRVUtils.cpp:210
bool hasBuiltinTypePrefix(StringRef Name)
Definition: SPIRVUtils.cpp:335
Type * getMDOperandAsType(const MDNode *N, unsigned I)
Definition: SPIRVUtils.cpp:237
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:50
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)
Definition: SPIRVUtils.cpp:192
#define N