Skip to content

Commit 6108401

Browse files
committed
Remove class-name changes
1 parent 3176910 commit 6108401

File tree

6 files changed

+18
-242
lines changed

6 files changed

+18
-242
lines changed

mlir/include/mlir/Target/Cpp/CppEmitter.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ namespace emitc {
2828
/// with matching id are emitted.
2929
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
3030
bool declareVariablesAtTop = false,
31-
StringRef fileId = {}, bool emitClass = false,
32-
StringRef className = {},
33-
StringRef fieldNameAttribute = {});
31+
StringRef fileId = {});
3432
} // namespace emitc
3533
} // namespace mlir
3634

mlir/lib/Target/Cpp/TranslateRegistration.cpp

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,13 @@ void registerToCppTranslation() {
3333
"file-id", llvm::cl::desc("Emit emitc.file ops with matching id"),
3434
llvm::cl::init(""));
3535

36-
static llvm::cl::opt<bool> emitClass(
37-
"emit-class",
38-
llvm::cl::desc("If specified, the output will be a class where "
39-
"the function(s) in the module are methods "
40-
"Enables class-related options"),
41-
llvm::cl::init(false));
42-
43-
static llvm::cl::opt<std::string> className(
44-
"class-name",
45-
llvm::cl::desc("Mandatory class name if --emit-class is set"),
46-
llvm::cl::init(""));
47-
48-
static llvm::cl::opt<std::string> fieldNameAttribute(
49-
"field-name-attribute",
50-
llvm::cl::desc("Mandatory name of the attribute to use as field name if "
51-
"--emit-class is set(default=tf_saved_model.index_path)"),
52-
llvm::cl::init("tf_saved_model.index_path"));
53-
5436
TranslateFromMLIRRegistration reg(
5537
"mlir-to-cpp", "translate from mlir to cpp",
5638
[](Operation *op, raw_ostream &output) {
57-
if (emitClass) {
58-
if (className.empty()) {
59-
llvm::errs() << "Error: --class-name is mandatory when "
60-
"--emit-class is set.\n";
61-
return mlir::failure();
62-
}
63-
if (fieldNameAttribute.empty()) {
64-
llvm::errs() << "Error: --field-name-attribute is mandatory when "
65-
"--emit-class is set.\n";
66-
return mlir::failure();
67-
}
68-
return emitc::translateToCpp(
69-
op, output,
70-
/*declareVariablesAtTop=*/declareVariablesAtTop,
71-
/*fileId=*/fileId, /*emitClass=*/emitClass,
72-
/*className=*/className,
73-
/*fieldNameAttribute=*/fieldNameAttribute);
74-
}
7539
return emitc::translateToCpp(
7640
op, output,
7741
/*declareVariablesAtTop=*/declareVariablesAtTop,
78-
/*fileId=*/fileId, /*emitClass=*/emitClass, /*className=*/className,
79-
/*fieldNameAttribute=*/fieldNameAttribute);
42+
/*fileId=*/fileId);
8043
},
8144
[](DialectRegistry &registry) {
8245
// clang-format off
@@ -87,4 +50,4 @@ void registerToCppTranslation() {
8750
});
8851
}
8952

90-
} // namespace mlir
53+
} // namespace mlir

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 15 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ inline LogicalResult interleaveCommaWithError(const Container &c,
6868
return interleaveWithError(c.begin(), c.end(), eachFn, [&]() { os << ", "; });
6969
}
7070

