[llvm-bugs] [Bug 51807] New: SROA does not split alloca into more fine-graied allocas iff index into it is variable

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 9 06:01:56 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51807

            Bug ID: 51807
           Summary: SROA does not split alloca into more fine-graied
                    allocas iff index into it is variable
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: lebedev.ri at gmail.com
                CC: llvm-bugs at lists.llvm.org

Let's suppose we have the following:
```
struct Outer {
    int pre[4];
    int array[4];
    int post[4];
};

int src(int value, int idx0, int idx1) {
    Outer storage;
    storage.array[idx0] = value;
    return storage.array[idx1];
}
```
So only the `Outer::array` is only ever used.
You'd think we'd be able to chop up the `storage` allocation,
and only allocate the `array` itself.

Alive doesn't like that because iff idx1 is not in [0, 4), 
we'll now have poison instead of reading from `Outer::post[0]`.
But it seems like alive2 doesn't complain iff we make the index in-bounds:

int tgt(int value, int idx0, int idx1) {
    int array[4];
    array[idx0] = value;
    return array[idx1 & 3];
}



$ ./alive-tv /tmp/test.ll --smt-to=60000

----------------------------------------
define i32 @src(i32 %value, i32 %idx0, i32 %idx1) noread nowrite nofree
willreturn {
%entry:
  %storage = alloca i64 48, align 4, dead
  %0 = bitcast * %storage to *
  start_lifetime * %0
  %idxprom = sext i32 %idx0 to i64
  %arrayidx = gep inbounds * %storage, 48 x i64 0, 1 x i64 16, 4 x i64 %idxprom
  store i32 %value, * %arrayidx, align 4
  %idxprom2 = sext i32 %idx1 to i64
  %arrayidx3 = gep inbounds * %storage, 48 x i64 0, 1 x i64 16, 4 x i64
%idxprom2
  %1 = load i32, * %arrayidx3, align 4
  free * %0 unconstrained
  ret i32 %1
}
=>
define i32 @tgt(i32 %value, i32 %idx0, i32 %idx1) noread nowrite nofree
willreturn {
%entry:
  %array = alloca i64 16, align 16, dead
  %0 = bitcast * %array to *
  start_lifetime * %0
  %idxprom = sext i32 %idx0 to i64
  %arrayidx = gep inbounds * %array, 16 x i64 0, 4 x i64 %idxprom
  store i32 %value, * %arrayidx, align 4
  %and = and i32 %idx1, 3
  %1 = zext i32 %and to i64
  %arrayidx2 = gep inbounds * %array, 16 x i64 0, 4 x i64 %1
  %2 = load i32, * %arrayidx2, align 4
  free * %0 unconstrained
  ret i32 %2
}
ERROR: Timeout


Can we do this, or is this also illegal?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210909/80b30d9a/attachment.html>


More information about the llvm-bugs mailing list