xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SPIRV/SPIRVUtils.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
181ad6265SDimitry Andric //===--- SPIRVUtils.h ---- SPIR-V Utility Functions -------------*- C++ -*-===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric //
981ad6265SDimitry Andric // This file contains miscellaneous utility functions.
1081ad6265SDimitry Andric //
1181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
1481ad6265SDimitry Andric #define LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
1581ad6265SDimitry Andric 
1681ad6265SDimitry Andric #include "MCTargetDesc/SPIRVBaseInfo.h"
1781ad6265SDimitry Andric #include "llvm/IR/IRBuilder.h"
1881ad6265SDimitry Andric #include <string>
1981ad6265SDimitry Andric 
2081ad6265SDimitry Andric namespace llvm {
2181ad6265SDimitry Andric class MCInst;
2281ad6265SDimitry Andric class MachineFunction;
2381ad6265SDimitry Andric class MachineInstr;
2481ad6265SDimitry Andric class MachineInstrBuilder;
2581ad6265SDimitry Andric class MachineIRBuilder;
2681ad6265SDimitry Andric class MachineRegisterInfo;
2781ad6265SDimitry Andric class Register;
2881ad6265SDimitry Andric class StringRef;
2981ad6265SDimitry Andric class SPIRVInstrInfo;
3081ad6265SDimitry Andric 
3181ad6265SDimitry Andric // Add the given string as a series of integer operand, inserting null
3281ad6265SDimitry Andric // terminators and padding to make sure the operands all have 32-bit
3381ad6265SDimitry Andric // little-endian words.
34*bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, MCInst &Inst);
35*bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, MachineInstrBuilder &MIB);
36*bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, IRBuilder<> &B,
37*bdd1243dSDimitry Andric                   std::vector<Value *> &Args);
3881ad6265SDimitry Andric 
3981ad6265SDimitry Andric // Read the series of integer operands back as a null-terminated string using
4081ad6265SDimitry Andric // the reverse of the logic in addStringImm.
41*bdd1243dSDimitry Andric std::string getStringImm(const MachineInstr &MI, unsigned StartIndex);
4281ad6265SDimitry Andric 
4381ad6265SDimitry Andric // Add the given numerical immediate to MIB.
44*bdd1243dSDimitry Andric void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB);
4581ad6265SDimitry Andric 
4681ad6265SDimitry Andric // Add an OpName instruction for the given target register.
47*bdd1243dSDimitry Andric void buildOpName(Register Target, const StringRef &Name,
48*bdd1243dSDimitry Andric                  MachineIRBuilder &MIRBuilder);
4981ad6265SDimitry Andric 
5081ad6265SDimitry Andric // Add an OpDecorate instruction for the given Reg.
51*bdd1243dSDimitry Andric void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder,
52*bdd1243dSDimitry Andric                      SPIRV::Decoration::Decoration Dec,
5381ad6265SDimitry Andric                      const std::vector<uint32_t> &DecArgs,
54*bdd1243dSDimitry Andric                      StringRef StrImm = "");
55*bdd1243dSDimitry Andric void buildOpDecorate(Register Reg, MachineInstr &I, const SPIRVInstrInfo &TII,
56*bdd1243dSDimitry Andric                      SPIRV::Decoration::Decoration Dec,
5781ad6265SDimitry Andric                      const std::vector<uint32_t> &DecArgs,
58*bdd1243dSDimitry Andric                      StringRef StrImm = "");
5981ad6265SDimitry Andric 
6081ad6265SDimitry Andric // Convert a SPIR-V storage class to the corresponding LLVM IR address space.
61*bdd1243dSDimitry Andric unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC);
6281ad6265SDimitry Andric 
6381ad6265SDimitry Andric // Convert an LLVM IR address space to a SPIR-V storage class.
64*bdd1243dSDimitry Andric SPIRV::StorageClass::StorageClass
65*bdd1243dSDimitry Andric addressSpaceToStorageClass(unsigned AddrSpace);
6681ad6265SDimitry Andric 
67*bdd1243dSDimitry Andric SPIRV::MemorySemantics::MemorySemantics
68*bdd1243dSDimitry Andric getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC);
6981ad6265SDimitry Andric 
70*bdd1243dSDimitry Andric SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord);
71fcaf7f86SDimitry Andric 
7281ad6265SDimitry Andric // Find def instruction for the given ConstReg, walking through
7381ad6265SDimitry Andric // spv_track_constant and ASSIGN_TYPE instructions. Updates ConstReg by def
7481ad6265SDimitry Andric // of OpConstant instruction.
75*bdd1243dSDimitry Andric MachineInstr *getDefInstrMaybeConstant(Register &ConstReg,
76*bdd1243dSDimitry Andric                                        const MachineRegisterInfo *MRI);
7781ad6265SDimitry Andric 
7881ad6265SDimitry Andric // Get constant integer value of the given ConstReg.
79*bdd1243dSDimitry Andric uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI);
8081ad6265SDimitry Andric 
81fcaf7f86SDimitry Andric // Check if MI is a SPIR-V specific intrinsic call.
82*bdd1243dSDimitry Andric bool isSpvIntrinsic(MachineInstr &MI, Intrinsic::ID IntrinsicID);
83fcaf7f86SDimitry Andric 
8481ad6265SDimitry Andric // Get type of i-th operand of the metadata node.
85*bdd1243dSDimitry Andric Type *getMDOperandAsType(const MDNode *N, unsigned I);
86*bdd1243dSDimitry Andric 
87*bdd1243dSDimitry Andric // If OpenCL or SPIR-V builtin function name is recognized, return a demangled
88*bdd1243dSDimitry Andric // name, otherwise return an empty string.
89*bdd1243dSDimitry Andric std::string getOclOrSpirvBuiltinDemangledName(StringRef Name);
90*bdd1243dSDimitry Andric 
91*bdd1243dSDimitry Andric // If Type is a pointer type and it is not opaque pointer, return its
92*bdd1243dSDimitry Andric // element type, otherwise return Type.
93*bdd1243dSDimitry Andric const Type *getTypedPtrEltType(const Type *Type);
94*bdd1243dSDimitry Andric 
95*bdd1243dSDimitry Andric // Check if given LLVM type is a special opaque builtin type.
96*bdd1243dSDimitry Andric bool isSpecialOpaqueType(const Type *Ty);
97*bdd1243dSDimitry Andric } // namespace llvm
9881ad6265SDimitry Andric #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H
99