Exception Handling Interfaces, Implementations, and Evaluation

Similar documents
FP_IEEE_DENORM_GET_ Procedure

Efficient floating point: software and hardware

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Chapter 12. CPU Structure and Function. Yonsei University

There are different characteristics for exceptions. They are as follows:

Control Abstraction. Hwansoo Han

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Q.1 Explain Computer s Basic Elements

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

Lecture 10. Floating point arithmetic GPUs in perspective

Volume II, Section 5 Table of Contents

CISC 662 Graduate Computer Architecture Lecture 7 - Multi-cycles. Interrupts and Exceptions. Device Interrupt (Say, arrival of network message)

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CISC 662 Graduate Computer Architecture Lecture 7 - Multi-cycles

NAN propagation versus fault trapping in floating point code

Last time: forwarding/stalls. CS 6354: Branch Prediction (con t) / Multiple Issue. Why bimodal: loops. Last time: scheduling to avoid stalls

Principles of Programming Languages

CPU Structure and Function

What are Exceptions? EE 457 Unit 8. Exception Processing. Exception Examples 1. Exceptions What Happens When Things Go Wrong

A read or write being atomic means that its effect is as if it happens instantaneously.

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Lecture 17 Floating point

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

Instruction Level Parallelism. ILP, Loop level Parallelism Dependences, Hazards Speculation, Branch prediction

Protection and System Calls. Otto J. Anshus

William Stallings Computer Organization and Architecture

Appendix C: Pipelining: Basic and Intermediate Concepts

EE 457 Unit 8. Exceptions What Happens When Things Go Wrong

Chapter 7 Control I Expressions and Statements

GUJARAT TECHNOLOGICAL UNIVERSITY MASTER OF COMPUTER APPLICATION SEMESTER: III

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

CPU Structure and Function. Chapter 12, William Stallings Computer Organization and Architecture 7 th Edition

G Programming Languages - Fall 2012

Fixed-Point Math and Other Optimizations

CSE 160 Lecture 14. Floating Point Arithmetic Condition Variables

Lecture 2: Architectural Support for OSes

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

William Stallings Computer Organization and Architecture. Chapter 11 CPU Structure and Function

UNIT- 5. Chapter 12 Processor Structure and Function

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Computer Architecture Set Four. Arithmetic

Architecture and OS. To do. q Architecture impact on OS q OS impact on architecture q Next time: OS components and structure

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Processes. Johan Montelius KTH

Lecture 7: Pipelining Contd. More pipelining complications: Interrupts and Exceptions

Instruction Pipelining Review

High Performance Computer Architecture Prof. Ajit Pal Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

A process. the stack

Fundamentals of Programming Languages. PL quality factors Lecture 01 sl. dr. ing. Ciprian-Bogdan Chirila

Chapter 1 Computer System Overview

Operating Systems (2INC0) 2018/19. Introduction (01) Dr. Tanir Ozcelebi. Courtesy of Prof. Dr. Johan Lukkien. System Architecture and Networking Group

In examining performance Interested in several things Exact times if computable Bounded times if exact not computable Can be measured

Advanced processor designs

Anti-Inspiration. It s true hard work never killed anybody, but I figure, why take the chance? Ronald Reagan, US President

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CSE 120 Principles of Operating Systems

CS450/650 Notes Winter 2013 A Morton. Superscalar Pipelines

24-vm.txt Mon Nov 21 22:13: Notes on Virtual Machines , Fall 2011 Carnegie Mellon University Randal E. Bryant.

Software-Controlled Multithreading Using Informing Memory Operations

Donn Morrison Department of Computer Science. TDT4255 ILP and speculation

Advanced issues in pipelining

HPC VT Machine-dependent Optimization

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

CPU Structure and Function

Preface Introduction... 23

ECE 552 / CPS 550 Advanced Computer Architecture I. Lecture 15 Very Long Instruction Word Machines

Thomas Polzer Institut für Technische Informatik

Topic 18: Virtual Memory

Minimizing Data hazard Stalls by Forwarding Data Hazard Classification Data Hazards Present in Current MIPS Pipeline

CMCS Mohamed Younis CMCS 611, Advanced Computer Architecture 1

