<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - [LTO] Private symbol order reversed during IR linking with resolution-based API"
   href="https://bugs.llvm.org/show_bug.cgi?id=32441">32441</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LTO] Private symbol order reversed during IR linking with resolution-based API
          </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>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Object
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tobias@codeaurora.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=18183" name="attach_18183" title="Test case">attachment 18183</a> <a href="attachment.cgi?id=18183&action=edit" title="Test case">[details]</a></span>
Test case

I'm seeing an issue with customer code that relies on the order of private
symbols (strings), used as initializers for global variables, being preserved
during LTO. The old LTO API does this, the resolution-based API reverses them.
I'm not sure what the C standard says about this, but it just seems arbitrary
to do this.

I've tracked down the root of the issue to ModuleSymbolTable::addModule, so
Peter I'd appreciate your comments on this.

In the following, I'm referring to the trivial test case attached to this bug.
It defines two globals, bar1 and bar2, which are initialized with strings (.str
and .str.1). There is a function foo which uses them in the order bar1 and then
bar2 (this is important).

Let's first run this through the old API:
llvm-lto privateorder.bc -exported-symbol=bar1 -exported-symbol=bar2
-exported-symbol=foo -save-merged-module -o privateorder.o

In ModuleLinker::run(), the 'kept' globals are added in the order
variables-functions-aliases (here: bar1, bar2, foo). The IRMover materializes
them in this order from its worklist, and all is good. The merged module
(privateorder.o.merged.bc) has @.str and @.str1 in the correct order. 

Now if I run this through the new API things are different in
privateorder.o.0.0.preopt.bc:
llvm-lto2 privateorder.bc -r privateorder.bc,bar1,px -r privateorder.bc,bar2,px
-r privateorder.bc,foo,px -r privateorder.bc,puts, -o privateorder.o
-save-temps

ModuleSymbolTable::addModule() builds the list of symbols in the module by
iterating over them in the order of functions-variables-aliases, which in this
case leads to the Keep list being [foo,bar1,bar2] in LTO::addModule. The
IRMover now materializes foo first, which triggers its dependences bar1 and
bar2 to be scheduled for materialization by the Mapper, which in turn triggers
the strings (which will be processed in reverse by the Mapper). When the
IRMover reaches bar1 and bar2 in its worklist, they and their initializers are
already materialized.

Now, the easy fix appears to be to change the order in
ModuleSymbolTable::addModule() from functions-variables-aliases to
variables-functions-aliases. I then see the exact same materialization order as
with the old API.

My question for you all is:
Would this be safe/correct? Was there a particular reason why a different
iteration order was chosen for the ModuleSymbolTable? I feel that the
convention across LLVM tends to be variables then functions, rather than the
other way around.

Thanks,
Tobias</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>