[llvm-bugs] [Bug 38019] New: WebAssembly: Inverting i1 at O0 produces invalid code

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jul 2 09:10:37 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38019

            Bug ID: 38019
           Summary: WebAssembly: Inverting i1 at O0 produces invalid code
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: WebAssembly
          Assignee: unassignedbugs at nondot.org
          Reporter: alex at crichton.co
                CC: dan433584 at gmail.com, llvm-bugs at lists.llvm.org

Created attachment 20507
  --> https://bugs.llvm.org/attachment.cgi?id=20507&action=edit
IR

First discovered as an innocuous TODO [1] it looks like LLVM's code generation
for i1 may not be valid for WebAssembly when compiling at O0. Starting with
Rust code that looks like:


extern {
    fn return_one() -> u32;
    fn panic() -> !;
}

#[no_mangle]
pub unsafe extern fn test() {
    let tmp: bool = !(return_one() == 1);
    if tmp { panic() }
    if !tmp { panic() }
}


it then generates the attached IR (also linked [2]). When this IR is complied
with:

    llc foo.ll -filetype=obj -o foo.o -O0

it generates this wasm file [3]. The wasm file notably contains:

  (set_local $var$4
   (i32.eq
    (get_local $var$2)
    (get_local $var$3)
   )
  )
  (set_local $var$5
   (i32.const -1)
  )
  (set_local $var$6
   (i32.xor
    (get_local $var$4)
    (get_local $var$5)
   )
  )

which looks like it's equating a variable (correctly) into $var$4, but then it
attempts to invert this variable with an i32.xor instruction which xors either
1 or 0, so the xor of 1 doesn't work (generates 0xfffffffc instead of 0).

I can provide an example running in the browser as well if necessary, but it
may also suffice to say that when run in Firefox the above Rust code executes
successfully! (unexpectedly)

[1]:
https://github.com/rustwasm/wasm-bindgen/blob/dcb3415da8650b918e5b7f6d65ab071fff7aa6fa/tests/all/webidl/simple.rs#L46-L47
[2]:
https://gist.github.com/alexcrichton/2baf6f95f00275858c8b81d6eff25327#file-foo-ll
[3]: https://gist.github.com/alexcrichton/e5cafad8455795aff2c40652fe4d4dc3

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180702/6bc7ea13/attachment.html>


More information about the llvm-bugs mailing list