Lecture 10 Exceptions and Interrupts. How are exceptions generated?

Reminder: tutorials start next week!

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

COS 318: Operating Systems

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION

Exception Handling. Precise Exception Handling. Exception Types. Exception Handling Terminology

The Procedure Abstraction

CS Computer Architecture. 1. Explain Carry Look Ahead adders in detail

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Real-Time and Embedded Systems (M) Lecture 19

Compiler Design. Code Shaping. Hwansoo Han

CSC 631: High-Performance Computer Architecture

55:132/22C:160, HPCA Spring 2011

For our next chapter, we will discuss the emulation process which is an integral part of virtual machines.

Examples of Code Roaches. First Draft List Cem Kaner September 11, 2005

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

CSE 820 Graduate Computer Architecture. week 6 Instruction Level Parallelism. Review from Last Time #1

CS 252 Graduate Computer Architecture. Lecture 4: Instruction-Level Parallelism

Introduction to Computer Science and Business

CISC / RISC. Complex / Reduced Instruction Set Computers

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Rationale for TR Extension to the programming language C. Decimal Floating-Point Arithmetic

.. \ , Floating Point. Arithmetic. (The IEEE Standard) :l.

Data/Thread Level Speculation (TLS) in the Stanford Hydra Chip Multiprocessor (CMP)

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Notes Interrupts and Exceptions. The book uses exception as a general term for all interrupts...

COP4020 Programming Languages. Exception Handling Prof. Robert van Engelen

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Transcription:

Exception Handling Interfaces, Implementations, and Evaluation David Bindel E. Jason Riedy U.C. Berkeley Exception Handling p.1/41

What do we want? We want to produce programs which can detect exceptional conditions and react to them. We also want these programs to be supported by our friendly neighborhood programming environments and amenable to optimization on current and future platforms. Exception Handling p.2/41

Thesis The standard specifies interface requirements Interfaces have multiple good implementations Good design is hard, but interface criteria include Minimality Orthogonality Clarity We prefer explicit and local control and data flow Exception Handling p.3/41

Outline Motivating and rejected examples Deferring debugging... Survey of software interfaces Hardware support Hardware / software mapping A scarecrow proposal Exception Handling p.4/41

Motivating / Archetypal Examples Algorithms that exceptions make risky abort, wasting minimal work (Eureka exit) then possibly do something else (complex multiply, scaling) Slightly change the arithmetic substitute a limit for an exceptional result (continued fractions, replacement) Soften the arithmetic s boundaries extend the dynamic range (long products, counting mode) Communicate the quality of a result Exception Handling p.5/41

Rejected Examples Any of the following could overly constrain our choices: Supporting heavy modifications to the arithmetic. UN, OV, etc. Allowing extremely non-local, implicit control and data flow. Considering any particular hardware implementation. Requiring specific debugging tools... Exception Handling p.6/41

Deferring Debugging General purpose tools handle uninitialized data. Purify, valgrind, etc. Different applications need different retrospective diagnostic facilities. We re not sure how to support future debugging tools. (path-based, etc.) We need to keep debugging in mind, but it is a quality of implementation issue. Exception Handling p.7/41

Survey of Software Interfaces Motivating and rejected examples Survey of software interfaces Try-catch Flag testing Explicit trapping Substitution Flag-carrying types Conditional branching FP ops Hardware support Hardware / software mapping A scarecrow proposal Exception Handling p.8/41

Try-Catch try { fast and sloppy code } catch (exceptional cases) { slow and careful } Floating-point mechanism exists in: fpmenu BASIC Ada Numerical Turing Common Lisp (optional) Borneo (specification) Exception Handling p.9/41

Try-Catch Language aspects: Scope is always specified as linguistic blocks. Extent: Can called functions also raise exceptions? Are thrown exceptions specified statically? How do callers / callees communicate which exceptions are interesting? Is the try block interrupted precisely? Can execution be resumed or statements restarted? Exception Handling p.10/41

Try-Catch Benefits: Matches existing, non-fp practice. Limits optimization impact to blocks. Drawbacks: Existing practice is often mis-managed. Exception Handling p.11/41

