llvm-project
f34d0686 - [SROA] Refactor rewritePartition alloca type selection process (#167771)

Commit
3 days ago
[SROA] Refactor rewritePartition alloca type selection process (#167771) This PR does two things: 1. Refactor the rewritePartition type selection process to move the logic inside of a lambda. Previously the selection process would make use of a mutable `SliceTy`. Each phase of the type selection would do something like `if (!SliceTy) { // try to set sliceTy` }. But you also have `if (!SliceTy && <condition>)` and `if (!SliceTy || <condition>)`. I think this style makes the priority mechanism confusing. The new way I wrote the selection process is equivalent (except for the second contribution of this PR), and works by checking a condition, followed by returning the selected type right away. I think it makes the priority clearer. 2. What motivated the rewrite is that there are some cases with small aggregate allocas that have mixed type loads and stores that SROA fails to promote. For example, given: ``` %alloca = alloca [2 x half] store i32 42, ptr %alloca %val = load float, ptr %alloca ``` Previously, SROA would: - Find no common type between `i32` and `float` - Use `getTypePartition` which returns `[2 x half]` - Create a new `alloca [2 x half]` This PR adds an additional check: ``` if (LargestIntTy && DL.getTypeAllocSize(LargestIntTy).getFixedValue() >= P.size() && isIntegerWideningViable(P, LargestIntTy, DL)) return {LargestIntTy, true, nullptr}; ``` which allows the alloca above to get promoted as `i32`. The larger rewrite helps with this because this check is super specific. I only want it to apply when there is no common type and when all the loads and stores can be widened for promotion. I also want to apply it on the subtype returned from getTypePartition. The larger rewrite makes it very clear when this optimization occurs. I also don't want it to apply when vector promotion is possible. After this change, the example is optimized to: ``` %0 = bitcast i32 42 to float ret void ``` The alloca is completely eliminated.
Author
Parents
Loading