Skip to content

Commit 30ce354

Browse files
committed
tablegen: add -emit-comments option
This is helpful for debugging the tablegen tool itself.
1 parent 1167e32 commit 30ce354

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

include/llvm-dialects/TableGen/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ namespace llvm_dialects {
2222

2323
void emitHeader(llvm::raw_ostream& out);
2424

25+
bool shouldEmitComments();
26+
2527
} // namespace llvm_dialects

include/llvm-dialects/TableGen/Constraints.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class ConstraintSystem {
7878
bool addConstraint(llvm::raw_ostream &errs, llvm::Init *init, Variable *self);
7979
void merge(ConstraintSystem &&rhs);
8080

81+
void print(llvm::raw_ostream &out, llvm::StringRef prefix) const;
82+
void dump() const;
83+
8184
private:
8285
bool addConstraintImpl(llvm::raw_ostream &errs, llvm::Init *init,
8386
Variable *self);
@@ -103,6 +106,8 @@ class Constraint {
103106

104107
virtual ~Constraint() = default;
105108

109+
virtual void print(llvm::raw_ostream &out, llvm::StringRef prefix) const = 0;
110+
106111
Kind getKind() const { return m_kind; }
107112
llvm::Init *getInit() const { return m_init; }
108113
Variable *getSelf() const { return m_self; }
@@ -113,6 +118,7 @@ class Constraint {
113118
protected:
114119
Constraint(Kind kind) : m_kind(kind) {}
115120

121+
void printVariables(llvm::raw_ostream &out) const;
116122
void addVariable(Variable *variable);
117123

118124
private:
@@ -140,6 +146,8 @@ class Apply : public Constraint {
140146
return c->getKind() == Kind::Apply;
141147
}
142148

149+
void print(llvm::raw_ostream &out, llvm::StringRef prefix) const override;
150+
143151
Predicate *getPredicate() const { return m_predicate; }
144152
llvm::ArrayRef<Variable *> arguments() const { return m_arguments; }
145153

@@ -159,6 +167,8 @@ class LogicOr : public Constraint {
159167

160168
static bool classof(const Constraint *c) { return c->getKind() == Kind::Or; }
161169

170+
void print(llvm::raw_ostream &out, llvm::StringRef prefix) const override;
171+
162172
llvm::ArrayRef<ConstraintSystem> branches() const { return m_branches; }
163173
llvm::ArrayRef<llvm::Init *> branchInits() const { return m_branchInits; }
164174

include/llvm-dialects/TableGen/Evaluator.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ class Evaluator {
131131
/// detected.
132132
bool check();
133133

134-
void setEmitComments(bool comments) { m_comments = comments; }
135-
136134
private:
137135
void checkErrors();
138136
std::string evaluateImpl(Variable *variable);

lib/TableGen/Common.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@
1616

1717
#include "llvm-dialects/TableGen/Common.h"
1818

19+
#include "llvm/Support/CommandLine.h"
20+
1921
using namespace llvm_dialects;
2022
using namespace llvm;
2123

24+
static cl::opt<bool>
25+
g_emitComments("emit-comments",
26+
cl::desc("emit debug comments in generated source files"),
27+
cl::Hidden, cl::init(false));
28+
2229
void llvm_dialects::emitHeader(raw_ostream& out) {
2330
out << "// DO NOT EDIT! This file is automatically generated by llvm-dialects-tblgen.\n\n";
2431
}
32+
33+
bool llvm_dialects::shouldEmitComments() { return g_emitComments; }

lib/TableGen/Constraints.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm-dialects/TableGen/Format.h"
2222
#include "llvm-dialects/TableGen/Predicates.h"
2323

24+
#include "llvm/Support/Debug.h"
2425
#include "llvm/TableGen/Record.h"
2526

2627
using namespace llvm;
@@ -60,6 +61,16 @@ Variable *Scope::createVariable(StringRef name, Predicate *predicate) {
6061
return variable;
6162
}
6263

64+
void ConstraintSystem::print(raw_ostream &out, StringRef prefix) const {
65+
for (const auto &constraintPtr : m_constraints)
66+
constraintPtr->print(out, prefix);
67+
}
68+
69+
void ConstraintSystem::dump() const {
70+
dbgs() << "Constraint system:\n";
71+
print(dbgs(), " ");
72+
}
73+
6374
bool ConstraintSystem::addConstraint(raw_ostream &errs, Init *init,
6475
Variable *self) {
6576
if (!addConstraintImpl(errs, init, self)) {
@@ -232,11 +243,48 @@ std::string Constraint::toString() const {
232243
return (Twine(m_init->getAsString()) + ":" + m_self->toString()).str();
233244
}
234245

246+
void Constraint::printVariables(raw_ostream &out) const {
247+
bool isFirst = true;
248+
for (const Variable *var : m_variables) {
249+
if (!isFirst)
250+
out << ", ";
251+
out << var->toString();
252+
isFirst = false;
253+
}
254+
}
255+
235256
void Constraint::addVariable(Variable *variable) {
236257
if (!llvm::is_contained(m_variables, variable))
237258
m_variables.push_back(variable);
238259
}
239260

261+
void Apply::print(raw_ostream &out, StringRef prefix) const {
262+
out << prefix << "apply " << m_predicate->getInit()->getAsString() << ": ";
263+
bool isFirst = true;
264+
for (Variable *var : m_arguments) {
265+
if (!isFirst)
266+
out << ", ";
267+
if (var)
268+
out << var->toString();
269+
else
270+
out << "(null)";
271+
isFirst = false;
272+
}
273+
out << '\n';
274+
275+
out << prefix << " free variables: ";
276+
printVariables(out);
277+
out << '\n';
278+
}
279+
280+
void LogicOr::print(raw_ostream &out, StringRef prefix) const {
281+
out << prefix << "or, free variables: ";
282+
printVariables(out);
283+
out << '\n';
284+
for (const auto &branch : m_branches)
285+
branch.print(out, (Twine(prefix) + " ").str());
286+
}
287+
240288
MetaType *MetaType::type() {
241289
static MetaType typeMetaType(Kind::Type);
242290
return &typeMetaType;

lib/TableGen/Evaluator.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm-dialects/TableGen/Evaluator.h"
1818

19+
#include "llvm-dialects/TableGen/Common.h"
1920
#include "llvm-dialects/TableGen/Constraints.h"
2021
#include "llvm-dialects/TableGen/Format.h"
2122
#include "llvm-dialects/TableGen/Predicates.h"
@@ -204,6 +205,8 @@ Evaluator::Evaluator(SymbolTable &symbols, Assignment &assignment,
204205
if (!assignment.lookup(variable).empty())
205206
m_planner.setKnown(variable);
206207
}
208+
209+
m_comments = shouldEmitComments();
207210
}
208211

209212
Evaluator::~Evaluator() { checkErrors(); }
@@ -279,7 +282,6 @@ std::string Evaluator::evaluateImpl(Variable *goal) {
279282
SymbolScope symbolScope{&m_symbols};
280283
Evaluator nested(*m_symbols, nestedAssignment, tg->getSystem(), m_out,
281284
m_fmt);
282-
nested.setEmitComments(m_comments);
283285
std::string value = nested.evaluate(tg->variables()[plan->argumentIndex]);
284286
if (value.empty()) {
285287
m_errs << "TgPredicate expression evaluation failed\n";
@@ -304,6 +306,11 @@ std::string Evaluator::evaluateImpl(Variable *goal) {
304306
}
305307

306308
bool Evaluator::check() {
309+
if (m_comments) {
310+
m_out << "// Checking the constraint system:\n";
311+
m_system.print(m_out, "// ");
312+
}
313+
307314
SmallVector<bool> checkedConstraints;
308315
checkedConstraints.resize(m_system.m_constraints.size(), false);
309316

0 commit comments

Comments
 (0)