[clang] b4984ca - [mlir][ODS] Include headers required by generated bytecode readers and writers (#241226)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 01:15:22 PST 2026
Author: Markus Böck
Date: 2026-11-12T09:15:14Z
New Revision: b4984ca753763a36a92610441393e9c557b9206a
URL: https://github.com/llvm/llvm-project/commit/b4984ca753763a36a92610441393e9c557b9206a
DIFF: https://github.com/llvm/llvm-project/commit/b4984ca753763a36a92610441393e9c557b9206a.diff
LOG: [mlir][ODS] Include headers required by generated bytecode readers and writers (#241226)
The generated bytecode readers and writers use `DialectBytecodeReader`,
`DialectBytecodeWriter` and `TypeSwitch` but did not include the headers
declaring them. They therefore relied on the surrounding source file to
include these headers first.
The generated readers and writers now include `BytecodeImplementation.h`
and `TypeSwitch.h`.
As these includes must not be expanded within a namespace, all users now
include the generated file at global scope rather than within their
anonymous namespace.
Similar to #236155 and #238735 but for the bytecode generator.
Co-authored-by: Claude <noreply at anthropic.com>
Added:
mlir/test/mlir-tblgen/cpp-includes-bytecode.td
Modified:
clang/lib/CIR/Dialect/IR/CIRDialectBytecode.cpp
mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp
mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
mlir/lib/IR/BuiltinDialectBytecode.cpp
mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
Removed:
################################################################################
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialectBytecode.cpp b/clang/lib/CIR/Dialect/IR/CIRDialectBytecode.cpp
index 51555b6ecbad00..1f469e4b2e4197 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialectBytecode.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialectBytecode.cpp
@@ -14,7 +14,6 @@
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/TypeSwitch.h"
#include <limits>
#include <type_traits>
@@ -163,12 +162,16 @@ static LogicalResult readCheckedInt(DialectBytecodeReader &reader,
return success();
}
+} // namespace
+
//===--------------------------------------------------------------------===//
// Tablegen generated bytecode functions
//===--------------------------------------------------------------------===//
#include "clang/CIR/Dialect/IR/CIRDialectBytecode.cpp.inc"
+namespace {
+
//===--------------------------------------------------------------------===//
// CIRDialectBytecodeInterface
//===--------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
index 597690e7c77a2c..8245827067a0dc 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
@@ -14,7 +14,6 @@
#include "mlir/IR/Diagnostics.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/TypeSwitch.h"
#include <type_traits>
using namespace mlir;
@@ -104,6 +103,7 @@ static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
storage = std::nullopt;
return success();
}
+} // namespace
//===--------------------------------------------------------------------===//
// Tablegen generated bytecode functions
@@ -111,6 +111,7 @@ static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
#include "mlir/Dialect/LLVMIR/LLVMDialectBytecode.cpp.inc"
+namespace {
//===--------------------------------------------------------------------===//
// LLVMDialectBytecodeInterface
//===--------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp b/mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp
index 870f13d4fb8c2c..707be2ad4731b3 100644
--- a/mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp
+++ b/mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp
@@ -13,7 +13,6 @@
#include "mlir/Dialect/Quant/IR/QuantTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "llvm/ADT/APFloat.h"
-#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
using namespace mlir::quant;
@@ -52,9 +51,11 @@ static void writeOptionalSignedVarInt(DialectBytecodeWriter &writer,
if (val.has_value())
writer.writeSignedVarInt(*val);
}
+} // namespace
#include "mlir/Dialect/Quant/IR/QuantDialectBytecode.cpp.inc"
+namespace {
/// This class implements the bytecode interface for the Quant dialect.
struct QuantDialectBytecodeInterface : public BytecodeDialectInterface {
QuantDialectBytecodeInterface(Dialect *dialect)
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 079c65c8fafcf5..5ab22c375eb7f5 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -28,7 +28,6 @@
#include "mlir/Transforms/InliningUtils.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVectorExtras.h"
-#include "llvm/ADT/TypeSwitch.h"
#include <numeric>
#include <type_traits>
@@ -43,12 +42,11 @@ using namespace mlir::tosa;
// Tosa dialect interface includes.
//===----------------------------------------------------------------------===//
+#include "mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc"
#include "mlir/Dialect/Tosa/IR/TosaEnums.cpp.inc"
#include "mlir/Dialect/Tosa/IR/TosaInterfaces.cpp.inc"
namespace {
-#include "mlir/Dialect/Tosa/IR/TosaDialectBytecode.cpp.inc"
-
//===----------------------------------------------------------------------===//
// Dialect Function Inliner Interface.
//===----------------------------------------------------------------------===//
@@ -118,7 +116,6 @@ struct TosaDialectBytecodeInterface : public BytecodeDialectInterface {
return success();
}
};
-
} // namespace
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/IR/BuiltinDialectBytecode.cpp b/mlir/lib/IR/BuiltinDialectBytecode.cpp
index 1390769dd8e6ff..007918589df290 100644
--- a/mlir/lib/IR/BuiltinDialectBytecode.cpp
+++ b/mlir/lib/IR/BuiltinDialectBytecode.cpp
@@ -20,7 +20,6 @@
#include "mlir/IR/IntegerSet.h"
#include "mlir/IR/Location.h"
#include "mlir/Support/LLVM.h"
-#include "llvm/ADT/TypeSwitch.h"
#include <cstdint>
using namespace mlir;
@@ -544,9 +543,11 @@ static void writeDenseTypedElementsAttr(DialectBytecodeWriter &writer,
writer.writeOwnedBlob(attr.getRawData());
}
+} // namespace
#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
+namespace {
/// This class implements the bytecode interface for the builtin dialect.
struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
BuiltinDialectBytecodeInterface(Dialect *dialect)
diff --git a/mlir/test/mlir-tblgen/cpp-includes-bytecode.td b/mlir/test/mlir-tblgen/cpp-includes-bytecode.td
new file mode 100644
index 00000000000000..79e08d5352f818
--- /dev/null
+++ b/mlir/test/mlir-tblgen/cpp-includes-bytecode.td
@@ -0,0 +1,15 @@
+// RUN: mlir-tblgen -gen-bytecode -bytecode-dialect=Test -I %S/../../include %s | FileCheck %s
+
+include "mlir/IR/BytecodeBase.td"
+
+// The bytecode readers and writers include the headers declaring the bytecode
+// reader and writer and `TypeSwitch`, which is used by the write dispatch.
+
+// CHECK: #include "llvm/ADT/TypeSwitch.h"
+// CHECK-NEXT: #include "mlir/Bytecode/BytecodeImplementation.h"
+// CHECK-EMPTY:
+// CHECK-NOT: #include
+
+def TestDialectTypes : DialectTypes<"Test"> {
+ let elems = [ReservedOrDead];
+}
diff --git a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
index 8f94f4b7d35316..f7d6c7aa0b07b4 100644
--- a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "CppGenUtilities.h"
#include "mlir/Support/IndentedOstream.h"
#include "mlir/TableGen/GenInfo.h"
#include "llvm/ADT/MapVector.h"
@@ -458,6 +459,16 @@ struct AttrOrType {
};
} // namespace
+/// Headers declaring the C++ constructs used by the generated readers and
+/// writers.
+static constexpr StringRef bytecodeIncludes[] = {
+ // `::llvm::TypeSwitch`.
+ "llvm/ADT/TypeSwitch.h",
+ // `::mlir::DialectBytecodeReader`, `::mlir::DialectBytecodeWriter`,
+ // `::mlir::getChecked`, `::mlir::readResourceHandle`.
+ "mlir/Bytecode/BytecodeImplementation.h",
+};
+
static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
MapVector<StringRef, AttrOrType> dialectAttrOrType;
for (const Record *it :
@@ -481,6 +492,7 @@ static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
"one in input file or specified via dialect option)");
auto it = dialectAttrOrType.front();
+ mlir::tblgen::emitIncludes(os, bytecodeIncludes);
Generator gen(os);
const std::vector<const Record *> *vecs[] = {&it.second.attr,
More information about the cfe-commits
mailing list