llvm / llvm/Polygeist

[WhileToFor] If not moved into after region

Open
#154 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
624
Forks
170
PR merge metrics
No merged PRs in 30d

Description

As an example:
```c
unsigned hash_thingy(unsigned v) {
for (int i = 0; i < 3; ++i) {
v = (v ^ 234567) ^ (v >> 16);
}
for (int i = 0; i < 3; ++i) {
v = (v ^ 12345) ^ (v >> 16);
}
return v;
}
```

lowers to:
```mlir
func @hash_thingy(%arg0: i32) -> i32 attributes {llvm.linkage = #llvm.linkage} {
%c0 = arith.constant 0 : index
%c0_i32 = arith.constant 0 : i32
%c3_i32 = arith.constant 3 : i32
%c234567_i32 = arith.constant 234567 : i32
%c16_i32 = arith.constant 16 : i32
%c1_i32 = arith.constant 1 : i32
%c3 = arith.constant 3 : index
%c1 = arith.constant 1 : index
%0:3 = scf.while (%arg1 = %c0_i32, %arg2 = %arg0) : (i32, i32) -> (i32, i32, i32) {
%2 = arith.cmpi slt, %arg1, %c3_i32 : i32
%3:3 = scf.if %2 -> (i32, i32, i32) {
%4 = arith.xori %arg2, %c234567_i32 : i32
%5 = arith.shrui %arg2, %c16_i32 : i32
%6 = arith.xori %4, %5 : i32
%7 = arith.addi %arg1, %c1_i32 : i32
%8 = llvm.mlir.undef : i32
scf.yield %7, %6, %8 : i32, i32, i32
} else {
scf.yield %arg1, %arg2, %arg2 : i32, i32, i32
}
scf.condition(%2) %3#0, %3#1, %3#2 : i32, i32, i32
} do {
^bb0(%arg1: i32, %arg2: i32, %arg3: i32): // no predecessors
scf.yield %arg1, %arg2 : i32, i32
}
%1:2 = scf.for %arg1 = %c0 to %c3 step %c1 iter_args(%arg2 = %0#2, %arg3 = %0#2) -> (i32, i32) {
%2 = arith.xori %arg2, %c234567_i32 : i32
%3 = arith.shrui %arg2, %c16_i32 : i32
%4 = arith.xori %2, %3 : i32
scf.yield %4, %4 : i32, i32
}
return %1#1 : i32
}
```

The second loop makes good sense, but the first one is a bit strange due to being lowered to a while loop as well as the existence of the `llvm.mlir.undef` operation.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the WhileToFor lowering but no source file or test. Reproduce the hash_thingy example and inspect the generated MLIR, then trace the first loop through the WhileToFor entry point. Done means an agreed lowering avoids the reported unnecessary scf.while and llvm.mlir.undef behavior, with a regression test if the project has one.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.