Skip to content

[REFACTOR][IR] Delete class Bool and class Integer boxed-type wrappers#19636

Merged
spectrometerHBH merged 5 commits into
apache:mainfrom
tqchen:tvm-delete-integer-bool-classes
May 29, 2026
Merged

[REFACTOR][IR] Delete class Bool and class Integer boxed-type wrappers#19636
spectrometerHBH merged 5 commits into
apache:mainfrom
tqchen:tvm-delete-integer-bool-classes

Conversation

@tqchen
Copy link
Copy Markdown
Member

@tqchen tqchen commented May 28, 2026

Background

class Integer : public IntImm and class Bool : public IntImm were thin
wrappers sharing IntImmNode with no separate node class and no FFI
registration. They existed to provide implicit int→Integer constructors and
a .IntValue() / operator bool() accessor, but the same functionality is
available directly through IntImm.

What this PR does

Migrates all call sites away from Integer / Bool and then deletes the
class definitions. The changes are split into four commits, each
independently buildable:

Commit 1 – [REFACTOR][TIR] Replace IR-position Integer(N) / Bool(b)
constructors with IntImm(DataType::Int(32), N) / IntImm(DataType::Bool(), b)
across ~62 source files (arith, relax analysis, s_tir schedule state, transform
passes, codegen).

Commit 2 – [REFACTOR][SCHEDULE] Migrate Schedule and MetaSchedule
trace-boxing code: Integer(N) attrs in TracedScheduleIntImm(DataType::Int(32), N);
ffi::Array<Integer> schedule-rule parameters → int64_t; Bool(b) attrs →
IntImm(DataType::Bool(), b).

Commit 3 – [REFACTOR][TOPI] Migrate topi container signatures
(ffi::Array<Integer>ffi::Array<int64_t>) and update all internal
usages (.IntValue() → plain int64_t, .defined() → removed,
->value → direct indexing). Also handles stray Integer / Bool
variables in clml codegen, make_packed_api, infer_layout_utils, and
relax distributed code.

Commit 4 – [REFACTOR][IR] Delete class Bool, class Integer,
TypeTraits<Bool>, and TypeTraits<Integer> from include/tvm/ir/expr.h.

Canonical replacements

Old New
Integer(N) IntImm(DataType::Int(32), N)
Bool(b) IntImm(DataType::Bool(), b)
x.IntValue() x->value
x as bool x->value != 0
ffi::Array<Integer> ffi::Array<int64_t>

Testing

  • All 118 C++ unit tests pass (./cpptest)
  • tests/python/s_tir/ — 1251 passed (14 pre-existing failures unrelated to this change, all in TIR transform tests with annotation-mismatch errors)
  • tests/python/relax/ — passes (excluding pre-existing torch/torchvision import failures in frontend tests)

tqchen added 4 commits May 28, 2026 12:17
…tImm/const_true/const_false

Bool(true) and Bool(false) in PrimExpr position add no information over
IntImm(DataType::Bool(), 1) and IntImm(DataType::Bool(), 0) (which
const_true() and const_false() produce). Integer(N) in PrimExpr position
adds no information over IntImm(DataType::Int(32), N); both wrap the same
IntImmNode. Replace throughout the 62-file IR, arith, relax, s_tir,
tirx, topi, and target/cuda call sites.
…hedule and MetaSchedule

Schedule trace attrs and MetaSchedule decision payloads boxed ints
into Integer() to pass through ffi::Any. IntImm(DataType::Int(32), N)
is the correct canonical form for integer constants in IR position;
Integer() was a redundant wrapper. Replace across traced_schedule.cc,
concrete_schedule, and ~15 MetaSchedule files.

Also change MultiLevelTilingWideVector's vector_length_in_bits parameter
from Integer to int64_t in both the header declaration and implementation,
matching the underlying field type.
…ol callers

- Change ffi::Array<Integer> to ffi::Array<int64_t> in topi headers
  (strided_slice, reduction, transform, nn, utils, nn/group_norm,
  nn/instance_norm, nn/layer_norm, nn/rms_norm) and corresponding
  sources; update internal usages (.IntValue(), .defined(), ->value,
  ->dtype) to plain int64_t arithmetic
- Migrate ShardingNode::sharding_dim and CalculateConstantBytes/
  CalculateWorkspaceBytes signatures from Integer to int64_t
- Migrate codegen_c.h constants_byte_alignment_ field from Integer
  to int64_t
