Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions llvm/include/llvm/TableGen/CodeGenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
#ifndef LLVM_TABLEGEN_CODEGENHELPERS_H
#define LLVM_TABLEGEN_CODEGENHELPERS_H

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/raw_ostream.h"
#include <string>

namespace llvm {
// Simple RAII helper for emitting ifdef-undef-endif scope.
class IfDefEmitter {
public:
IfDefEmitter(raw_ostream &OS, StringRef Name) : Name(Name.str()), OS(OS) {
OS << "#ifdef " << Name << "\n"
<< "#undef " << Name << "\n\n";
IfDefEmitter(raw_ostream &OS, const Twine &Name) : Name(Name.str()), OS(OS) {
OS << "#ifdef " << this->Name << "\n"
<< "#undef " << this->Name << "\n\n";
}
~IfDefEmitter() { OS << "\n#endif // " << Name << "\n\n"; }

Expand All @@ -37,10 +37,10 @@ class IfDefEmitter {
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
class IncludeGuardEmitter {
public:
IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
IncludeGuardEmitter(raw_ostream &OS, const Twine &Name)
: Name(Name.str()), OS(OS) {
OS << "#ifndef " << Name << "\n"
<< "#define " << Name << "\n\n";
OS << "#ifndef " << this->Name << "\n"
<< "#define " << this->Name << "\n\n";
}
~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }

Expand All @@ -54,10 +54,10 @@ class IncludeGuardEmitter {
// namespace scope.
class NamespaceEmitter {
public:
NamespaceEmitter(raw_ostream &OS, StringRef NameUntrimmed)
: Name(trim(NameUntrimmed).str()), OS(OS) {
if (!Name.empty())
OS << "namespace " << Name << " {\n";
NamespaceEmitter(raw_ostream &OS, const Twine &Name)
: Name(trim(Name)), OS(OS) {
if (!this->Name.empty())
OS << "namespace " << this->Name << " {\n";
}

~NamespaceEmitter() { close(); }
Expand All @@ -77,9 +77,11 @@ class NamespaceEmitter {
// }
//
// and cannot use "namespace ::mlir::toy".
static StringRef trim(StringRef Name) {
Name.consume_front("::");
return Name;
static std::string trim(const Twine &Name) {
SmallString<64> Storage;
StringRef StrRef = Name.toStringRef(Storage);
StrRef.consume_front("::");
return StrRef.str();
}
std::string Name;
raw_ostream &OS;
Expand Down
14 changes: 5 additions & 9 deletions llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
return;

StringRef Lang = DirLang.getName();
IncludeGuardEmitter IncGuard(OS, (Twine("LLVM_") + Lang + "_INC").str());
IncludeGuardEmitter IncGuard(OS, Twine("LLVM_") + Lang + "_INC");

OS << "#include \"llvm/ADT/ArrayRef.h\"\n";

Expand Down Expand Up @@ -941,10 +941,8 @@ static void generateClauseSet(ArrayRef<const Record *> VerClauses,
static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
Frontend FE, raw_ostream &OS) {

std::string IfDefName{"GEN_"};
IfDefName += getFESpelling(FE).upper();
IfDefName += "_DIRECTIVE_CLAUSE_SETS";
IfDefEmitter Scope(OS, IfDefName);
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
"_DIRECTIVE_CLAUSE_SETS");

StringRef Namespace =
getFESpelling(FE == Frontend::Flang ? Frontend::LLVM : FE);
Expand Down Expand Up @@ -985,10 +983,8 @@ static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
// allowances (allowed, allowed once, allowed exclusive and required).
static void generateDirectiveClauseMap(const DirectiveLanguage &DirLang,
Frontend FE, raw_ostream &OS) {
std::string IfDefName{"GEN_"};
IfDefName += getFESpelling(FE).upper();
IfDefName += "_DIRECTIVE_CLAUSE_MAP";
IfDefEmitter Scope(OS, IfDefName);
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
"_DIRECTIVE_CLAUSE_MAP");

OS << "{\n";

Expand Down
7 changes: 2 additions & 5 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4938,10 +4938,7 @@ static void emitOpDefShard(const RecordKeeper &records,
ArrayRef<const Record *> defs,
const Dialect &dialect, unsigned shardIndex,
unsigned shardCount, raw_ostream &os) {
std::string shardGuard = "GET_OP_DEFS_";
std::string indexStr = std::to_string(shardIndex);
shardGuard += indexStr;
IfDefEmitter scope(os, shardGuard);
IfDefEmitter scope(os, "GET_OP_DEFS_" + Twine(shardIndex));

// Emit the op registration hook in the first shard.
const char *const opRegistrationHook =
Expand All @@ -4968,7 +4965,7 @@ static void emitOpDefShard(const RecordKeeper &records,
os << "}\n";

// Generate the per-shard op definitions.
emitOpClassDefs(records, defs, os, indexStr);
emitOpClassDefs(records, defs, os, std::to_string(shardIndex));
}

/// Emit op definitions for all op records.
Expand Down
3 changes: 1 addition & 2 deletions mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
}

void InterfaceGenerator::emitInterfaceTraitDecl(const Interface &interface) {
auto cppNamespace = (interface.getCppNamespace() + "::detail").str();
llvm::NamespaceEmitter ns(os, cppNamespace);
llvm::NamespaceEmitter ns(os, interface.getCppNamespace() + "::detail");

StringRef interfaceName = interface.getName();
auto interfaceTraitsName = (interfaceName + "InterfaceTraits").str();
Expand Down