MLIR  16.0.0git
MlirTblgenMain.cpp
Go to the documentation of this file.
1 //===- MlirTblgenMain.cpp - MLIR Tablegen Driver main -----------*- C++ -*-===//
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 // Main entry function for mlir-tblgen for when built as standalone binary.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
15 #include "mlir/TableGen/GenInfo.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/FormatVariadic.h"
19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/TableGen/Error.h"
22 #include "llvm/TableGen/Main.h"
23 #include "llvm/TableGen/Record.h"
24 
25 using namespace mlir;
26 using namespace llvm;
27 
29 
31 
32 // Returns if there is a use of `init` in `record`.
33 static bool findUse(Record &record, Init *init,
35  auto it = known.find(&record);
36  if (it != known.end())
37  return it->second;
38 
39  auto memoize = [&](bool val) {
40  known[&record] = val;
41  return val;
42  };
43 
44  for (const RecordVal &val : record.getValues()) {
45  Init *valInit = val.getValue();
46  if (valInit == init)
47  return true;
48  if (auto *di = dyn_cast<DefInit>(valInit)) {
49  if (findUse(*di->getDef(), init, known))
50  return memoize(true);
51  } else if (auto *di = dyn_cast<DagInit>(valInit)) {
52  for (Init *arg : di->getArgs())
53  if (auto *di = dyn_cast<DefInit>(arg))
54  if (findUse(*di->getDef(), init, known))
55  return memoize(true);
56  } else if (ListInit *li = dyn_cast<ListInit>(valInit)) {
57  for (Init *jt : li->getValues())
58  if (jt == init)
59  return memoize(true);
60  }
61  }
62  return memoize(false);
63 }
64 
65 static void warnOfDeprecatedUses(RecordKeeper &records) {
66  // This performs a direct check for any def marked as deprecated and then
67  // finds all uses of deprecated def. Deprecated defs are not expected to be
68  // either numerous or long lived.
69  bool deprecatedDefsFounds = false;
70  for (auto &it : records.getDefs()) {
71  const RecordVal *r = it.second->getValue("odsDeprecated");
72  if (!r || !r->getValue())
73  continue;
74 
76  if (auto *si = dyn_cast<StringInit>(r->getValue())) {
77  for (auto &jt : records.getDefs()) {
78  // Skip anonymous defs.
79  if (jt.second->isAnonymous())
80  continue;
81  // Skip all outside main file to avoid flagging redundantly.
82  unsigned buf =
83  SrcMgr.FindBufferContainingLoc(jt.second->getLoc().front());
84  if (buf != SrcMgr.getMainFileID())
85  continue;
86 
87  if (findUse(*jt.second, it.second->getDefInit(), hasUse)) {
88  PrintWarning(jt.second->getLoc(),
89  "Using deprecated def `" + it.first + "`");
90  PrintNote(si->getAsUnquotedString());
91  deprecatedDefsFounds = true;
92  }
93  }
94  }
95  }
96  if (deprecatedDefsFounds &&
98  PrintFatalNote("Error'ing out due to deprecated defs");
99 }
100 
101 // Generator to invoke.
102 static const mlir::GenInfo *generator;
103 
104 // TableGenMain requires a function pointer so this function is passed in which
105 // simply wraps the call to the generator.
106 static bool mlirTableGenMain(raw_ostream &os, RecordKeeper &records) {
108  warnOfDeprecatedUses(records);
109 
110  if (!generator) {
111  os << records;
112  return false;
113  }
114  return generator->invoke(records, os);
115 }
116 
117 int mlir::MlirTblgenMain(int argc, char **argv) {
118 
119  llvm::InitLLVM y(argc, argv);
120 
121  llvm::cl::opt<DeprecatedAction, true> actionOnDeprecated(
122  "on-deprecated", llvm::cl::desc("Action to perform on deprecated def"),
123  llvm::cl::values(
124  clEnumValN(DeprecatedAction::None, "none", "No action"),
125  clEnumValN(DeprecatedAction::Warn, "warn", "Warn on use"),
126  clEnumValN(DeprecatedAction::Error, "error", "Error on use")),
127  cl::location(actionOnDeprecatedValue), llvm::cl::init(Warn));
128 
129  llvm::cl::opt<const mlir::GenInfo *, true, mlir::GenNameParser> generator(
130  "", llvm::cl::desc("Generator to run"), cl::location(::generator));
131 
132  cl::ParseCommandLineOptions(argc, argv);
133 
134  return TableGenMain(argv[0], &mlirTableGenMain);
135 }
Include the generated interface declarations.
static bool mlirTableGenMain(raw_ostream &os, RecordKeeper &records)
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
static bool findUse(Record &record, Init *init, llvm::DenseMap< Record *, bool > &known)
Structure to group information about a generator (argument to invoke via mlir-tblgen, description, and generator function).
Definition: GenInfo.h:29
int MlirTblgenMain(int argc, char **argv)
Main Program for tools like &#39;mlir-tblgen&#39; with custom backends.
DeprecatedAction
static DeprecatedAction actionOnDeprecatedValue
static void warnOfDeprecatedUses(RecordKeeper &records)
bool invoke(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) const
Invokes the generator and returns whether the generator failed.
Definition: GenInfo.h:37
static const mlir::GenInfo * generator