Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn TF Types

From Leeroopedia


Knowledge Sources
Domains Model_Conversion, MLIR_Infrastructure
Last Updated 2026-02-09 19:00 GMT

Overview

Implements and declares the TensorFlow-specific MLIR type system, including type classification, ref type conversion, subtype management, type compatibility checking, and broadcast compatibility logic used by the mlir2ncnn converter.

Description

The TF type system spans two files: tf_types.h (header, 380 lines) and tf_types.cc (implementation, 462 lines).

The header establishes a multi-level type hierarchy:

  • TensorFlowType is the root base class extending mlir::Type
  • TensorFlowTypeImpl<Derived> is a CRTP template using Type::TypeBase for simple TF types
  • tf_types.def macros generate concrete type classes (via HANDLE_TF_TYPE) for each TF data type
  • TensorFlowRefType extends TensorFlowType to represent mutable reference types, providing get() (convert to ref), RemoveRef() (convert back), and verification
  • TypeWithSubtypeStorage holds an ArrayRef<TensorType> for subtypes
  • ResourceType and VariantType are concrete types with subtypes

The implementation file provides:

  • Shape utility iterators (OperandShapeIterator, ResultShapeIterator) that wrap MLIR operation iterators to extract tensor shapes
  • Type classification methods (TensorFlowType::classof, TensorFlowRefType::classof, TensorFlowTypeWithSubtype::classof) using tf_types.def macros
  • BroadcastCompatible checks whether two type ranges are broadcast-compatible by comparing element types and shapes
  • GetCastCompatibleType returns a refined type that is cast-compatible with both input types
  • GetCastCompatibleShape merges two ranked shapes dimension-by-dimension, accepting dynamic dimensions as wildcards
  • Helper functions: HasCompatibleElementTypes, AreCastCompatible, ArraysAreCastCompatible, DropSubTypes, DropRefType, DropRefAndSubTypes

Usage

This is the foundational type system for the TensorFlow MLIR dialect. It is included by tf_traits.h, tf_dialect.h, tf_types.cc, and transitively by most other TF dialect files. The type hierarchy and utility functions it defines are essential for MLIR's type checking, verification, and transformation infrastructure to work correctly with TensorFlow operations in the mlir2ncnn conversion pipeline.

Code Reference

Source Location

Signature

namespace mlir {
namespace TF {

class OperandShapeIterator final
    : public llvm::mapped_iterator<Operation::operand_iterator,
      llvm::Optional<ArrayRef<int64_t>>(*)(Value)> {
public:
    explicit OperandShapeIterator(Operation::operand_iterator it);
};

class ResultShapeIterator final
    : public llvm::mapped_iterator<Operation::result_iterator,
      llvm::Optional<ArrayRef<int64_t>>(*)(Value)> {
public:
    explicit ResultShapeIterator(Operation::result_iterator it);
};

class TensorFlowType : public Type {
public:
    using Type::Type;
    static bool classof(Type type);
};

static inline bool IsValidTFElementType(Type type);
static inline bool IsValidTFTensorType(Type type);

bool BroadcastCompatible(TypeRange lhs, TypeRange rhs);
Type GetCastCompatibleType(Type a, Type b, bool may_ignore_ref_type_a);

} // namespace TF
} // namespace mlir

Import

// Library component - linked into mlir2ncnn
#include "tf_types.h"

I/O Contract

Inputs

Name Type Required Description
type mlir::Type Yes An MLIR type to classify or check compatibility
lhs, rhs mlir::TypeRange Yes Type ranges for broadcast compatibility checking

Outputs

Name Type Description
classof result bool Whether a type belongs to the TensorFlow type hierarchy
compatible result bool Whether types are broadcast or cast compatible
refined type mlir::Type A cast-compatible refined type from two input types

Usage Examples

Type Classification

// Check if a type is a valid TF element type
if (mlir::TF::IsValidTFElementType(type)) {
    // type is float, integer, complex, or TensorFlowType
}

// Check if a type belongs to the TF dialect
if (mlir::TF::TensorFlowType::classof(type)) {
    // type is from the "tf" dialect namespace
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment