Skip to content

Comments

Mlir tensor linalg support#2

Open
alirezazd wants to merge 26 commits intomainfrom
mlir-tensor-linalg-support
Open

Mlir tensor linalg support#2
alirezazd wants to merge 26 commits intomainfrom
mlir-tensor-linalg-support

Conversation

@alirezazd
Copy link
Owner

No description provided.

alirezazd and others added 20 commits August 11, 2025 13:48
- Create arith_to_xls.h header with pattern population function
- Implement populateArithToXlsPatterns in arith_to_xls.cc
- Integrate arith-to-xls pattern application in translation pipeline
- Add translate_arith_addf_folding.mlir test for constant folding validation
- Added multiple MLIR test files for arithmetic operations, including `or`, `xor`, `subi`, `muli`, `divsi`, `divui`, `remsi`, `remui`, `shli`, and `shrui` to validate the conversion from MLIR arith dialect to XLS dialect.
-streamline the test suite.
- Created a new `tensor_ops.mlir` file to test various tensor operations, including empty tensors, inline dense tensors, and tensor extraction.
- Deleted unnecessary translation tests for arith
- Enhanced the `xls_translate` tool to support tensor operations and dense resource extraction, including zero-initialization for empty tensors.
- Updated the translation patterns to handle dense resource elements and added new patterns for resource-backed constants.
- Registered the Linalg dialect in the translation tool to support additional operations.
- Add linalg-to-xls pass for converting linalg.generic to arith operations
- Support element-wise operations: addf, mulf, subf, cmpf, select
- Add automatic xls=true attribute management via add-xls-attributes pass
- Integrate with mlir_to_xls_ir build rule for seamless conversion
- Clean up test data and remove unsupported complex patterns
- Remove example files and build targets for linalg_generic*
…tions

- Add LinalgElementwiseToArithPattern supporting multi-input operations with single arithmetic operations
- Add LinalgFillToTensorSplatPattern for fill operations
- Add LinalgComplexToDslxPattern for complex operations requiring DSLX codegen
- Implement input detection that ignores unused inputs in multi-input operations
- Support mixed tensor-scalar combinations with automatic scalar-to-tensor conversion
- Add test suite in linalg_generic_ops.mlir covering all supported patterns
- Clean up and refactor linalg_to_xls.cc removing unnecessary comments and debug code
- Add DSLX codegen infrastructure for future complex operation support
- Add linalg analysis library for structured operation analysis

Pattern priority:
- Fill operations (benefit=25): Convert to tensor.splat
- Element-wise operations (benefit=20): Convert to arith operations
- Complex operations (benefit=10): Route to DSLX codegen

Supported operations:
- Tensor + Tensor, Tensor + Scalar, Scalar + Tensor
- Multi-input with unused inputs (e.g., 3 inputs, only 2 used)
- Fill operations with constants
- All arithmetic operations: addf, mulf, subf

Complex operations (multiple operations, reductions) are routed to DSLX codegen for future implementation.
This commit implements a complete MLIR Linalg-to-XLS transformation pipeline with the following components:

Core Analysis:
- LinalgGeneric analysis and internal representation
- Broadcast analysis for tensor operations
- Loop scheduling with ordering, tiling, and unrolling
- Unroll planning for multi-dimensional lane operations

Code Generation:
- DSLX code generation from analyzed Linalg operations
- Reduction utilities with serial, tree, and Kahan policies
- Lane operations for unrolled loop handling
- Region body lowering and evaluation

Testing:
- Comprehensive test suite covering all components
- Broadcast analysis tests (T1-T5)
- Schedule generation tests (T1-T7)
- Scalar interpreter tests (T1-T6)
- Reduction utilities tests
- Unroll planning tests
- Lane operations tests
- DSLX codegen tests

Key Features:
- Support for 4D tensor operations with mixed indexing
- Broadcasting analysis and validation
- Multiple reduction policies (serial, tree, Kahan)
- Multi-dimensional unrolling with lane planning
- DSLX code generation with proper array handling
- Zero-initialization and accumulator logic
- Comprehensive error handling and validation

