xref: /llvm-project/llvm/lib/Target/SPIRV/SPIRVBuiltins.h (revision 3ed2a81358e11a582eb5cc3edf711447767036e6)
1 //===-- SPIRVBuiltins.h - SPIR-V Built-in 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 // Lowering builtin function calls and types using their demangled names.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
14 #define LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
15 
16 #include "SPIRVGlobalRegistry.h"
17 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
18 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
19 
20 namespace llvm {
21 namespace SPIRV {
22 /// Parses the name part of the demangled builtin call.
23 std::string lookupBuiltinNameHelper(StringRef DemangledCall,
24                                     FPDecorationId *DecorationId = nullptr);
25 /// Lowers a builtin function call using the provided \p DemangledCall skeleton
26 /// and external instruction \p Set.
27 ///
28 /// \return the lowering success status if the called function is a recognized
29 /// builtin, std::nullopt otherwise.
30 ///
31 /// \p DemangledCall is the skeleton of the lowered builtin function call.
32 /// \p Set is the external instruction set containing the given builtin.
33 /// \p OrigRet is the single original virtual return register if defined,
34 /// Register(0) otherwise.
35 /// \p OrigRetTy is the type of the \p OrigRet.
36 /// \p Args are the arguments of the lowered builtin call.
37 std::optional<bool> lowerBuiltin(const StringRef DemangledCall,
38                                  InstructionSet::InstructionSet Set,
39                                  MachineIRBuilder &MIRBuilder,
40                                  const Register OrigRet, const Type *OrigRetTy,
41                                  const SmallVectorImpl<Register> &Args,
42                                  SPIRVGlobalRegistry *GR);
43 
44 /// Helper function for finding a builtin function attributes
45 /// by a demangled function name. Defined in SPIRVBuiltins.cpp.
46 std::tuple<int, unsigned, unsigned>
47 mapBuiltinToOpcode(const StringRef DemangledCall,
48                    SPIRV::InstructionSet::InstructionSet Set);
49 
50 /// Parses the provided \p ArgIdx argument base type in the \p DemangledCall
51 /// skeleton. A base type is either a basic type (e.g. i32 for int), pointer
52 /// element type (e.g. i8 for char*), or builtin type (TargetExtType).
53 ///
54 /// \return LLVM Type or nullptr if unrecognized
55 ///
56 /// \p DemangledCall is the skeleton of the lowered builtin function call.
57 /// \p ArgIdx is the index of the argument to parse.
58 Type *parseBuiltinCallArgumentBaseType(const StringRef DemangledCall,
59                                        unsigned ArgIdx, LLVMContext &Ctx);
60 bool parseBuiltinTypeStr(SmallVector<StringRef, 10> &BuiltinArgsTypeStrs,
61                          const StringRef DemangledCall, LLVMContext &Ctx);
62 Type *parseBuiltinCallArgumentType(StringRef TypeStr, LLVMContext &Ctx);
63 
64 /// Translates a string representing a SPIR-V or OpenCL builtin type to a
65 /// TargetExtType that can be further lowered with lowerBuiltinType().
66 ///
67 /// \return A TargetExtType representing the builtin SPIR-V type.
68 ///
69 /// \p TypeName is the full string representation of the SPIR-V or OpenCL
70 /// builtin type.
71 TargetExtType *parseBuiltinTypeNameToTargetExtType(std::string TypeName,
72                                                    LLVMContext &Context);
73 
74 /// Handles the translation of the provided special opaque/builtin type \p Type
75 /// to SPIR-V type. Generates the corresponding machine instructions for the
76 /// target type or gets the already existing OpType<...> register from the
77 /// global registry \p GR.
78 ///
79 /// \return A machine instruction representing the OpType<...> SPIR-V type.
80 ///
81 /// \p Type is the special opaque/builtin type to be lowered.
82 SPIRVType *lowerBuiltinType(const Type *Type,
83                             AccessQualifier::AccessQualifier AccessQual,
84                             MachineIRBuilder &MIRBuilder,
85                             SPIRVGlobalRegistry *GR);
86 } // namespace SPIRV
87 } // namespace llvm
88 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
89