Try-Catch Observations: Case without resumption / restart can be implemented through either traps or flags. Catching invalid is often followed by testing in-scope variables to determine which invalid op occurred. Exception Handling p.12/41

Flag-Testing double f (double x) { save environment do work; if (flags raised) do alternate work; restore environment merge proper flags return result } Exists in: C99 Many platform-dependent libraries Borneo (specification) Exception Handling p.13/41

Flag-Testing double f (double x) { fenv_t fenv; feholdexcept(&fenv); do work; if (flags raised) do alternate work; fesetenv(&fenv); return out1+out2; } Exists in: C99 Many platform-dependent libraries Borneo (specification) Exception Handling p.14/41

Flag-Testing Language aspects: Scope: Are flags set by block, or though a global datum? Extent: How do flags pass through subroutines? Benefits: Predictable control flow. Drawbacks: All operations share state. Subexp movement and compile-time evaluation often incorrect. Flag tests clutter code. Exception Handling p.15/41

Flag-Testing double f (double x) { fenv_t fenv; feholdexcept(&fenv); do work; if (flags raised) do alternate work; fesetenv(&fenv); return out1+out2; } Observations: Almost all uses follow the above pattern, including a few operations to set output flags implicitly. Compilers must virtualize and track flags for optimization. Exception Handling p.16/41

Explicit Trapping Implementations Sun s libm9x SIGFPE handling (wmexcp, fpmenu) Aspects Scope: dynamic Extent: dynamic Benefits Unknown Drawbacks No portable interfaces Nigh-impossible to use Serious non-local, implicit effects Exception Handling p.17/41

Substitution FPE_PRESUB(FE_INVALID,+INFINITY) for (i = 0; i < n_items; ++i) newprice[i] = price[i] + bidincr[i]; FPE_END_PRESUB Exists in: IEEE defaults fpmenu: presub and counting Exception Handling p.18/41

Substitution FPE_COUNT(&cnt) for (i = 0; i < N; ++i) out *= A[i] + B[i]; FPE_END_COUNT Exists in: IEEE defaults fpmenu: presub and counting Exception Handling p.19/41

Substitution Language aspects: Static scope, but static or dynamic extent How do you determine the replaced type? Do you consider operands? Get the sign? Location of count or other implicit operands? Benefits: Well-defined, can have very limited scope Many implementation / optimization options Drawbacks: Only two functionalities out of how many? Exception Handling p.20/41

New : Flag-Carrying Types In the continued fraction code: double f, f1,...; flagdouble r; int j;... r = d1/d; f1 = -r * d; if (!flagtest_and_clear(r, INVALID)) continue; // fixup... Explicit syntax for the desired result. Useful when only a few items are flagged. Exception Handling p.21/41

Flag-Carrying Types Language aspects: Scope and extent match value types. Static typing = static flags Relies on expression evaluation typing Exception Handling p.22/41

Flag-Carrying Types Benefits; Everything is explicit. Optimizations use existing frameworks. User control over which expressions require flags. Programmers understand data-flow. Drawbacks: Verbose (sub- and dynamic typing help) Observations: Flagged compile-time constants keep flags. Subexpressions can be lifted. Exception Handling p.23/41

New : Conditional Branch FP-Ops complex operator* (complex x, complex y) { let double operator*(double,double) = trapping_mult(double, OVERFLOW: ov_label, UNDERFLOW: un_label, INVALID: not_complex_label); double operator+(double,double) = trapping_add(double, INVALID: infs_label); double operator-(double,double) = trapping_sub(double, INVALID: infs_label); in { return complex (real(x)*real(y) - imag(x)*imag(y), real(x)*imag(y) - imag(x)*real(y)); } ov_label:... } We mentioned spaghetti code... Exception Handling p.24/41

Hardware Support Motivating and rejected examples Survey of software interfaces Hardware support Existing hardware: flags Existing hardware: traps Flags versus traps Hardware / software mapping A scarecrow proposal Exception Handling p.25/41

Existing HW: Flags Basic operations: Save registers Restore registers Test flags One or more registers visible in ISA May include last instruction flags May be additional internal storage e.g. with reorder buffer entry Exception Handling p.26/41

