| 1 | //===- EmitC.cpp - C Interface for EmitC dialect --------------------------===// |
| 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 | #include "mlir-c/Dialect/EmitC.h" |
| 10 | #include "mlir/CAPI/Registration.h" |
| 11 | #include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 12 | |
| 13 | using namespace mlir; |
| 14 | using namespace mlir::emitc; |
| 15 | |
| 16 | MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(EmitC, emitc, mlir::emitc::EmitCDialect) |
| 17 | |
| 18 | // Ensure the C-API enums are uint64_t-castable to C++ equivalents. |
| 19 | static_assert(static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_EQ) == |
| 20 | static_cast<uint64_t>(emitc::CmpPredicate::eq) && |
| 21 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_NE) == |
| 22 | static_cast<uint64_t>(emitc::CmpPredicate::ne) && |
| 23 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_LT) == |
| 24 | static_cast<uint64_t>(emitc::CmpPredicate::lt) && |
| 25 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_LE) == |
| 26 | static_cast<uint64_t>(emitc::CmpPredicate::le) && |
| 27 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_GT) == |
| 28 | static_cast<uint64_t>(emitc::CmpPredicate::gt) && |
| 29 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_GE) == |
| 30 | static_cast<uint64_t>(emitc::CmpPredicate::ge) && |
| 31 | static_cast<uint64_t>(MLIR_EMITC_CMP_PREDICATE_THREE_WAY) == |
| 32 | static_cast<uint64_t>(emitc::CmpPredicate::three_way), |
| 33 | "MlirEmitCCmpPredicate (C-API) and CmpPredicate (C++) mismatch" ); |
| 34 | |
| 35 | //===---------------------------------------------------------------------===// |
| 36 | // ArrayType |
| 37 | //===---------------------------------------------------------------------===// |
| 38 | |
| 39 | bool mlirTypeIsAEmitCArrayType(MlirType type) { |
| 40 | return isa<emitc::ArrayType>(Val: unwrap(c: type)); |
| 41 | } |
| 42 | |
| 43 | MlirTypeID mlirEmitCArrayTypeGetTypeID(void) { |
| 44 | return wrap(cpp: emitc::ArrayType::getTypeID()); |
| 45 | } |
| 46 | |
| 47 | MlirType mlirEmitCArrayTypeGet(intptr_t nDims, int64_t *shape, |
| 48 | MlirType elementType) { |
| 49 | return wrap( |
| 50 | cpp: emitc::ArrayType::get(shape: llvm::ArrayRef(shape, nDims), elementType: unwrap(c: elementType))); |
| 51 | } |
| 52 | |
| 53 | MlirStringRef mlirEmitCArrayTypeGetName(void) { |
| 54 | return wrap(ref: emitc::ArrayType::name); |
| 55 | } |
| 56 | |
| 57 | //===---------------------------------------------------------------------===// |
| 58 | // LValueType |
| 59 | //===---------------------------------------------------------------------===// |
| 60 | |
| 61 | bool mlirTypeIsAEmitCLValueType(MlirType type) { |
| 62 | return isa<emitc::LValueType>(Val: unwrap(c: type)); |
| 63 | } |
| 64 | |
| 65 | MlirTypeID mlirEmitCLValueTypeGetTypeID(void) { |
| 66 | return wrap(cpp: emitc::LValueType::getTypeID()); |
| 67 | } |
| 68 | |
| 69 | MlirType mlirEmitCLValueTypeGet(MlirType valueType) { |
| 70 | return wrap(cpp: emitc::LValueType::get(valueType: unwrap(c: valueType))); |
| 71 | } |
| 72 | |
| 73 | MlirStringRef mlirEmitCLValueTypeGetName(void) { |
| 74 | return wrap(ref: emitc::LValueType::name); |
| 75 | } |
| 76 | |
| 77 | //===---------------------------------------------------------------------===// |
| 78 | // OpaqueType |
| 79 | //===---------------------------------------------------------------------===// |
| 80 | |
| 81 | bool mlirTypeIsAEmitCOpaqueType(MlirType type) { |
| 82 | return isa<emitc::OpaqueType>(Val: unwrap(c: type)); |
| 83 | } |
| 84 | |
| 85 | MlirTypeID mlirEmitCOpaqueTypeGetTypeID(void) { |
| 86 | return wrap(cpp: emitc::OpaqueType::getTypeID()); |
| 87 | } |
| 88 | |
| 89 | MlirType mlirEmitCOpaqueTypeGet(MlirContext ctx, MlirStringRef value) { |
| 90 | return wrap(cpp: emitc::OpaqueType::get(context: unwrap(c: ctx), value: unwrap(ref: value))); |
| 91 | } |
| 92 | |
| 93 | MlirStringRef mlirEmitCOpaqueTypeGetName(void) { |
| 94 | return wrap(ref: emitc::OpaqueType::name); |
| 95 | } |
| 96 | |
| 97 | //===---------------------------------------------------------------------===// |
| 98 | // PointerType |
| 99 | //===---------------------------------------------------------------------===// |
| 100 | |
| 101 | bool mlirTypeIsAEmitCPointerType(MlirType type) { |
| 102 | return isa<emitc::PointerType>(Val: unwrap(c: type)); |
| 103 | } |
| 104 | |
| 105 | MlirTypeID mlirEmitCPointerTypeGetTypeID(void) { |
| 106 | return wrap(cpp: emitc::PointerType::getTypeID()); |
| 107 | } |
| 108 | |
| 109 | MlirType mlirEmitCPointerTypeGet(MlirType pointee) { |
| 110 | return wrap(cpp: emitc::PointerType::get(pointee: unwrap(c: pointee))); |
| 111 | } |
| 112 | |
| 113 | MlirStringRef mlirEmitCPointerTypeGetName(void) { |
| 114 | return wrap(ref: emitc::PointerType::name); |
| 115 | } |
| 116 | |
| 117 | //===---------------------------------------------------------------------===// |
| 118 | // PtrDiffTType |
| 119 | //===---------------------------------------------------------------------===// |
| 120 | |
| 121 | bool mlirTypeIsAEmitCPtrDiffTType(MlirType type) { |
| 122 | return isa<emitc::PtrDiffTType>(Val: unwrap(c: type)); |
| 123 | } |
| 124 | |
| 125 | MlirTypeID mlirEmitCPtrDiffTTypeGetTypeID(void) { |
| 126 | return wrap(cpp: emitc::PtrDiffTType::getTypeID()); |
| 127 | } |
| 128 | |
| 129 | MlirType mlirEmitCPtrDiffTTypeGet(MlirContext ctx) { |
| 130 | return wrap(cpp: emitc::PtrDiffTType::get(ctx: unwrap(c: ctx))); |
| 131 | } |
| 132 | |
| 133 | MlirStringRef mlirEmitCPtrDiffTTypeGetName(void) { |
| 134 | return wrap(ref: emitc::PtrDiffTType::name); |
| 135 | } |
| 136 | |
| 137 | //===---------------------------------------------------------------------===// |
| 138 | // SignedSizeTType |
| 139 | //===---------------------------------------------------------------------===// |
| 140 | |
| 141 | bool mlirTypeIsAEmitCSignedSizeTType(MlirType type) { |
| 142 | return isa<emitc::SignedSizeTType>(Val: unwrap(c: type)); |
| 143 | } |
| 144 | |
| 145 | MlirTypeID mlirEmitCSignedSizeTTypeGetTypeID(void) { |
| 146 | return wrap(cpp: emitc::SignedSizeTType::getTypeID()); |
| 147 | } |
| 148 | |
| 149 | MlirType mlirEmitCSignedSizeTTypeGet(MlirContext ctx) { |
| 150 | return wrap(cpp: emitc::SignedSizeTType::get(ctx: unwrap(c: ctx))); |
| 151 | } |
| 152 | |
| 153 | MlirStringRef mlirEmitCSignedSizeTTypeGetName(void) { |
| 154 | return wrap(ref: emitc::SignedSizeTType::name); |
| 155 | } |
| 156 | |
| 157 | //===---------------------------------------------------------------------===// |
| 158 | // SizeTType |
| 159 | //===---------------------------------------------------------------------===// |
| 160 | |
| 161 | bool mlirTypeIsAEmitCSizeTType(MlirType type) { |
| 162 | return isa<emitc::SizeTType>(Val: unwrap(c: type)); |
| 163 | } |
| 164 | |
| 165 | MlirTypeID mlirEmitCSizeTTypeGetTypeID(void) { |
| 166 | return wrap(cpp: emitc::SizeTType::getTypeID()); |
| 167 | } |
| 168 | |
| 169 | MlirType mlirEmitCSizeTTypeGet(MlirContext ctx) { |
| 170 | return wrap(cpp: emitc::SizeTType::get(ctx: unwrap(c: ctx))); |
| 171 | } |
| 172 | |
| 173 | MlirStringRef mlirEmitCSizeTTypeGetName(void) { |
| 174 | return wrap(ref: emitc::SizeTType::name); |
| 175 | } |
| 176 | |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | // CmpPredicate attribute. |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
| 181 | bool mlirAttributeIsAEmitCCmpPredicate(MlirAttribute attr) { |
| 182 | return llvm::isa<emitc::CmpPredicateAttr>(Val: unwrap(c: attr)); |
| 183 | } |
| 184 | |
| 185 | MlirAttribute mlirEmitCCmpPredicateAttrGet(MlirContext ctx, |
| 186 | MlirEmitCCmpPredicate val) { |
| 187 | return wrap(cpp: (Attribute)emitc::CmpPredicateAttr::get( |
| 188 | context: unwrap(c: ctx), val: static_cast<emitc::CmpPredicate>(val))); |
| 189 | } |
| 190 | |
| 191 | MlirStringRef mlirEmitCCmpPredicateAttrGetName(void) { |
| 192 | return wrap(ref: emitc::CmpPredicateAttr::name); |
| 193 | } |
| 194 | |
| 195 | MlirEmitCCmpPredicate mlirEmitCCmpPredicateAttrGetValue(MlirAttribute attr) { |
| 196 | return static_cast<MlirEmitCCmpPredicate>( |
| 197 | llvm::cast<emitc::CmpPredicateAttr>(Val: unwrap(c: attr)).getValue()); |
| 198 | } |
| 199 | |
| 200 | MlirTypeID mlirEmitCCmpPredicateAttrGetTypeID(void) { |
| 201 | return wrap(cpp: emitc::CmpPredicateAttr::getTypeID()); |
| 202 | } |
| 203 | |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | // Opaque attribute. |
| 206 | //===----------------------------------------------------------------------===// |
| 207 | |
| 208 | bool mlirAttributeIsAEmitCOpaque(MlirAttribute attr) { |
| 209 | return llvm::isa<emitc::OpaqueAttr>(Val: unwrap(c: attr)); |
| 210 | } |
| 211 | |
| 212 | MlirAttribute mlirEmitCOpaqueAttrGet(MlirContext ctx, MlirStringRef value) { |
| 213 | return wrap(cpp: (Attribute)emitc::OpaqueAttr::get(context: unwrap(c: ctx), value: unwrap(ref: value))); |
| 214 | } |
| 215 | |
| 216 | MlirStringRef mlirEmitCOpaqueAttrGetName(void) { |
| 217 | return wrap(ref: emitc::OpaqueAttr::name); |
| 218 | } |
| 219 | |
| 220 | MlirStringRef mlirEmitCOpaqueAttrGetValue(MlirAttribute attr) { |
| 221 | return wrap(ref: llvm::cast<emitc::OpaqueAttr>(Val: unwrap(c: attr)).getValue()); |
| 222 | } |
| 223 | |
| 224 | MlirTypeID mlirEmitCOpaqueAttrGetTypeID(void) { |
| 225 | return wrap(cpp: emitc::OpaqueAttr::getTypeID()); |
| 226 | } |
| 227 | |