Implementation:Tencent Ncnn TF Dialect
| Knowledge Sources | |
|---|---|
| Domains | Model_Conversion, MLIR_Infrastructure |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Implements the TensorFlow MLIR dialect, including dialect registration, custom attribute and type parsing/printing, constant materialization, and operation-specific logic needed to load and process TensorFlow MLIR files in the mlir2ncnn converter.
Description
The TensorFlowDialect constructor registers all TF operations (from TableGen-generated tf_all_ops.cc.inc), all TF types (from tf_types.def macros), and custom attributes (ShapeAttr, FuncAttr). It enables allowUnknownOperations() since not all TF ops are registered.
Stub verifier functions (Verify, VerifyPartitionedCall, VerifyStridedSliceBase, VerifyUnsortedSegmentReduction) return success unconditionally, simplifying the port from upstream TensorFlow.
The parseAttribute override dispatches to ParseShapeAttr (which handles shape<*> for unranked and shape<2x3x?> for ranked shapes) and ParseFuncAttr (which handles func<@symbol, {attrs}> format).
The parseType override handles standard TF types via the tf_types.def macro and custom types like resource and variant with subtypes via ParseTypeWithSubtype<T>.
The materializeConstant method creates ConstOp instances, and ConstOp::build methods handle conversion of scalar attributes to DenseElementsAttr tensors. WhileRegionOp implements LoopLikeOpInterface methods for loop body access, outside-of-loop value detection, and operation hoisting.
The file ends by including the TableGen-generated tf_all_ops.cc.inc for operation class implementations.
Usage
This is the most important TensorFlow dialect implementation file in the mlir2ncnn tool. It enables the MLIR parser to load TensorFlow-exported .mlir files by providing all the type, attribute, and operation infrastructure. Without this file, the converter cannot parse any TensorFlow MLIR input. It is not used directly but is linked into the mlir2ncnn binary.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: tools/mlir/tf_dialect.cpp
- Lines: 1-312
Signature
namespace mlir {
static LogicalResult Verify(...);
static LogicalResult VerifyPartitionedCall(...);
static LogicalResult VerifyStridedSliceBase(...);
static LogicalResult VerifyUnsortedSegmentReduction(...);
namespace TF {
TensorFlowDialect::TensorFlowDialect(MLIRContext* context);
// Custom attribute parsing
ShapeAttr ParseShapeAttr(MLIRContext* context, StringRef spec, Location loc);
} // namespace TF
} // namespace mlir
Import
// Library component - linked into mlir2ncnn
#include "tf_dialect.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| context | mlir::MLIRContext* | Yes | The MLIR context to register the TF dialect with |
Outputs
| Name | Type | Description |
|---|---|---|
| TensorFlowDialect | mlir::Dialect | Registered dialect with TF operations, types, and attributes |
Usage Examples
Registering the TF Dialect
mlir::MLIRContext context;
context.getOrLoadDialect<mlir::TF::TensorFlowDialect>();
// Now the context can parse TensorFlow MLIR files