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

Fix duplicate name when lowering scf.if #30

Merged
merged 31 commits into from
Dec 10, 2024
Merged

Conversation

rikhuijzer
Copy link
Owner

@rikhuijzer rikhuijzer commented Dec 6, 2024

Fixes a bug where variables are not automatically renamed when lowered from inside the region. This, for example, comes up when lowering

func.func @main() -> i64 {
  %x = arith.constant false
  scf.if %x {
    %0 = arith.constant 2 : i64
  } else {
    %0 = arith.constant 3 : i64
  }
  %1 = arith.constant 0 : i64
  return %1 : i64
}

to

func.func @main() -> i64 {
  %0 = arith.constant false
  cf.cond_br %0, ^bb1, ^bb2
^bb1: 
  %1 = arith.constant 2 : i64
  cf.br ^bb3
^bb2:
  %2 = arith.constant 3 : i64
  cf.br ^bb3
^bb3:
  %3 = arith.constant 0 : i64
  return %3 : i64
}

Notice that the second definition of %0 has to be renamed.

Now it makes sense why MLIR always renames variables. If you don't, any variable that is inlined and has a name that is the same as another variable needs to have a new name. But say you have %0, %1 and now you inline another %0. What name would you give it? %0_0? You cannot just call it %2 because, if I'm not mistaken, LLVM will then start to complain (fair enough) because %2 occurs before %1. Just ignoring names and giving everything a new ("fresh") name leads to much cleaner generated code.

@rikhuijzer rikhuijzer changed the title Fix unique with blocks Fix duplicate name when lowering scf.if Dec 7, 2024
@rikhuijzer rikhuijzer merged commit d87c4c0 into main Dec 10, 2024
7 checks passed
@rikhuijzer rikhuijzer deleted the rh/fix-unique-with-blocks branch December 10, 2024 15:24
rikhuijzer added a commit that referenced this pull request Dec 19, 2024
Now that variable names are less important (#30), the lowering can be
simplified.
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