[LLVMdev] Use rand_r() instead of non-reentrant thread-unsafe rand() in GetRandomNumber()

Dmitry Mikushin dmitry at kernelgen.org
Sat Dec 1 08:17:48 PST 2012


Dear all,

In our LLVM-based compiler pipeline a major part of code generation is
taken into application runtime. One side-effect of this organization is a
need to be very careful about using code that might diverge application
state. And we found that simple generation of temporary files over LLVM
APIs introduces random noise into the program result. There reason is that
LLVM's GetRandomNumber call uses rand(), that clashes with rand() used in
application. Taking in account that many client applications developers may
be not aware of this at all [and just complain to us, that our compiler is
bad because their program is now wrong], we propose switching to self-
state maintaining rand() on LLVM side. Attached patch simply replaces the
calls to srand() and rand() with a call to rand_r(), which uses static
unsigned x value to keep its state locally.

Best,
- D.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121201/ffc5d28b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm.rand.patch
Type: application/octet-stream
Size: 401 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121201/ffc5d28b/attachment.obj>


More information about the llvm-dev mailing list