Skip to content

Wrong optimizr code generation #5433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ElijahGr opened this issue Aug 1, 2017 · 3 comments
Closed

Wrong optimizr code generation #5433

ElijahGr opened this issue Aug 1, 2017 · 3 comments
Labels

Comments

@ElijahGr
Copy link

ElijahGr commented Aug 1, 2017

Expression (ival = ( int) val) is executed even if safeCast==false

example for this issue: d.cpp

#include <stdio.h>
#include <limits.h>

double nextVal()
{
  static double vals[4]={1, 2, 70203888290798.0, 4};
  static int index;
  index &= 3;
  return vals[index++];
}

void test()
{
  double val = nextVal();
  printf("test %f ", val);
  int ival = 0;
  bool safeCast = (val>=INT_MIN && val<=INT_MAX);
  printf("cast %d ", safeCast);

  if (safeCast)
  {
    ival = ( int) val; //code is always executed even if safeCast==false
  }

  if ((double)ival == val)
  {
    printf("val %f is int\n", val);
    return;
  }
  printf("%f not int\n", val);
}

int main()
{
  test();
  test();
  test(); //raise integer overflow error 
  test();
}

Compile command:
emcc -O2 d.cpp -o d.html -s WASM=1

This error not appears without any optimization or BINARYEN_TRAP_MODE='clamp'.
Looks like the compiler believe that expression (ival = ( int) val) has no side effects this is not the case.

@kripken
Copy link
Member

kripken commented Aug 1, 2017

This looks like the same as WebAssembly/binaryen#1110 , where the LLVM optimizer can move code around in ways that can trap in wasm. (The binaryen optimizer is aware of this, but not the LLVM optimizer.)

This seems to be a common issue, maybe we need to think about a plan for this that includes all the compilers and optimizers.

@dschuff
Copy link
Member

dschuff commented Nov 14, 2017

See also #5498

@stale
Copy link

stale bot commented Sep 19, 2019

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Sep 19, 2019
@stale stale bot closed this as completed Sep 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants