<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57529>57529</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang AArch64TargetInfo does not initialize ABI correctly on Mac M1
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          elliottslaughter
      </td>
    </tr>
</table>

<pre>
    Clang is structured in a way that makes it hard to correctly initialize the target ABI for macOS M1 targets. Clang itself addresses the issue in `Driver::BuildCompilation`, where it sets a `-target-abi` flag if necessary to correctly initialize the ABI. However, clients of Clang that use the `CompilerInvocation::CreateFromArgs` entry point (or other, lower-level entry points) have no good way to get access to this setting, without either parsing the output of `Driver::BuildCompilation` or hard-coding it themselves. Such clients include LLDB and Terra (https://terralang.org/).

See https://github.com/terralang/terra/issues/605 for an example of how this has caused miscompilations in Terra.

## Root Cause Analysis

Clang's AArch64TargetInfo initializes an ABI of `aapcs` by default:

https://github.com/llvm/llvm-project/blob/e05edb19adbfd1b24f58d583e4b5b4d742f982ee/clang/lib/Basic/Targets/AArch64.cpp#L66

You can scan the rest of the file for `darwinpcs`. You won't find it. Despite having (apparent) customizations for Darwin, this file does not know how to properly initialize the ABI for Darwin AArch64 targets.

To avoid miscompilations (especially with varargs), a value of `darwinpcs` is required. Clang sets this value in `RenderAArch64ABI`, which is called transitively by `Driver::BuildCompilation`.

https://github.com/llvm/llvm-project/blob/e05edb19adbfd1b24f58d583e4b5b4d742f982ee/clang/lib/Driver/ToolChains/Clang.cpp#L1842-L1854

As you can see in the code above, this value is not stored anywhere in the `ToolChain`. It is only stored in `CmdArgs`, which makes its way (eventually) into the arguments for a job constructed by `Driver::BuildCompilation`. Therefore, clients that use the `CompilerInvocation::CreateFromArgs` entry point do not take advantage of any of this logic, and have to find a way to pass the `-target-abi` flag themselves.

## Impact

Clang itself works fine because it initializes the `-target-abi` flag via `Driver::BuildCompilation`.

However, clients of Clang that use `CompilerInvocation::CreateFromArgs` (or other, lower-level entry points) have no reasonable way to access this flag. The best that can be done at the moment is to run `Driver::BuildCompilation`, find the `-cc1` job, and look through the flags for `-target-abi`. There is not even a helper method on `Compilation` or `ToolChain` to help fish out this information based on what `Driver::BuildCompilation` returns.

## Reproduction

Because it requires a client of Clang other than Clang itself, the only way to reproduce is to build a Clang client. Terra as of commit [f46c5cabd13632417d382e27d35b91f8eac923a4](https://github.com/terralang/terra/commit/f46c5cabd13632417d382e27d35b91f8eac923a4) is sufficient for this purpose, and works with LLVM 14. A minimal reproducer can be found at https://github.com/terralang/terra/issues/605, along with stack traces and code analysis.

It is likely (though I have not tested this) that writing varargs code in LLDB on macOS M1 would trigger the same issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFV01v4zYQ_TX2hYhhy1_xwQfHwaIBsiiwGxTokaJGNjcUqZKUXffX9w0lOcpusJttCxRxLFkSZ4Zv3rwZ5a64bPdG2oPQQYToGxUbT4XQVkhxlhcRjzKKSj5TEDqKo_SFiE4o5z2paC54UEctjf6L8Cj-pT9QFLu7B1E6j4Xq18_i46y7HiaicxYDmVLIovAUAmzzWh1CQ-x5tJree30iP5rv8LlrtCn2rqq1kVE7i9ujbC_OR_LEQQUYRrS4fNO6uZG5xi9RGglXpbCk4EX6y3dDR8wT8Ys7EzuGfWU0WVh2ZRd0gqIJ7dOw34ZE_sGenGojS_HuPclIH7yrdv4QOBDYgfPaaRvFKLsFMA42khcDh_7GwKkZPhZG2QZon0hYJw7OFW0ycA50peL98K945LRRjNoeEiY6Hl0TBWm2L2rpg06Rk8DlGnewmx_DKxAhp_pGuUKndLGJCjk7EXL4uVHHKzzaKtMUJB4f7--EtIV4Iu8lb_MYYx3YR_YBn8iXGceJ84d0aTMZTe9H0137_ZlIvF5xwCaafKJcNVzen-OYCAOkPqymy0Q3aQX9KavaEO_z6M4tQEcZhJLIXC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