Existing HW: Traps Basic operations: Enable trapping Disable trapping Set handler Currently require OS support Need privileged mode to set handler Handler runs in privileged mode Trap enable/disable on IA32 costs more than flag save/restore Exception Handling p.27/41

Flags versus Traps Traps are an optimization for flag test and branch But flag tests are reasonably inexpensive! Flag tests need only occur at synchronization points (identified by programmer or compiler) There are other possible optimizations: Execution predicated on flag settings Conditional branch FP ops And others... Compiler could optimize away explicit tests Exception Handling p.28/41

HW/SW Mapping Motivating and rejected examples Survey of software interfaces Hardware support Hardware / software mapping Extended range: a case study fpmenu implementation notes Interfaces and implementations Performance A scarecrow proposal Exception Handling p.29/41

Extended Range: A Case Study scaled_double prod; for (i = 0; i < n; ++i) prod *= a[i]; Extend range by implementing a scaled precision: No exceptions: scale on every operation Flags: test after each operation Traps: use counting mode Can optimize first two cases by blocking. Exception Handling p.30/41

Extended Range: A Case Study scaled_double prod; for (i = 0; i < n; i += BLOCK) { prod_tmp = fast product over block if (no range exception) prod *= prod_tmp; else prod *= scaled subproduct } Compiler ideally generates this from previous code Otherwise, little worse than blocking matrix codes Could probably use similar automatic tuning Exception Handling p.31/41

HW/SW Mapping fpmenu: Uses SIGFPE handler + ugly C macros to implement try/catch and replacement On exception try/catch: restore state, jump to user substitution: decode, compute, writeback other: re-execute instruction Exception Handling p.32/41

HW/SW Mapping fpmenu: handler choice really needs compiler input must manually add fwait instructions makes most optimizations dangerous context save penalty on try/catch entry instruction re-execution and toggling traps are both expensive Compiler support would help, but some problems are intrinsic to trap-based handling. Exception Handling p.33/41

Interfaces and Implementations Several implementations for software interfaces Compile flag test and branch to trapping code Implement try-catch handling with flag tests Software resources need not map directly to HW Map HW invalid flag to multiple software flags Support flag-carrying types with virtualized flags Merge local HW flags into virtual global register We standardize interface requirements, not implementations. Simple basic interfaces are easier to reason about and permit adequate room to optimize. Exception Handling p.34/41

Performance: Traps v. Flags Platforms tested: PPro@233MHz, P3@800Mhz, and P4@1.4GHz Tested continued fractions, long products. Results: Blocked flag tests are fine. Blocked flag tests usually faster than trapping. Immediate flag tests are considerably slower. Detrimental in tight loops like long product. Same as trapping in continued fractions. Saving machine state for try-catch is slow. Exception Handling p.35/41

Scarecrow Proposal Motivating and rejected examples Survey of software interfaces Hardware support Hardware / software mapping A scarecrow proposal Exception Handling p.36/41

Scarecrow Proposal A scarecrow is a spooky outline of a straw-man. SHALL be able to tie flags to value types. Compilers already need this for optimizations. Can use this to deliver information on exceptions affecting results. Generalizes to vectors in many ways. Exception Handling p.37/41

Scarecrow Proposal Helpers: SHALL provide a presubstitution mechanism Presubstitute the result of an expression. Any unhandled conditions remain raised. Types are known. SHALL provide scaled-exponent type Many uses, can be heavily optimized. Must explicitly determine the substitute s sign. Note that doubled-precisions don t round correctly. Exception Handling p.38/41

Scarecrow Proposal SHALL require programmers to declare interest in flags Scope and extent? All flags or particular flags? SHALL provide an invalid hierarchy MAY allow users to add conditions Borneo s admit-yields is a good example. Static scope and extent flag declarations make user-defined, software flags reasonable. Exception Handling p.39/41

Scarecrow Proposal edit Move exception handling optimizations to an informative annex. Describe fast trapping or conditional operations as optimizations edit Eliminate signaling NaNs. Only current use of signaling NaNs: debugging. Exception Handling p.40/41

End. You re still here? Go home. Exception Handling p.41/41