As discussed in https://reviews.llvm.org/D140886, emitting a warning if the old API is used may be beneficial to encourage migration to the new fold API.
This reuse the existing `Deprecated` infrastructure within TableGen, and simply marks the `def` for `kEmitRawAttributesFolder` causing a use of it in a record (even if set within a base class) to emit a warning.
Error message as printed in the terminal:
```
Included from C:/llvm-project/mlir/python/mlir/dialects/TensorOps.td:13:
Included from C:/llvm-project/mlir/include\mlir/Dialect/Tensor/IR/TensorOps.td:12:
C:/llvm-project/mlir/include\mlir/Dialect/Tensor/IR/TensorBase.td:14:5: warning: Using deprecated def `kEmitRawAttributesFolder`
def Tensor_Dialect : Dialect {
^
note: 'useFoldAPI' of 'kEmitRawAttributesFolder' (default) has been deprecated and is pending removal. Please switch to 'kEmitFoldAdaptorFolder'. See https://discourse.llvm.org/t/psa-new-improved-fold-method-signature-has-landed-please-update-your-downstream-projects/67618
```
Differential Revision: https://reviews.llvm.org/D141604
#ifndef DIALECTBASE_TD
#define DIALECTBASE_TD
+// Helper for marking deprecated classes or defs. To mark a def as deprecated,
+// mix in the `Deprecate` class with a reason.
+class Deprecated<string reason> {
+ string odsDeprecated = reason;
+}
+
//===----------------------------------------------------------------------===//
// Dialect definitions
//===----------------------------------------------------------------------===//
+class EmitFolderBase;
// Generate 'fold' method with 'ArrayRef<Attribute>' parameter.
// New code should prefer using 'kEmitFoldAdaptorFolder' and
// consider 'kEmitRawAttributesFolder' deprecated and to be
// removed in the future.
-defvar kEmitRawAttributesFolder = 0;
+def kEmitRawAttributesFolder : EmitFolderBase, Deprecated<
+ "'useFoldAPI' of 'kEmitRawAttributesFolder' (default) has been deprecated "
+ # "and is pending removal. Please switch to 'kEmitFoldAdaptorFolder'. See "
+ # "https://discourse.llvm.org/t/psa-new-improved-fold-method-signature-has-landed-please-update-your-downstream-projects/67618"
+> {}
// Generate 'fold' method with 'FoldAdaptor' parameter.
-defvar kEmitFoldAdaptorFolder = 1;
+def kEmitFoldAdaptorFolder : EmitFolderBase {}
class Dialect {
// The name of the dialect.
bit isExtensible = 0;
// Fold API to use for operations in this dialect.
- int useFoldAPI = kEmitRawAttributesFolder;
+ EmitFolderBase useFoldAPI = kEmitRawAttributesFolder;
}
#endif // DIALECTBASE_TD
string result = r;
}
-// Helper for marking deprecated classes or defs. To mark a def as deprecated,
-// mix in the `Deprecate` class with a reason.
-class Deprecated<string reason> {
- string odsDeprecated = reason;
-}
-
//===----------------------------------------------------------------------===//
// Predicate definitions
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
#include "mlir/TableGen/Dialect.h"
+#include "llvm/ADT/StringSwitch.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
}
Dialect::FolderAPI Dialect::getFolderAPI() const {
- int64_t value = def->getValueAsInt("useFoldAPI");
- if (value < static_cast<int64_t>(FolderAPI::RawAttributes) ||
- value > static_cast<int64_t>(FolderAPI::FolderAdaptor))
+ llvm::Record *value = def->getValueAsDef("useFoldAPI");
+ auto converted =
+ llvm::StringSwitch<std::optional<Dialect::FolderAPI>>(value->getName())
+ .Case("kEmitRawAttributesFolder", FolderAPI::RawAttributes)
+ .Case("kEmitFoldAdaptorFolder", FolderAPI::FolderAdaptor)
+ .Default(std::nullopt);
+
+ if (!converted)
llvm::PrintFatalError(def->getLoc(),
"Invalid value for dialect field `useFoldAPI`");
- return static_cast<FolderAPI>(value);
+ return *converted;
}
bool Dialect::operator==(const Dialect &other) const {
// Skip anonymous defs.
if (jt.second->isAnonymous())
continue;
- // Skip all outside main file to avoid flagging redundantly.
- unsigned buf =
- SrcMgr.FindBufferContainingLoc(jt.second->getLoc().front());
- if (buf != SrcMgr.getMainFileID())
- continue;
if (findUse(*jt.second, it.second->getDefInit(), hasUse)) {
PrintWarning(jt.second->getLoc(),
include "mlir/IR/OpBase.td"
+def Bad : EmitFolderBase;
+
def Test_Dialect : Dialect {
let name = "test";
let cppNamespace = "NS";
- let useFoldAPI = 3;
+ let useFoldAPI = Bad;
}
def InvalidValue_Op : Op<Test_Dialect, "invalid_op"> {