- Replace Downcast<Integer/Bool> with Downcast<IntImm> in schedule
  primitives, metaschedule helpers, and misc transform passes
- Convert remaining Integer/Bool local variables and function return
  types (clml codegen, make_packed_api, infer_layout_utils, etc.)
  to IntImm or plain int64_t
- Update stale doc-comment "Type: Integer/Bool" annotations in
  function.h and tirx/function.h to "Type: IntImm"
- Fix test files: nested_msg_test.cc (NestedMsg<IntImm>), ir_functor_test.cc,
  arith_simplify_test.cc
…r/expr.h

Remove the Bool and Integer thin-wrapper classes (which were both
subclasses of IntImm sharing IntImmNode) along with their
TypeTraits<Integer> and TypeTraits<Bool> specializations.

All call sites were migrated to IntImm in the preceding three commits.
The canonical replacements are:
- Integer(N)    → IntImm(DataType::Int(32), N)
- Bool(b)       → IntImm(DataType::Bool(), b)
- x.IntValue()  → x->value
- x operator bool → x->value != 0
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the codebase by replacing the usage of Integer and Bool classes with IntImm or primitive int64_t types across various TVM, Relax, and TOPI components. The review feedback is highly constructive, pointing out a redundant ternary operator in transform.h and suggesting the use of the more idiomatic const_true() helper instead of manually constructing boolean IntImm instances in several files where <tvm/tirx/op.h> is included.

Comment thread include/tvm/topi/transform.h Outdated
Comment thread src/te/operation/create_primfunc.cc Outdated
Comment thread src/relax/analysis/struct_info_analysis.cc Outdated
Comment thread src/relax/ir/dataflow_matcher.cc Outdated
Comment thread src/relax/transform/adjust_matmul_order.cc Outdated
Comment thread src/relax/transform/fuse_tir.cc Outdated
…pply const_true cleanups

After migrating topi container signatures from Array<Integer>, the
strided_slice family lost two features the original Array<Integer>
parameters carried:

  1. Nullable entries. begin/end accept None entries meaning "use
     stride-direction default". The Array<int64_t> port silently
     dropped the !defined() branches.
  2. Index dtype propagation. begin[0]->dtype drove the index dtype
     for IntImm/PrimExpr construction; hardcoding Int(64) widened
     int32 callers.

Restore both features by porting begin/end to
ffi::Array<ffi::Optional<IntImm>> and strides to ffi::Array<IntImm>:

- include/tvm/topi/detail/strided_slice.h: ConvertToVec gets
  Optional<IntImm>/IntImm params; reinstates the None branches
  for begin and end.
- include/tvm/topi/transform.h: strided_slice / strided_slice_with_axes
  / StridedSliceOutputShape match the new types; index_dtype is
  derived from begin[0]->dtype again; max_range uses max_value(dtype);
  stride dtype from strides[i]->dtype.
- src/topi/transform.cc: static-path cast now produces
  Array<Optional<IntImm>> for begin/end and Array<IntImm> for strides.
- include/tvm/topi/nn.h: the strided_slice caller in spatial_pad_to_batch
  builds Array<Optional<IntImm>>/Array<IntImm> instead of Array<int64_t>.

axes remains Array<int64_t> on the C++ side (only host integers are
read). python/tvm/topi/transform.py drops the begin/end/strides
_unbox shim — the FFI now marshals Python ints and IntImm directly —
but keeps a small axes-only IntImm→int conversion for relax legalize
callers that produce IntImm-valued axes lists.

Also apply six mechanical cleanups from review feedback:
- include/tvm/topi/transform.h: remove redundant ternary (Int(64) ? Int(64))
- src/te/operation/create_primfunc.cc: IntImm(Bool(),1) → const_true()
- src/relax/analysis/struct_info_analysis.cc: same
- src/relax/ir/dataflow_matcher.cc: same
- src/relax/transform/adjust_matmul_order.cc: same
- src/relax/transform/fuse_tir.cc: same (no namespace qualifier needed,
  already in tvm::tirx; relax-namespace sites use tirx::const_true())

Clang-format and ruff-format reformats from pre-commit included.
@tqchen tqchen force-pushed the tvm-delete-integer-bool-classes branch from 69c0900 to 8bb7fd6 Compare May 28, 2026 18:36
@tqchen
Copy link
Copy Markdown
Member Author

tqchen commented May 29, 2026

@tvm-bot rerun

@spectrometerHBH spectrometerHBH merged commit 878903f into apache:main May 29, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants