1 //===- OpGenHelpers.cpp - MLIR operation generator helpers ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines helpers used in the op generators. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "OpGenHelpers.h" 14 #include "llvm/Support/CommandLine.h" 15 #include "llvm/Support/FormatVariadic.h" 16 #include "llvm/Support/Regex.h" 17 #include "llvm/TableGen/Error.h" 18 19 using namespace llvm; 20 using namespace mlir; 21 using namespace mlir::tblgen; 22 23 cl::OptionCategory opDefGenCat("Options for op definition generators"); 24 25 static cl::opt<std::string> opIncFilter( 26 "op-include-regex", 27 cl::desc("Regex of name of op's to include (no filter if empty)"), 28 cl::cat(opDefGenCat)); 29 static cl::opt<std::string> opExcFilter( 30 "op-exclude-regex", 31 cl::desc("Regex of name of op's to exclude (no filter if empty)"), 32 cl::cat(opDefGenCat)); 33 getOperationName(const Record & def)34static std::string getOperationName(const Record &def) { 35 auto prefix = def.getValueAsDef("opDialect")->getValueAsString("name"); 36 auto opName = def.getValueAsString("opName"); 37 if (prefix.empty()) 38 return std::string(opName); 39 return std::string(llvm::formatv("{0}.{1}", prefix, opName)); 40 } 41 42 std::vector<Record *> getRequestedOpDefinitions(const RecordKeeper & recordKeeper)43mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper &recordKeeper) { 44 Record *classDef = recordKeeper.getClass("Op"); 45 if (!classDef) 46 PrintFatalError("ERROR: Couldn't find the 'Op' class!\n"); 47 48 llvm::Regex includeRegex(opIncFilter), excludeRegex(opExcFilter); 49 std::vector<Record *> defs; 50 for (const auto &def : recordKeeper.getDefs()) { 51 if (!def.second->isSubClassOf(classDef)) 52 continue; 53 // Include if no include filter or include filter matches. 54 if (!opIncFilter.empty() && 55 !includeRegex.match(getOperationName(*def.second))) 56 continue; 57 // Unless there is an exclude filter and it matches. 58 if (!opExcFilter.empty() && 59 excludeRegex.match(getOperationName(*def.second))) 60 continue; 61 defs.push_back(def.second.get()); 62 } 63 64 return defs; 65 } 66