<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Wrong address space in load pre-splitting in SROA"
   href="https://llvm.org/bugs/show_bug.cgi?id=26154">26154</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong address space in load pre-splitting in SROA
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>escha@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This is a copy-paste of an internal bug report; I might be able to conjure up a
test case if necessary (it's GPU code with multiple address spaces), but the
bug looks pretty straightforward. The reporter supplied a patch. The context is
a load from addrspace(1) which is then stored to the stack, which SROA
mishandles.

Report:

The problem appears to be that when pre-splitting the load, SROA seems to be
using the address space of the store instead of the address space of the load.
The function is SROA::presplitLoadsAndStores and the code is:

      auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
      auto *PartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace());
      …
        PLoad = IRB.CreateAlignedLoad(
            getAdjustedPtr(IRB, DL, LoadBasePtr,
                           APInt(DL.getPointerSizeInBits(), PartOffset),
                           PartPtrTy, LoadBasePtr->getName() + “.”),

diff --git a/lib/Transforms/Scalar/SROA.cpp b/lib/Transforms/Scalar/SROA.cpp
index 9c69b6c..48c907f 100644
--- a/lib/Transforms/Scalar/SROA.cpp
+++ b/lib/Transforms/Scalar/SROA.cpp
@@ -3919,7 +3919,8 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI,
AllocaSlices &AS) {
         PLoad = IRB.CreateAlignedLoad(
             getAdjustedPtr(IRB, DL, LoadBasePtr,
                            APInt(DL.getPointerSizeInBits(), PartOffset),
-                           PartPtrTy, LoadBasePtr->getName() + "."),
+                           PartTy->getPointerTo(LI->getPointerAddressSpace()),
+                           LoadBasePtr->getName() + "."),
             getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false,
             LI->getName());
       }</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>