Skip to content

Commit 7675f54

Browse files
authored
[MLIR] Introduce new C bindings to differentiate between discardable and inherent attributes (llvm#66332)
This is part of the transition toward properly splitting the two groups. This only introduces new C APIs, the Python bindings are unaffected. No API is removed.
1 parent 5746407 commit 7675f54

File tree

4 files changed

+137
-22
lines changed

4 files changed

+137
-22
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,25 +576,77 @@ MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumSuccessors(MlirOperation op);
576576
MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetSuccessor(MlirOperation op,
577577
intptr_t pos);
578578

579+
/// Returns true if this operation defines an inherent attribute with this name.
580+
/// Note: the attribute can be optional, so
581+
/// `mlirOperationGetInherentAttributeByName` can still return a null attribute.
582+
MLIR_CAPI_EXPORTED bool
583+
mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name);
584+
585+
/// Returns an inherent attribute attached to the operation given its name.
586+
MLIR_CAPI_EXPORTED MlirAttribute
587+
mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name);
588+
589+
/// Sets an inherent attribute by name, replacing the existing if it exists.
590+
/// This has no effect if "name" does not match an inherent attribute.
591+
MLIR_CAPI_EXPORTED void
592+
mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name,
593+
MlirAttribute attr);
594+
595+
/// Returns the number of discardable attributes attached to the operation.
596+
MLIR_CAPI_EXPORTED intptr_t
597+
mlirOperationGetNumDiscardableAttributes(MlirOperation op);
598+
599+
/// Return `pos`-th discardable attribute of the operation.
600+
MLIR_CAPI_EXPORTED MlirNamedAttribute
601+
mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos);
602+
603+
/// Returns a discardable attribute attached to the operation given its name.
604+
MLIR_CAPI_EXPORTED MlirAttribute mlirOperationGetDiscardableAttributeByName(
605+
MlirOperation op, MlirStringRef name);
606+
607+
/// Sets a discardable attribute by name, replacing the existing if it exists or
608+
/// adding a new one otherwise. The new `attr` Attribute is not allowed to be
609+
/// null, use `mlirOperationRemoveDiscardableAttributeByName` to remove an
610+
/// Attribute instead.
611+
MLIR_CAPI_EXPORTED void
612+
mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name,
613+
MlirAttribute attr);
614+
615+
/// Removes a discardable attribute by name. Returns false if the attribute was
616+
/// not found and true if removed.
617+
MLIR_CAPI_EXPORTED bool
618+
mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,
619+
MlirStringRef name);
620+
579621
/// Returns the number of attributes attached to the operation.
622+
/// Deprecated, please use `mlirOperationGetNumInherentAttributes` or
623+
/// `mlirOperationGetNumDiscardableAttributes`.
580624
MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumAttributes(MlirOperation op);
581625

582626
/// Return `pos`-th attribute of the operation.
627+
/// Deprecated, please use `mlirOperationGetInherentAttribute` or
628+
/// `mlirOperationGetDiscardableAttribute`.
583629
MLIR_CAPI_EXPORTED MlirNamedAttribute
584630
mlirOperationGetAttribute(MlirOperation op, intptr_t pos);
585631

586632
/// Returns an attribute attached to the operation given its name.
633+
/// Deprecated, please use `mlirOperationGetInherentAttributeByName` or
634+
/// `mlirOperationGetDiscardableAttributeByName`.
587635
MLIR_CAPI_EXPORTED MlirAttribute
588636
mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name);
589637

590638
/// Sets an attribute by name, replacing the existing if it exists or
591639
/// adding a new one otherwise.
640+
/// Deprecated, please use `mlirOperationSetInherentAttributeByName` or
641+
/// `mlirOperationSetDiscardableAttributeByName`.
592642
MLIR_CAPI_EXPORTED void mlirOperationSetAttributeByName(MlirOperation op,
593643
MlirStringRef name,
594644
MlirAttribute attr);
595645

