<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 - LLD is doing undesirable identical code folding when invoked with -Wl,--icf=safe"
   href="https://bugs.llvm.org/show_bug.cgi?id=47533">47533</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLD is doing undesirable identical code folding when invoked with -Wl,--icf=safe
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </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>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arpit07531@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, smithp352@googlemail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Please find the example source code where this issue is reproducible here - 
<a href="https://github.com/arpit07531/llvm-lld-icf-bug/tree/master/helloWorldNative2">https://github.com/arpit07531/llvm-lld-icf-bug/tree/master/helloWorldNative2</a>


I basically have the following template method. I invoke this method for
various exception types to determine if the input exception_ptr is of the same
type. Please note that here exception types are not inheriting std::exception
class.


    template<class T>
    __attribute__((noinline))
    bool IsError(const std::exception_ptr &err) noexcept
    {
        if (!err)
        {
            return false;
        }

        try
        {
            std::rethrow_exception(err);
        }
        catch (const T &)
        {
            return true;
        }
        catch (...)
        {
            return false;
        }
    }

Example Usage of this method :
---------------------------------
bool result1 =
CustomException::IsError<CustomException::Exception_Type1>(std::make_exception_ptr(CustomException::Exception_Type1{0x01}));
bool result2 =
CustomException::IsError<CustomException::Exception_Type2>(std::make_exception_ptr(CustomException::Exception_Type2{0x02}));
bool result3 =
CustomException::IsError<CustomException::Exception_Type3>(std::make_exception_ptr(CustomException::Exception_Type3{0x03}));

Expected Behavior
--------------------------------
result1, result2 and result3 should all return true.

Actual Behavior
--------------------------------
result 1 is true,
result 2 is false.
result 3 is false. 

Issue here
--------------------------------
Please refer to the nm output below. Linker is treating IsError implementations
for different types as Identical code, and retaining implementations for only
one of the template types and folding the rest.
A single implementation can't cater to all template types in this case, as
IsError method will alwasy return false for the template types for which
IsError implementation is folded by the linker, because the flow will always
reach catch(...) for them.

nm -C -a
"D:\offpkg\ASProjects\helloWorldNative2\app\build\intermediates\merged_native_libs\debug\out\lib\armeabi-v7a\libnative-lib.so"
|grep IsError
00015629 t bool
CustomException::IsError<CustomException::Exception_Type1>(std::exception_ptr
const&)
00015629 t bool
CustomException::IsError<CustomException::Exception_Type2>(std::exception_ptr
const&)
00015629 t bool
CustomException::IsError<CustomException::Exception_Type3>(std::exception_ptr
const&)


How to repro this issue:
-------------------------
- Explicitly mark the IsError method as no-inline.
- Compiler flags - -ffunction-sections -fdata-sections -Os -flto=thin.
- Linker flags - -Wl,--gc-sections -flto=thin -Wl,--icf-safe.
- LLD versions - issue is reproducible on all lld versions.</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>