Support Library

Abstract

This document provides some details on LLVM’s Support Library, located in thesource at lib/Support and include/llvm/Support. The library’s purposeis to shield LLVM from the differences between operating systems for the fewservices LLVM needs from the operating system. Much of LLVM is written usingportability features of standard C++. However, in a few areas, system dependentfacilities are needed and the Support Library is the wrapper around thosesystem calls.

By centralizing LLVM’s use of operating system interfaces, we make it possiblefor the LLVM tool chain and runtime libraries to be more easily ported to newplatforms since (theoretically) only lib/Support needs to be ported. Thislibrary also unclutters the rest of LLVM from #ifdef use and special cases forspecific operating systems. Such uses are replaced with simple calls to theinterfaces provided in include/llvm/Support.

Note that the Support Library is not intended to be a complete operating systemwrapper (such as the Adaptive Communications Environment (ACE) or ApachePortable Runtime (APR)), but only provides the functionality necessary tosupport LLVM.

The Support Library was originally referred to as the System Library, writtenby Reid Spencer who formulated the design based on similar work originatingfrom the eXtensible Programming System (XPS). Several people helped with theeffort; especially, Jeff Cohen and Henrik Bach on the Win32 port.

Keeping LLVM Portable

In order to keep LLVM portable, LLVM developers should adhere to a set ofportability rules associated with the Support Library. Adherence to these rulesshould help the Support Library achieve its goal of shielding LLVM from thevariations in operating system interfaces and doing so efficiently. Thefollowing sections define the rules needed to fulfill this objective.

Don’t Include System Headers

Except in lib/Support, no LLVM source code should directly #include asystem header. Care has been taken to remove all such #includes from LLVMwhile lib/Support was being developed. Specifically this means that headerfiles like “unistd.h”, “windows.h”, “stdio.h”, and “string.h”are forbidden to be included by LLVM source code outside the implementation oflib/Support.

To obtain system-dependent functionality, existing interfaces to the systemfound in include/llvm/Support should be used. If an appropriate interface isnot available, it should be added to include/llvm/Support and implemented inlib/Support for all supported platforms.

Don’t Expose System Headers

The Support Library must shield LLVM from all system headers. To obtainsystem level functionality, LLVM source must#include "llvm/Support/Thing.h" and nothing else. This means thatThing.h cannot expose any system header files. This protects LLVM fromaccidentally using system specific functionality and only allows it viathe lib/Support interface.

Use Standard C Headers

The standard C headers (the ones beginning with “c”) are allowed to beexposed through the lib/Support interface. These headers and the things theydeclare are considered to be platform agnostic. LLVM source files may includethem directly or obtain their inclusion through lib/Support interfaces.

Use Standard C++ Headers

The standard C++ headers from the standard C++ library and standardtemplate library may be exposed through the lib/Support interface. Theseheaders and the things they declare are considered to be platform agnostic.LLVM source files may include them or obtain their inclusion throughlib/Support interfaces.

High Level Interface

The entry points specified in the interface of lib/Support must be aimed atcompleting some reasonably high level task needed by LLVM. We do not want tosimply wrap each operating system call. It would be preferable to wrap severaloperating system calls that are always used in conjunction with one another byLLVM.

For example, consider what is needed to execute a program, wait for it tocomplete, and return its result code. On Unix, this involves the followingoperating system calls: getenv, fork, execve, and wait. Thecorrect thing for lib/Support to provide is a function, sayExecuteProgramAndWait, that implements the functionality completely. whatwe don’t want is wrappers for the operating system calls involved.

There must not be a one-to-one relationship between operating systemcalls and the Support library’s interface. Any such interface function will besuspicious.

No Unused Functionality

There must be no functionality specified in the interface of lib/Supportthat isn’t actually used by LLVM. We’re not writing a general purpose operatingsystem wrapper here, just enough to satisfy LLVM’s needs. And, LLVM doesn’tneed much. This design goal aims to keep the lib/Support interface small andunderstandable which should foster its actual use and adoption.

No Duplicate Implementations

The implementation of a function for a given platform must be written exactlyonce. This implies that it must be possible to apply a function’simplementation to multiple operating systems if those operating systems canshare the same implementation. This rule applies to the set of operatingsystems supported for a given class of operating system (e.g. Unix, Win32).

No Virtual Methods

The Support Library interfaces can be called quite frequently by LLVM. In orderto make those calls as efficient as possible, we discourage the use of virtualmethods. There is no need to use inheritance for implementation differences, itjust adds complexity. The #include mechanism works just fine.