596646
/// Removes an attribute by name. Returns false if the attribute was not found
597647
/// and true if removed.
648+
/// Deprecated, please use `mlirOperationRemoveInherentAttributeByName` or
649+
/// `mlirOperationRemoveDiscardableAttributeByName`.
598650
MLIR_CAPI_EXPORTED bool mlirOperationRemoveAttributeByName(MlirOperation op,
599651
MlirStringRef name);
600652

mlir/include/mlir/IR/Operation.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,23 @@ class alignas(8) Operation final
457457
if (attributes.set(name, value) != value)
458458
attrs = attributes.getDictionary(getContext());
459459
}
460+
void setDiscardableAttr(StringRef name, Attribute value) {
461+
setDiscardableAttr(StringAttr::get(getContext(), name), value);
462+
}
463+
464+
/// Remove the discardable attribute with the specified name if it exists.
465+
/// Return the attribute that was erased, or nullptr if there was no attribute
466+
/// with such name.
467+
Attribute removeDiscardableAttr(StringAttr name) {
468+
NamedAttrList attributes(attrs);
469+
Attribute removedAttr = attributes.erase(name);
470+
if (removedAttr)
471+
attrs = attributes.getDictionary(getContext());
472+
return removedAttr;
473+
}
474+
Attribute removeDiscardableAttr(StringRef name) {
475+
return removeDiscardableAttr(StringAttr::get(getContext(), name));
476+
}
460477

461478
/// Return all of the discardable attributes on this operation.
462479
ArrayRef<NamedAttribute> getDiscardableAttrs() { return attrs.getValue(); }

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,53 @@ MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
595595
return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
596596
}
597597

598+
MLIR_CAPI_EXPORTED bool
599+
mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name) {
600+
std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
601+
return attr.has_value();
602+
}
603+
604+
MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
605+
MlirStringRef name) {
606+
std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
607+
if (attr.has_value())
608+
return wrap(*attr);
609+
return {};
610+
}
611+
612+
void mlirOperationSetInherentAttributeByName(MlirOperation op,
613+
MlirStringRef name,
614+
MlirAttribute attr) {
615+
unwrap(op)->setInherentAttr(
616+
StringAttr::get(unwrap(op)->getContext(), unwrap(name)), unwrap(attr));
617+
}
618+
619+
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op) {
620+
return static_cast<intptr_t>(unwrap(op)->getDiscardableAttrs().size());
621+
}
622+
623+
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op,
624+
intptr_t pos) {
625+
NamedAttribute attr = unwrap(op)->getDiscardableAttrs()[pos];
626+
return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
627+
}
628+
629+
MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
630+
MlirStringRef name) {
631+
return wrap(unwrap(op)->getDiscardableAttr(unwrap(name)));
632+
}
633+
634+
void mlirOperationSetDiscardableAttributeByName(MlirOperation op,
635+
MlirStringRef name,
636+
MlirAttribute attr) {
637+
unwrap(op)->setDiscardableAttr(unwrap(name), unwrap(attr));
638+
}
639+
640+
bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,
641+
MlirStringRef name) {
642+
return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
643+
}
644+
598645
intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
599646
return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
600647
}

