Skip to content

Enable loop ctrl with placeholder for SSA and backward ctrl move#36

Merged
tancheng merged 2 commits intomainfrom
ctrl_to_data_for_loop
Jun 16, 2025
Merged

Enable loop ctrl with placeholder for SSA and backward ctrl move#36
tancheng merged 2 commits intomainfrom
ctrl_to_data_for_loop

Conversation

@tancheng
Copy link
Copy Markdown
Contributor

Enable loop ctrl with placeholder for SSA and backward ctrl move, basically,

  • neura.reserve is added as a phi's operand if the real dependent value is defined within the same block but after the phi's creation
  • Insert the backward ctrl move after the real value's define, to indicate there is a loop-carried dependency without violating SSA

The provided test can demonstrate this PR's funtionality:

func.func @loop_test() -> f32 {
  %n = llvm.mlir.constant(10 : i64) : i64
  %c0 = llvm.mlir.constant(0 : i64) : i64
  %c1 = llvm.mlir.constant(1 : i64) : i64
  %c1f = llvm.mlir.constant(3.0 : f32) : f32
  %acc_init = llvm.mlir.constant(0.0 : f32) : f32

  llvm.br ^bb1(%c0, %acc_init : i64, f32)

^bb1(%i: i64, %acc: f32):  // loop body + check + increment
  %next_acc = llvm.fadd %acc, %c1f : f32
  %i_next = llvm.add %i, %c1 : i64
  %cmp = llvm.icmp "slt" %i_next, %n : i64
  llvm.cond_br %cmp, ^bb1(%i_next, %next_acc : i64, f32), ^exit(%next_acc : f32)

^exit(%result: f32):
  return %result : f32
}

After transformation:

func.func @loop_test() -> f32 attributes {accelerator = "neura"} {
  %0 = "neura.constant"() <{predicate = true, value = 10 : i64}> : () -> !neura.data<i64, i1>
  %1 = "neura.constant"() <{predicate = true, value = 0 : i64}> : () -> !neura.data<i64, i1>
  %2 = "neura.constant"() <{predicate = true, value = 1 : i64}> : () -> !neura.data<i64, i1>
  %3 = "neura.constant"() <{predicate = true, value = 3.000000e+00 : f32}> : () -> !neura.data<f32, i1>
  %4 = "neura.constant"() <{predicate = true, value = 0.000000e+00 : f32}> : () -> !neura.data<f32, i1>
  %5 = neura.reserve : !neura.data<i64, i1>
  %6 = "neura.phi"(%1, %5) : (!neura.data<i64, i1>, !neura.data<i64, i1>) -> !neura.data<i64, i1>
  %7 = neura.reserve : !neura.data<f32, i1>
  %8 = "neura.phi"(%4, %7) : (!neura.data<f32, i1>, !neura.data<f32, i1>) -> !neura.data<f32, i1>
  %9 = "neura.fadd"(%8, %3) : (!neura.data<f32, i1>, !neura.data<f32, i1>) -> !neura.data<f32, i1>
  neura.ctrl_mov %9 -> %7 : !neura.data<f32, i1> !neura.data<f32, i1>
  %10 = "neura.add"(%6, %2) : (!neura.data<i64, i1>, !neura.data<i64, i1>) -> !neura.data<i64, i1>
  neura.ctrl_mov %10 -> %5 : !neura.data<i64, i1> !neura.data<i64, i1>
  %11 = "neura.icmp"(%10, %0) <{cmpType = "slt"}> : (!neura.data<i64, i1>, !neura.data<i64, i1>) -> !neura.data<i1, i1>
  "neura.return"(%9) : (!neura.data<f32, i1>) -> ()
}

@tancheng tancheng self-assigned this Jun 15, 2025
@tancheng tancheng added the new feature New feature or request label Jun 15, 2025
@tancheng tancheng linked an issue Jun 15, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@ShangkunLi ShangkunLi left a comment

Choose a reason for hiding this comment

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

LGTM

@tancheng tancheng merged commit c8899ed into main Jun 16, 2025
1 check passed
@tancheng
Copy link
Copy Markdown
Contributor Author

tancheng commented Jun 16, 2025

LGTM

lol, i found the ctrl_mov is inserted wrongly (when I implementing recMII util func in a new branch), will fix forward ASAP :-)

ShangkunLi pushed a commit that referenced this pull request Mar 12, 2026
Enable loop ctrl with placeholder for SSA and backward ctrl move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ctrl to data flow transformation -- LLVM IR level

2 participants