Skip to content

Conversation

@prithayan
Copy link
Contributor

@prithayan prithayan commented Oct 17, 2025

This PR adds a complete implementation of the ConvertCombToLLVM pass that converts Comb and related dialect operations to LLVM IR. The pass is designed to only convert operations inside func.func operations, leaving all other operations (such as hw.module) unconverted.

Previously, the CombToLLVM conversion infrastructure only provided pattern population functions (populateCombToLLVMConversionPatterns) but no complete pass implementation.

This PR adds:

  1. A conversion pass that orchestrates the complete lowering pipeline from Comb/HW to LLVM
  2. Selective conversion scope that only processes func.func operations, allowing mixed representations where:
    • Software-like functions are lowered to LLVM IR for execution/simulation
    • Hardware modules remain in their original form for other passes or backends
  3. Comprehensive test coverage that adds tests covering all major Comb operations

Example Input:

func.func @my_function(%a: i32, %b: i32) -> i32 {
  %result = comb.add %a, %b : i32
  return %result : i32
}

hw.module @my_hardware_module(in %x: i32, in %y: i32, out z: i32) {
  %sum = comb.add %x, %y : i32
  hw.output %sum : i32
}

Output after --convert-comb-to-llvm:

llvm.func @my_function(%arg0: i32, %arg1: i32) -> i32 {
  %0 = llvm.add %arg0, %arg1 : i32
  llvm.return %0 : i32
}

hw.module @my_hardware_module(in %x : i32, in %y : i32, out z : i32) {
  %0 = comb.add %x, %y : i32
  hw.output %0 : i32
}

Implementation detail:

The pass applies conversion patterns in a specific order to ensure correct lowering:

Input (Comb/HW/Func/SCF dialects)
    ↓
1. SCF → ControlFlow (if/while → branches)
    ↓
2. Func → LLVM (func.func → llvm.func)
    ↓
3. ControlFlow → LLVM (br/cond_br → llvm.br/llvm.cond_br)
    ↓
4. Comb → Arith (comb.add → arith.addi, etc.)
    ↓
5. Arith → LLVM (arith.addi → llvm.add, etc.)
    ↓
6. Index → LLVM (index operations)
    ↓
7. Function interface type conversions
    ↓
8. Comb → LLVM (comb.parity → llvm.ctpop, hw.output → llvm.return)
    ↓
Output (LLVM dialect only, inside func.func)

This change complements the existing conversion infrastructure:

  • CombToArith: Converts most Comb operations to Arith dialect (used by this pass)
  • HWToLLVM: Provides type conversions for HW types (used by this pass)
  • ArithToLLVM: Converts Arith operations to LLVM dialect (used by this pass)
  • FuncToLLVM: Converts Func operations to LLVM dialect (used by this pass)
  • SCFToControlFlow: Converts SCF to ControlFlow dialect (used by this pass)

The CombToLLVM pass orchestrates all these conversions into a single, complete lowering pipeline, but only applies them to func.func operations.

Note: This PR has been developed with help from Calude Sonnet 4.5

@prithayan prithayan force-pushed the dev/pbarua/comb-to-llvm branch 2 times, most recently from d95a6e4 to 47c256e Compare October 17, 2025 18:09
@prithayan prithayan marked this pull request as ready for review October 29, 2025 18:06
Copy link
Member

@maerhart maerhart left a comment

Choose a reason for hiding this comment

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

This is already getting close to a pass that does a full conversion to LLVM including HW aggregates etc. Been thinking of implementing one based on dialect interfaces for quite a while now.

Given that it also converts SCF and index operations and in the future probably also HW aggregates, we might want to call it something like ConvertToLLVM instead of ConvertCombToLLVM and put it in a separate file.
Otherwise LGTM

@prithayan prithayan force-pushed the dev/pbarua/comb-to-llvm branch from 1754f18 to 72a756d Compare October 30, 2025 21:45
@prithayan
Copy link
Contributor Author

This is already getting close to a pass that does a full conversion to LLVM including HW aggregates etc. Been thinking of implementing one based on dialect interfaces for quite a while now.

Given that it also converts SCF and index operations and in the future probably also HW aggregates, we might want to call it something like ConvertToLLVM instead of ConvertCombToLLVM and put it in a separate file. Otherwise LGTM

Thanks @maerhart for the detailed review. I agree, this is not just CombToLLVM. So, created the new pass ConvertToLLVM instead. You can add future extensions and patterns as necessary there.

@prithayan prithayan force-pushed the dev/pbarua/comb-to-llvm branch from 4534435 to bf04b41 Compare October 31, 2025 21:19
@prithayan prithayan merged commit f25eebe into main Nov 4, 2025
7 checks passed
@prithayan prithayan deleted the dev/pbarua/comb-to-llvm branch November 4, 2025 18:14
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.

3 participants