| 1 | |
| 2 | |
| 3 | //===- Interfaces.cpp - C Interface for MLIR Interfaces -------------------===// |
| 4 | // |
| 5 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | // See https://llvm.org/LICENSE.txt for license information. |
| 7 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "mlir-c/Interfaces.h" |
| 12 | |
| 13 | #include "mlir/CAPI/IR.h" |
| 14 | #include "mlir/CAPI/Interfaces.h" |
| 15 | #include "mlir/CAPI/Support.h" |
| 16 | #include "mlir/CAPI/Wrap.h" |
| 17 | #include "mlir/IR/ValueRange.h" |
| 18 | #include "mlir/Interfaces/InferTypeOpInterface.h" |
| 19 | #include "mlir/Interfaces/SideEffectInterfaces.h" |
| 20 | #include "llvm/ADT/ScopeExit.h" |
| 21 | #include <optional> |
| 22 | |
| 23 | using namespace mlir; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | std::optional<RegisteredOperationName> |
| 28 | getRegisteredOperationName(MlirContext context, MlirStringRef opName) { |
| 29 | StringRef name(opName.data, opName.length); |
| 30 | std::optional<RegisteredOperationName> info = |
| 31 | RegisteredOperationName::lookup(name, ctx: unwrap(c: context)); |
| 32 | return info; |
| 33 | } |
| 34 | |
| 35 | std::optional<Location> maybeGetLocation(MlirLocation location) { |
| 36 | std::optional<Location> maybeLocation; |
| 37 | if (!mlirLocationIsNull(location)) |
| 38 | maybeLocation = unwrap(c: location); |
| 39 | return maybeLocation; |
| 40 | } |
| 41 | |
| 42 | SmallVector<Value> unwrapOperands(intptr_t nOperands, MlirValue *operands) { |
| 43 | SmallVector<Value> unwrappedOperands; |
| 44 | (void)unwrapList(size: nOperands, first: operands, storage&: unwrappedOperands); |
| 45 | return unwrappedOperands; |
| 46 | } |
| 47 | |
| 48 | DictionaryAttr unwrapAttributes(MlirAttribute attributes) { |
| 49 | DictionaryAttr attributeDict; |
| 50 | if (!mlirAttributeIsNull(attr: attributes)) |
| 51 | attributeDict = llvm::cast<DictionaryAttr>(Val: unwrap(c: attributes)); |
| 52 | return attributeDict; |
| 53 | } |
| 54 | |
| 55 | SmallVector<std::unique_ptr<Region>> unwrapRegions(intptr_t nRegions, |
| 56 | MlirRegion *regions) { |
| 57 | // Create a vector of unique pointers to regions and make sure they are not |
| 58 | // deleted when exiting the scope. This is a hack caused by C++ API expecting |
| 59 | // an list of unique pointers to regions (without ownership transfer |
| 60 | // semantics) and C API making ownership transfer explicit. |
| 61 | SmallVector<std::unique_ptr<Region>> unwrappedRegions; |
| 62 | unwrappedRegions.reserve(N: nRegions); |
| 63 | for (intptr_t i = 0; i < nRegions; ++i) |
| 64 | unwrappedRegions.emplace_back(Args: unwrap(c: *(regions + i))); |
| 65 | llvm::scope_exit cleaner([&]() { |
| 66 | for (auto ®ion : unwrappedRegions) |
| 67 | region.release(); |
| 68 | }); |
| 69 | return unwrappedRegions; |
| 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | bool mlirOperationImplementsInterface(MlirOperation operation, |
| 75 | MlirTypeID interfaceTypeID) { |
| 76 | std::optional<RegisteredOperationName> info = |
| 77 | unwrap(c: operation)->getRegisteredInfo(); |
| 78 | return info && info->hasInterface(interfaceID: unwrap(c: interfaceTypeID)); |
| 79 | } |
| 80 | |
| 81 | bool mlirOperationImplementsInterfaceStatic(MlirStringRef operationName, |
| 82 | MlirContext context, |
| 83 | MlirTypeID interfaceTypeID) { |
| 84 | std::optional<RegisteredOperationName> info = RegisteredOperationName::lookup( |
| 85 | name: StringRef(operationName.data, operationName.length), ctx: unwrap(c: context)); |
| 86 | return info && info->hasInterface(interfaceID: unwrap(c: interfaceTypeID)); |
| 87 | } |
| 88 | |
| 89 | MlirTypeID mlirInferTypeOpInterfaceTypeID() { |
| 90 | return wrap(cpp: InferTypeOpInterface::getInterfaceID()); |
| 91 | } |
| 92 | |
| 93 | MlirLogicalResult mlirInferTypeOpInterfaceInferReturnTypes( |
| 94 | MlirStringRef opName, MlirContext context, MlirLocation location, |
| 95 | intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, |
| 96 | void *properties, intptr_t nRegions, MlirRegion *regions, |
| 97 | MlirTypesCallback callback, void *userData) { |
| 98 | StringRef name(opName.data, opName.length); |
| 99 | std::optional<RegisteredOperationName> info = |
| 100 | getRegisteredOperationName(context, opName); |
| 101 | if (!info) |
| 102 | return mlirLogicalResultFailure(); |
| 103 | |
| 104 | std::optional<Location> maybeLocation = maybeGetLocation(location); |
| 105 | SmallVector<Value> unwrappedOperands = unwrapOperands(nOperands, operands); |
| 106 | DictionaryAttr attributeDict = unwrapAttributes(attributes); |
| 107 | SmallVector<std::unique_ptr<Region>> unwrappedRegions = |
| 108 | unwrapRegions(nRegions, regions); |
| 109 | |
| 110 | SmallVector<Type> inferredTypes; |
| 111 | // The C API passes an opaque void*; we trust the caller to pass the correct |
| 112 | // properties type for this operation. |
| 113 | // TODO: Create a C API that's more type-safe. |
| 114 | PropertyRef propertyRef = |
| 115 | properties ? PropertyRef(info->getOpPropertiesTypeID(), properties) |
| 116 | : PropertyRef(); |
| 117 | if (failed(Result: info->getInterface<InferTypeOpInterface>()->inferReturnTypes( |
| 118 | unwrap(c: context), maybeLocation, unwrappedOperands, attributeDict, |
| 119 | propertyRef, unwrappedRegions, inferredTypes))) |
| 120 | return mlirLogicalResultFailure(); |
| 121 | |
| 122 | SmallVector<MlirType> wrappedInferredTypes; |
| 123 | wrappedInferredTypes.reserve(N: inferredTypes.size()); |
| 124 | for (Type t : inferredTypes) |
| 125 | wrappedInferredTypes.push_back(Elt: wrap(cpp: t)); |
| 126 | callback(wrappedInferredTypes.size(), wrappedInferredTypes.data(), userData); |
| 127 | return mlirLogicalResultSuccess(); |
| 128 | } |
| 129 | |
| 130 | MlirTypeID mlirInferShapedTypeOpInterfaceTypeID() { |
| 131 | return wrap(cpp: InferShapedTypeOpInterface::getInterfaceID()); |
| 132 | } |
| 133 | |
| 134 | MlirLogicalResult mlirInferShapedTypeOpInterfaceInferReturnTypes( |
| 135 | MlirStringRef opName, MlirContext context, MlirLocation location, |
| 136 | intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, |
| 137 | void *properties, intptr_t nRegions, MlirRegion *regions, |
| 138 | MlirShapedTypeComponentsCallback callback, void *userData) { |
| 139 | std::optional<RegisteredOperationName> info = |
| 140 | getRegisteredOperationName(context, opName); |
| 141 | if (!info) |
| 142 | return mlirLogicalResultFailure(); |
| 143 | |
| 144 | std::optional<Location> maybeLocation = maybeGetLocation(location); |
| 145 | SmallVector<Value> unwrappedOperands = unwrapOperands(nOperands, operands); |
| 146 | DictionaryAttr attributeDict = unwrapAttributes(attributes); |
| 147 | SmallVector<std::unique_ptr<Region>> unwrappedRegions = |
| 148 | unwrapRegions(nRegions, regions); |
| 149 | |
| 150 | SmallVector<ShapedTypeComponents> inferredTypeComponents; |
| 151 | // The C API passes an opaque void*; we trust the caller to pass the correct |
| 152 | // properties type for this operation. |
| 153 | PropertyRef propertyRef = |
| 154 | properties ? PropertyRef(info->getOpPropertiesTypeID(), properties) |
| 155 | : PropertyRef(); |
| 156 | if (failed(Result: info->getInterface<InferShapedTypeOpInterface>() |
| 157 | ->inferReturnTypeComponents( |
| 158 | unwrap(c: context), maybeLocation, |
| 159 | mlir::ValueRange(llvm::ArrayRef(unwrappedOperands)), |
| 160 | attributeDict, propertyRef, unwrappedRegions, |
| 161 | inferredTypeComponents))) |
| 162 | return mlirLogicalResultFailure(); |
| 163 | |
| 164 | bool hasRank; |
| 165 | intptr_t rank; |
| 166 | const int64_t *shapeData; |
| 167 | for (const ShapedTypeComponents &t : inferredTypeComponents) { |
| 168 | if (t.hasRank()) { |
| 169 | hasRank = true; |
| 170 | rank = t.getDims().size(); |
| 171 | shapeData = t.getDims().data(); |
| 172 | } else { |
| 173 | hasRank = false; |
| 174 | rank = 0; |
| 175 | shapeData = nullptr; |
| 176 | } |
| 177 | callback(hasRank, rank, shapeData, wrap(cpp: t.getElementType()), |
| 178 | wrap(cpp: t.getAttribute()), userData); |
| 179 | } |
| 180 | return mlirLogicalResultSuccess(); |
| 181 | } |
| 182 | |
| 183 | //===---------------------------------------------------------------------===// |
| 184 | // ConditionallySpeculatable |
| 185 | //===---------------------------------------------------------------------===// |
| 186 | |
| 187 | MlirTypeID mlirConditionallySpeculatableOpInterfaceTypeID() { |
| 188 | return wrap(cpp: ConditionallySpeculatable::getInterfaceID()); |
| 189 | } |
| 190 | |
| 191 | /// Fallback model for the ConditionallySpeculatable interface that uses C API |
| 192 | /// callbacks. |
| 193 | class ConditionallySpeculatableOpInterfaceFallbackModel |
| 194 | : public mlir::ConditionallySpeculatable::FallbackModel< |
| 195 | ConditionallySpeculatableOpInterfaceFallbackModel> { |
| 196 | public: |
| 197 | /// Sets the callbacks that this FallbackModel will use. |
| 198 | /// NB: the callbacks can only be set through this method as the |
| 199 | /// RegisteredOperationName::attachInterface mechanism default-constructs |
| 200 | /// the FallbackModel without being able to provide arguments. |
| 201 | void |
| 202 | setCallbacks(MlirConditionallySpeculatableOpInterfaceCallbacks callbacks) { |
| 203 | this->callbacks = callbacks; |
| 204 | } |
| 205 | |
| 206 | ~ConditionallySpeculatableOpInterfaceFallbackModel() { |
| 207 | if (callbacks.destruct) |
| 208 | callbacks.destruct(callbacks.userData); |
| 209 | } |
| 210 | |
| 211 | static TypeID getInterfaceID() { |
| 212 | return ConditionallySpeculatable::getInterfaceID(); |
| 213 | } |
| 214 | |
| 215 | static bool classof(const mlir::ConditionallySpeculatable::Concept *op) { |
| 216 | // Enable casting back to the FallbackModel from the Interface. This is |
| 217 | // necessary as attachInterface(...) default-constructs the FallbackModel |
| 218 | // without being able to pass in the callbacks and returns just the Concept. |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | Speculation::Speculatability getSpeculatability(Operation *op) const { |
| 223 | assert(callbacks.getSpeculatability && |
| 224 | "getSpeculatability callback not set" ); |
| 225 | |
| 226 | switch (callbacks.getSpeculatability(wrap(cpp: op), callbacks.userData)) { |
| 227 | case MlirSpeculatabilityNotSpeculatable: |
| 228 | return Speculation::NotSpeculatable; |
| 229 | case MlirSpeculatabilitySpeculatable: |
| 230 | return Speculation::Speculatable; |
| 231 | case MlirSpeculatabilityRecursivelySpeculatable: |
| 232 | return Speculation::RecursivelySpeculatable; |
| 233 | } |
| 234 | llvm_unreachable("unknown speculatability" ); |
| 235 | } |
| 236 | |
| 237 | private: |
| 238 | MlirConditionallySpeculatableOpInterfaceCallbacks callbacks; |
| 239 | }; |
| 240 | |
| 241 | /// Attach a ConditionallySpeculatable FallbackModel to the given named op. |
| 242 | /// The FallbackModel uses the provided callbacks to implement the interface. |
| 243 | void mlirConditionallySpeculatableOpInterfaceAttachFallbackModel( |
| 244 | MlirContext ctx, MlirStringRef opName, |
| 245 | MlirConditionallySpeculatableOpInterfaceCallbacks callbacks) { |
| 246 | // Look up the operation definition in the context. |
| 247 | std::optional<RegisteredOperationName> opInfo = |
| 248 | RegisteredOperationName::lookup(name: unwrap(ref: opName), ctx: unwrap(c: ctx)); |
| 249 | |
| 250 | assert(opInfo.has_value() && "operation not found in context" ); |
| 251 | |
| 252 | // NB: the following default-constructs the FallbackModel _without_ being able |
| 253 | // to provide arguments. |
| 254 | opInfo->attachInterface<ConditionallySpeculatableOpInterfaceFallbackModel>(); |
| 255 | // Cast to get the underlying FallbackModel and set the callbacks. |
| 256 | auto *model = cast<ConditionallySpeculatableOpInterfaceFallbackModel>( |
| 257 | Val: opInfo |
| 258 | ->getInterface<ConditionallySpeculatableOpInterfaceFallbackModel>()); |
| 259 | assert(model && |
| 260 | "Failed to get ConditionallySpeculatableOpInterfaceFallbackModel" ); |
| 261 | model->setCallbacks(callbacks); |
| 262 | } |
| 263 | |
| 264 | MlirSpeculatability mlirConditionallySpeculatableOpInterfaceGetSpeculatability( |
| 265 | MlirOperation operation) { |
| 266 | auto iface = dyn_cast<ConditionallySpeculatable>(Val: unwrap(c: operation)); |
| 267 | assert(iface && "operation does not implement ConditionallySpeculatable" ); |
| 268 | |
| 269 | switch (iface.getSpeculatability()) { |
| 270 | case Speculation::NotSpeculatable: |
| 271 | return MlirSpeculatabilityNotSpeculatable; |
| 272 | case Speculation::Speculatable: |
| 273 | return MlirSpeculatabilitySpeculatable; |
| 274 | case Speculation::RecursivelySpeculatable: |
| 275 | return MlirSpeculatabilityRecursivelySpeculatable; |
| 276 | } |
| 277 | llvm_unreachable("unknown speculatability" ); |
| 278 | } |
| 279 | |
| 280 | //===---------------------------------------------------------------------===// |
| 281 | // MemoryEffectOpInterface |
| 282 | //===---------------------------------------------------------------------===// |
| 283 | |
| 284 | MlirMemoryEffect mlirMemoryEffectsAllocateGet() { |
| 285 | return wrap( |
| 286 | cpp: static_cast<MemoryEffects::Effect *>(MemoryEffects::Allocate::get())); |
| 287 | } |
| 288 | |
| 289 | MlirMemoryEffect mlirMemoryEffectsFreeGet() { |
| 290 | return wrap(cpp: static_cast<MemoryEffects::Effect *>(MemoryEffects::Free::get())); |
| 291 | } |
| 292 | |
| 293 | MlirMemoryEffect mlirMemoryEffectsReadGet() { |
| 294 | return wrap(cpp: static_cast<MemoryEffects::Effect *>(MemoryEffects::Read::get())); |
| 295 | } |
| 296 | |
| 297 | MlirMemoryEffect mlirMemoryEffectsWriteGet() { |
| 298 | return wrap( |
| 299 | cpp: static_cast<MemoryEffects::Effect *>(MemoryEffects::Write::get())); |
| 300 | } |
| 301 | |
| 302 | MlirTypeID mlirMemoryEffectGetEffectID(MlirMemoryEffect effect) { |
| 303 | return wrap(cpp: unwrap(c: effect)->getEffectID()); |
| 304 | } |
| 305 | |
| 306 | MlirSideEffectResource mlirSideEffectsDefaultResourceGet() { |
| 307 | return wrap(cpp: static_cast<SideEffects::Resource *>( |
| 308 | SideEffects::DefaultResource::get())); |
| 309 | } |
| 310 | |
| 311 | MlirMemoryEffectInstance mlirMemoryEffectInstanceCreate( |
| 312 | MlirMemoryEffect effect, MlirAttribute parameters, int stage, |
| 313 | bool effectOnFullRegion, MlirSideEffectResource resource) { |
| 314 | return wrap(cpp: new MemoryEffects::EffectInstance( |
| 315 | unwrap(c: effect), unwrap(c: parameters), stage, effectOnFullRegion, |
| 316 | unwrap(c: resource))); |
| 317 | } |
| 318 | |
| 319 | MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpOperand( |
| 320 | MlirMemoryEffect effect, MlirOpOperand opOperand, MlirAttribute parameters, |
| 321 | int stage, bool effectOnFullRegion, MlirSideEffectResource resource) { |
| 322 | return wrap(cpp: new MemoryEffects::EffectInstance( |
| 323 | unwrap(c: effect), unwrap(c: opOperand), unwrap(c: parameters), stage, |
| 324 | effectOnFullRegion, unwrap(c: resource))); |
| 325 | } |
| 326 | |
| 327 | MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpResult( |
| 328 | MlirMemoryEffect effect, MlirValue result, MlirAttribute parameters, |
| 329 | int stage, bool effectOnFullRegion, MlirSideEffectResource resource) { |
| 330 | return wrap(cpp: new MemoryEffects::EffectInstance( |
| 331 | unwrap(c: effect), cast<OpResult>(Val: unwrap(c: result)), unwrap(c: parameters), stage, |
| 332 | effectOnFullRegion, unwrap(c: resource))); |
| 333 | } |
| 334 | |
| 335 | MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForBlockArgument( |
| 336 | MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters, |
| 337 | int stage, bool effectOnFullRegion, MlirSideEffectResource resource) { |
| 338 | return wrap(cpp: new MemoryEffects::EffectInstance( |
| 339 | unwrap(c: effect), cast<BlockArgument>(Val: unwrap(c: blockArgument)), |
| 340 | unwrap(c: parameters), stage, effectOnFullRegion, unwrap(c: resource))); |
| 341 | } |
| 342 | |
| 343 | MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForSymbol( |
| 344 | MlirMemoryEffect effect, MlirAttribute symbol, MlirAttribute parameters, |
| 345 | int stage, bool effectOnFullRegion, MlirSideEffectResource resource) { |
| 346 | return wrap(cpp: new MemoryEffects::EffectInstance( |
| 347 | unwrap(c: effect), cast<SymbolRefAttr>(Val: unwrap(c: symbol)), unwrap(c: parameters), |
| 348 | stage, effectOnFullRegion, unwrap(c: resource))); |
| 349 | } |
| 350 | |
| 351 | void mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance) { |
| 352 | delete unwrap(c: instance); |
| 353 | } |
| 354 | |
| 355 | MlirMemoryEffectInstance |
| 356 | mlirMemoryEffectInstanceClone(MlirMemoryEffectInstance instance) { |
| 357 | return wrap(cpp: new MemoryEffects::EffectInstance(*unwrap(c: instance))); |
| 358 | } |
| 359 | |
| 360 | MlirMemoryEffect |
| 361 | mlirMemoryEffectInstanceGetEffect(MlirMemoryEffectInstance instance) { |
| 362 | return wrap(cpp: unwrap(c: instance)->getEffect()); |
| 363 | } |
| 364 | |
| 365 | MlirSideEffectResource |
| 366 | mlirMemoryEffectInstanceGetResource(MlirMemoryEffectInstance instance) { |
| 367 | return wrap(cpp: unwrap(c: instance)->getResource()); |
| 368 | } |
| 369 | |
| 370 | int mlirMemoryEffectInstanceGetStage(MlirMemoryEffectInstance instance) { |
| 371 | return unwrap(c: instance)->getStage(); |
| 372 | } |
| 373 | |
| 374 | bool mlirMemoryEffectInstanceGetEffectOnFullRegion( |
| 375 | MlirMemoryEffectInstance instance) { |
| 376 | return unwrap(c: instance)->getEffectOnFullRegion(); |
| 377 | } |
| 378 | |
| 379 | MlirAttribute |
| 380 | mlirMemoryEffectInstanceGetParameters(MlirMemoryEffectInstance instance) { |
| 381 | return wrap(cpp: unwrap(c: instance)->getParameters()); |
| 382 | } |
| 383 | |
| 384 | MlirValue mlirMemoryEffectInstanceGetValue(MlirMemoryEffectInstance instance) { |
| 385 | return wrap(cpp: unwrap(c: instance)->getValue()); |
| 386 | } |
| 387 | |
| 388 | MlirAttribute |
| 389 | mlirMemoryEffectInstanceGetSymbolRef(MlirMemoryEffectInstance instance) { |
| 390 | return wrap(cpp: unwrap(c: instance)->getSymbolRef()); |
| 391 | } |
| 392 | |
| 393 | MlirTypeID mlirMemoryEffectsOpInterfaceTypeID() { |
| 394 | return wrap(cpp: MemoryEffectOpInterface::getInterfaceID()); |
| 395 | } |
| 396 | |
| 397 | /// Fallback model for the MemoryEffectsOpInterface that uses C API callbacks. |
| 398 | class MemoryEffectOpInterfaceFallbackModel |
| 399 | : public mlir::MemoryEffectOpInterface::FallbackModel< |
| 400 | MemoryEffectOpInterfaceFallbackModel> { |
| 401 | public: |
| 402 | /// Sets the callbacks that this FallbackModel will use. |
| 403 | /// NB: the callbacks can only be set through this method as the |
| 404 | /// RegisteredOperationName::attachInterface mechanism default-constructs |
| 405 | /// the FallbackModel without being able to provide arguments. |
| 406 | void setCallbacks(MlirMemoryEffectsOpInterfaceCallbacks callbacks) { |
| 407 | this->callbacks = callbacks; |
| 408 | } |
| 409 | |
| 410 | ~MemoryEffectOpInterfaceFallbackModel() { |
| 411 | if (callbacks.destruct) |
| 412 | callbacks.destruct(callbacks.userData); |
| 413 | } |
| 414 | |
| 415 | static TypeID getInterfaceID() { |
| 416 | return MemoryEffectOpInterface::getInterfaceID(); |
| 417 | } |
| 418 | |
| 419 | static bool classof(const mlir::MemoryEffectOpInterface::Concept *op) { |
| 420 | // Enable casting back to the FallbackModel from the Interface. This is |
| 421 | // necessary as attachInterface(...) default-constructs the FallbackModel |
| 422 | // without being able to pass in the callbacks and returns just the Concept. |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | getEffects(Operation *op, |
| 428 | SmallVectorImpl<MemoryEffects::EffectInstance> &effects) const { |
| 429 | assert(callbacks.getEffects && "getEffects callback not set" ); |
| 430 | callbacks.getEffects( |
| 431 | wrap(cpp: op), |
| 432 | [](intptr_t numEffects, MlirMemoryEffectInstance *effectInstances, |
| 433 | void *userData) { |
| 434 | auto *unwrappedEffects = |
| 435 | static_cast<SmallVectorImpl<MemoryEffects::EffectInstance> *>( |
| 436 | userData); |
| 437 | unwrappedEffects->reserve(N: unwrappedEffects->size() + numEffects); |
| 438 | for (intptr_t i = 0; i < numEffects; ++i) |
| 439 | unwrappedEffects->push_back(Elt: *unwrap(c: effectInstances[i])); |
| 440 | }, |
| 441 | &effects, callbacks.userData); |
| 442 | } |
| 443 | |
| 444 | private: |
| 445 | MlirMemoryEffectsOpInterfaceCallbacks callbacks; |
| 446 | }; |
| 447 | |
| 448 | /// Attach a MemoryEffectsOpInterface FallbackModel to the given named op. |
| 449 | /// The FallbackModel uses the provided callbacks to implement the interface. |
| 450 | void mlirMemoryEffectsOpInterfaceAttachFallbackModel( |
| 451 | MlirContext ctx, MlirStringRef opName, |
| 452 | MlirMemoryEffectsOpInterfaceCallbacks callbacks) { |
| 453 | // Look up the operation definition in the context |
| 454 | std::optional<RegisteredOperationName> opInfo = |
| 455 | RegisteredOperationName::lookup(name: unwrap(ref: opName), ctx: unwrap(c: ctx)); |
| 456 | |
| 457 | assert(opInfo.has_value() && "operation not found in context" ); |
| 458 | |
| 459 | // NB: the following default-constructs the FallbackModel _without_ being able |
| 460 | // to provide arguments. |
| 461 | opInfo->attachInterface<MemoryEffectOpInterfaceFallbackModel>(); |
| 462 | // Cast to get the underlying FallbackModel and set the callbacks. |
| 463 | auto *model = cast<MemoryEffectOpInterfaceFallbackModel>( |
| 464 | Val: opInfo->getInterface<MemoryEffectOpInterfaceFallbackModel>()); |
| 465 | assert(model && "Failed to get MemoryEffectOpInterfaceFallbackModel" ); |
| 466 | model->setCallbacks(callbacks); |
| 467 | } |
| 468 | |
| 469 | void mlirMemoryEffectsOpInterfaceGetEffects( |
| 470 | MlirOperation operation, MlirMemoryEffectInstancesCallback callback, |
| 471 | void *userData) { |
| 472 | auto iface = dyn_cast<MemoryEffectOpInterface>(Val: unwrap(c: operation)); |
| 473 | assert(iface && "operation does not implement MemoryEffectOpInterface" ); |
| 474 | |
| 475 | SmallVector<MemoryEffects::EffectInstance> effects; |
| 476 | iface.getEffects(effects); |
| 477 | SmallVector<MlirMemoryEffectInstance> wrappedEffects; |
| 478 | wrappedEffects.reserve(N: effects.size()); |
| 479 | for (MemoryEffects::EffectInstance &effect : effects) |
| 480 | wrappedEffects.push_back(Elt: wrap(cpp: &effect)); |
| 481 | callback(wrappedEffects.size(), wrappedEffects.data(), userData); |
| 482 | } |
| 483 | |