71-
template <typename Container, typename UnaryFunctor>
72-
inline LogicalResult interleaveWithNewLineWithError(const Container &c,
73-
raw_ostream &os,
74-
UnaryFunctor eachFn) {
75-
return interleaveWithError(c.begin(), c.end(), eachFn,
76-
[&]() { os << ";\n"; });
77-
}
78-
7971
/// Return the precedence of a operator as an integer, higher values
8072
/// imply higher precedence.
8173
static FailureOr<int> getOperatorPrecedence(Operation *operation) {
@@ -124,8 +116,7 @@ namespace {
124116
/// Emitter that uses dialect specific emitters to emit C++ code.
125117
struct CppEmitter {
126118
explicit CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
127-
StringRef fileId, bool emitClass, StringRef className,
128-
StringRef fieldNameAttribute);
119+
StringRef fileId);
129120

130121
/// Emits attribute or returns failure.
131122
LogicalResult emitAttribute(Location loc, Attribute attr);
@@ -242,15 +233,6 @@ struct CppEmitter {
242233
/// be declared at the beginning of a function.
243234
bool shouldDeclareVariablesAtTop() { return declareVariablesAtTop; };
244235

245-
// Returns whether we should emit a C++ class
246-
bool shouldPrintClass() { return emitClass; };
247-
248-
// Returns the class name to emit
249-
std::string getClassName() { return className; };
250-
251-
// Returns the field name to use in the map
252-
std::string getfieldNameAttribute() { return fieldNameAttribute; };
253-
254236
/// Returns whether this file op should be emitted
255237
bool shouldEmitFile(FileOp file) {
256238
return !fileId.empty() && file.getId() == fileId;
@@ -286,18 +268,6 @@ struct CppEmitter {
286268
/// Only emit file ops whos id matches this value.
287269
std::string fileId;
288270

289-
/// Controls whether the output should be a C++ class.
290-
/// If true, the generated C++ code will be encapsulated within a class,
291-
/// and functions from the input module will become its member functions.
292-
const bool emitClass;
293-
294-
/// The specified name for the generated C++ class
295-
const std::string className;
296-
297-
/// Name of the MLIR attribute to use as a field name within the generated
298-
/// class
299-
const std::string fieldNameAttribute;
300-
301271
/// Map from value to name of C++ variable that contain the name.
302272
ValueMapper valueMapper;
303273

@@ -1063,17 +1033,6 @@ static LogicalResult printFunctionArgs(CppEmitter &emitter,
10631033
}));
10641034
}
10651035

1066-
static LogicalResult printFields(CppEmitter &emitter, Operation *functionOp,
1067-
Region::BlockArgListType arguments) {
1068-
raw_indented_ostream &os = emitter.ostream();
1069-
1070-
return (interleaveWithNewLineWithError(
1071-
arguments, os, [&](BlockArgument arg) -> LogicalResult {
1072-
return emitter.emitVariableDeclaration(
1073-
functionOp->getLoc(), arg.getType(), emitter.getOrCreateName(arg));
1074-
}));
1075-
}
1076-
10771036
static LogicalResult printFunctionBody(CppEmitter &emitter,
10781037
Operation *functionOp,
10791038
Region::BlockListType &blocks) {
@@ -1178,45 +1137,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
11781137
return success();
11791138
}
11801139

1181-
static LogicalResult emitClassFields(CppEmitter &emitter,
1182-
emitc::FuncOp functionOp) {
1183-
raw_indented_ostream &os = emitter.ostream();
1184-
auto argAttrs = functionOp.getArgAttrs();
1185-
Operation *operation = functionOp.getOperation();
1186-
if (failed(printFields(emitter, operation, functionOp.getArguments())))
1187-
return failure();
1188-
os << ";\n";
1189-
1190-
std::map<std::string, Value> fields;
1191-
os << "\nstd::map<std::string, char*> _buffer_map {";
1192-
if (argAttrs) {
1193-
for (const auto [a, v] : zip(*argAttrs, functionOp.getArguments())) {
1194-
if (auto da = dyn_cast<mlir::DictionaryAttr>(a)) {
1195-
auto nv = da.getNamed(emitter.getfieldNameAttribute())->getValue();
1196-
auto name = cast<mlir::StringAttr>(cast<mlir::ArrayAttr>(nv)[0]).str();
1197-
auto Ins = fields.insert({name, v});
1198-
if (!Ins.second)
1199-
return failure();
1200-
os << " { \"" << name << "\"" << ", reinterpret_cast<char*>("
1201-
<< emitter.getOrCreateName(v) << ") }, ";
1202-
}
1203-
}
1204-
} else
1205-
return failure();
1206-
1207-
os << "};\n";
1208-
os << "char* getBufferForName(const std::string& name) const {\n";
1209-
os.indent();
1210-
os.indent();
1211-
os << "auto it = _buffer_map.find(name);\n";
1212-
os << "return (it == _buffer_map.end()) ? nullptr : it->second;\n";
1213-
os.unindent();
1214-
os.unindent();
1215-
os << "}\n\n";
1216-
1217-
return success();
1218-
}
1219-
12201140
static LogicalResult printOperation(CppEmitter &emitter,
12211141
emitc::FuncOp functionOp) {
12221142
// We need to declare variables at top if the function has multiple blocks.
@@ -1228,30 +1148,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
12281148

12291149
CppEmitter::Scope scope(emitter);
12301150
raw_indented_ostream &os = emitter.ostream();
1231-
Operation *operation = functionOp.getOperation();
1232-
if (emitter.shouldPrintClass()) {
1233-
if (functionOp.isExternal()) {
1234-
// TODO: Determine the best long-term strategy for external functions.
1235-
// Currently, we're skipping over this functionOp.
1236-
// We have considered using emitWarning() which would return
1237-
// InFlightDiagnostic which seems can be automatically converted to
1238-
// LogicalResult since this is done in emitAttributes where emitError is
1239-
// converted to LogicalResult. However, it requires that we pass in a
1240-
// location which at first glance we don't have in this scope. Open to
1241-
// further discussion on this.
1242-
os << "Warning: Cannot process external function '"
1243-
<< functionOp.getName() << "'. "
1244-
<< "This functionOp lacks a body so we will skip over it.";
1245-
return success();
1246-
}
1247-
os << "class " << emitter.getClassName() << " final {\n";
1248-
os << "public: \n";
1249-
os.indent();
1250-
1251-
if (failed(emitClassFields(emitter, functionOp)))
1252-
return failure();
1253-
}
1254-
12551151
if (functionOp.getSpecifiers()) {
12561152
for (Attribute specifier : functionOp.getSpecifiersAttr()) {
12571153
os << cast<StringAttr>(specifier).str() << " ";
@@ -1261,37 +1157,23 @@ static LogicalResult printOperation(CppEmitter &emitter,
12611157
if (failed(emitter.emitTypes(functionOp.getLoc(),
12621158
functionOp.getFunctionType().getResults())))
12631159
return failure();
1264-
// TODO: We may wanna consider having the name of the function be execute in
1265-
// the case that we want to emit a class instead of main. Leaving as is for
1266-
// now to make the change smaller.
12671160
os << " " << functionOp.getName();
12681161

12691162
os << "(";
1270-
1271-
if (!emitter.shouldPrintClass()) {
1272-
if (functionOp.isExternal()) {
1273-
if (failed(printFunctionArgs(emitter, operation,
1274-
functionOp.getArgumentTypes())))
1275-
return failure();
1276-
os << ");";
1277-
return success();
1278-
}
1279-
if (failed(
1280-
printFunctionArgs(emitter, operation, functionOp.getArguments())))
1163+
Operation *operation = functionOp.getOperation();
1164+
if (functionOp.isExternal()) {
1165+
if (failed(printFunctionArgs(emitter, operation,
1166+
functionOp.getArgumentTypes())))
12811167
return failure();
1168+
os << ");";
1169+
return success();
12821170
}
1171+
if (failed(printFunctionArgs(emitter, operation, functionOp.getArguments())))
1172+
return failure();
12831173
os << ") {\n";
1284-
12851174
if (failed(printFunctionBody(emitter, operation, functionOp.getBlocks())))
12861175
return failure();
1287-
1288-
if (emitter.shouldPrintClass()) {
1289-
os << "}\n";
1290-
os.unindent();
1291-
os << "};\n";
1292-
} else {
1293-
os << "}\n";
1294-
}
1176+
os << "}\n";
12951177

12961178
return success();
12971179
}
@@ -1328,11 +1210,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
13281210
}
13291211

13301212
CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
1331-
StringRef fileId, bool emitClass, StringRef className,
1332-
StringRef fieldNameAttribute)
1213+
StringRef fileId)
13331214
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1334-
fileId(fileId.str()), emitClass(emitClass), className(className.str()),
1335-
fieldNameAttribute(fieldNameAttribute.str()) {
1215+
fileId(fileId.str()) {
13361216
valueInScopeCount.push(0);
13371217
labelInScopeCount.push(0);
13381218
}
@@ -1915,10 +1795,7 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
19151795

19161796
LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
19171797
bool declareVariablesAtTop,
1918-
StringRef fileId, bool emitClass,
1919-
StringRef className,
1920-
StringRef fieldNameAttribute) {
1921-
CppEmitter emitter(os, declareVariablesAtTop, fileId, emitClass, className,
1922-
fieldNameAttribute);
1798+
StringRef fileId) {
1799+
CppEmitter emitter(os, declareVariablesAtTop, fileId);
19231800
return emitter.emitOperation(*op, /*trailingSemicolon=*/false);
1924-
}
1801+
}

mlir/test/mlir-translate/emit-class-neg-external.mlir

Lines changed: 0 additions & 8 deletions
This file was deleted.

mlir/test/mlir-translate/emit-class-neg-noArgAttrs.mlir

Lines changed: 0 additions & 15 deletions
This file was deleted.

mlir/test/mlir-translate/emit-class.mlir

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)