//===- EnumPythonBindingGen.cpp - Generator of Python API for ODS enums ---===// // // 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 // //===----------------------------------------------------------------------===// // // EnumPythonBindingGen uses ODS specification of MLIR enum attributes to // generate the corresponding Python binding classes. // //===----------------------------------------------------------------------===// #include "OpGenHelpers.h" #include "mlir/TableGen/AttrOrTypeDef.h" #include "mlir/TableGen/Attribute.h" #include "mlir/TableGen/Dialect.h" #include "mlir/TableGen/GenInfo.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/TableGen/Record.h" usingnamespacemlir; usingnamespacemlir::tblgen; formatv; Record; RecordKeeper; /// File header and includes. constexpr const char *fileHeader = …; /// Makes enum case name Python-compatible, i.e. UPPER_SNAKE_CASE. static std::string makePythonEnumCaseName(StringRef name) { … } /// Emits the Python class for the given enum. static void emitEnumClass(EnumAttr enumAttr, raw_ostream &os) { … } /// Attempts to extract the bitwidth B from string "uintB_t" describing the /// type. This bitwidth information is not readily available in ODS. Returns /// `false` on success, `true` on failure. static bool extractUIntBitwidth(StringRef uintType, int64_t &bitwidth) { … } /// Emits an attribute builder for the given enum attribute to support automatic /// conversion between enum values and attributes in Python. Returns /// `false` on success, `true` on failure. static bool emitAttributeBuilder(const EnumAttr &enumAttr, raw_ostream &os) { … } /// Emits an attribute builder for the given dialect enum attribute to support /// automatic conversion between enum values and attributes in Python. Returns /// `false` on success, `true` on failure. static bool emitDialectEnumAttributeBuilder(StringRef attrDefName, StringRef formatString, raw_ostream &os) { … } /// Emits Python bindings for all enums in the record keeper. Returns /// `false` on success, `true` on failure. static bool emitPythonEnums(const RecordKeeper &records, raw_ostream &os) { … } // Registers the enum utility generator to mlir-tblgen. static mlir::GenRegistration genPythonEnumBindings("gen-python-enum-bindings", "Generate Python bindings for enum attributes", &emitPythonEnums);