This PR is quite similiar to #174700. In this PR, I added a C API for each (upstream) MLIR attributes to retrieve its name (for example, `StringAttr -> mlirStringAttrGetName() -> "builtin.string"`), and exposed a corresponding type_name class attribute in the Python bindings (e.g., `StringAttr.attr_name -> "builtin.string"`). This can be used in various places to avoid hard-coded strings, such as eliminating the manual string in `irdl.base("#builtin.string")`. Note that parts of this PR (mainly mechanical changes) were produced via GitHub Copilot and GPT-5.2. I have manually reviewed the changes and verified them with tests to ensure correctness.
54 lines
2.0 KiB
C++
54 lines
2.0 KiB
C++
//===- IRDL.cpp - C Interface for IRDL dialect ----------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir-c/Dialect/IRDL.h"
|
|
#include "mlir/CAPI/Registration.h"
|
|
#include "mlir/Dialect/IRDL/IR/IRDL.h"
|
|
#include "mlir/Dialect/IRDL/IRDLLoading.h"
|
|
|
|
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(IRDL, irdl, mlir::irdl::IRDLDialect)
|
|
|
|
MlirLogicalResult mlirLoadIRDLDialects(MlirModule module) {
|
|
return wrap(mlir::irdl::loadDialects(unwrap(module)));
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// VariadicityAttr
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
MlirAttribute mlirIRDLVariadicityAttrGet(MlirContext ctx, MlirStringRef value) {
|
|
return wrap(mlir::irdl::VariadicityAttr::get(
|
|
unwrap(ctx), mlir::irdl::symbolizeVariadicity(unwrap(value)).value()));
|
|
}
|
|
|
|
MlirStringRef mlirIRDLVariadicityAttrGetName(void) {
|
|
return wrap(mlir::irdl::VariadicityAttr::name);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// VariadicityArrayAttr
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
MlirAttribute mlirIRDLVariadicityArrayAttrGet(MlirContext ctx, intptr_t nValues,
|
|
MlirAttribute const *values) {
|
|
llvm::SmallVector<mlir::Attribute> attrs;
|
|
llvm::ArrayRef<mlir::Attribute> unwrappedAttrs =
|
|
unwrapList(nValues, values, attrs);
|
|
|
|
llvm::SmallVector<mlir::irdl::VariadicityAttr> variadicities;
|
|
for (auto attr : unwrappedAttrs)
|
|
variadicities.push_back(llvm::cast<mlir::irdl::VariadicityAttr>(attr));
|
|
|
|
return wrap(
|
|
mlir::irdl::VariadicityArrayAttr::get(unwrap(ctx), variadicities));
|
|
}
|
|
|
|
MlirStringRef mlirIRDLVariadicityArrayAttrGetName(void) {
|
|
return wrap(mlir::irdl::VariadicityArrayAttr::name);
|
|
}
|