No Exposed Functions

Any functions defined by system libraries (i.e. not defined by lib/Support)must not be exposed through the lib/Support interface, even if the headerfile for that function is not exposed. This prevents inadvertent use of systemspecific functionality.

For example, the stat system call is notorious for having variations in thedata it provides. lib/Support must not declare stat nor allow it to bedeclared. Instead it should provide its own interface to discoveringinformation about files and directories. Those interfaces may be implemented interms of stat but that is strictly an implementation detail. The interfaceprovided by the Support Library must be implemented on all platforms (eventhose without stat).

No Exposed Data

Any data defined by system libraries (i.e. not defined by lib/Support) mustnot be exposed through the lib/Support interface, even if the header filefor that function is not exposed. As with functions, this prevents inadvertentuse of data that might not exist on all platforms.

Minimize Soft Errors

Operating system interfaces will generally provide error results for everylittle thing that could go wrong. In almost all cases, you can divide theseerror results into two groups: normal/good/soft and abnormal/bad/hard. That is,some of the errors are simply information like “file not found”, “insufficientprivileges”, etc. while other errors are much harder like “out of space”, “baddisk sector”, or “system call interrupted”. We’ll call the first group “soft”errors and the second group “hard” errors.

lib/Support must always attempt to minimize soft errors. This is a designrequirement because the minimization of soft errors can affect the granularityand the nature of the interface. In general, if you find that you’re wanting tothrow soft errors, you must review the granularity of the interface because itis likely you’re trying to implement something that is too low level. The ruleof thumb is to provide interface functions that can’t fail, except whenfaced with hard errors.

For a trivial example, suppose we wanted to add an “OpenFileForWriting”function. For many operating systems, if the file doesn’t exist, attempting toopen the file will produce an error. However, lib/Support should not simplythrow that error if it occurs because its a soft error. The problem is that theinterface function, OpenFileForWriting is too low level. It should beOpenOrCreateFileForWriting. In the case of the soft “doesn’t exist” error,this function would just create it and then open it for writing.

This design principle needs to be maintained in lib/Support because itavoids the propagation of soft error handling throughout the rest of LLVM.Hard errors will generally just cause a termination for an LLVM tool so don’tbe bashful about throwing them.

Rules of thumb:

  • Don’t throw soft errors, only hard errors.
  • If you’re tempted to throw a soft error, re-think the interface.
  • Handle internally the most common normal/good/soft error conditionsso the rest of LLVM doesn’t have to.

No throw Specifications

None of the lib/Support interface functions may be declared with C++throw() specifications on them. This requirement makes sure that thecompiler does not insert additional exception handling code into the interfacefunctions. This is a performance consideration: lib/Support functions areat the bottom of many call chains and as such can be frequently called. Weneed them to be as efficient as possible. However, no routines in the systemlibrary should actually throw exceptions.

Code Organization

Implementations of the Support Library interface are separated by their generalclass of operating system. Currently only Unix and Win32 classes are definedbut more could be added for other operating system classifications. Todistinguish which implementation to compile, the code in lib/Support usesthe LLVM_ON_UNIX and _WIN32 #defines. Each source file inlib/Support, after implementing the generic (operating system independent)functionality needs to include the correct implementation using a set of#if defined(LLVM_ON_XYZ) directives. For example, if we hadlib/Support/Path.cpp, we’d expect to see in that file:

  1. #if defined(LLVM_ON_UNIX)
  2. #include "Unix/Path.inc"
  3. #endif
  4. #if defined(_WIN32)
  5. #include "Windows/Path.inc"
  6. #endif

The implementation in lib/Support/Unix/Path.inc should handle all Unixvariants. The implementation in lib/Support/Windows/Path.inc should handleall Windows variants. What this does is quickly inc the basic classof operating system that will provide the implementation. The specific detailsfor a given platform must still be determined through the use of #ifdef.

Consistent Semantics

The implementation of a lib/Support interface can vary drastically betweenplatforms. That’s okay as long as the end result of the interface function isthe same. For example, a function to create a directory is pretty straightforward on all operating system. System V IPC on the other hand isn’t evensupported on all platforms. Instead of “supporting” System V IPC,lib/Support should provide an interface to the basic concept ofinter-process communications. The implementations might use System V IPC ifthat was available or named pipes, or whatever gets the job done effectivelyfor a given operating system. In all cases, the interface and theimplementation must be semantically consistent.