Sequence 5.1 Building stack frames in LLVM

Similar documents
Today's Topics. CISC 458 Winter J.R. Cordy

Pointer Analysis for C/C++ with cclyzer. George Balatsouras University of Athens

LLVM Tutorial. John Criswell University of Rochester

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

Compiler Construction: LLVMlite

CS 314 Principles of Programming Languages. Lecture 13

Homework I - Solution

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

CIS 341 Midterm March 2, 2017 SOLUTIONS

CA Compiler Construction

LLVM and IR Construction

Stacks and Function Calls

System Software Assignment 1 Runtime Support for Procedures

DEVIRTUALIZATION IN LLVM

LLVM code generation and implementation of nested functions for the SimpliC language

Baggy bounds with LLVM

Programming Languages

CS 161 Exam II Winter 2018 FORM 1

CS31 Discussion 1E Spring 17 : week 08

Lecture 23 CIS 341: COMPILERS

Semantic actions for declarations and expressions

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Semantic actions for declarations and expressions. Monday, September 28, 15

What about Object-Oriented Languages?

Project Compiler. CS031 TA Help Session November 28, 2011

Lecture08: Scope and Lexical Address

Naming in OOLs and Storage Layout Comp 412

CSE 307: Principles of Programming Languages

Run-time Environments - 2

CIT Week13 Lecture

Pointers. Memory. void foo() { }//return

CS113: Lecture 4. Topics: Functions. Function Activation Records

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks

G Programming Languages - Fall 2012

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Run-Time Data Structures

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

IR Generation. May 13, Monday, May 13, 13

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Memory and Pointers written by Cathy Saxton

Implementing Subprograms

COMP 524 Spring 2018 Midterm Thursday, March 1

CSC 2400: Computer Systems. Using the Stack for Function Calls

Targeting LLVM IR. LLVM IR, code emission, assignment 4

Compilation 2014 Activation Records

G Programming Languages - Fall 2012

Lecture 9 CIS 341: COMPILERS

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

Dispatch techniques and closure representations

Compiler construction 2009

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Pointers II. Class 31

Three-Address Code IR


Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

CSE 3302 Notes 5: Memory Management

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Semantic actions for declarations and expressions

Basic Types, Variables, Literals, Constants

Chapter 9 :: Subroutines and Control Abstraction

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Summer 2003 Lecture 14 07/02/03

4 Functional Programming Languages

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

CSCI312 Principles of Programming Languages!

Short Notes of CS201

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

CS201 - Introduction to Programming Glossary By

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Programming Languages: Lecture 12

ebpf-based tracing tools under 32 bit architectures

CS 314 Principles of Programming Languages. Lecture 11

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Technical Questions. Q 1) What are the key features in C programming language?

CS 314 Principles of Programming Languages

TDDE18 & 726G77. Functions

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Variation of Pointers

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Language Reference Manual simplicity

Wednesday, October 15, 14. Functions

TWISTER. a language designed for image manipulation

Lecture 2 Overview of the LLVM Compiler

TILE PROCESSOR APPLICATION BINARY INTERFACE

Chapter 10 Implementing Subprograms

Functions BCA-105. Few Facts About Functions:

CSC 2400: Computer Systems. Using the Stack for Function Calls

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Procedure Call. Procedure Call CS 217. Involves following actions

Research opportuni/es with me

Type Checking Binary Operators

C Programming Basics II

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Absolute C++ Walter Savitch

LLVM IR and Transform Pipeline. L25: Modern Compiler Design

Programming Languages Third Edition. Chapter 7 Basic Semantics

Transcription:

Sequence 5.1 Building stack frames in LLVM P. de Oliveira Castro S. Tardieu 1/13 P. de Oliveira Castro, S. Tardieu

Reminder: Stack frames We have seen earlier that: A function can access its local variables and parameters (a function parameter as seen from the function is no different than a local variable). A function can access local variables defined outside the function (escaping local variables) if they are lexically visible, as well as outer function parameters. Function parameters and escaping local variables are stored in a frame, which is itself stored onto the stack. This is also called the function stack frame. 2/13 P. de Oliveira Castro, S. Tardieu

Reminder: Stack frames (continued) Every frame contains a pointer to the stack frame one level up. This pointer is passed as an extra parameter to every function. Do not hesitate to refer to the sequence from week 3 again should you need to refresh your memory. 3/13 P. de Oliveira Castro, S. Tardieu

