1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
//===- BuiltinTypes.cpp - C Interface to MLIR Builtin Types ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir-c/BuiltinTypes.h"
#include "mlir-c/AffineMap.h"
#include "mlir-c/IR.h"
#include "mlir/CAPI/AffineMap.h"
#include "mlir/CAPI/IR.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Types.h"
using namespace mlir;
//===----------------------------------------------------------------------===//
// Integer types.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAInteger(MlirType type) {
return unwrap(type).isa<IntegerType>();
}
MlirType mlirIntegerTypeGet(MlirContext ctx, unsigned bitwidth) {
return wrap(IntegerType::get(unwrap(ctx), bitwidth));
}
MlirType mlirIntegerTypeSignedGet(MlirContext ctx, unsigned bitwidth) {
return wrap(IntegerType::get(unwrap(ctx), bitwidth, IntegerType::Signed));
}
MlirType mlirIntegerTypeUnsignedGet(MlirContext ctx, unsigned bitwidth) {
return wrap(IntegerType::get(unwrap(ctx), bitwidth, IntegerType::Unsigned));
}
unsigned mlirIntegerTypeGetWidth(MlirType type) {
return unwrap(type).cast<IntegerType>().getWidth();
}
bool mlirIntegerTypeIsSignless(MlirType type) {
return unwrap(type).cast<IntegerType>().isSignless();
}
bool mlirIntegerTypeIsSigned(MlirType type) {
return unwrap(type).cast<IntegerType>().isSigned();
}
bool mlirIntegerTypeIsUnsigned(MlirType type) {
return unwrap(type).cast<IntegerType>().isUnsigned();
}
//===----------------------------------------------------------------------===//
// Index type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAIndex(MlirType type) { return unwrap(type).isa<IndexType>(); }
MlirType mlirIndexTypeGet(MlirContext ctx) {
return wrap(IndexType::get(unwrap(ctx)));
}
//===----------------------------------------------------------------------===//
// Floating-point types.
//===----------------------------------------------------------------------===//
bool mlirTypeIsABF16(MlirType type) { return unwrap(type).isBF16(); }
MlirType mlirBF16TypeGet(MlirContext ctx) {
return wrap(FloatType::getBF16(unwrap(ctx)));
}
bool mlirTypeIsAF16(MlirType type) { return unwrap(type).isF16(); }
MlirType mlirF16TypeGet(MlirContext ctx) {
return wrap(FloatType::getF16(unwrap(ctx)));
}
bool mlirTypeIsAF32(MlirType type) { return unwrap(type).isF32(); }
MlirType mlirF32TypeGet(MlirContext ctx) {
return wrap(FloatType::getF32(unwrap(ctx)));
}
bool mlirTypeIsAF64(MlirType type) { return unwrap(type).isF64(); }
MlirType mlirF64TypeGet(MlirContext ctx) {
return wrap(FloatType::getF64(unwrap(ctx)));
}
//===----------------------------------------------------------------------===//
// None type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsANone(MlirType type) { return unwrap(type).isa<NoneType>(); }
MlirType mlirNoneTypeGet(MlirContext ctx) {
return wrap(NoneType::get(unwrap(ctx)));
}
//===----------------------------------------------------------------------===//
// Complex type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAComplex(MlirType type) {
return unwrap(type).isa<ComplexType>();
}
MlirType mlirComplexTypeGet(MlirType elementType) {
return wrap(ComplexType::get(unwrap(elementType)));
}
MlirType mlirComplexTypeGetElementType(MlirType type) {
return wrap(unwrap(type).cast<ComplexType>().getElementType());
}
//===----------------------------------------------------------------------===//
// Shaped type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAShaped(MlirType type) { return unwrap(type).isa<ShapedType>(); }
MlirType mlirShapedTypeGetElementType(MlirType type) {
return wrap(unwrap(type).cast<ShapedType>().getElementType());
}
bool mlirShapedTypeHasRank(MlirType type) {
return unwrap(type).cast<ShapedType>().hasRank();
}
int64_t mlirShapedTypeGetRank(MlirType type) {
return unwrap(type).cast<ShapedType>().getRank();
}
bool mlirShapedTypeHasStaticShape(MlirType type) {
return unwrap(type).cast<ShapedType>().hasStaticShape();
}
bool mlirShapedTypeIsDynamicDim(MlirType type, intptr_t dim) {
return unwrap(type).cast<ShapedType>().isDynamicDim(
static_cast<unsigned>(dim));
}
int64_t mlirShapedTypeGetDimSize(MlirType type, intptr_t dim) {
return unwrap(type).cast<ShapedType>().getDimSize(static_cast<unsigned>(dim));
}
bool mlirShapedTypeIsDynamicSize(int64_t size) {
return ShapedType::isDynamic(size);
}
bool mlirShapedTypeIsDynamicStrideOrOffset(int64_t val) {
return ShapedType::isDynamicStrideOrOffset(val);
}
//===----------------------------------------------------------------------===//
// Vector type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAVector(MlirType type) { return unwrap(type).isa<VectorType>(); }
MlirType mlirVectorTypeGet(intptr_t rank, const int64_t *shape,
MlirType elementType) {
return wrap(
VectorType::get(llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType)));
}
MlirType mlirVectorTypeGetChecked(MlirLocation loc, intptr_t rank,
const int64_t *shape, MlirType elementType) {
return wrap(VectorType::getChecked(
unwrap(loc), llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType)));
}
//===----------------------------------------------------------------------===//
// Ranked / Unranked tensor type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsATensor(MlirType type) { return unwrap(type).isa<TensorType>(); }
bool mlirTypeIsARankedTensor(MlirType type) {
return unwrap(type).isa<RankedTensorType>();
}
bool mlirTypeIsAUnrankedTensor(MlirType type) {
return unwrap(type).isa<UnrankedTensorType>();
}
MlirType mlirRankedTensorTypeGet(intptr_t rank, const int64_t *shape,
MlirType elementType, MlirAttribute encoding) {
return wrap(RankedTensorType::get(
llvm::makeArrayRef(shape, static_cast<size_t>(rank)), unwrap(elementType),
unwrap(encoding)));
}
MlirType mlirRankedTensorTypeGetChecked(MlirLocation loc, intptr_t rank,
const int64_t *shape,
MlirType elementType,
MlirAttribute encoding) {
return wrap(RankedTensorType::getChecked(
unwrap(loc), llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType), unwrap(encoding)));
}
MlirAttribute mlirRankedTensorTypeGetEncoding(MlirType type) {
return wrap(unwrap(type).cast<RankedTensorType>().getEncoding());
}
MlirType mlirUnrankedTensorTypeGet(MlirType elementType) {
return wrap(UnrankedTensorType::get(unwrap(elementType)));
}
MlirType mlirUnrankedTensorTypeGetChecked(MlirLocation loc,
MlirType elementType) {
return wrap(UnrankedTensorType::getChecked(unwrap(loc), unwrap(elementType)));
}
//===----------------------------------------------------------------------===//
// Ranked / Unranked MemRef type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAMemRef(MlirType type) { return unwrap(type).isa<MemRefType>(); }
MlirType mlirMemRefTypeGet(MlirType elementType, intptr_t rank,
const int64_t *shape, intptr_t numMaps,
MlirAffineMap const *affineMaps,
MlirAttribute memorySpace) {
SmallVector<AffineMap, 1> maps;
(void)unwrapList(numMaps, affineMaps, maps);
return wrap(
MemRefType::get(llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType), maps, unwrap(memorySpace)));
}
MlirType mlirMemRefTypeGetChecked(MlirLocation loc, MlirType elementType,
intptr_t rank, const int64_t *shape,
intptr_t numMaps,
MlirAffineMap const *affineMaps,
MlirAttribute memorySpace) {
SmallVector<AffineMap, 1> maps;
(void)unwrapList(numMaps, affineMaps, maps);
return wrap(MemRefType::getChecked(
unwrap(loc), llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType), maps, unwrap(memorySpace)));
}
MlirType mlirMemRefTypeContiguousGet(MlirType elementType, intptr_t rank,
const int64_t *shape,
MlirAttribute memorySpace) {
return wrap(
MemRefType::get(llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType), llvm::None, unwrap(memorySpace)));
}
MlirType mlirMemRefTypeContiguousGetChecked(MlirLocation loc,
MlirType elementType, intptr_t rank,
const int64_t *shape,
MlirAttribute memorySpace) {
return wrap(MemRefType::getChecked(
unwrap(loc), llvm::makeArrayRef(shape, static_cast<size_t>(rank)),
unwrap(elementType), llvm::None, unwrap(memorySpace)));
}
intptr_t mlirMemRefTypeGetNumAffineMaps(MlirType type) {
return static_cast<intptr_t>(
unwrap(type).cast<MemRefType>().getAffineMaps().size());
}
MlirAffineMap mlirMemRefTypeGetAffineMap(MlirType type, intptr_t pos) {
return wrap(unwrap(type).cast<MemRefType>().getAffineMaps()[pos]);
}
MlirAttribute mlirMemRefTypeGetMemorySpace(MlirType type) {
return wrap(unwrap(type).cast<MemRefType>().getMemorySpace());
}
bool mlirTypeIsAUnrankedMemRef(MlirType type) {
return unwrap(type).isa<UnrankedMemRefType>();
}
MlirType mlirUnrankedMemRefTypeGet(MlirType elementType,
MlirAttribute memorySpace) {
return wrap(
UnrankedMemRefType::get(unwrap(elementType), unwrap(memorySpace)));
}
MlirType mlirUnrankedMemRefTypeGetChecked(MlirLocation loc,
MlirType elementType,
MlirAttribute memorySpace) {
return wrap(UnrankedMemRefType::getChecked(unwrap(loc), unwrap(elementType),
unwrap(memorySpace)));
}
MlirAttribute mlirUnrankedMemrefGetMemorySpace(MlirType type) {
return wrap(unwrap(type).cast<UnrankedMemRefType>().getMemorySpace());
}
//===----------------------------------------------------------------------===//
// Tuple type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsATuple(MlirType type) { return unwrap(type).isa<TupleType>(); }
MlirType mlirTupleTypeGet(MlirContext ctx, intptr_t numElements,
MlirType const *elements) {
SmallVector<Type, 4> types;
ArrayRef<Type> typeRef = unwrapList(numElements, elements, types);
return wrap(TupleType::get(unwrap(ctx), typeRef));
}
intptr_t mlirTupleTypeGetNumTypes(MlirType type) {
return unwrap(type).cast<TupleType>().size();
}
MlirType mlirTupleTypeGetType(MlirType type, intptr_t pos) {
return wrap(unwrap(type).cast<TupleType>().getType(static_cast<size_t>(pos)));
}
//===----------------------------------------------------------------------===//
// Function type.
//===----------------------------------------------------------------------===//
bool mlirTypeIsAFunction(MlirType type) {
return unwrap(type).isa<FunctionType>();
}
MlirType mlirFunctionTypeGet(MlirContext ctx, intptr_t numInputs,
MlirType const *inputs, intptr_t numResults,
MlirType const *results) {
SmallVector<Type, 4> inputsList;
SmallVector<Type, 4> resultsList;
(void)unwrapList(numInputs, inputs, inputsList);
(void)unwrapList(numResults, results, resultsList);
return wrap(FunctionType::get(unwrap(ctx), inputsList, resultsList));
}
intptr_t mlirFunctionTypeGetNumInputs(MlirType type) {
return unwrap(type).cast<FunctionType>().getNumInputs();
}
intptr_t mlirFunctionTypeGetNumResults(MlirType type) {
return unwrap(type).cast<FunctionType>().getNumResults();
}
MlirType mlirFunctionTypeGetInput(MlirType type, intptr_t pos) {
assert(pos >= 0 && "pos in array must be positive");
return wrap(
unwrap(type).cast<FunctionType>().getInput(static_cast<unsigned>(pos)));
}
MlirType mlirFunctionTypeGetResult(MlirType type, intptr_t pos) {
assert(pos >= 0 && "pos in array must be positive");
return wrap(
unwrap(type).cast<FunctionType>().getResult(static_cast<unsigned>(pos)));
}
|