Exception Handling - Sonic Gigabit Fiber Internet &...

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

Transcript of Exception Handling - Sonic Gigabit Fiber Internet &...

Page 1: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Exception HandlingInterfaces, Implementations, and Evaluation

David Bindel E. Jason Riedy

U.C. Berkeley

Exception Handling – p.1/41

Page 2: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 3: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 4: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Outline

• Motivating and rejected examples• Deferring debugging...

• Survey of software interfaces• Hardware support• Hardware / software mapping• A scarecrow proposal

Exception Handling – p.4/41

Page 5: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 6: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 7: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Deferring Debugging

• General purpose tools handle uninitialized data.• Purify, valgrind, etc.

• Different applications need different retrospectivediagnostic facilities.

• We’re not sure how to support future debuggingtools. (path-based, etc.)

We need to keep debugging in mind, but it is a “qualityof implementation” issue.

Exception Handling – p.7/41

Page 8: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 9: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Try-Catch

try {fast and sloppy code

}catch (exceptional cases) {

slow and careful}

Floating-point mechanism exists in:• fpmenu• Ada• Numerical Turing

• BASIC• Common Lisp (optional)• Borneo (specification)

Exception Handling – p.9/41

Page 10: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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 whichexceptions are interesting?

• Is the try block interrupted precisely?• Can execution be resumed or statements

restarted?

Exception Handling – p.10/41

Page 11: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 12: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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 opoccurred.

Exception Handling – p.12/41

Page 13: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Flag-Testing

double f (double x) {save environmentdo work;if (flags raised) do alternate work;restore environmentmerge proper flagsreturn result

}Exists in:

• C99• Many platform-dependent libraries• Borneo (specification)

Exception Handling – p.13/41

Page 14: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 15: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 16: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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, includinga few operations to set output flags implicitly.

• Compilers must virtualize and track flags foroptimization.

Exception Handling – p.16/41

Page 17: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 18: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 19: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 20: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 21: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

‘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

Page 22: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 23: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 24: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

‘New’: Conditional Branch FP-Ops

complex operator* (complex x, complex y){

letdouble 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

Page 25: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 26: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 27: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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 flagsave/restore

Exception Handling – p.27/41

Page 28: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 29: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 30: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 31: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Extended Range: A Case Study

scaled_double prod;for (i = 0; i < n; i += BLOCK) {

prod_tmp = fast product over blockif (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

Page 32: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 33: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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 areintrinsic to trap-based handling.

Exception Handling – p.33/41

Page 34: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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, notimplementations. Simple basic interfaces are easier toreason about and permit adequate room to optimize.

Exception Handling – p.34/41

Page 35: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Performance: Traps v. Flags

• Platforms tested: PPro@233MHz, P3@800Mhz,and [email protected]

• 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

Page 36: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Scarecrow Proposal

• Motivating and rejected examples• Survey of software interfaces• Hardware support• Hardware / software mapping• A scarecrow proposal

Exception Handling – p.36/41

Page 37: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 38: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

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

Page 39: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Scarecrow Proposal

SHALL require programmers to declare interest in flags• Scope and extent?• All flags or particular flags?

SHALL provide an invalid hierarchyMAY 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

Page 40: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

Scarecrow Proposal

edit Move exception handling optimizations to aninformative annex.• Describe fast trapping or conditional operations

as optimizationsedit Eliminate signaling NaNs.

• Only current use of signaling NaNs: debugging.

Exception Handling – p.40/41

Page 41: Exception Handling - Sonic Gigabit Fiber Internet & Phonejriedy/CV/material/ieee754-2002-08-22... · 2020-05-08 · Exception Handling Interfaces, Implementations, and Evaluation

End.

You’re still here? Go home.

Exception Handling – p.41/41