diff --git a/mlir/include/mlir/Support/TypeID.h b/mlir/include/mlir/Support/TypeID.h --- a/mlir/include/mlir/Support/TypeID.h +++ b/mlir/include/mlir/Support/TypeID.h @@ -135,6 +135,7 @@ /// Enable hashing TypeID. friend ::llvm::hash_code hash_value(TypeID id); + int hackTypeID; private: TypeID(const Storage *storage) : storage(storage) {} @@ -147,7 +148,8 @@ /// Enable hashing TypeID. inline ::llvm::hash_code hash_value(TypeID id) { - return DenseMapInfo::getHashValue(id.storage); + // return DenseMapInfo::getHashValue(id.storage); + return id.hackTypeID; } //===----------------------------------------------------------------------===// @@ -246,13 +248,17 @@ // circumstances a hard-to-catch runtime bug when a TypeID is hidden in two // different shared libraries and instances of the same class only gets the same // TypeID inside a given DSO. -#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME) \ +#define MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(CLASS_NAME, HACKTYPEID) \ namespace mlir { \ namespace detail { \ template <> \ class TypeIDResolver { \ public: \ - static TypeID resolveTypeID() { return id; } \ + static TypeID resolveTypeID() { \ + id.hackTypeID = hackTypeID; \ + return id; \ + } \ + static const int hackTypeID; \ \ private: \ static SelfOwningTypeID id; \ @@ -260,20 +266,29 @@ } /* namespace detail */ \ } /* namespace mlir */ -#define MLIR_DEFINE_EXPLICIT_TYPE_ID(CLASS_NAME) \ +#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME) \ + MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(CLASS_NAME, 1) + +#define MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(CLASS_NAME, HACKTYPEID) \ namespace mlir { \ namespace detail { \ SelfOwningTypeID TypeIDResolver::id = {}; \ + const int TypeIDResolver::hackTypeID = HACKTYPEID; \ } /* namespace detail */ \ } /* namespace mlir */ +#define MLIR_DEFINE_EXPLICIT_TYPE_ID(CLASS_NAME) \ + MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(CLASS_NAME, 1) + // Declare/define an explicit, **internal**, specialization of TypeID for the // given class. This is useful for providing an explicit specialization of // TypeID for a class that is known to be internal to a specific library. It // should be placed within a public section of the declaration of the class. -#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME) \ +#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID_2_ARGS(CLASS_NAME, \ + HACKTYPEID) \ static ::mlir::TypeID resolveTypeID() { \ static ::mlir::SelfOwningTypeID id; \ + id.hackTypeID = HACKTYPEID; \ return id; \ } \ static_assert( \ @@ -282,6 +297,9 @@ "`MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID` must be placed in a " \ "public section of `" #CLASS_NAME "`"); +#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME) \ + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID_2_ARGS(CLASS_NAME, 1) + //===----------------------------------------------------------------------===// // TypeIDAllocator //===----------------------------------------------------------------------===// @@ -321,7 +339,12 @@ operator TypeID() const { return getTypeID(); } /// Return the TypeID owned by this object. - TypeID getTypeID() const { return TypeID::getFromOpaquePointer(this); } + TypeID getTypeID() const { + auto t = TypeID::getFromOpaquePointer(this); + t.hackTypeID = hackTypeID; + return t; + } + int hackTypeID; }; } // namespace mlir @@ -331,7 +354,7 @@ //===----------------------------------------------------------------------===// /// Explicitly register a set of "builtin" types. -MLIR_DECLARE_EXPLICIT_TYPE_ID(void) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(void, 5000) namespace llvm { template <> diff --git a/mlir/include/mlir/TableGen/CodeGenHelpers.h b/mlir/include/mlir/TableGen/CodeGenHelpers.h --- a/mlir/include/mlir/TableGen/CodeGenHelpers.h +++ b/mlir/include/mlir/TableGen/CodeGenHelpers.h @@ -19,6 +19,7 @@ #include "llvm/ADT/MapVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" +#include namespace llvm { class RecordKeeper; @@ -246,4 +247,9 @@ } // namespace tblgen } // namespace mlir +int nextCounterVal(); +void lock(); +void unlock(); +int findLast(const std::string &filename); + #endif // MLIR_TABLEGEN_CODEGENHELPERS_H diff --git a/mlir/include/mlir/Tools/PDLL/AST/Types.h b/mlir/include/mlir/Tools/PDLL/AST/Types.h --- a/mlir/include/mlir/Tools/PDLL/AST/Types.h +++ b/mlir/include/mlir/Tools/PDLL/AST/Types.h @@ -294,14 +294,14 @@ } // namespace pdll } // namespace mlir -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::AttributeTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::ConstraintTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::OperationTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::RangeTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::RewriteTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::TupleTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::TypeTypeStorage) -MLIR_DECLARE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::ValueTypeStorage) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::AttributeTypeStorage, 5001) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::ConstraintTypeStorage, 5002) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::OperationTypeStorage, 5003) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::RangeTypeStorage, 5004) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::RewriteTypeStorage, 5005) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::TupleTypeStorage, 5006) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::TypeTypeStorage, 5007) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(mlir::pdll::ast::detail::ValueTypeStorage, 5008) namespace llvm { template <> diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp --- a/mlir/lib/AsmParser/Parser.cpp +++ b/mlir/lib/AsmParser/Parser.cpp @@ -718,8 +718,8 @@ }; } // namespace -MLIR_DECLARE_EXPLICIT_TYPE_ID(OperationParser::DeferredLocInfo *) -MLIR_DEFINE_EXPLICIT_TYPE_ID(OperationParser::DeferredLocInfo *) +MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(OperationParser::DeferredLocInfo *, 5009) +MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(OperationParser::DeferredLocInfo *, 5010) OperationParser::OperationParser(ParserState &state, ModuleOp topLevelOp) : Parser(state), opBuilder(topLevelOp.getRegion()), topLevelOp(topLevelOp) { diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp --- a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp +++ b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp @@ -32,7 +32,7 @@ } // namespace bufferization } // namespace mlir -MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::bufferization::AnalysisState) +MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(mlir::bufferization::AnalysisState, 5011) #define DEBUG_TYPE "bufferizable-op-interface" #define DBGS() (llvm::dbgs() << '[' << DEBUG_TYPE << "] ") diff --git a/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp b/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp --- a/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp @@ -57,7 +57,7 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SetVector.h" -MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::bufferization::OneShotAnalysisState) +MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(mlir::bufferization::OneShotAnalysisState, 5012) // Run mlir-opt with `-debug-only="one-shot-analysis"` for detailed debug // output. diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp --- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp +++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp @@ -629,10 +629,13 @@ // Emit the TypeID explicit specializations to have a single definition for // each of these. for (const AttrOrTypeDef &def : defs) - if (!def.getDialect().getCppNamespace().empty()) - os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" + if (!def.getDialect().getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal" << n << "\n"; + os << "MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(" << def.getDialect().getCppNamespace() << "::" << def.getCppClassName() - << ")\n"; + << ", " << n << ")\n"; + } return false; } @@ -842,10 +845,13 @@ gen.emitDef(os); } // Emit the TypeID explicit specializations to have a single symbol def. - if (!def.getDialect().getCppNamespace().empty()) - os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" + if (!def.getDialect().getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal" << n << "\n"; + os << "MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(" << def.getDialect().getCppNamespace() << "::" << def.getCppClassName() - << ")\n"; + << ", " << n << ")\n"; + } } Dialect firstDialect = defs.front().getDialect(); @@ -928,3 +934,84 @@ TypeDefGenerator generator(records, os); return generator.emitDecls(typeDialect); }); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int fd; +std::vector counterVals(100000); + +int findLast(const std::string &filename) { + int last; + if (std::ifstream file{filename}) { + if (file >> last) { + int temp = 0; + while (file >> temp) + last = temp; + return last; + } + // if at least two numbers were read from the file + return -1; + } + // return -1 if the file doesn't open or the file doesn't read properly + return -1; +} + +int nextCounterVal() { + auto next = counterVals.back() + 1; + std::cout << "next " << next << "\n"; + counterVals.emplace_back(next); + return next; +} + +void lock() { + char path[] = "/tmp/hacktypeid.txt"; + fd = open(path, O_RDONLY | O_CREAT); + if (fd != -1) { + if (flock(fd, LOCK_EX) == 0) { + std::cout << "The file was locked" << std::endl; + } else { + perror("The file was not locked."); + exit(EXIT_FAILURE); + } + + int lastNumber = findLast(path); + std::cout << "last number: " << lastNumber << "\n"; + counterVals.emplace_back(lastNumber); + } else { + perror("Error opening file during lock."); + exit(EXIT_FAILURE); + } +} + +void unlock() { + std::ofstream ofile; + ofile.open("/tmp/hacktypeid.txt", + std::ios::app); // app is append which means it + // will put the text at the end + counterVals.shrink_to_fit(); + std::cout << "number new counter vals " << counterVals.size() << "\n"; + for (int i = 1; i < counterVals.size(); ++i) { + auto c = counterVals[i]; + if (c > 0) + ofile << c << "\n"; + } + + ofile.close(); + if (flock(fd, LOCK_UN) == 0) { + std::cout << "The file was unlocked." << std::endl; + } else { + perror("The file was not unlocked."); + exit(EXIT_FAILURE); + } + close(fd); +} diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp --- a/mlir/tools/mlir-tblgen/DialectGen.cpp +++ b/mlir/tools/mlir-tblgen/DialectGen.cpp @@ -222,9 +222,12 @@ // End the dialect decl. os << "};\n"; } - if (!dialect.getCppNamespace().empty()) - os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace() - << "::" << dialect.getCppClassName() << ")\n"; + if (!dialect.getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal " << n << "\n"; + os << "MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(" << dialect.getCppNamespace() + << "::" << dialect.getCppClassName() << ", " << n << ")\n"; + } } static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper, @@ -273,9 +276,12 @@ std::string cppClassName = dialect.getCppClassName(); // Emit the TypeID explicit specializations to have a single symbol def. - if (!dialect.getCppNamespace().empty()) - os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace() - << "::" << cppClassName << ")\n"; + if (!dialect.getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal" << n << "\n"; + os << "MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(" << dialect.getCppNamespace() + << "::" << cppClassName << ", " << n << ")\n"; + } // Emit all nested namespaces. NamespaceEmitter nsEmitter(os, dialect); diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -3796,9 +3796,12 @@ OpEmitter::emitDecl(op, os, staticVerifierEmitter); } // Emit the TypeID explicit specialization to have a single definition. - if (!op.getCppNamespace().empty()) - os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << op.getCppNamespace() - << "::" << op.getCppClassName() << ")\n\n"; + if (!op.getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal" << n << "\n"; + os << "MLIR_DECLARE_EXPLICIT_TYPE_ID_2_ARGS(" << op.getCppNamespace() + << "::" << op.getCppClassName() << ", " << n << ")\n\n"; + } } else { { NamespaceEmitter emitter(os, op.getCppNamespace()); @@ -3807,9 +3810,12 @@ OpEmitter::emitDef(op, os, staticVerifierEmitter); } // Emit the TypeID explicit specialization to have a single definition. - if (!op.getCppNamespace().empty()) - os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << op.getCppNamespace() - << "::" << op.getCppClassName() << ")\n\n"; + if (!op.getCppNamespace().empty()) { + auto n = nextCounterVal(); + std::cout << "nextCounterVal" << n << "\n"; + os << "MLIR_DEFINE_EXPLICIT_TYPE_ID_2_ARGS(" << op.getCppNamespace() + << "::" << op.getCppClassName() << ", " << n << ")\n\n"; + } } } } diff --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/tools/mlir-tblgen/PassGen.cpp --- a/mlir/tools/mlir-tblgen/PassGen.cpp +++ b/mlir/tools/mlir-tblgen/PassGen.cpp @@ -18,6 +18,7 @@ #include "llvm/Support/FormatVariadic.h" #include "llvm/TableGen/Error.h" #include "llvm/TableGen/Record.h" +#include "mlir/TableGen/CodeGenHelpers.h" using namespace mlir; using namespace mlir::tblgen; @@ -174,6 +175,7 @@ /// {1}: The base class for the pass. /// {2): The command line argument for the pass. /// {3}: The dependent dialects registration. +/// {4}: HackTypeID const char *const baseClassBegin = R"( template class {0}Base : public {1} { @@ -215,7 +217,7 @@ /// Explicitly declare the TypeID for this class. We declare an explicit private /// instantiation because Pass classes should only be visible by the current /// library. - MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID({0}Base) + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID_2_ARGS({0}Base, {5}) )"; @@ -312,10 +314,16 @@ dependentDialect); } + auto n = nextCounterVal(); + os << "namespace impl {\n"; - os << llvm::formatv(baseClassBegin, passName, pass.getBaseClass(), - pass.getArgument(), pass.getSummary(), - dependentDialectRegistrations); + os << llvm::formatv(baseClassBegin, + passName, + pass.getBaseClass(), + pass.getArgument(), + pass.getSummary(), + dependentDialectRegistrations, + n); if (ArrayRef options = pass.getOptions(); !options.empty()) { os.indent(2) << llvm::formatv( @@ -410,7 +418,7 @@ /// Explicitly declare the TypeID for this class. We declare an explicit private /// instantiation because Pass classes should only be visible by the current /// library. - MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID({0}Base) + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID_2_ARGS({0}Base, {5}) protected: )"; @@ -426,9 +434,15 @@ dialectsOs << llvm::formatv(dialectRegistrationTemplate, dependentDialect); } - os << llvm::formatv(oldPassDeclBegin, defName, pass.getBaseClass(), - pass.getArgument(), pass.getSummary(), - dependentDialectRegistrations); + auto n = nextCounterVal(); + os << llvm::formatv(oldPassDeclBegin, + defName, + pass.getBaseClass(), + pass.getArgument(), + pass.getSummary(), + dependentDialectRegistrations, + n + ); emitPassOptionDecls(pass, os); emitPassStatisticDecls(pass, os); os << "};\n"; diff --git a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp --- a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp +++ b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#include "mlir/TableGen/CodeGenHelpers.h" #include "mlir/TableGen/GenInfo.h" #include "mlir/Tools/mlir-tblgen/MlirTblgenMain.h" #include "llvm/TableGen/Record.h" @@ -23,5 +24,9 @@ os << records; return false; }); - -int main(int argc, char **argv) { return MlirTblgenMain(argc, argv); } +int main(int argc, char **argv) { + lock(); + auto t = MlirTblgenMain(argc, argv); + unlock(); + return t; +}