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 from 3e9782a to d95a6e4 Compare October 17, 2025 18:07
@prithayan prithayan force-pushed the dev/pbarua/comb-to-llvm branch from d95a6e4 to 47c256e Compare October 17, 2025 18:09
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.

1 participant