My Project
LLVMDialect.h
Go to the documentation of this file.
1 //===- LLVMDialect.h - MLIR LLVM IR dialect ---------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 LLVM IR dialect in MLIR, containing LLVM operations and
10 // LLVM type system.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_LLVMIR_LLVMDIALECT_H_
15 #define MLIR_DIALECT_LLVMIR_LLVMDIALECT_H_
16 
17 #include "mlir/IR/Dialect.h"
18 #include "mlir/IR/Function.h"
19 #include "mlir/IR/OpDefinition.h"
21 #include "mlir/IR/TypeSupport.h"
22 #include "mlir/IR/Types.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Type.h"
27 
28 #include "mlir/Dialect/LLVMIR/LLVMOpsEnums.h.inc"
29 
30 namespace llvm {
31 class Type;
32 class LLVMContext;
33 } // end namespace llvm
34 
35 namespace mlir {
36 namespace LLVM {
37 class LLVMDialect;
38 
39 namespace detail {
40 struct LLVMTypeStorage;
41 struct LLVMDialectImpl;
42 } // namespace detail
43 
44 class LLVMType : public mlir::Type::TypeBase<LLVMType, mlir::Type,
45  detail::LLVMTypeStorage> {
46 public:
47  enum Kind {
48  LLVM_TYPE = FIRST_LLVM_TYPE,
49  };
50 
51  using Base::Base;
52 
53  static bool kindof(unsigned kind) { return kind == LLVM_TYPE; }
54 
55  LLVMDialect &getDialect();
56  llvm::Type *getUnderlyingType() const;
57 
59  bool isFloatTy() { return getUnderlyingType()->isFloatTy(); }
60  bool isDoubleTy() { return getUnderlyingType()->isDoubleTy(); }
61  bool isIntegerTy() { return getUnderlyingType()->isIntegerTy(); }
62  bool isIntegerTy(unsigned bitwidth) {
63  return getUnderlyingType()->isIntegerTy(bitwidth);
64  }
65 
67  LLVMType getArrayElementType();
68  unsigned getArrayNumElements();
69  bool isArrayTy();
70 
72  LLVMType getVectorElementType();
73  bool isVectorTy();
74 
76  LLVMType getFunctionParamType(unsigned argIdx);
77  unsigned getFunctionNumParams();
78  LLVMType getFunctionResultType();
79  bool isFunctionTy();
80 
82  LLVMType getPointerTo(unsigned addrSpace = 0);
83  LLVMType getPointerElementTy();
84  bool isPointerTy();
85 
87  LLVMType getStructElementType(unsigned i);
88  unsigned getStructNumElements();
89  bool isStructTy();
90 
92  static LLVMType getDoubleTy(LLVMDialect *dialect);
93  static LLVMType getFloatTy(LLVMDialect *dialect);
94  static LLVMType getHalfTy(LLVMDialect *dialect);
95  static LLVMType getFP128Ty(LLVMDialect *dialect);
96  static LLVMType getX86_FP80Ty(LLVMDialect *dialect);
97 
99  static LLVMType getIntNTy(LLVMDialect *dialect, unsigned numBits);
100  static LLVMType getInt1Ty(LLVMDialect *dialect) {
101  return getIntNTy(dialect, /*numBits=*/1);
102  }
103  static LLVMType getInt8Ty(LLVMDialect *dialect) {
104  return getIntNTy(dialect, /*numBits=*/8);
105  }
106  static LLVMType getInt8PtrTy(LLVMDialect *dialect) {
107  return getInt8Ty(dialect).getPointerTo();
108  }
109  static LLVMType getInt16Ty(LLVMDialect *dialect) {
110  return getIntNTy(dialect, /*numBits=*/16);
111  }
112  static LLVMType getInt32Ty(LLVMDialect *dialect) {
113  return getIntNTy(dialect, /*numBits=*/32);
114  }
115  static LLVMType getInt64Ty(LLVMDialect *dialect) {
116  return getIntNTy(dialect, /*numBits=*/64);
117  }
118 
120  static LLVMType getArrayTy(LLVMType elementType, uint64_t numElements);
121  static LLVMType getFunctionTy(LLVMType result, ArrayRef<LLVMType> params,
122  bool isVarArg);
123  static LLVMType getFunctionTy(LLVMType result, bool isVarArg) {
124  return getFunctionTy(result, llvm::None, isVarArg);
125  }
126  static LLVMType getStructTy(LLVMDialect *dialect, ArrayRef<LLVMType> elements,
127  bool isPacked = false);
128  static LLVMType getStructTy(LLVMDialect *dialect, bool isPacked = false) {
129  return getStructTy(dialect, llvm::None, isPacked);
130  }
131  template <typename... Args>
132  static typename std::enable_if<llvm::are_base_of<LLVMType, Args...>::value,
133  LLVMType>::type
134  getStructTy(LLVMType elt1, Args... elts) {
135  SmallVector<LLVMType, 8> fields({elt1, elts...});
136  return getStructTy(&elt1.getDialect(), fields);
137  }
138  static LLVMType getVectorTy(LLVMType elementType, unsigned numElements);
139  static LLVMType getVoidTy(LLVMDialect *dialect);
140 
141 private:
142  friend LLVMDialect;
143 
145  static LLVMType get(MLIRContext *context, llvm::Type *llvmType);
146 
149  static LLVMType getLocked(LLVMDialect *dialect,
150  function_ref<llvm::Type *()> typeBuilder);
151 };
152 
154 #define GET_OP_CLASSES
155 #include "mlir/Dialect/LLVMIR/LLVMOps.h.inc"
156 
157 class LLVMDialect : public Dialect {
158 public:
159  explicit LLVMDialect(MLIRContext *context);
160  ~LLVMDialect();
161  static StringRef getDialectNamespace() { return "llvm"; }
162 
163  llvm::LLVMContext &getLLVMContext();
164  llvm::Module &getLLVMModule();
165 
167  Type parseType(DialectAsmParser &parser) const override;
168 
170  void printType(Type type, DialectAsmPrinter &os) const override;
171 
174  LogicalResult verifyRegionArgAttribute(Operation *op, unsigned regionIdx,
175  unsigned argIdx,
176  NamedAttribute argAttr) override;
177 
178 private:
179  friend LLVMType;
180 
181  std::unique_ptr<detail::LLVMDialectImpl> impl;
182 };
183 
188 Value createGlobalString(Location loc, OpBuilder &builder, StringRef name,
189  StringRef value, LLVM::Linkage linkage,
190  LLVM::LLVMDialect *llvmDialect);
191 
195 
196 } // end namespace LLVM
197 } // end namespace mlir
198 
199 #endif // MLIR_DIALECT_LLVMIR_LLVMDIALECT_H_
Definition: InferTypeOpInterface.cpp:20
static LLVMType getFunctionTy(LLVMType result, bool isVarArg)
Definition: LLVMDialect.h:123
static LLVMType getInt1Ty(LLVMDialect *dialect)
Definition: LLVMDialect.h:100
LLVMDialect & getDialect()
Definition: LLVMDialect.cpp:1513
Definition: PassRegistry.cpp:413
static StringRef getDialectNamespace()
Definition: LLVMDialect.h:161
Definition: Operation.h:27
Definition: Attributes.h:139
Definition: LLVM.h:49
bool isIntegerTy(unsigned bitwidth)
Definition: LLVMDialect.h:62
bool isDoubleTy()
Definition: LLVMDialect.h:60
static LLVMType getInt8PtrTy(LLVMDialect *dialect)
Definition: LLVMDialect.h:106
Definition: Location.h:52
std::pair< Identifier, Attribute > NamedAttribute
Definition: Attributes.h:264
Definition: LogicalResult.h:18
Definition: LLVM.h:37
static std::enable_if< llvm::are_base_of< LLVMType, Args... >::value, LLVMType >::type getStructTy(LLVMType elt1, Args... elts)
Definition: LLVMDialect.h:134
bool isFloatTy()
Utilities to identify types.
Definition: LLVMDialect.h:59
static LLVMType getInt32Ty(LLVMDialect *dialect)
Definition: LLVMDialect.h:112
Value createGlobalString(Location loc, OpBuilder &builder, StringRef name, StringRef value, LLVM::Linkage linkage, LLVM::LLVMDialect *llvmDialect)
Definition: LLVMDialect.cpp:1658
static LLVMType getInt16Ty(LLVMDialect *dialect)
Definition: LLVMDialect.h:109
bool isIntegerTy()
Definition: LLVMDialect.h:61
Definition: Dialect.h:39
bool satisfiesLLVMModule(Operation *op)
Definition: LLVMDialect.cpp:1687
LLVMType getPointerTo(unsigned addrSpace=0)
Pointer type utilities.
Definition: LLVMDialect.cpp:1551
Type parseType(llvm::StringRef typeStr, MLIRContext *context)
Definition: Types.h:84
static bool kindof(unsigned kind)
Definition: LLVMDialect.h:53
Definition: Value.h:38
Definition: LLVM.h:35
Definition: DialectImplementation.h:33
Definition: StorageUniquerSupport.h:30
Definition: MLIRContext.h:34
Definition: StandardTypes.h:63
static LLVMType getInt64Ty(LLVMDialect *dialect)
Definition: LLVMDialect.h:115
Definition: Builders.h:158
Definition: LLVMDialect.h:157
static LLVMType getInt8Ty(LLVMDialect *dialect)
Definition: LLVMDialect.h:103
Definition: DialectImplementation.h:100
static LLVMType getStructTy(LLVMDialect *dialect, bool isPacked=false)
Definition: LLVMDialect.h:128
Kind
Definition: LLVMDialect.h:47
Definition: LLVMDialect.h:44