There Is Nothing Wrong with Out-Of-Thin-Air: Compiler Optimization and Memory Models

There Is Nothing Wrong with Out-Of-Thin-Air: Compiler Optimization and Memory Models

There is Nothing Wrong with Out-of-Thin-Air: Compiler Optimization and Memory Models Clark Verbrugge Allan Kielstra Yi Zhang School of Computer Science IBM Toronto Lab School of Computer Science McGill University Markham, Canada McGill University Montreal,´ Canada [email protected] Montreal,´ Canada [email protected] [email protected] Abstract wide array of compiler optimizations [3], and it is practically neces- Memory models are used in concurrent systems to specify visibil- sary to allow current and potential future optimizations to co-exist ity properties of shared data. A practical memory model, however, with any memory consistency model. must permit code optimization as well as provide a useful seman- The JMM does not allow all compiler optimizations; certainly tics for programmers. Here we extend recent observations that the some forms of code motion are forbidden, as described in the origi- current Java memory model imposes significant restrictions on the nal specification [1]. Manson et al. provide a proof that some basic ability to optimize code. Beyond the known and potentially cor- optimizations are allowed, but the actual scope of allowed versus rectable proof concerns illustrated by others we show that major forbidden optimizations is far from clear. Other authors have since constraints on code generation and optimization can in fact be de- demonstrated that through examination of the model subtleties, rived from fundamental properties and guarantees provided by the other simple reordering-optimizations are also disallowed [4, 5]. memory model. To address this and accommodate a better balance Some of these concerns have been recently addressed, and repairs between programmability and optimization we present ideas for a to the model have been described [6]. Here we show that even simple concurrency semantics for Java that avoids basic problems with improvements to the justification process to permit these opti- at a cost of backward compatibility. mizations, other very basic problems exist. In particular, the often- mentioned “out-of-thin-air” guarantee, as well as the necessity of Categories and Subject Descriptors D.1.3 [Concurrent Pro- preserving sequential consistency for race-free programs result in gramming]: Parallel programming; D.3.3 [Language Constructs significant constraints on optimization, limiting important existing and Features]: Concurrent, distributed, and parallel languages; techniques and imposing additional analysis costs. D.3.4 [Processors]: Compilers As a possible solution we explore a conceptually simple modifi- cation to Java semantics that guarantees lack of race-conditions by General Terms Languages, Design, Performance construction. This design uses a syntactically trivial change to the Keywords memory consistency, compiler optimization type system, enforcing behaviours akin to OpenMP [7], UPC [8] or C# [9] data-sharing directives to ensure shared data are easily distinguished by the compiler. The design we propose has similar 1. Introduction goals to the JMM, but takes the stance that program comprehension The recently revised Java memory model (JMM) provides a precise by both developers and compiler writers must take priority. Our ap- memory consistency model for a Java context [1]. The JMM tries to proach represents a basic shift in perspective—rather than starting satisfy perhaps three main goals: allowing compiler optimizations, from a model which requires expensive and difficult-to-understand giving precise and useful formal guarantees to programmers writ- conflict/race detection for basic safety, we start from an inherently ing “correct” concurrent programs, and giving a precise semantics safe model, allowing compiler optimizations to more easily pre- to concurrent programs even when “incorrect,” or containing data serve safety during transformation. races. We make the following specific contributions: A precise semantics and guarantees of sequential consistency • “Out-of-thin-air” values are a known consequence of code op- (SC) for data-race-free (DRF) programs are extremely useful to timization and generation [2, 10]. We extend these observations programmers and for helping define the allowable behaviours of and show that disallowing such behaviour impacts large classes a Java virtual machine; Saraswat et al. consider the connection be- of useful optimization that use or reuse space to improve perfor- tween DRF and SC as the Fundamental Property [2]. The impor- mance. tance of allowing compiler optimizations is also critical; Java vir- • tual machine implementations achieve impressive speeds due to a We show that the most useful, basic guarantee of the JMM, ensur- ing sequential consistency for race-free programs, imposes major and arguably unacceptable constraints on program optimization. • We propose a syntactically trivial change to the Java language Permission to make digital or hard copies of all or part of this work for personal or that enables practical preservation of race-free, sequentially con- classroom use is granted without fee provided that copies are not made or distributed sistent execution. Our design simplifies preservation of safety for profit or commercial advantage and that copies bear this notice and the full citation properties in optimization while still enabling advanced opti- on the first page. To copy otherwise, to republish, to post on servers or to redistribute mizations, and could also be easily incorporated into different to lists, requires prior specific permission and/or a fee. concurrent language environments, such as C++ [10]. MSPC’11, June 5, 2011, San Jose, California, USA. Copyright c 2011 ACM 978-1-4503-0794-9/11/06. $10.00 2. Related Work complexity of determining whether out-of-thin-air data has been Our work is intended to provide a simpler and more practical mem- exposed. ory model that accommodates both programmers and compiler Our work is partly motivated by the complexity that determin- writers. We build quite specifically on previous work on the Java ing data conflicts, and more specifically data races adds to analy- memory model, as well as on more generic consistency properties. sis. Static and dynamic approaches to this problem have been tried, The current JMM [1] is a significant achievement, addressing with the former having the advantage of avoiding runtime over- basic flaws found by Pugh [11] in the original specification, and in head, and several static, type-based approaches have been explored particular relaxing the need for compilers to ensure a consistency [19, 20]. These designs use the (sets of) locks that guard shared data property slightly stronger than coherence [12]. The need to provide as types, ensuring that any potential runtime access respects the a semantics for racey programs, however, make the justification type system by holding the required locks. Although this approach part of this specification quite complex. Close analysis by several has been shown to be quite effective, the resulting type systems authors subsequently has resulted in identification of subtle flaws. require non-trivial language extension, significant annotation, and Cenciarelli et al. point out that some independent statements cannot still require some form of escape mechanism to allow the program- be reordered under the justification model in the JMM [4], and mer to perform (statically) type-unsafe activities. For more precise data-race analysis, dynamic approaches that track actual control Aspinall and Sev˘ c˘´ık show a number of examples of disallowed code flow and thread behaviour can be more accurate. Overhead is a con- changes, illustrating a variety of surprising consequences [5]. The cern, however, and recent results have shown the performance loss latter two authors have extended their work, demonstrating both of performing precise, runtime verification can be reduced to an specific proof errors as well as offering modifications that eliminate impressive, but still quite significant 8-fold slowdown [21]. most of the problems [6, 13]. Designation of private versus shared data is a of course a Much of the complexity in the JMM is unnecessary in the new common concept in parallel and multi-process programming. C++ model for multithreading, which provides no guarantees at Our model essentially provides a Java embedding of a subset of all for programs with race conditions [10]. Programs must be cor- UPC [8], Titanium [22], or OpenMP [7] data sharing behaviours, rectly synchronized to have defined semantics, and if so, sequen- with a similar model to UPC in using a default private scope for tial consistency is guaranteed. This is in keeping with the overall data and requiring explicit specification of shared memory. More emphasis on complete programmer control (and responsibility) in complex sharing models, such as the different OpenMP data initial- C/C++ programming, and provides a dramatically simpler concep- ization modes (firstprivate or copyin) can also be supported. Other tual model. designs for full implementations of OpenMP such as JOMP [23] The JMM and C++ consistency models, as well as many oth- and the more recent JaMP [24] have concentrated on supporting ers, focus on the data-race-free-0 (DRF0) property as core and de- the parallel primitives more than the sharing directives, and are sireable behaviour [14]. Programs without races can be shown to based on parsing structured comment blocks containing standard result in only sequentially consistent execution, provided the com- OpenMP directives. This approach is in fact the basis for a

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us