<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">I’ve actually got an implementation of this as an arbitrary precision integer extension that I’ve written up an RFC for (but not submitted).  Below is my copy/pasted
 RFC (again, not reviewed, but I DO have an implementation of it that I need to prepare for review).  I suspect my implementation will do what you need out of it.  Its actually more significant than just adding a normal attribute.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><o:p> </o:p></b></p>
<p class="MsoNormal"><b>Introduction<o:p></o:p></b></p>
<p class="MsoNormal">As we all know, LLVM-IR supports arbitrary precision integers via the iN syntax.  Integers that aren’t powers-of-two aren’t particularly interesting/useful on most hardware however, so this functionality isn’t typically used, and in fact,
 Clang does not have such support.  However, certain architectures DO exist where the ability to express these values (and do math without promotion to full integers) is valuable from a performance perspective, since these values can be expressed more efficiently
 in hardware.  <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’ve developed a solution to expose these types in clang, and am proposing to contribute it back to the Clang community if it is sufficiently acceptable to the group.  As rebasing/refactoring for the open-source version of clang is a significant
 effort, I’d like to get feedback on the concept and approach before putting in this effort.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b>Syntax</b><o:p></o:p></p>
<p class="MsoNormal">The syntax I’d chosen for this was as a Typedef Attribute.  This permits the consumer to define various ways to expose this functionality in their code.  Additionally, we’ve limited it to int/unsigned typedefs only for simplicity’s sake.
 The code looks something like:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">// Typical way to expose this in C:<o:p></o:p></p>
<p class="MsoNormal">typedef int __attribute__((__ap_int(3))) ap_int3;<o:p></o:p></p>
<p class="MsoNormal">typedef unsigned __attribute__((__ap_int(3))) ap_uint3;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">// Better way to expose it in modern C++:<o:p></o:p></p>
<p class="MsoNormal">template<unsigned bits> using ap_int = int __attribute__((__ap_int(bits)));<o:p></o:p></p>
<p class="MsoNormal">template<unsigned bits> using ap_uint = unsigned __attribute__((__ap_int(bits)));<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">For our usages, we just wrapped these in a type that would better express the further configurable semantics, but I suspect these are useful in their own right.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b>Conversions/Promotions<o:p></o:p></b></p>
<p class="MsoNormal">We consider conversions to/from integers integral promotions that follow normal conversion rules, and bool conversions are also supported (just like integers).   AP-Int math is done at the size of the largest operand in order to be consistent
 with C rules as best as possible.  Conversions to/from integers and literals happen consistently with the other integer types.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b>Size/Align<o:p></o:p></b></p>
<p class="MsoNormal">One important consideration that was made is how size/alignment is expressed in the language.  Sizeof, for example works exclusively on bytes, so an ap_int<13> couldn’t be directly expressed.  Alignments are required to be a power-of-two,
 otherwise they are not expressable on some hardware.  In implementation, we chose to use the next largest alignment (up to 64 bits).  Additionally, for the sake of the standard library (and common array patterns), we were required to make sizeof also round
 up to the next power of two.  This is most consistent with what a majority of LLVM backends will do (for example, an ap_int<24> will be expressed as a int-32 on most platforms), and was required to make common array patterns work.  Unfortuantely, this results
 in arrays on these platforms having ‘padding’ bits, but there isn’t really a way around that.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, is this something that the clang community is interested in?  I’ll of course expect extensive review through phabricator once it is ready, but I’d like to gauge interest (as well as determine if there is any “over my dead body” comments)
 before putting in the significant additional effort.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></a></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><a name="_____replyseparator"></a><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Tingyuan LIANG via llvm-dev<br>
<b>Sent:</b> Sunday, March 3, 2019 4:48 AM<br>
<b>To:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> [llvm-dev] Add Bitwidth Attribute in Clang without Modification in Source Code of Clang<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">Hi all,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    I am handling some arbitrary precision integers (e.g. 13-bit) in the source code with Clang. Temporary, I declare them as structs like:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">              struct APINT13 VarA;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    and during parsing the AST and generating IR, I transform the type of these variables into arbitrary precision integers.
<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    I read some other documentations and find that adding attributes for variables could be a nicer way to handle arbitrary precision integer.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    However, as default, there is no such attribute as "bitwidth" in Clang. I found the following link and get some hints but the instructions listed in the website require me to
 modify the source code of Clang ("Attr.td", "include/clang/Sema/ParsedAttr.h" and "utils/TableGen/ClangAttrEmitter.cpp").<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">   
<a href="http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute" id="LPlnk337070">
http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-attribute</a><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    I just wonder whether there is any way to add attributes like plugins without touching the source code of Clang, because to rebuild Clang could be time-consuming. As I know,
 LLVM passes can work as plugins and I guess there could be the similar way to handle attribute addition with Clang.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">    Thanks in advance for your time and suggestion! \^_^/<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div id="signature">
<div id="divtagdefaultwrapper">
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">Best regards,
<o:p></o:p></span></p>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">------------------------------------------<o:p></o:p></span></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">Tingyuan LIANG<o:p></o:p></span></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">MPhil Student<o:p></o:p></span></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">Department of Electronic and Computer Engineering<o:p></o:p></span></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Calibri",sans-serif;color:black">The Hong Kong University of Science and Technology<o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>