Building stack frames in LLVM We can use a simple yet powerful model in LLVM to build stack frames: For every function, we declare a new compound type with: the upper stack frame pointer; every escaping parameter; every escaping local variable created in this function. The appropriate stack frame pointer is passed as the first argument of every function call. At function entry, a new stack frame object of the proper compound type is allocated using alloca and the upper stack frame pointer and escaping parameters are copied. Escaping local variables are now uninitialized fields of this frame object. Non-escaping local variables and parameters are not stored in the frame. 4/13 P. de Oliveira Castro, S. Tardieu

Isn t that costly though? Creating an object using alloca, copying the parameters into it, and forcing escaping local variables to be fields of this object residing in physical memory has a cost. This cost is the price to pay to get access to those parameters and escaping local variables from an inner function. However, the mem2reg optimization pass of LLVM will take care of removing the extra copies if we do not need to pass a pointer to this frame object to another function, in which case there will be no cost. 5/13 P. de Oliveira Castro, S. Tardieu

Could we optimize it further? In a production-quality compiler, some optimizations would be implemented. For example: If a function does not call other functions, it does not need to build a frame object. This is already taken care of in the mem2reg optimization pass of LLVM. Such a function is called a leaf function. If a function does not use any outer variable and is a leaf function, it does not need to receive the outer frame pointer. Let us call it a pure function. If a function does not use any outer variable and only call pure functions, then it is itself a pure function and does not need to receive the outer frame pointer. This list of optimizations is in no way exhaustive. 6/13 P. de Oliveira Castro, S. Tardieu

Visualizing a frame (from sequence 3.3) let var a := 3 function f(b: int) = let function add() = a := a + b in add() end in f(2); print_int(a) /* Prints 5 */ end 7/13 P. de Oliveira Castro, S. Tardieu

Visualizing frame types in LLVM We build LLVM types named ft_ (for frame type) followed by the full path of the function. Here is the corresponding LLVM IR: %ft_main = type { i32 } # { a } %ft_main.f = type { %ft_main*, i32 } # { up, b } %ft_main.f.add = type { %ft_main.f* } # { up } Except for main, which has no frame up since it represents the toplevel, every frame starts with a pointer to an object of the lexically outer frame type (called static link). 8/13 P. de Oliveira Castro, S. Tardieu

Building the frame type in LLVM Creating a frame in LLVM is easy: llvm::structtype::create(context, members, name) creates a global structure with the given name in the compilation context. members is a vector of types contained in the structure. As for any other type, an object can be created on the stack using Builder.CreateAlloca(frame_type, nullptr, [name]). The second parameter is nullptr because we do not want to create an array, only a value. 9/13 P. de Oliveira Castro, S. Tardieu

Manipulating an object stored in the frame LLVM makes it easy to refer to objects through llvm::value pointers. Such a value might be: a constant; a reference to a global variable; a reference to a value on the stack; a reference pointed onto another llvm::value, possibly with an offset. In fact, a llvm::value can be viewed as a tree, and can reference other llvm::value. 10/13 P. de Oliveira Castro, S. Tardieu

Referencing an object in the frame The IR builder in LLVM has methods to help building complex llvm::value objects: CreateStructGEP(type, object, position) creates a value containing the address of a given field in a struct object whose type is given. GEP means Get Element Pointer. 11/13 P. de Oliveira Castro, S. Tardieu

Building the frame in LLVM Here is the IR code for the function f defined previously: define void @main.f(%ft_main* %sl, i32 %b) { # Allocate frame object %frame = alloca %ft_main.f # Store outer frame pointer (%sl) at first position in %frame %0 = getelementptr inbounds %ft_main.f, %ft_main.f* %frame, i32 0, i32 0 store %ft_main* %sl, %ft_main** %0 # Store %b parameter at second position in %frame %1 = getelementptr inbounds %ft_main.f, %ft_main.f* %frame, i32 0, i32 1 store i32 %b, i32* %1 # Call add function and give it a pointer to the frame object call void @main.f.add(%ft_main.f* %frame) ret void } 12/13 P. de Oliveira Castro, S. Tardieu

Conclusion A frame compound type is defined for every function. The frame contains a slot for the outer frame pointer (except at top-level), every escaping parameter, and every escaping local variable. At function entry, the frame is allocated and the outer frame pointer (also called static link) and function parameters are copied into the frame. Escaping local variables become fields of the frame object. 13/13 P. de Oliveira Castro, S. Tardieu