This is an archive of the discontinued LLVM Phabricator instance.

Fix for Bug 5053
AbandonedPublic

Authored by swaroop.sridhar on Sep 17 2014, 2:33 PM.

Details

Summary

Fix for LLI failure on Windows\X86: http://llvm.org/bugs/show_bug.cgi?id=5053

The here problem is that LLI.exe crashes on Windows\X86 when single precession floating point intrinsics like the following are used: acos, asin, atan, atan2, ceil, copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh, sqrt, tan, tanh

The above intrinsics are defined as inline-expansions in math.h, and are not exported by msvcr120.dll (Win32 API GetProcAddress returns null).

For an FREM instruction, the JIT compiler generates a call to a stub for the fmodf() intrinsic, and adds a relocation to fixup at load time. The loader searches the libraries for the function, but fails because the symbol is not exported. So, the call target remains NULL and the execution crashes.

Since the math functions are loaded at JIT/runtime, the JIT can patch CALL instruction directly instead of the searching the libraries’ exported symbols.
However, this fix caused build failures due to unresolved symbols like _fmodf at link time.

Therefore, the current fix defines helper functions in the Runtime link/load library to perform the above operations. The address of these helper functions are used to patch up the CALL instruction at load time.

Diff Detail

Event Timeline

swaroop.sridhar retitled this revision from to Fix for Bug 5053.
swaroop.sridhar updated this object.
swaroop.sridhar edited the test plan for this revision. (Show Details)
swaroop.sridhar added reviewers: Bigcheese, asl.
swaroop.sridhar added a subscriber: Unknown Object (MLST).

Added test case to CodeGen\X86

rnk added a subscriber: rnk.Oct 2 2014, 10:57 AM

Your test case already passes for me. Can you elaborate on when this fails and in what environment, i.e. VS version, LLVM version, MCJIT vs interpreter, etc?

lib/Support/Windows/DynamicLibrary.inc
138

This should either be SYMBOL1 or the define should drop the trailing 1.

lib/Support/Windows/explicit_symbols.inc
74

What version of VS are you testing with? I don't believe this will compile on VS 2012, because it only has _copysign. See the 2012 MSDN docs vs the 2013 docs:
http://msdn.microsoft.com/en-us/library/0yafk1hc(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/0yafk1hc(v=vs.120).aspx

Furthermore, with VS2013, I think we can simply use the EXPLICIT_SYMBOL mechanism instead of a new approach.

test/CodeGen/X86/win_frem.ll
1 ↗(On Diff #14327)

With the fix committed, this should be "used to crash" instead of "crashes".

Also, this test is an execution test that belongs in test/ExecutionEngine instead of test/CodeGen/X86.

Thanks a lot for the review Reid.

  1. I'm using VS2013, LLVM 3.5, and not using MCJIT to repro this failure.
  2. I've updated the test case to look like the actual test reported in Bug 5053. The problem should repro now, can you please try again?
  3. I've fixed the issues you've identified. -> Fixed the wording in the comments for the test case -> Made copysignf VS2013 only
  4. In DynamicLibrary.inc, I've simplified the implementation of the inline_ *() functions. Now, inline_fmodf(x,y) simply calls fmodf(x,y) instead of performing (float)fmod(x,y)

I've attached a new patch for with the updates. Please take a look at it.

Thanks,
Swaroop.

swaroop.sridhar updated this object.
swaroop.sridhar added a reviewer: rnk.

Addressed Code review feedback from Reid. Updated the diff in the revision, instead of attaching a patch in my earlier comment.
[Changes are
1.I'm using VS2013, LLVM 3.5, and not using MCJIT to repro this failure.
2.I've updated the test case to look like the actual test reported in Bug 5053. The problem should repro now, can you please try again?
3.I've fixed the issues you've identified. -> Fixed the wording in the comments for the test case -> Made copysignf VS2013 only
4.In DynamicLibrary.inc, I've simplified the implementation of the inline_ *() functions. Now, inline_fmodf(x,y) simply calls fmodf(x,y) instead of performing (float)fmod(x,y)]

lhames added a subscriber: lhames.Oct 13 2014, 7:34 PM

Hi Swaroop,

Copied from the bug report:

The old JIT is deprecated in 3.5, and has been removed entirely on the development branch of LLVM. Could you try this with MCJIT and see if your problem reproduces there?

Just run 'lli -use-mcjit testcase.ll' on 3.5, or simply 'lli testcase.ll' on the development branch where MCJIT is the default.

Cheers,
Lang.

Hi Lang,

Is the MCJit supported on Windows?

I tried the following using lli MCJIT (on the development branch of LLVM), but LLI failed with the following error:

Debug\bin\lli.exe llvm\test\ExecutionEngine\hello.ll
LLVM ERROR: Incompatible object format!
Stack dump:
0. Program arguments: Debug\bin\lli.exe D:\enlist\JIT\llvm\test\ExecutionEngine\hello.ll

Thanks,
Swaroop.

I verified that the same failure repros on Windows when using MCJit, and that the patch fixes it.
I've updated the test case with the target triple "windows-msvc-elf" so that it works on MCJit.
Here's the new patch:

lib/Support/Windows/DynamicLibrary.inc
138

Fixed this, thanks.

lib/Support/Windows/explicit_symbols.inc
74

I've changed this to copysignf to be VS2013 only, thanks.

test/CodeGen/X86/win_frem.ll
1 ↗(On Diff #14327)

Fixed the comment wording.

rnk edited edge metadata.Nov 13 2014, 1:45 PM

Looks pretty good. Do you want me to tweak the test case and commit this for you?

I checked the test case and it fails for me today, so this fixes a real bug.

test/ExecutionEngine/win-frem.ll
1

These tests run on all platforms supported by the JIT, so I'd just name it "frem.ll". Windows just happened to be the problematic one.

5

Use %lli, which expands with the appropriate -mtriple flag. You can also FileCheck the result.

7–8

Get rid of this so we can be target neutral.

15

main returns i32?

OK Thanks Reid, would be great if you can get this fix checked in.

Thanks,
Swaroop.

swaroop.sridhar abandoned this revision.Oct 5 2015, 3:28 PM

This fix is already checked in by rnk