Skip to content

Conversation

@arhik
Copy link
Contributor

@arhik arhik commented Jan 17, 2026

This commit enables reduce_sum and reduce_max operations on all numeric types, extending beyond the previous float-only support.

Infrastructure Changes

Bytecode Layer

  • Added IntegerIdentityOp struct with signed/unsigned handling
  • Added encode_tagged_int! for integer identity encoding
  • Added mask_to_width function with zigzag encoding for signed types adhering to LEBP128.
  • Added encode_identity! dispatch for FloatIdentityOp and IntegerIdentityOp
  • Refactored ReduceIdentity → IdentityOp for extensibility

Compiler Layer

  • Refactored emit_reduce! to use dispatch-based approach
  • Added operation_identity dispatch for add/max operations
  • Added encode_reduce_body dispatch for float and integer operations
  • Removed T <: AbstractFloat constraints from intrinsics

Language Layer

  • Removed type constraints from reduce_sum and reduce_max in operations.jl

Test Coverage

Codegen Tests

  • Added FileCheck tests for Int32/UInt32 reduce_sum and reduce_max
  • Verifies correct IR generation (addi, maxi instructions)

Execution Tests

  • Factory pattern for easy extension (makeReduceKernel, cpu_reduce)
  • Tests 10 types: Int8, Int16, Int32, Int64, UInt16, UInt32, UInt64, Float16, Float32, Float64
  • Tests 2 operations: reduce_sum, reduce_max
  • CPU verification for all test cases
  • Type-appropriate input ranges to prevent overflow

Files Changed

  • src/bytecode/encodings.jl: Fix IdentityOp type annotation
  • src/bytecode/writer.jl: Integer identity infrastructure
  • src/compiler/intrinsics.jl: Import identity types
  • src/compiler/intrinsics/core.jl: Dispatch-based reduce implementation
  • src/cuTile.jl: Export identity types
  • src/language/operations.jl: Remove type constraints
  • test/codegen.jl: Add integer reduction codegen tests
  • test/execution.jl: Add extendable execution tests

Extensibility

The infrastructure is designed for easy extension:

  • Add new reduce operations by defining operation_identity and encode_reduce_body methods
  • Add new types by adding to TEST_TYPES array and appropriate data generation

This commit enables reduce_sum and reduce_max operations on all numeric types,
extending beyond the previous float-only support.

## Infrastructure Changes

### Bytecode Layer
- Added IntegerIdentityOp struct with signed/unsigned handling
- Added encode_tagged_int! for integer identity encoding
- Added mask_to_width function with zigzag encoding for signed types
- Added encode_identity! dispatch for FloatIdentityOp and IntegerIdentityOp
- Refactored ReduceIdentity → IdentityOp for extensibility

### Compiler Layer
- Refactored emit_reduce! to use dispatch-based approach
- Added operation_identity dispatch for add/max operations
- Added encode_reduce_body dispatch for float and integer operations
- Removed T <: AbstractFloat constraints from intrinsics

### Language Layer
- Removed type constraints from reduce_sum and reduce_max in operations.jl

## Test Coverage

### Codegen Tests
- Added FileCheck tests for Int32/UInt32 reduce_sum and reduce_max
- Verifies correct IR generation (addi, maxi instructions)

### Execution Tests
- Factory pattern for easy extension (makeReduceKernel, cpu_reduce)
- Tests 10 types: Int8, Int16, Int32, Int64, UInt16, UInt32, UInt64, Float16, Float32, Float64
- Tests 2 operations: reduce_sum, reduce_max
- CPU verification for all test cases
- Type-appropriate input ranges to prevent overflow

## Files Changed

- src/bytecode/encodings.jl: Fix IdentityOp type annotation
- src/bytecode/writer.jl: Integer identity infrastructure
- src/compiler/intrinsics.jl: Import identity types
- src/compiler/intrinsics/core.jl: Dispatch-based reduce implementation
- src/cuTile.jl: Export identity types
- src/language/operations.jl: Remove type constraints
- test/codegen.jl: Add integer reduction codegen tests
- test/execution.jl: Add extendable execution tests

## Extensibility

The infrastructure is designed for easy extension:
- Add new reduce operations by defining operation_identity and encode_reduce_body methods
- Add new types by adding to TEST_TYPES array and appropriate data generation
@arhik arhik marked this pull request as ready for review January 17, 2026 18:54
@arhik
Copy link
Contributor Author

arhik commented Jan 17, 2026

This PR simplifies and replaces #21 and makes the additional reduction operator contribution optional/independent/extensible.

- Constrain T <: Number in reduce_sum/reduce_max signatures for type safety
- Ensures only numeric types can be used with reduction operations
@arhik arhik changed the title feat: Add integer reduction support for reduce_sum and reduce_max feat: Add integer reduction support for reduce ops Jan 18, 2026
arhik added 6 commits January 20, 2026 01:08
- IntegerIdentityOp no longer stores signed::Bool field
- signedness inferred from dtype (T <: Signed)
- mask_to_width simplified to use T <: Signed for zigzag encoding
- All call sites updated
- Zigzag encoding now handled entirely in mask_to_width
- Single encode_varint! call regardless of signedness
Single-use functionality doesn't benefit from dispatch-based selection.

"""
ReduceIdentity
IdentityOp
Copy link
Member

Choose a reason for hiding this comment

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

Forgot to submit this comment yesterday:

It's not really an IdentityOp, is it? The Op makes me think this is an Tile IR operation, while we're simply dealing with values here AFAIU.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I had alternative name. I can't recollect it now. For now does IdentityValue or IdentityVal sounds good to you ?

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