xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SPIRV/SPIRVUtils.h (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
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 
16 #include "MCTargetDesc/SPIRVBaseInfo.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include <string>
19 
20 namespace llvm {
21 class MCInst;
22 class MachineFunction;
23 class MachineInstr;
24 class MachineInstrBuilder;
25 class MachineIRBuilder;
26 class MachineRegisterInfo;
27 class Register;
28 class StringRef;
29 class 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.
34 void addStringImm(const StringRef &Str, MCInst &Inst);
35 void addStringImm(const StringRef &Str, MachineInstrBuilder &MIB);
36 void 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.
41 std::string getStringImm(const MachineInstr &MI, unsigned StartIndex);
42 
43 // Add the given numerical immediate to MIB.
44 void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB);
45 
46 // Add an OpName instruction for the given target register.
47 void buildOpName(Register Target, const StringRef &Name,
48                  MachineIRBuilder &MIRBuilder);
49 
50 // Add an OpDecorate instruction for the given Reg.
51 void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder,
52                      SPIRV::Decoration::Decoration Dec,
53                      const std::vector<uint32_t> &DecArgs,
54                      StringRef StrImm = "");
55 void 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.
61 unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC);
62 
63 // Convert an LLVM IR address space to a SPIR-V storage class.
64 SPIRV::StorageClass::StorageClass
65 addressSpaceToStorageClass(unsigned AddrSpace);
66 
67 SPIRV::MemorySemantics::MemorySemantics
68 getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC);
69 
70 SPIRV::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.
75 MachineInstr *getDefInstrMaybeConstant(Register &ConstReg,
76                                        const MachineRegisterInfo *MRI);
77 
78 // Get constant integer value of the given ConstReg.
79 uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI);
80 
81 // Check if MI is a SPIR-V specific intrinsic call.
82 bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID);
83 
84 // Get type of i-th operand of the metadata node.
85 Type *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.
89 std::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.
93 const Type *getTypedPtrEltType(const Type *Type);
94 
95 // Check if a string contains a builtin prefix.
96 bool hasBuiltinTypePrefix(StringRef Name);
97 
98 // Check if given LLVM type is a special opaque builtin type.
99 bool isSpecialOpaqueType(const Type *Ty);
100 } // namespace llvm
101 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
102