<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'>Hi Bill,<br><br>I highly recommend that you use only vectors of elements which have a size which is a whole number of bytes. There are known issues with how we handle the more-general cases, see:<br><br>https://llvm.org/bugs/show_bug.cgi?id=1784<br>https://llvm.org/bugs/show_bug.cgi?id=22603<br>https://llvm.org/bugs/show_bug.cgi?id=27600<br><br>In short, different parts of the compiler disagree on whether <8 x i1> is one or eight bytes long, and some parts do nonsensical things altogether. There are a limited subset of cases where the current infrastructure works well (mostly for handling vectors of i1 for vectorized comparisons), but if you stray too far you'll run into problems. That having been said, we would like to fix these things, and so if you find problems, please do file bug reports about them.<br><br> -Hal <br><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Ginger Bill via llvm-dev" <llvm-dev@lists.llvm.org><br><b>To: </b>llvm-dev@lists.llvm.org<br><b>Sent: </b>Monday, August 15, 2016 3:54:58 AM<br><b>Subject: </b>[llvm-dev] [LLVM IR] Possible compiler bug: <N x i1> vector instructions ordering causing different results<br><br>

  
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">I have been using LLVM as a backend for my compiler (I'm
      not using the LLVM libraries but my own to generate the necessary
      IR for numerous reasons). At the moment, I am implementing vector
      operations. Comparisons of vectors emit vectors of booleans<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);"><N x i1></code><span class="Apple-converted-space"> </span>and these are causing me
      problems.</p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">To access vector elements, I have been using<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">extractelement</code><span class="Apple-converted-space"> </span>and<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">insertelement</code><span class="Apple-converted-space"> </span>however, I am getting some
      weird behaviour when I execute these instructions in a different
      orders. The code examples below have the same instructions and
      should be logically the same. Version 1 outputs<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">BAA</code>while
      Version 2 outputs<span class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">BAB</code>.
      Version 2 is the logically correct version but I cannot figure out
      why version 1 outputs the wrong version but has the exact same
      instructions, just in a different order. <br>
    </p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">I'm suspecting this may be a compiler bug rather than a
      code error. It may be due to the way vectors structured. I could
      not find any documentation on what the size of a vector is but it
      seems that vector elements get packed. <code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);"><N x iM></code>
      size == (N*M+7)/8 bytes. This also suggests why the FAQ suggests
      not to use <span class="Apple-converted-space"></span><code style="margin: 0px; padding: 1px 5px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">getelementptr</code><span class="Apple-converted-space"> on vectors as the elements may
        not be byte aligned.<br>
      </span></p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">As a workaround, is there a way to make sure a boolean
      vector isn't packed where each element takes up a byte, or convert
      a vector of booleans to either a vector of i8, or extra
      instructions to prevent this from happening?<br>
    </p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">Regards,</p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">Bill</p>
    <p style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">--------<br>
    </p>
    <h2 style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 19px; line-height: 1.3; word-wrap: break-word; font-weight: 400; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">Version 1</h2>
    <pre style="margin: 0px 0px 1em; padding: 5px; border: 0px none; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; word-wrap: normal; color: rgb(36, 39, 41); font-style: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; background-color: rgb(239, 240, 241);"><code style="margin: 0px; padding: 0px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);">; Version 1 - Generated by my naïve SSA generator<br>; Outputs: BAA (incorrect)<br>declare i32 @putchar(i32)<br><br>define void @main() {<br>entry:<br>    %0 = alloca <8 x i1>, align 8 ; v<br>    store <8 x i1> zeroinitializer, <8 x i1>* %0<br>    %1 = alloca <8 x i1>, align 8<br>    store <8 x i1> zeroinitializer, <8 x i1>* %1<br>    %2 = load <8 x i1>, <8 x i1>* %1, align 8<br>    %3 = insertelement <8 x i1> %2, i1 true, i64 0<br>    %4 = insertelement <8 x i1> %3, i1 false, i64 1<br>    %5 = insertelement <8 x i1> %4, i1 true, i64 2<br>    %6 = insertelement <8 x i1> %5, i1 false, i64 3<br>    %7 = insertelement <8 x i1> %6, i1 true, i64 4<br>    %8 = insertelement <8 x i1> %7, i1 false, i64 5<br>    %9 = insertelement <8 x i1> %8, i1 true, i64 6<br>    %10 = insertelement <8 x i1> %9, i1 false, i64 7<br>    store <8 x i1> %10, <8 x i1>* %0<br><br>    %11 = load <8 x i1>, <8 x i1>* %0, align 8<br>    %12 = extractelement <8 x i1> %11, i64 0<br>    %13 = zext i1 %12 to i32<br>    %14 = add i32 %13, 65 ; + 'A'<br>    %15 = call i32 @putchar(i32 %14)<br><br>    %16 = load <8 x i1>, <8 x i1>* %0, align 8<br>    %17 = extractelement <8 x i1> %16, i64 1<br>    %18 = zext i1 %17 to i32<br>    %19 = add i32 %18, 65 ; + 'A'<br>    %20 = call i32 @putchar(i32 %19)<br><br>    %21 = load <8 x i1>, <8 x i1>* %0, align 8<br>    %22 = extractelement <8 x i1> %21, i64 2<br>    %23 = zext i1 %22 to i32<br>    %24 = add i32 %23, 65 ; + 'A'<br>    %25 = call i32 @putchar(i32 %24)<br><br>    %26 = call i32 @putchar(i32 10) ; \n<br><br>    ret void<br>}<br></code></pre>
    <hr style="border: 0px none; color: rgb(214, 217, 220); height: 1px; margin-bottom: 20px; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(214, 217, 220);">
    <h2 style="margin: 0px 0px 1em; padding: 0px; border: 0px none; font-size: 19px; line-height: 1.3; word-wrap: break-word; font-weight: 400; color: rgb(36, 39, 41); font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; font-style: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: rgb(255, 255, 255);">Version 2</h2>
    <pre style="margin: 0px 0px 1em; padding: 5px; border: 0px none; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; word-wrap: normal; color: rgb(36, 39, 41); font-style: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; background-color: rgb(239, 240, 241);"><code style="margin: 0px; padding: 0px; border: 0px none; font-size: 13px; font-family: Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);">; Version 2 - Manually modified version of Version 1<br>; Outputs: BAB (correct)<br>declare i32 @putchar(i32)<br><br>define void @main() {<br>entry:<br>    %0 = alloca <8 x i1>, align 8 ; v<br>    store <8 x i1> zeroinitializer, <8 x i1>* %0<br>    %1 = alloca <8 x i1>, align 8<br>    store <8 x i1> zeroinitializer, <8 x i1>* %1<br>    %2 = load <8 x i1>, <8 x i1>* %1, align 8<br>    %3 = insertelement <8 x i1> %2, i1 true, i64 0<br>    %4 = insertelement <8 x i1> %3, i1 false, i64 1<br>    %5 = insertelement <8 x i1> %4, i1 true, i64 2<br>    %6 = insertelement <8 x i1> %5, i1 false, i64 3<br>    %7 = insertelement <8 x i1> %6, i1 true, i64 4<br>    %8 = insertelement <8 x i1> %7, i1 false, i64 5<br>    %9 = insertelement <8 x i1> %8, i1 true, i64 6<br>    %10 = insertelement <8 x i1> %9, i1 false, i64 7<br>    store <8 x i1> %10, <8 x i1>* %0<br><br>    %11 = load <8 x i1>, <8 x i1>* %0, align 8<br>    %12 = load <8 x i1>, <8 x i1>* %0, align 8<br>    %13 = load <8 x i1>, <8 x i1>* %0, align 8<br><br>    %14 = extractelement <8 x i1> %11, i64 0<br>    %15 = extractelement <8 x i1> %12, i64 1<br>    %16 = extractelement <8 x i1> %13, i64 2<br><br>    %17 = zext i1 %14 to i32<br>    %18 = zext i1 %15 to i32<br>    %19 = zext i1 %16 to i32<br><br>    %20 = add i32 %17, 65 ; + 'A'<br>    %21 = add i32 %18, 65 ; + 'A'<br>    %22 = add i32 %19, 65 ; + 'A'<br><br>    %23 = call i32 @putchar(i32 %20)<br>    %24 = call i32 @putchar(i32 %21)<br>    %25 = call i32 @putchar(i32 %22)<br><br>    %26 = call i32 @putchar(i32 10) ; \n<br><br>    ret void<br>}<br></code></pre>
  <br>_______________________________________________<br>LLVM Developers mailing list<br>llvm-dev@lists.llvm.org<br>http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br></blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Assistant Computational Scientist<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>