<div dir="ltr">


















<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>Hi,<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>Recently we are trying to
implement AIX pragma align/pack stack effect, but noticed current
implementation model in Clang has some limitations. <span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>That results from mixing
`#pragma pack` and `#pragma align` by using only </span><span lang="EN-US">an unsigned number</span><span lang="EN-US"> </span><span>to
represent both so that </span><span lang="EN-US">they could override each other.</span><span><span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>Some examples we have been
aware of are:<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>1. The current diagnostic for
pragma pack does not produce a valid diagnostic when align pragma is involved.
For example,<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">$ cat test_diag.c<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">#pragma align =
natural <span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">$ clang
test_diag.c -S <span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">test_diag.c:1:9:
warning: unterminated '#pragma pack (push, ...)' at end of file [-Wpragma-pack]<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">#pragma align =
natural <span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span>        </span>^<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">test_diag.c:1:9:
note: did you intend to use '#pragma pack (pop)' instead of '#pragma pack()'?<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">1 warning
generated.<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="background:rgb(217,217,217) none repeat scroll 0% 0%"><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>The diagnostic is misleading
by indicating some `<i><span style="color:black;background:rgb(217,217,217) none repeat scroll 0% 0%">'#pragma pack </span></i>`
is unterminated. This is caused by not differentiating between align pragma and
pack pragma in clang’s current align and pack stack model.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>2<b>.</b>There exists<b> </b>platforms
like AIX that cannot have pragma align/pack stack effect simply represented by an
alignment value. The stack effect between align and pack pragmas on AIX is different.
<span></span></span></font></p>

<pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif"><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif">(1)They don’t override but create combinations with each other sometimes.<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif"><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif">Eg.<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>#pragma align(natural)<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>#pragma pack(2)<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif"><span> </span></span></font></pre>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span lang="EN-US">In current Clang’s implementation we get pack(2) in effect here without
knowing align mode information above it;<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span lang="EN-US"><span> </span><span></span></span></font></p>

<pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif" lang="EN-US">However, </span><span style="font-family:"Georgia",serif">XL on AIX would need to align objects to natural alignment first and then reduced by pack(2).<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif"><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif">(2)Additional complication comes in with `#pragma align(reset)` and `#pragma pack(pop)`.<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif"><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif">Eg.<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>#pragma pack(2)<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>#pragma align = packed<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>#pragma pack(pop)<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif" lang="EN-US">In current Clang’s implementation we get pack(2) in effect here.<span></span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span><span> </span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span style="font-family:"Georgia",serif">However, for XL on AIX, `#pragma pack(pop)` won’t be able to pop the stack, because `#pragma align` sets a baseline, and there is no pragma pack applied to it. Without distinguishing pragma align and pramgma pack, the `</span><span>#pragma align = packed</span><span style="font-family:"Georgia",serif">` would be popped out mistakenly for AIX.</span><span><span></span></span></font></pre>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><b><span>3.</span></b><span> Features added in later like PCH only record pragma
alignment number. In platforms like AIX, baseline align mode information will
be lost.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<div><br></div><div><br></div><p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>Based on current limitations,
we are proposing a new model by splitting up the alignment value into an </span><span lang="EN-US">align mode and a
pack number so that we would be able to know what is the combined effect of
align and pack in our stack.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>The proposed patch has been posted
here as a reference:<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><a href="https://reviews.llvm.org/D87702" style="color:blue;text-decoration:underline"><span>https://reviews.llvm.org/D87702</span></a><span> <span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>This patch implements the
proposed new model,<span>  </span>implements AIX
pragma align/stack effect based on that, and updates pragma align/pack related
PCH handling process.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>But there are several things
I would like to point out particularly:<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>(1) In `clang/include/clang/Sema/Sema.h`,<span></span></span></font></p>

<pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>    </span>bool operator==(const AlignPackInfo &Info) const {<span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>      </span>return std::tie(AlignMode, PackNumber, PackAttr, AIXStack) ==<span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>   </span><span>          </span>std::tie(Info.AlignMode, Info.PackNumber, Info.PackAttr,<span></span></font></pre><pre style="margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span>                      </span>Info.AIXStack);<span></span></font></pre><pre style="text-indent:24pt;margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2">}<span></span></font></pre><pre style="text-indent:24pt;margin:0cm 0cm 0.0001pt;font-family:"Courier New""><font size="2"><span> </span></font></pre>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>We are trying to pursue a
stricter “equal” definition of if two pragma are equal. That would change some
previous behavior, for example:<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">a) <span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">#pragma
align(packed)<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">#pragma
pack(1) // AlignMode = Packed,PackNumber = 1,IsPackAttr<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">b)<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New"">#pragma
align(packed) //AlignMode = Packed,PackNumber = 1, <span> </span><span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span>                        </span>IsAlignAttr<span></span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><i><span style="font-family:"Courier New""><span> </span></span></i></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span style="background:rgb(217,217,217) none repeat scroll 0% 0%"><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>Since we only care about
unsigned numbers previously, these two pragma settings would be treated as equal.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>But now, they would not. So
on platforms where the previous result is required, we would need to compare
the `PackNumber` of `AlignPackInfo` objects instead of comparing themselves
directly.<span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span>(2) In the first diagnostic
limitation mentioned above, we handle it by leaving a FIXME in the current patch in
order to avoid scope creep. But we believe the new `AlignPackInfo` struct
provides a good foundation to address this issue in the future. <span></span></span></font></p>

<p class="MsoNormal" style="margin:0cm;font-family:"Georgia",serif"><font size="2"><span><span> </span></span></font></p>

<p class="MsoNormal" style="text-align:justify;margin:0cm;font-family:"Georgia",serif"><font size="2"><span lang="EN-US">Please let me
know if there are any questions about our current proposal and design. Your
feedback is appreciated.</span><span><span></span></span></font></p>

<p class="MsoNormal" style="text-align:justify;margin:0cm;font-family:"Georgia",serif"><font size="2"><span lang="EN-US"> </span><span><span></span></span></font></p>

<p class="MsoNormal" style="text-align:justify;margin:0cm;font-family:"Georgia",serif"><font size="2"><span lang="EN-US">Regards,</span><span><span></span></span></font></p><p class="MsoNormal" style="text-align:justify;margin:0cm;font-family:"Georgia",serif"><font size="2"><span style="color:rgb(136,136,136)" lang="EN-US">Xiangling
Liao</span><span style="color:rgb(136,136,136)"><span></span></span></font></p>





</div>