This PR implements python enum bindings for *all* the enums - this includes `I*Attrs` (including positional/bit) and `Dialect/EnumAttr`.
There are a few parts to this:
1. CMake: a small addition to `declare_mlir_dialect_python_bindings` and `declare_mlir_dialect_extension_python_bindings` to generate the enum, a boolean arg `GEN_ENUM_BINDINGS` to make it opt-in (even though it works for basically all of the dialects), and an optional `GEN_ENUM_BINDINGS_TD_FILE` for handling corner cases.
2. EnumPythonBindingGen.cpp: there are two weedy aspects here that took investigation:
1. If an enum attribute is not a `Dialect/EnumAttr` then the `EnumAttrInfo` record is canonical, as far as both the cases of the enum **and the `AttrDefName`**. On the otherhand, if an enum is a `Dialect/EnumAttr` then the `EnumAttr` record has the correct `AttrDefName` ("load bearing", i.e., populates `ods.ir.AttributeBuilder('<NAME>')`) but its `enum` field contains the cases, which is an instance of `EnumAttrInfo`. The solution is to generate an one enum class for both `Dialect/EnumAttr` and "independent" `EnumAttrInfo` but to make that class interopable with two builder registrations that both do the right thing (see next sub-bullet).
2. Because we don't have a good connection to cpp `EnumAttr`, i.e., only the `enum class` getters are exposed (like `DimensionAttr::get(Dimension value)`), we have to resort to parsing e.g., `Attribute.parse(f'#gpu<dim {x}>')`. This means that the set of supported `assemblyFormat`s (for the enum) is fixed at compile of MLIR (currently 2, the only 2 I saw). There might be some things that could be done here but they would require quite a bit more C API work to support generically (e.g., casting ints to enum cases and binding all the getters or going generically through the `symbolize*` methods, like `symbolizeDimension(uint32_t)` or `symbolizeDimension(StringRef)`).
A few small changes:
1. In addition, since this patch registers default builders for attributes where people might've had their own builders already written, I added a `replace` param to `AttributeBuilder.insert` (`False` by default).
2. `makePythonEnumCaseName` can't handle all the different ways in which people write their enum cases, e.g., `llvm.CConv.Intel_OCL_BI`, which gets turned into `INTEL_O_C_L_B_I` (because `llvm::convertToSnakeFromCamelCase` doesn't look for runs of caps). So I dropped it. On the otherhand regularization does need to done because some enums have `None` as a case (and others might have other python keywords).
3. I turned on `llvm` dialect generation here in order to test `nvvm.WGMMAScaleIn`, which is an enum with [[ d7e26b5620/mlir/include/mlir/IR/EnumAttr.td (L22-L25) | no explicit discriminator ]] for the `neg` case.
Note, dialects that didn't get a `GEN_ENUM_BINDINGS` don't have any enums to generate.
Let me know if I should add more tests (the three trivial ones I added exercise both the supported `assemblyFormat`s and `replace=True`).
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D157934
132 lines
5.2 KiB
C++
132 lines
5.2 KiB
C++
//===- Globals.h - MLIR Python extension globals --------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef MLIR_BINDINGS_PYTHON_GLOBALS_H
|
|
#define MLIR_BINDINGS_PYTHON_GLOBALS_H
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "PybindUtils.h"
|
|
|
|
#include "mlir-c/IR.h"
|
|
#include "mlir/CAPI/Support.h"
|
|
#include "llvm/ADT/DenseMap.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
namespace mlir {
|
|
namespace python {
|
|
|
|
/// Globals that are always accessible once the extension has been initialized.
|
|
class PyGlobals {
|
|
public:
|
|
PyGlobals();
|
|
~PyGlobals();
|
|
|
|
/// Most code should get the globals via this static accessor.
|
|
static PyGlobals &get() {
|
|
assert(instance && "PyGlobals is null");
|
|
return *instance;
|
|
}
|
|
|
|
/// Get and set the list of parent modules to search for dialect
|
|
/// implementation classes.
|
|
std::vector<std::string> &getDialectSearchPrefixes() {
|
|
return dialectSearchPrefixes;
|
|
}
|
|
void setDialectSearchPrefixes(std::vector<std::string> newValues) {
|
|
dialectSearchPrefixes.swap(newValues);
|
|
}
|
|
|
|
/// Clears positive and negative caches regarding what implementations are
|
|
/// available. Future lookups will do more expensive existence checks.
|
|
void clearImportCache();
|
|
|
|
/// Loads a python module corresponding to the given dialect namespace.
|
|
/// No-ops if the module has already been loaded or is not found. Raises
|
|
/// an error on any evaluation issues.
|
|
/// Note that this returns void because it is expected that the module
|
|
/// contains calls to decorators and helpers that register the salient
|
|
/// entities.
|
|
void loadDialectModule(llvm::StringRef dialectNamespace);
|
|
|
|
/// Adds a user-friendly Attribute builder.
|
|
/// Raises an exception if the mapping already exists and replace == false.
|
|
/// This is intended to be called by implementation code.
|
|
void registerAttributeBuilder(const std::string &attributeKind,
|
|
pybind11::function pyFunc,
|
|
bool replace = false);
|
|
|
|
/// Adds a user-friendly type caster. Raises an exception if the mapping
|
|
/// already exists and replace == false. This is intended to be called by
|
|
/// implementation code.
|
|
void registerTypeCaster(MlirTypeID mlirTypeID, pybind11::function typeCaster,
|
|
bool replace = false);
|
|
|
|
/// Adds a concrete implementation dialect class.
|
|
/// Raises an exception if the mapping already exists.
|
|
/// This is intended to be called by implementation code.
|
|
void registerDialectImpl(const std::string &dialectNamespace,
|
|
pybind11::object pyClass);
|
|
|
|
/// Adds a concrete implementation operation class.
|
|
/// Raises an exception if the mapping already exists.
|
|
/// This is intended to be called by implementation code.
|
|
void registerOperationImpl(const std::string &operationName,
|
|
pybind11::object pyClass);
|
|
|
|
/// Returns the custom Attribute builder for Attribute kind.
|
|
std::optional<pybind11::function>
|
|
lookupAttributeBuilder(const std::string &attributeKind);
|
|
|
|
/// Returns the custom type caster for MlirTypeID mlirTypeID.
|
|
std::optional<pybind11::function> lookupTypeCaster(MlirTypeID mlirTypeID,
|
|
MlirDialect dialect);
|
|
|
|
/// Looks up a registered dialect class by namespace. Note that this may
|
|
/// trigger loading of the defining module and can arbitrarily re-enter.
|
|
std::optional<pybind11::object>
|
|
lookupDialectClass(const std::string &dialectNamespace);
|
|
|
|
/// Looks up a registered operation class (deriving from OpView) by operation
|
|
/// name. Note that this may trigger a load of the dialect, which can
|
|
/// arbitrarily re-enter.
|
|
std::optional<pybind11::object>
|
|
lookupOperationClass(llvm::StringRef operationName);
|
|
|
|
private:
|
|
static PyGlobals *instance;
|
|
/// Module name prefixes to search under for dialect implementation modules.
|
|
std::vector<std::string> dialectSearchPrefixes;
|
|
/// Map of dialect namespace to external dialect class object.
|
|
llvm::StringMap<pybind11::object> dialectClassMap;
|
|
/// Map of full operation name to external operation class object.
|
|
llvm::StringMap<pybind11::object> operationClassMap;
|
|
/// Map of attribute ODS name to custom builder.
|
|
llvm::StringMap<pybind11::object> attributeBuilderMap;
|
|
/// Map of MlirTypeID to custom type caster.
|
|
llvm::DenseMap<MlirTypeID, pybind11::object> typeCasterMap;
|
|
/// Cache for map of MlirTypeID to custom type caster.
|
|
llvm::DenseMap<MlirTypeID, pybind11::object> typeCasterMapCache;
|
|
|
|
/// Set of dialect namespaces that we have attempted to import implementation
|
|
/// modules for.
|
|
llvm::StringSet<> loadedDialectModulesCache;
|
|
/// Cache of operation name to external operation class object. This is
|
|
/// maintained on lookup as a shadow of operationClassMap in order for repeat
|
|
/// lookups of the classes to only incur the cost of one hashtable lookup.
|
|
llvm::StringMap<pybind11::object> operationClassMapCache;
|
|
};
|
|
|
|
} // namespace python
|
|
} // namespace mlir
|
|
|
|
#endif // MLIR_BINDINGS_PYTHON_GLOBALS_H
|