Skip to content

Commit c26e36d

Browse files
committed
fixed multidialect bug
1 parent e980d88 commit c26e36d

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- IRDLToCpp.cpp - Converts IRDL definitions to C++ -------------------===//
22
//
3-
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// Part of the LLVM Project, under the A0ache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
@@ -31,14 +31,14 @@ constexpr char definitionMacroFlag[] = "GEN_DIALECT_DEF";
3131
namespace {
3232

3333
struct DialectStrings {
34-
StringRef dialectName;
35-
StringRef dialectCppName;
36-
StringRef dialectCppShortName;
37-
StringRef dialectBaseTypeName;
38-
39-
StringRef namespaceOpen;
40-
StringRef namespaceClose;
41-
StringRef namespacePath;
34+
std::string dialectName;
35+
std::string dialectCppName;
36+
std::string dialectCppShortName;
37+
std::string dialectBaseTypeName;
38+
39+
std::string namespaceOpen;
40+
std::string namespaceClose;
41+
std::string namespacePath;
4242
};
4343

4444
struct TypeStrings {
@@ -248,8 +248,6 @@ static LogicalResult generateInclude(irdl::DialectOp dialect,
248248
static const auto typeHeaderDeclTemplate = irdl::detail::Template(
249249
#include "Templates/TypeHeaderDecl.txt"
250250
);
251-
output << "#ifdef " << declarationMacroFlag << "\n#undef "
252-
<< declarationMacroFlag << "\n";
253251

254252
irdl::detail::dictionary dict;
255253
fillDict(dict, dialectStrings);
@@ -285,8 +283,6 @@ static LogicalResult generateInclude(irdl::DialectOp dialect,
285283
return failure();
286284
}
287285

288-
output << "#endif // " << declarationMacroFlag << "\n";
289-
290286
return success();
291287
}
292288

@@ -375,9 +371,6 @@ static LogicalResult generateLib(irdl::DialectOp dialect, raw_ostream &output,
375371
irdl::detail::dictionary dict;
376372
fillDict(dict, dialectStrings);
377373

378-
output << "#ifdef " << definitionMacroFlag << "\n#undef "
379-
<< definitionMacroFlag << "\n";
380-
381374
typeHeaderDefTemplate.render(output, dict);
382375

383376
SmallVector<std::string> typeNames;
@@ -465,7 +458,6 @@ static LogicalResult generateLib(irdl::DialectOp dialect, raw_ostream &output,
465458
output << perOpDefinitions;
466459
dialectDefTemplate.render(output, dict);
467460

468-
output << "#endif // " << definitionMacroFlag << "\n";
469461
return success();
470462
}
471463

@@ -517,14 +509,13 @@ irdl::translateIRDLDialectToCpp(llvm::ArrayRef<irdl::DialectOp> dialects,
517509
#include "Templates/TypeDef.txt"
518510
);
519511

520-
llvm::LogicalResult result = success();
512+
llvm::SmallMapVector<DialectOp, DialectStrings, 2> dialectStringTable;
521513

522514
for (auto dialect : dialects) {
523-
StringRef dialectName = dialect.getSymName();
524-
525-
if (failed(verifySupported(dialect))) {
515+
if (failed(verifySupported(dialect)))
526516
return failure();
527-
}
517+
518+
StringRef dialectName = dialect.getSymName();
528519

529520
SmallVector<SmallString<8>> namespaceAbsolutePath{{"mlir"}, dialectName};
530521
std::string namespaceOpen;
@@ -553,18 +544,34 @@ irdl::translateIRDLDialectToCpp(llvm::ArrayRef<irdl::DialectOp> dialects,
553544
dialectStrings.namespaceClose = namespaceClose;
554545
dialectStrings.namespacePath = namespacePath;
555546

556-
output << headerTemplateText;
547+
dialectStringTable[dialect] = std::move(dialectStrings);
548+
}
549+
550+
// generate the actual header
551+
output << headerTemplateText;
557552

558-
if (failed(generateInclude(dialect, output, dialectStrings))) {
553+
output << llvm::formatv("#ifdef {0}\n#undef {0}\n", declarationMacroFlag);
554+
for (auto dialect : dialects) {
555+
556+
auto &dialectStrings = dialectStringTable[dialect];
557+
auto &dialectName = dialectStrings.dialectName;
558+
559+
if (failed(generateInclude(dialect, output, dialectStrings)))
559560
return dialect->emitError("Error in Dialect " + dialectName +
560561
" while generating headers");
561-
}
562+
}
563+
output << llvm::formatv("#endif // #ifdef {}\n", declarationMacroFlag);
564+
565+
output << llvm::formatv("#ifdef {0}\n#undef {0}\n ", definitionMacroFlag);
566+
for (auto &dialect : dialects) {
567+
auto &dialectStrings = dialectStringTable[dialect];
568+
auto &dialectName = dialectStrings.dialectName;
562569

563-
if (failed(generateLib(dialect, output, dialectStrings))) {
570+
if (failed(generateLib(dialect, output, dialectStrings)))
564571
return dialect->emitError("Error in Dialect " + dialectName +
565572
" while generating library");
566-
}
567573
}
574+
output << llvm::formatv("#endif // #ifdef {}\n", definitionMacroFlag);
568575

569-
return result;
576+
return success();
570577
}

0 commit comments

Comments
 (0)