cpu toolchain launch postmortem - llvm.org · technology – “snc” 2004 prodg psp® ......

87
CPU Toolchain Launch Postmortem Greg Bedwell

Upload: vannga

Post on 16-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

CPU Toolchain Launch Postmortem

Greg Bedwell

Page 2: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

x86-64 AMD “Jaguar” 8-core CPU

1.84 TFLOPS AMD Radeon™ based GPU

8GB GDDR5 RAM

Page 3: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

http://llvm.org/devmtg/2013-11/

Page 4: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

postmortem noun

“an analysis or discussion of an event after it is over”

http://www.merriam-webster.com/dictionary/postmortem

Now that we have successfully launched PlayStation®4 it is a good time to look back on our initial period of development up to

that point

Page 5: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

First, some history…

Page 6: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

1990 Psy-Q 16-bit home systems

SN Systems Ltd. was founded in 1988 to

provide development tools for the games

industry

Page 7: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

1994 Psy-Q PlayStation®

Psy-Q included a version of GCC that was highly

customized for the needs of game

developers

Page 8: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2000 ProDG PlayStation®2

Continued to provide GCC but started

researching a proprietary compiler technology – “SNC”

Page 9: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2004 ProDG PSP® (PlayStation®Portable)

Provided SNC as part of the ProDG suite of tools although GCC was also

available

Page 10: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2006 ProDG PlayStation®3

Sony Computer Entertainment Inc.

acquired SN Systems in 2005

Provided SNC as part of the ProDG suite of tools although GCC was also

available

Page 11: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2011

PlayStation®Vita

CPU Compiler is SNC

Page 12: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2013 PlayStation®4

CPU Compiler is Clang

Page 13: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

The CPU compiler project is a global effort

Page 14: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Builds and build systems (and test systems)

Page 15: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

We wanted to make use of all of our pre-existing

test suites and test systems, which are

shared across all targets

Page 16: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile
Page 17: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

MinSizeRel

Release

RelWithDebInfo

Page 18: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

MinSizeRel

Release

RelWithDebInfo

Page 19: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

2h 41m 24s

12m 56s 7m 44s

0

2000

4000

6000

8000

10000

12000

Debug Release+Asserts Release

seco

nd

s

Clang configuration effect on game build time

Too slow for continuous integration testing!

Useful for continuous integration testing but

still too much overhead over the pure release configuration of clang

Page 20: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

Checking

Release

The same set of build configurations as we use

for SNC

“Debug” for debugging

“Release” for releasing

“Checking” (Release+Asserts) for

continuous integration testing

Page 21: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

Checking

Release

O

Page 22: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

0

5

10

15

20

25

30

35

40

45

Default /Zi (Produce PDB) /Zi (Produce PDB) and /OPT:REF /OPT:REF

clan

g.ex

e s

ize

(M

B)

No executable size overhead for enabling

debug data

Page 23: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

Checking

Release

O

Page 24: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Optimized Assertions Debug Info

Debug

Checking

Release

Suppress Windows crash dialog box for

Checking and Release builds

Page 25: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Improving test coverage

Page 26: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Game code is usually

BIG For a new platform, the amount of code that exists is small

Things we

know

Page 27: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Users value

CORRECTNESS over all else

Things we

know

Page 28: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Why write tests

when I can write a test generator?

Page 29: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Why write tests

when I can write a test generator? already have _

Page 30: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Random input

Result

Generate random C++ tests using SIMD

language extensions and intrinsics to increase test

coverage

Page 31: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result

Random input

Page 32: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result

Random input

Page 33: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result Random

input

Page 34: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result Random

input

Page 35: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result Random

input

Page 36: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result Random

input

Undefined behaviour makes runtime-

behaviour random testing hard!

Page 37: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Result Random

input

Solution: Use a ‘safe’ wrapper to make all undefined behaviour

defined for the purpose of the test

Page 38: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Reducing optimization bugs

Page 39: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

bugpoint

Clang integration

Windows

Page 40: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

An alternative approach

Page 41: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

*Not representative of actual visuals

SNC’s max_opts

Page 42: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile
Page 43: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC’s max_opts

Page 44: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC’s max_opts

Page 45: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC’s max_opts

Page 46: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC’s max_opts

Page 47: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC’s max_opts

Page 48: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC optimizer

SSA Form Rule Based

Every transformation is guarded

by a specific check

SNC’s max_opts

Page 49: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

SNC keeps an internal counter of the number of transformations

it performs

Allows the user to specify a limit on the command line after which no further

transformations can be performed

Page 50: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -O2

Page 51: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -O0

Page 52: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -O0

-O2

Page 53: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -O0

-O2

Page 54: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -O0

-O2

Page 55: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts We’ve found the game source file

with the bad transformation

Page 56: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -Xmax_opts=2048

Page 57: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -Xmax_opts=1024

Page 58: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -Xmax_opts=512

Page 59: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -Xmax_opts=988

Page 60: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

“Autochop” harness SNC’s

max_opts -Xmax_opts=989

Now we’ve found the specific transformation causing the miscompile

Page 61: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

A compiler trace shows us the culprit optimization

Page 62: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Narrowed down to a single difference in IR

Just a single line change in the IR

Page 63: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

A question for the community:

SNC’s max_opts Would this work in

LLVM/Clang? (even if just at pass level)

Page 64: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

The release process

Page 65: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Docs

End-user documentation is lacking

Release notes aimed at Clang/LLVM developers, not users

Page 66: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Docs

We plan to contribute our documentation improvements to the community

Page 67: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Docs

We plan to contribute our documentation improvements to the community

Page 68: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Forward compatibility

Page 69: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI System libraries

v3.x v3.x

Page 70: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI v3.x

System libraries

v4.x

Page 71: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI v3.x v5.x

System libraries

Page 72: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI v3.x v6.x

System libraries

Page 73: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI v3.x v13.x

System libraries

Page 74: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI

Maintaining a stable ABI is a

MUST (including maintaining existing ABI bugs)

Page 75: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

We have created a full IA64 ABI test suite

Page 76: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

ABI TEST

SUITE

We hope to contribute our test suite to the community

(some logistics still to be worked out)

Page 77: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Developer reaction

Page 78: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile
Page 79: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

But…

Page 80: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

#pragma optimize

Most requested feature by an

order of magnitude and already supported by all the other major compilers

Page 81: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

#pragma optimize

This is the most common

use-case:

Game runs too slowly at –O0,

but is very hard to debug

at –O2

Page 82: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

#pragma optimize

Solution: use a pragma to selectively disable optimization

on a small set of functions to be

debugged

We proposed this on the mailing lists, but it is a major change and we got a limited response

Page 83: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

#pragma optimize

Short term solution: function level attribute to disable

optimization

Not user-friendly for more than a very small number of

functions at a time!

Page 84: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

#pragma optimize

Many of our users abstract this away behind a compiler-

independent interface. Function

attribute does not fit this model!

We still need a range-based solution

Page 85: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

In summary

Page 86: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

Thanks to all of you who helped make Clang and

LLVM great!

Our initial experience with Clang and LLVM has been very positive

Page 87: CPU Toolchain Launch Postmortem - llvm.org · technology – “SNC” 2004 ProDG PSP® ... max_opts -Xmax_opts=989 Now we’ve found the specific transformation causing the miscompile

There are still improvements

that can be made…

We will be working alongside you to make them