<div dir="ltr"><p>This RFC is to ask whether the community is interested in further discussion of iN bytes support. Last time the issue was on the agenda in May and the discussion was triggered by Jesper Antonsson's patches (see <a href="https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html"></a><a href="https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html">https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html</a>).</p>
<p>It seems that, while some downstream areas benefit from non-8-bit bytes support, this feature is barely maintainable given the lack of utilization targets in the upstream. The reason why I would like to again raise the matter is that we, the TON Labs team, would like to upstream our backend solution.</p>
<p>The backend generates code for TON virtual machine designed to run smart contracts in TON blockchain (see the original specifications for TVM and TON respectively at <a href="https://test.ton.org/tvm.pdf"></a><a href="https://test.ton.org/tvm.pdf">https://test.ton.org/tvm.pdf</a> and at <a href="https://test.ton.org/tblkch.pdf"></a><a href="https://test.ton.org/tblkch.pdf">https://test.ton.org/tblkch.pdf</a>).</p>
<p>The target has the following key particularities:</p>
<ul>
<li>stack-based virtual machine</li>
<li>257-bit wide integers, signed magnitude representation</li>
<li>no float point arithmetic support</li>
<li>persistent storage</li>
<li>no "native" memory; modeling is possible by costly</li>
<li>presence of custom types (it is exactly the reason for upstreaming)</li>
</ul>
<p>Given that the TVM only operates with 257 bits wide numbers, we changed LLVM in downstream to get a 257 bits byte. At the moment, we have a hacky implementation with a new byte size hardcoded. For a reference: the scope was to change approximately 20 files in LLVM and about a dozen in Clang. Later on, we plan to integrate the new byte size with data layout according to <a href="https://archive.fosdem.org/2017/schedule/event/llvm_16_bit/"></a><a href="https://archive.fosdem.org/2017/schedule/event/llvm_16_bit/">https://archive.fosdem.org/2017/schedule/event/llvm_16_bit/</a>. And if the community decides to move on, we will upstream and maintain it.</p>
<p>We realize that a 257 bits byte is quite unusual, but for smart contracts it is ok to have at least 256 bits numbers. The leading VM for smart contracts, Ethereum VM, introduced this practice and other blockchain VMs followed. Thus, while TVM might be the first LLVM-based target for blockchain that needs the feature, it is not necessarily the last one. We also found mentions of 12, 16 and 24 bits wide bytes in non-8-bits byte discussions in the past (in reverse chronological order: <a href="https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html"></a><span style="background-color:rgb(255,255,255)"><font color="#000000"><a href="https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html" style="">https://lists.llvm.org/pipermail/llvm-dev/2019-May/132080.html</a>, <a href="http://lists.llvm.org/pipermail/llvm-dev/2017-January/109335.html" target="_blank" rel="noopener noreferrer" style="cursor: pointer; overflow-wrap: break-word;">http://lists.llvm.org/pipermail/llvm-dev/2017-January/109335.html</a>, <a href="http://lists.llvm.org/pipermail/llvm-dev/2017-January/108901.html"></a><a href="http://lists.llvm.org/pipermail/llvm-dev/2017-January/108901.html" style="">http://lists.llvm.org/pipermail/llvm-dev/2017-January/108901.html</a>, <a href="http://lists.llvm.org/pipermail/llvm-dev/2015-March/083177.html"></a><a href="http://lists.llvm.org/pipermail/llvm-dev/2015-March/083177.html" style="">http://lists.llvm.org/pipermail/llvm-dev/2015-March/083177.html</a>, <a href="http://lists.llvm.org/pipermail/llvm-dev/2014-September/076543.html"></a><a href="http://lists.llvm.org/pipermail/llvm-dev/2014-September/076543.html" style="">http://lists.llvm.org/pipermail/llvm-dev/2014-September/076543.html</a>, <a href="http://lists.llvm.org/pipermail/llvm-dev/2009-September/026027.html" target="_blank" rel="noopener noreferrer" style="cursor: pointer; overflow-wrap: break-word;">http://lists.llvm.org/pipermail/llvm-dev/2009-September/026027.html</a></font></span>).</p>
<p>Our Toolchain is going to be based only on OSS. It allows using the backend without getting any proprietary software. Also, we hope that implementation for a target similar to TVM would help to generalize some concepts in LLVM and to make the whole framework better suit non-mainstream architectures.</p>
<p>Aside from non-i8 bytes, we would like to bring stack machine support in the Target Independent Code generator. The matter will be discussed at the developers' meeting, see <a href="http://llvm.org/devmtg/2019-10/talk-abstracts.html#bof2"></a><a href="http://llvm.org/devmtg/2019-10/talk-abstracts.html#bof2">http://llvm.org/devmtg/2019-10/talk-abstracts.html#bof2</a>.</p>
<p>LLVM and Clang for TVM are available at (<a href="https://github.com/tonlabs/TON-Compiler"></a><a href="https://github.com/tonlabs/TON-Compiler">https://github.com/tonlabs/TON-Compiler</a>). It is currently under LLVM 7 and it can only produce assembler; we have not specified our object file format yet). Moreover, we have introduced custom IR types to model Tuples, Slices, Builders, Cells from the specification. We are going to do an LLVM update and consider using opaque types before starting to upstream.<br><br>--<br>Kind regards, Dmitry Borisenkov</p></div>