mlir/test/CAPI/ir.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -407,24 +407,23 @@ static void printFirstOfEach(MlirContext ctx, MlirOperation operation) {
407407
fprintf(stderr, "\n");
408408
// CHECK: Terminator: func.return
409409

410-
// Get the attribute by index.
411-
MlirNamedAttribute namedAttr0 = mlirOperationGetAttribute(operation, 0);
412-
fprintf(stderr, "Get attr 0: ");
413-
mlirAttributePrint(namedAttr0.attribute, printToStderr, NULL);
410+
// Get the attribute by name.
411+
bool hasValueAttr = mlirOperationHasInherentAttributeByName(
412+
operation, mlirStringRefCreateFromCString("value"));
413+
if (hasValueAttr)
414+
// CHECK: Has attr "value"
415+
fprintf(stderr, "Has attr \"value\"");
416+
417+
MlirAttribute valueAttr0 = mlirOperationGetInherentAttributeByName(
418+
operation, mlirStringRefCreateFromCString("value"));
419+
fprintf(stderr, "Get attr \"value\": ");
420+
mlirAttributePrint(valueAttr0, printToStderr, NULL);
414421
fprintf(stderr, "\n");
415-
// CHECK: Get attr 0: 0 : index
416-
417-
// Now re-get the attribute by name.
418-
MlirAttribute attr0ByName = mlirOperationGetAttributeByName(
419-
operation, mlirIdentifierStr(namedAttr0.name));
420-
fprintf(stderr, "Get attr 0 by name: ");
421-
mlirAttributePrint(attr0ByName, printToStderr, NULL);
422-
fprintf(stderr, "\n");
423-
// CHECK: Get attr 0 by name: 0 : index
422+
// CHECK: Get attr "value": 0 : index
424423

425424
// Get a non-existing attribute and assert that it is null (sanity).
426425
fprintf(stderr, "does_not_exist is null: %d\n",
427-
mlirAttributeIsNull(mlirOperationGetAttributeByName(
426+
mlirAttributeIsNull(mlirOperationGetDiscardableAttributeByName(
428427
operation, mlirStringRefCreateFromCString("does_not_exist"))));
429428
// CHECK: does_not_exist is null: 1
430429

@@ -443,24 +442,24 @@ static void printFirstOfEach(MlirContext ctx, MlirOperation operation) {
443442
fprintf(stderr, "\n");
444443
// CHECK: Result 0 type: index
445444

446-
// Set a custom attribute.
447-
mlirOperationSetAttributeByName(operation,
448-
mlirStringRefCreateFromCString("custom_attr"),
449-
mlirBoolAttrGet(ctx, 1));
445+
// Set a discardable attribute.
446+
mlirOperationSetDiscardableAttributeByName(
447+
operation, mlirStringRefCreateFromCString("custom_attr"),
448+
mlirBoolAttrGet(ctx, 1));
450449
fprintf(stderr, "Op with set attr: ");
451450
mlirOperationPrint(operation, printToStderr, NULL);
452451
fprintf(stderr, "\n");
453452
// CHECK: Op with set attr: {{.*}} {custom_attr = true}
454453

455454
// Remove the attribute.
456455
fprintf(stderr, "Remove attr: %d\n",
457-
mlirOperationRemoveAttributeByName(
456+
mlirOperationRemoveDiscardableAttributeByName(
458457
operation, mlirStringRefCreateFromCString("custom_attr")));
459458
fprintf(stderr, "Remove attr again: %d\n",
460-
mlirOperationRemoveAttributeByName(
459+
mlirOperationRemoveDiscardableAttributeByName(
461460
operation, mlirStringRefCreateFromCString("custom_attr")));
462461
fprintf(stderr, "Removed attr is null: %d\n",
463-
mlirAttributeIsNull(mlirOperationGetAttributeByName(
462+
mlirAttributeIsNull(mlirOperationGetDiscardableAttributeByName(
464463
operation, mlirStringRefCreateFromCString("custom_attr"))));
465464
// CHECK: Remove attr: 1
466465
// CHECK: Remove attr again: 0
@@ -469,7 +468,7 @@ static void printFirstOfEach(MlirContext ctx, MlirOperation operation) {
469468
// Add a large attribute to verify printing flags.
470469
int64_t eltsShape[] = {4};
471470
int32_t eltsData[] = {1, 2, 3, 4};
472-
mlirOperationSetAttributeByName(
471+
mlirOperationSetDiscardableAttributeByName(
473472
operation, mlirStringRefCreateFromCString("elts"),
474473
mlirDenseElementsAttrInt32Get(
475474
mlirRankedTensorTypeGet(1, eltsShape, mlirIntegerTypeGet(ctx, 32),

0 commit comments

Comments
 (0)