//===- CodeGenHelpers.cpp - MLIR op definitions generator ---------------===// // // 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 // //===----------------------------------------------------------------------===// // // OpDefinitionsGen uses the description of operations to generate C++ // definitions for ops. // //===----------------------------------------------------------------------===// #include "mlir/TableGen/CodeGenHelpers.h" #include "mlir/TableGen/Operator.h" #include "mlir/TableGen/Pattern.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" #include "llvm/TableGen/Record.h" usingnamespacellvm; usingnamespacemlir; usingnamespacemlir::tblgen; /// Generate a unique label based on the current file name to prevent name /// collisions if multiple generated files are included at once. static std::string getUniqueOutputLabel(const llvm::RecordKeeper &records, StringRef tag) { … } StaticVerifierFunctionEmitter::StaticVerifierFunctionEmitter( raw_ostream &os, const llvm::RecordKeeper &records, StringRef tag) : … { … } void StaticVerifierFunctionEmitter::emitOpConstraints( ArrayRef<const llvm::Record *> opDefs) { … } void StaticVerifierFunctionEmitter::emitPatternConstraints( const llvm::ArrayRef<DagLeaf> constraints) { … } //===----------------------------------------------------------------------===// // Constraint Getters StringRef StaticVerifierFunctionEmitter::getTypeConstraintFn( const Constraint &constraint) const { … } // Find a uniqued attribute constraint. Since not all attribute constraints can // be uniqued, return std::nullopt if one was not found. std::optional<StringRef> StaticVerifierFunctionEmitter::getAttrConstraintFn( const Constraint &constraint) const { … } StringRef StaticVerifierFunctionEmitter::getSuccessorConstraintFn( const Constraint &constraint) const { … } StringRef StaticVerifierFunctionEmitter::getRegionConstraintFn( const Constraint &constraint) const { … } //===----------------------------------------------------------------------===// // Constraint Emission /// Code templates for emitting type, attribute, successor, and region /// constraints. Each of these templates require the following arguments: /// /// {0}: The unique constraint name. /// {1}: The constraint code. /// {2}: The constraint description. /// Code for a type constraint. These may be called on the type of either /// operands or results. static const char *const typeConstraintCode = …; /// Code for an attribute constraint. These may be called from ops only. /// Attribute constraints cannot reference anything other than `$_self` and /// `$_op`. /// /// TODO: Unique constraints for adaptors. However, most Adaptor::verify /// functions are stripped anyways. static const char *const attrConstraintCode = …; /// Code for a successor constraint. static const char *const successorConstraintCode = …; /// Code for a region constraint. Callers will need to pass in the region's name /// for emitting an error message. static const char *const regionConstraintCode = …; /// Code for a pattern type or attribute constraint. /// /// {3}: "Type type" or "Attribute attr". static const char *const patternAttrOrTypeConstraintCode = …; void StaticVerifierFunctionEmitter::emitConstraints( const ConstraintMap &constraints, StringRef selfName, const char *const codeTemplate) { … } void StaticVerifierFunctionEmitter::emitTypeConstraints() { … } void StaticVerifierFunctionEmitter::emitAttrConstraints() { … } void StaticVerifierFunctionEmitter::emitSuccessorConstraints() { … } void StaticVerifierFunctionEmitter::emitRegionConstraints() { … } void StaticVerifierFunctionEmitter::emitPatternConstraints() { … } //===----------------------------------------------------------------------===// // Constraint Uniquing /// An attribute constraint that references anything other than itself and the /// current op cannot be generically extracted into a function. Most /// prohibitive are operands and results, which require calls to /// `getODSOperands` or `getODSResults`. Attribute references are tricky too /// because ops use cached identifiers. static bool canUniqueAttrConstraint(Attribute attr) { … } std::string StaticVerifierFunctionEmitter::getUniqueName(StringRef kind, unsigned index) { … } void StaticVerifierFunctionEmitter::collectConstraint(ConstraintMap &map, StringRef kind, Constraint constraint) { … } void StaticVerifierFunctionEmitter::collectOpConstraints( ArrayRef<const Record *> opDefs) { … } void StaticVerifierFunctionEmitter::collectPatternConstraints( const llvm::ArrayRef<DagLeaf> constraints) { … } //===----------------------------------------------------------------------===// // Public Utility Functions //===----------------------------------------------------------------------===// std::string mlir::tblgen::escapeString(StringRef value) { … }