Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCFToCalyx] Issue with lowering scf::parallel to Calyx when there is scf::if in it #8086

Open
jiahanxie353 opened this issue Jan 15, 2025 · 1 comment
Assignees
Labels
Calyx The Calyx dialect

Comments

@jiahanxie353
Copy link
Contributor

We lower scf::parallel in SCFToCalyx by assuming constant loop bounds and strides, which enables us to unroll the parallel loop manually: #7830

The issue is that, if scf::parallel contains scf::if, whose condition variable depends on some constants and scf::parallel's loop induction variables, those condition variables will be eventually evaluated to constant true/false automatically even if we never implement this sort of "canonicalization" pass manually in SCFToCalyx.cpp (I guess it's done by some underling MLIR mechanisms?).

For example, if we begin with:

func.func() {
    %c0 = arith.constant 0 : index
    scf.parallel (%arg1, %arg2) = (%c0, %c0) to (%c2, %c2) step (%c1, %c1) {
      %1 = arith.remsi %arg2, %c2 : index
      %2 = arith.cmpi slt, %1, %c0 : index
      %3 = scf.if %2 -> (f32) {
        scf.yield ...
      }
    }
}

will turn into:

func.func() {
    %c0 = arith.constant 0 : index
    scf.parallel (%arg1, %arg2) = (%c0, %c0) to (%c2, %c2) step (%c1, %c1) {
    ^bb0:
      %1 = arith.remsi "value 0", %c2 : index
      %2 = arith.cmpi slt, %1, %c0 : index
      %3 = scf.if %2 -> (f32) {
        scf.yield ...
      }
    ^bb1:
      %1 = arith.remsi "value 1", %c2 : index
      %2 = arith.cmpi slt, %1, %c0 : index
      %3 = scf.if %2 -> (f32) {
        scf.yield ...
      }
    ^bb2:
    ...
    }
}

And in each basic block:

%1 = arith.remsi "value 0", %c2 : index
%2 = arith.cmpi slt, %1, %c0 : index

will be folded to constants automatically, turning the scf::if op to:

%3 = scf.if true/false {
 ...
}

If they were to be lowered to Calyx, it'll become:

calyx.if 1'd1/1'd0 {
...
}

but it's not allowed by Calyx' grammar: https://github.com/calyxir/calyx/blob/2e18d555ff00339f46c99cc9d8b698ee27ce25ad/calyx-frontend/src/syntax.pest#L348

Any idea on how to tackle this? @rachitnigam @cgyurgyik @andrewb1999 @mikeurbach Thanks!

@jiahanxie353 jiahanxie353 self-assigned this Jan 15, 2025
@jiahanxie353 jiahanxie353 added the Calyx The Calyx dialect label Jan 15, 2025
@cgyurgyik
Copy link
Member

Can't we just simplify scf.if true { body } => body?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Calyx The Calyx dialect
Projects
None yet
Development

No branches or pull requests

2 participants