20#include "llvm/ADT/ScopeExit.h"
27std::optional<RegisteredOperationName>
28getRegisteredOperationName(MlirContext context,
MlirStringRef opName) {
30 std::optional<RegisteredOperationName> info =
35std::optional<Location> maybeGetLocation(MlirLocation location) {
36 std::optional<Location> maybeLocation;
38 maybeLocation =
unwrap(location);
45 return unwrappedOperands;
48DictionaryAttr unwrapAttributes(MlirAttribute attributes) {
49 DictionaryAttr attributeDict;
50 if (!mlirAttributeIsNull(attributes))
51 attributeDict = llvm::cast<DictionaryAttr>(
unwrap(attributes));
56 MlirRegion *regions) {
62 unwrappedRegions.reserve(nRegions);
63 for (
intptr_t i = 0; i < nRegions; ++i)
64 unwrappedRegions.emplace_back(
unwrap(*(regions + i)));
65 llvm::scope_exit cleaner([&]() {
66 for (
auto ®ion : unwrappedRegions)
69 return unwrappedRegions;
75 MlirTypeID interfaceTypeID) {
76 std::optional<RegisteredOperationName> info =
77 unwrap(operation)->getRegisteredInfo();
78 return info && info->hasInterface(
unwrap(interfaceTypeID));
83 MlirTypeID interfaceTypeID) {
86 return info && info->hasInterface(
unwrap(interfaceTypeID));
90 return wrap(InferTypeOpInterface::getInterfaceID());
94 MlirStringRef opName, MlirContext context, MlirLocation location,
95 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
96 void *properties,
intptr_t nRegions, MlirRegion *regions,
99 std::optional<RegisteredOperationName> info =
100 getRegisteredOperationName(context, opName);
104 std::optional<Location> maybeLocation = maybeGetLocation(location);
106 DictionaryAttr attributeDict = unwrapAttributes(attributes);
108 unwrapRegions(nRegions, regions);
115 properties ?
PropertyRef(info->getOpPropertiesTypeID(), properties)
117 if (failed(info->getInterface<InferTypeOpInterface>()->inferReturnTypes(
118 unwrap(context), maybeLocation, unwrappedOperands, attributeDict,
119 propertyRef, unwrappedRegions, inferredTypes)))
123 wrappedInferredTypes.reserve(inferredTypes.size());
124 for (
Type t : inferredTypes)
125 wrappedInferredTypes.push_back(
wrap(t));
126 callback(wrappedInferredTypes.size(), wrappedInferredTypes.data(), userData);
131 return wrap(InferShapedTypeOpInterface::getInterfaceID());
135 MlirStringRef opName, MlirContext context, MlirLocation location,
136 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
137 void *properties,
intptr_t nRegions, MlirRegion *regions,
139 std::optional<RegisteredOperationName> info =
140 getRegisteredOperationName(context, opName);
144 std::optional<Location> maybeLocation = maybeGetLocation(location);
146 DictionaryAttr attributeDict = unwrapAttributes(attributes);
148 unwrapRegions(nRegions, regions);
154 properties ?
PropertyRef(info->getOpPropertiesTypeID(), properties)
156 if (failed(info->getInterface<InferShapedTypeOpInterface>()
157 ->inferReturnTypeComponents(
158 unwrap(context), maybeLocation,
160 attributeDict, propertyRef, unwrappedRegions,
161 inferredTypeComponents)))
170 rank = t.getDims().size();
171 shapeData = t.getDims().data();
177 callback(hasRank, rank, shapeData,
wrap(t.getElementType()),
178 wrap(t.getAttribute()), userData);
188 return wrap(ConditionallySpeculatable::getInterfaceID());
195 ConditionallySpeculatableOpInterfaceFallbackModel> {
203 this->callbacks = callbacks;
207 if (callbacks.destruct)
208 callbacks.destruct(callbacks.userData);
212 return ConditionallySpeculatable::getInterfaceID();
215 static bool classof(
const mlir::ConditionallySpeculatable::Concept *op) {
223 assert(callbacks.getSpeculatability &&
224 "getSpeculatability callback not set");
226 switch (callbacks.getSpeculatability(
wrap(op), callbacks.userData)) {
234 llvm_unreachable(
"unknown speculatability");
247 std::optional<RegisteredOperationName> opInfo =
250 assert(opInfo.has_value() &&
"operation not found in context");
256 auto *model = cast<ConditionallySpeculatableOpInterfaceFallbackModel>(
258 ->getInterface<ConditionallySpeculatableOpInterfaceFallbackModel>());
260 "Failed to get ConditionallySpeculatableOpInterfaceFallbackModel");
265 MlirOperation operation) {
266 auto iface = dyn_cast<ConditionallySpeculatable>(
unwrap(operation));
267 assert(iface &&
"operation does not implement ConditionallySpeculatable");
269 switch (iface.getSpeculatability()) {
277 llvm_unreachable(
"unknown speculatability");
312 MlirMemoryEffect effect, MlirAttribute parameters,
int stage,
313 bool effectOnFullRegion, MlirSideEffectResource resource) {
315 unwrap(effect),
unwrap(parameters), stage, effectOnFullRegion,
320 MlirMemoryEffect effect, MlirOpOperand opOperand, MlirAttribute parameters,
321 int stage,
bool effectOnFullRegion, MlirSideEffectResource resource) {
324 effectOnFullRegion,
unwrap(resource)));
328 MlirMemoryEffect effect, MlirValue
result, MlirAttribute parameters,
329 int stage,
bool effectOnFullRegion, MlirSideEffectResource resource) {
332 effectOnFullRegion,
unwrap(resource)));
336 MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters,
337 int stage,
bool effectOnFullRegion, MlirSideEffectResource resource) {
339 unwrap(effect), cast<BlockArgument>(
unwrap(blockArgument)),
340 unwrap(parameters), stage, effectOnFullRegion,
unwrap(resource)));
344 MlirMemoryEffect effect, MlirAttribute symbol, MlirAttribute parameters,
345 int stage,
bool effectOnFullRegion, MlirSideEffectResource resource) {
348 stage, effectOnFullRegion,
unwrap(resource)));
355MlirMemoryEffectInstance
365MlirSideEffectResource
371 return unwrap(instance)->getStage();
375 MlirMemoryEffectInstance instance) {
376 return unwrap(instance)->getEffectOnFullRegion();
381 return wrap(
unwrap(instance)->getParameters());
390 return wrap(
unwrap(instance)->getSymbolRef());
394 return wrap(MemoryEffectOpInterface::getInterfaceID());
400 MemoryEffectOpInterfaceFallbackModel> {
407 this->callbacks = callbacks;
411 if (callbacks.destruct)
412 callbacks.destruct(callbacks.userData);
416 return MemoryEffectOpInterface::getInterfaceID();
419 static bool classof(
const mlir::MemoryEffectOpInterface::Concept *op) {
429 assert(callbacks.getEffects &&
"getEffects callback not set");
430 callbacks.getEffects(
432 [](
intptr_t numEffects, MlirMemoryEffectInstance *effectInstances,
434 auto *unwrappedEffects =
437 unwrappedEffects->reserve(unwrappedEffects->size() + numEffects);
438 for (
intptr_t i = 0; i < numEffects; ++i)
439 unwrappedEffects->push_back(*
unwrap(effectInstances[i]));
441 &effects, callbacks.userData);
454 std::optional<RegisteredOperationName> opInfo =
457 assert(opInfo.has_value() &&
"operation not found in context");
463 auto *model = cast<MemoryEffectOpInterfaceFallbackModel>(
465 assert(model &&
"Failed to get MemoryEffectOpInterfaceFallbackModel");
472 auto iface = dyn_cast<MemoryEffectOpInterface>(
unwrap(operation));
473 assert(iface &&
"operation does not implement MemoryEffectOpInterface");
476 iface.getEffects(effects);
478 wrappedEffects.reserve(effects.size());
480 wrappedEffects.push_back(
wrap(&effect));
481 callback(wrappedEffects.size(), wrappedEffects.data(), userData);
MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpOperand(MlirMemoryEffect effect, MlirOpOperand opOperand, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with an operation operand.
MlirLogicalResult mlirInferShapedTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, void *properties, intptr_t nRegions, MlirRegion *regions, MlirShapedTypeComponentsCallback callback, void *userData)
Infers the return shaped type components of the operation.
MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForBlockArgument(MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with a block argument.
MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForSymbol(MlirMemoryEffect effect, MlirAttribute symbol, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with a symbol.
void mlirMemoryEffectsOpInterfaceGetEffects(MlirOperation operation, MlirMemoryEffectInstancesCallback callback, void *userData)
Gets the memory effects of the given operation.
int mlirMemoryEffectInstanceGetStage(MlirMemoryEffectInstance instance)
Returns the stage of the given instance.
MlirSideEffectResource mlirSideEffectsDefaultResourceGet()
Returns the singleton instance of the default side effect resource.
MlirAttribute mlirMemoryEffectInstanceGetSymbolRef(MlirMemoryEffectInstance instance)
Returns the symbol reference of the given instance, or a null attribute if there is no associated sym...
MlirAttribute mlirMemoryEffectInstanceGetParameters(MlirMemoryEffectInstance instance)
Returns the parameters of the given instance, or a null attribute if there are no parameters.
MlirTypeID mlirConditionallySpeculatableOpInterfaceTypeID()
Returns the interface TypeID of the ConditionallySpeculatable interface.
MlirTypeID mlirMemoryEffectGetEffectID(MlirMemoryEffect effect)
Returns the TypeID identifying the concrete type of the given memory effect.
MlirMemoryEffectInstance mlirMemoryEffectInstanceClone(MlirMemoryEffectInstance instance)
Creates a copy of a memory effect instance.
void mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance)
Destroys a memory effect instance created or cloned by APIs above.
MlirMemoryEffect mlirMemoryEffectsFreeGet()
Returns the singleton instance of the free memory effect.
MlirSideEffectResource mlirMemoryEffectInstanceGetResource(MlirMemoryEffectInstance instance)
Returns the side effect resource of the given instance.
bool mlirMemoryEffectInstanceGetEffectOnFullRegion(MlirMemoryEffectInstance instance)
Returns true if the given instance has effect on every single value of the resource.
MlirSpeculatability mlirConditionallySpeculatableOpInterfaceGetSpeculatability(MlirOperation operation)
Returns the speculatability of the given operation.
MlirMemoryEffect mlirMemoryEffectsReadGet()
Returns the singleton instance of the read memory effect.
bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
MlirTypeID mlirMemoryEffectsOpInterfaceTypeID()
Returns the interface TypeID of the MemoryEffectsOpInterface.
MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpResult(MlirMemoryEffect effect, MlirValue result, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with an operation result.
MlirValue mlirMemoryEffectInstanceGetValue(MlirMemoryEffectInstance instance)
Returns the value (OpOperand, OpResult, or BlockArgument) of the given instance, or a null value if t...
bool mlirOperationImplementsInterfaceStatic(MlirStringRef operationName, MlirContext context, MlirTypeID interfaceTypeID)
Returns true if the operation identified by its canonical string name implements the interface identi...
void mlirConditionallySpeculatableOpInterfaceAttachFallbackModel(MlirContext ctx, MlirStringRef opName, MlirConditionallySpeculatableOpInterfaceCallbacks callbacks)
Attach a ConditionallySpeculatable FallbackModel to the given named op.
void mlirMemoryEffectsOpInterfaceAttachFallbackModel(MlirContext ctx, MlirStringRef opName, MlirMemoryEffectsOpInterfaceCallbacks callbacks)
Attach a MemoryEffectsOpInterface FallbackModel to the given named op.
MlirTypeID mlirInferTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferTypeOpInterface.
MlirMemoryEffect mlirMemoryEffectsWriteGet()
Returns the singleton instance of the write memory effect.
MlirLogicalResult mlirInferTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, void *properties, intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback, void *userData)
Infers the return types of the operation identified by its canonical given the arguments that will be...
MlirMemoryEffect mlirMemoryEffectsAllocateGet()
Returns the singleton instance of the allocate memory effect.
MlirTypeID mlirInferShapedTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferShapedTypeOpInterface.
MlirMemoryEffectInstance mlirMemoryEffectInstanceCreate(MlirMemoryEffect effect, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance without an associated IR entity.
MlirMemoryEffect mlirMemoryEffectInstanceGetEffect(MlirMemoryEffectInstance instance)
Returns the memory effect of the given instance.
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Fallback model for the ConditionallySpeculatable interface that uses C API callbacks.
static bool classof(const mlir::ConditionallySpeculatable::Concept *op)
static TypeID getInterfaceID()
Speculation::Speculatability getSpeculatability(Operation *op) const
~ConditionallySpeculatableOpInterfaceFallbackModel()
void setCallbacks(MlirConditionallySpeculatableOpInterfaceCallbacks callbacks)
Sets the callbacks that this FallbackModel will use.
Fallback model for the MemoryEffectsOpInterface that uses C API callbacks.
void setCallbacks(MlirMemoryEffectsOpInterfaceCallbacks callbacks)
Sets the callbacks that this FallbackModel will use.
static bool classof(const mlir::MemoryEffectOpInterface::Concept *op)
~MemoryEffectOpInterfaceFallbackModel()
static TypeID getInterfaceID()
void getEffects(Operation *op, SmallVectorImpl< MemoryEffects::EffectInstance > &effects) const
Operation is the basic unit of execution within MLIR.
Type-safe wrapper around a void* for passing properties, including the properties structs of operatio...
static std::optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
ShapedTypeComponents that represents the components of a ShapedType.
static DerivedEffect * get()
static DefaultResource * get()
This class represents a specific resource that an effect applies to.
This class provides an efficient unique identifier for a specific C++ type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
MlirSpeculatability
Enum representing the speculatability of an operation.
@ MlirSpeculatabilityRecursivelySpeculatable
The operation is speculatable if all nested operations are speculatable.
@ MlirSpeculatabilitySpeculatable
The operation is speculatable.
@ MlirSpeculatabilityNotSpeculatable
The operation is not speculatable.
void(* MlirShapedTypeComponentsCallback)(bool, intptr_t, const int64_t *, MlirType, MlirAttribute, void *)
These callbacks are used to return multiple shaped type components from functions while transferring ...
void(* MlirMemoryEffectInstancesCallback)(intptr_t numEffects, MlirMemoryEffectInstance *effects, void *userData)
Callback for receiving a batch of memory effect instances.
void(* MlirTypesCallback)(intptr_t, MlirType *, void *)
These callbacks are used to return multiple types from functions while transferring ownership to the ...
static MlirLogicalResult mlirLogicalResultFailure(void)
Creates a logical result representing a failure.
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
SideEffects::EffectInstance< Effect > EffectInstance
constexpr auto RecursivelySpeculatable
Speculatability
This enum is returned from the getSpeculatability method in the ConditionallySpeculatable op interfac...
constexpr auto Speculatable
constexpr auto NotSpeculatable
Include the generated interface declarations.
Callbacks for implementing ConditionallySpeculatable from external code.
A logical result value, essentially a boolean with named states.
Callbacks for implementing MemoryEffectsOpInterface from external code.
A pointer to a sized fragment of a string, not necessarily null-terminated.
const char * data
Pointer to the first symbol.
size_t length
Length of the fragment.
This class represents the base class used for memory effects.