1 //===- OpGenHelpers.cpp - MLIR operation generator helpers ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines helpers used in the op generators.
11 //===----------------------------------------------------------------------===//
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"
21 using namespace mlir::tblgen
;
23 cl::OptionCategory
opDefGenCat("Options for op definition generators");
25 static cl::opt
<std::string
> opIncFilter(
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(
31 cl::desc("Regex of name of op's to exclude (no filter if empty)"),
32 cl::cat(opDefGenCat
));
34 static std::string
getOperationName(const Record
&def
) {
35 auto prefix
= def
.getValueAsDef("opDialect")->getValueAsString("name");
36 auto opName
= def
.getValueAsString("opName");
38 return std::string(opName
);
39 return std::string(llvm::formatv("{0}.{1}", prefix
, opName
));
43 mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper
&recordKeeper
) {
44 Record
*classDef
= recordKeeper
.getClass("Op");
46 PrintFatalError("ERROR: Couldn't find the 'Op' class!\n");
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
))
53 // Include if no include filter or include filter matches.
54 if (!opIncFilter
.empty() &&
55 !includeRegex
.match(getOperationName(*def
.second
)))
57 // Unless there is an exclude filter and it matches.
58 if (!opExcFilter
.empty() &&
59 excludeRegex
.match(getOperationName(*def
.second
)))
61 defs
.push_back(def
.second
.get());