1 //===- LLVMTypes.h - MLIR LLVM dialect types --------------------*- 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 defines the types for the LLVM dialect in MLIR. These MLIR types 10 // correspond to the LLVM IR type system. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_DIALECT_LLVMIR_LLVMTYPES_H_ 15 #define MLIR_DIALECT_LLVMIR_LLVMTYPES_H_ 16 17 #include "mlir/IR/Types.h" 18 #include "mlir/Interfaces/DataLayoutInterfaces.h" 19 #include "mlir/Interfaces/MemorySlotInterfaces.h" 20 #include <optional> 21 22 namespace llvm { 23 class ElementCount; 24 class TypeSize; 25 } // namespace llvm 26 27 namespace mlir { 28 29 class AsmParser; 30 class AsmPrinter; 31 32 namespace LLVM { 33 class LLVMDialect; 34 35 namespace detail { 36 struct LLVMFunctionTypeStorage; 37 struct LLVMPointerTypeStorage; 38 struct LLVMStructTypeStorage; 39 struct LLVMTypeAndSizeStorage; 40 } // namespace detail 41 } // namespace LLVM 42 } // namespace mlir 43 44 //===----------------------------------------------------------------------===// 45 // ODS-Generated Declarations 46 //===----------------------------------------------------------------------===// 47 48 #include "mlir/Dialect/LLVMIR/LLVMTypeInterfaces.h.inc" 49 50 #define GET_TYPEDEF_CLASSES 51 #include "mlir/Dialect/LLVMIR/LLVMTypes.h.inc" 52 53 namespace mlir { 54 namespace LLVM { 55 56 //===----------------------------------------------------------------------===// 57 // Trivial types. 58 //===----------------------------------------------------------------------===// 59 60 // Batch-define trivial types. 61 #define DEFINE_TRIVIAL_LLVM_TYPE(ClassName, TypeName) \ 62 class ClassName : public Type::TypeBase<ClassName, Type, TypeStorage> { \ 63 public: \ 64 using Base::Base; \ 65 static constexpr StringLiteral name = TypeName; \ 66 } 67 68 DEFINE_TRIVIAL_LLVM_TYPE(LLVMVoidType, "llvm.void"); 69 DEFINE_TRIVIAL_LLVM_TYPE(LLVMPPCFP128Type, "llvm.ppc_fp128"); 70 DEFINE_TRIVIAL_LLVM_TYPE(LLVMTokenType, "llvm.token"); 71 DEFINE_TRIVIAL_LLVM_TYPE(LLVMLabelType, "llvm.label"); 72 DEFINE_TRIVIAL_LLVM_TYPE(LLVMMetadataType, "llvm.metadata"); 73 74 #undef DEFINE_TRIVIAL_LLVM_TYPE 75 76 //===----------------------------------------------------------------------===// 77 // Printing and parsing. 78 //===----------------------------------------------------------------------===// 79 80 namespace detail { 81 /// Parses an LLVM dialect type. 82 Type parseType(DialectAsmParser &parser); 83 84 /// Prints an LLVM Dialect type. 85 void printType(Type type, AsmPrinter &printer); 86 } // namespace detail 87 88 /// Parse any MLIR type or a concise syntax for LLVM types. 89 ParseResult parsePrettyLLVMType(AsmParser &p, Type &type); 90 /// Print any MLIR type or a concise syntax for LLVM types. 91 void printPrettyLLVMType(AsmPrinter &p, Type type); 92 93 //===----------------------------------------------------------------------===// 94 // Utility functions. 95 //===----------------------------------------------------------------------===// 96 97 /// Returns `true` if the given type is compatible with the LLVM dialect. This 98 /// is an alias to `LLVMDialect::isCompatibleType`. 99 bool isCompatibleType(Type type); 100 101 /// Returns `true` if the given outer type is compatible with the LLVM dialect 102 /// without checking its potential nested types such as struct elements. 103 bool isCompatibleOuterType(Type type); 104 105 /// Returns `true` if the given type is a floating-point type compatible with 106 /// the LLVM dialect. 107 bool isCompatibleFloatingPointType(Type type); 108 109 /// Returns `true` if the given type is a vector type compatible with the LLVM 110 /// dialect. Compatible types include 1D built-in vector types of built-in 111 /// integers and floating-point values, LLVM dialect fixed vector types of LLVM 112 /// dialect pointers and LLVM dialect scalable vector types. 113 bool isCompatibleVectorType(Type type); 114 115 /// Returns the element type of any vector type compatible with the LLVM 116 /// dialect. 117 Type getVectorElementType(Type type); 118 119 /// Returns the element count of any LLVM-compatible vector type. 120 llvm::ElementCount getVectorNumElements(Type type); 121 122 /// Returns whether a vector type is scalable or not. 123 bool isScalableVectorType(Type vectorType); 124 125 /// Creates an LLVM dialect-compatible vector type with the given element type 126 /// and length. 127 Type getVectorType(Type elementType, unsigned numElements, 128 bool isScalable = false); 129 130 /// Creates an LLVM dialect-compatible vector type with the given element type 131 /// and length. 132 Type getVectorType(Type elementType, const llvm::ElementCount &numElements); 133 134 /// Creates an LLVM dialect-compatible type with the given element type and 135 /// length. 136 Type getFixedVectorType(Type elementType, unsigned numElements); 137 138 /// Creates an LLVM dialect-compatible type with the given element type and 139 /// length. 140 Type getScalableVectorType(Type elementType, unsigned numElements); 141 142 /// Returns the size of the given primitive LLVM dialect-compatible type 143 /// (including vectors) in bits, for example, the size of i16 is 16 and 144 /// the size of vector<4xi16> is 64. Returns 0 for non-primitive 145 /// (aggregates such as struct) or types that don't have a size (such as void). 146 llvm::TypeSize getPrimitiveTypeSizeInBits(Type type); 147 148 /// The positions of different values in the data layout entry for pointers. 149 enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 }; 150 151 /// Returns the value that corresponds to named position `pos` from the 152 /// data layout entry `attr` assuming it's a dense integer elements attribute. 153 /// Returns `std::nullopt` if `pos` is not present in the entry. 154 /// Currently only `PtrDLEntryPos::Index` is optional, and all other positions 155 /// may be assumed to be present. 156 std::optional<uint64_t> extractPointerSpecValue(Attribute attr, 157 PtrDLEntryPos pos); 158 159 } // namespace LLVM 160 } // namespace mlir 161 162 #endif // MLIR_DIALECT_LLVMIR_LLVMTYPES_H_ 163