Architecture:
- Modular design with clear separation of concerns
- Type-safe internal representations
- Extensible reduction and unrolling systems
- Clean, well-documented codebase
- Full test coverage for all components

This implementation provides a solid foundation for transforming MLIR Linalg operations into XLS-compatible DSLX code, enabling high-level tensor operations to be compiled to hardware.
- Add comprehensive LinalgReductionToXlsPattern for reduction operations
- Implement full analysis pipeline: LinalgGeneric → BroadcastAnalysis → Schedule → UnrollPlan
- Add DSLX code generation with complete end-to-end transformation
- Support n-dimensional tensors and unlimited loop scaling
- Generate functional DSLX code from complex linalg.generic operations
- Include detailed debug output for pipeline validation
- Update BUILD files for renamed dslx_codegen components

The pipeline now successfully transforms MLIR linalg.generic operations
through complete analysis and generates executable DSLX code, supporting
complex reduction patterns with n-dimensional tensors and unlimited
loop structures.

Tested with complex_linalg_example.mlir showing full pipeline execution
from 4D tensor analysis to DSLX code generation.
- Fix BuildZeroTensor to generate correct 1D array syntax with ellipsis
- Fix GetReturnType to flatten multi-dimensional outputs to 1D arrays
- Ensure proper type inference with float32::F32 and type aliases
- Generated DSLX now compiles and runs successfully with type_inference_v2

The codegen now produces syntactically correct DSLX that can be interpreted
and tested, providing a solid foundation for implementing loop body logic.

Signed-off-by: Alireza <alirezazd@gmail.com>
- Delete xls/contrib/mlir/testdata/linalg/ directory and all test files
- Delete xls/contrib/mlir/transforms/linalg/analysis/ directory
- Clean up old test infrastructure files
- Add LinalgCodegenConfig singleton for global configuration management
- Implement EvalLinalgGeneric with broadcast analysis and validation
- Add GenerateSchedule with loop ordering policies (kPreserve default)
- Add GenerateUnrollPlan with configurable unrolling
- Add DslxCodegen with simplified interface for DSLX emission
- Consolidate linalg evaluation into flat directory structure
- Clean up code style: remove verbose comments, uniformize patterns
- Add helper functions and reduction utilities
- Support serial reduction policy with no tiling by default
- Implement consistent error handling pattern with detailed error messages
- Extract ValidateReduction() and refactor DeriveShapes() for better organization
- Reorder LinalgEvalResults fields and update constructor signature
- Simplify function names and remove redundant fields
- Use std::pair for shapes and improve forward declarations
Remove old/unused files:
- dslx_codegen.cc/h (replaced by dslxgen.cc/h)
- lane_ops.cc/h (functionality moved elsewhere)
- reduction.cc/h (consolidated into main evaluation)
- reduction_utils.cc/h (utilities moved to core functions)
- unroll_plan_gen.cc/h (unused planning code)

These files were cleaned up during the linalg evaluation refactoring.
- Extract schedule-related types to schedule_types.h
- Improve error handling consistency in linalg_eval.cc
- Implement DslxGen class for converting Linalg ops to DSLX
- Add DSLX statement types with factory methods (MakeLet, MakeFor, MakeUpdate)
- Support nested parallel and reduction loops with proper accumulator management
- Generate complete DSLX functions with headers, signatures, and loop bodies
@alirezazd alirezazd self-assigned this Aug 21, 2025
- Change input operand names from 'input0/input1' to 'arg0/arg1' to avoid XLS reserved word
- Restore testdata BUILD.bazel to match main repository
- Delete unused linalg_to_xls.mlir test file
…trix multiplication

- Fix floating-point comparison syntax (float32::gt_2 for boolean results)
- Add support for cmpf_ogt and select operations in DSLX generation
- Implement proper if-then-else syntax for conditional expressions
- Add ExtractActualReductionOp to identify primary reduction operations
- Support multiplication reductions with correct initialization (F32_ONE)
- Fix tensor.splat output validation to allow both 0.0 and 1.0 initialization
- Validate reduction operations to only accept add, sub, mul (reject others)
- Enhance error messages with LinalgGenericToString for better debugging
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