github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/EDSC/CoreAPIs.cpp (about) 1 //===- Types.cpp - Implementations of MLIR Core C APIs --------------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 18 #include "mlir-c/Core.h" 19 20 #include "mlir/IR/AffineMap.h" 21 #include "mlir/IR/Attributes.h" 22 #include "mlir/IR/Function.h" 23 #include "mlir/IR/MLIRContext.h" 24 #include "mlir/IR/StandardTypes.h" 25 #include "mlir/IR/Types.h" 26 #include "mlir/Support/LLVM.h" 27 28 #include "llvm/ADT/StringSwitch.h" 29 30 using namespace mlir; 31 32 mlir_type_t makeScalarType(mlir_context_t context, const char *name, 33 unsigned bitwidth) { 34 mlir::MLIRContext *c = reinterpret_cast<mlir::MLIRContext *>(context); 35 mlir_type_t res = 36 llvm::StringSwitch<mlir_type_t>(name) 37 .Case("bf16", 38 mlir_type_t{mlir::FloatType::getBF16(c).getAsOpaquePointer()}) 39 .Case("f16", 40 mlir_type_t{mlir::FloatType::getF16(c).getAsOpaquePointer()}) 41 .Case("f32", 42 mlir_type_t{mlir::FloatType::getF32(c).getAsOpaquePointer()}) 43 .Case("f64", 44 mlir_type_t{mlir::FloatType::getF64(c).getAsOpaquePointer()}) 45 .Case("index", 46 mlir_type_t{mlir::IndexType::get(c).getAsOpaquePointer()}) 47 .Case("i", 48 mlir_type_t{ 49 mlir::IntegerType::get(bitwidth, c).getAsOpaquePointer()}) 50 .Default(mlir_type_t{nullptr}); 51 if (!res) { 52 llvm_unreachable("Invalid type specifier"); 53 } 54 return res; 55 } 56 57 mlir_type_t makeMemRefType(mlir_context_t context, mlir_type_t elemType, 58 int64_list_t sizes) { 59 auto t = mlir::MemRefType::get( 60 llvm::ArrayRef<int64_t>(sizes.values, sizes.n), 61 mlir::Type::getFromOpaquePointer(elemType), 62 {mlir::AffineMap::getMultiDimIdentityMap( 63 sizes.n, reinterpret_cast<mlir::MLIRContext *>(context))}, 64 0); 65 return mlir_type_t{t.getAsOpaquePointer()}; 66 } 67 68 mlir_type_t makeFunctionType(mlir_context_t context, mlir_type_list_t inputs, 69 mlir_type_list_t outputs) { 70 llvm::SmallVector<mlir::Type, 8> ins(inputs.n), outs(outputs.n); 71 for (unsigned i = 0; i < inputs.n; ++i) { 72 ins[i] = mlir::Type::getFromOpaquePointer(inputs.types[i]); 73 } 74 for (unsigned i = 0; i < outputs.n; ++i) { 75 outs[i] = mlir::Type::getFromOpaquePointer(outputs.types[i]); 76 } 77 auto ft = mlir::FunctionType::get( 78 ins, outs, reinterpret_cast<mlir::MLIRContext *>(context)); 79 return mlir_type_t{ft.getAsOpaquePointer()}; 80 } 81 82 mlir_type_t makeIndexType(mlir_context_t context) { 83 auto *ctx = reinterpret_cast<mlir::MLIRContext *>(context); 84 auto type = mlir::IndexType::get(ctx); 85 return mlir_type_t{type.getAsOpaquePointer()}; 86 } 87 88 mlir_attr_t makeIntegerAttr(mlir_type_t type, int64_t value) { 89 auto ty = Type::getFromOpaquePointer(reinterpret_cast<const void *>(type)); 90 auto attr = IntegerAttr::get(ty, value); 91 return mlir_attr_t{attr.getAsOpaquePointer()}; 92 } 93 94 mlir_attr_t makeBoolAttr(mlir_context_t context, bool value) { 95 auto *ctx = reinterpret_cast<mlir::MLIRContext *>(context); 96 auto attr = BoolAttr::get(value, ctx); 97 return mlir_attr_t{attr.getAsOpaquePointer()}; 98 } 99 100 unsigned getFunctionArity(mlir_func_t function) { 101 auto f = mlir::FuncOp::getFromOpaquePointer(function); 102 return f.getNumArguments(); 103 }