Easy IR generation Kai Nacke 3 February 2019 LLVM dev FOSDEM‘19

Slides:



Advertisements
Similar presentations
Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
Advertisements

Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Compiler Construction Intermediate Code Generation.
Program Representations. Representing programs Goals.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cpeg421-08S/final-review1 Course Review Tom St. John.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
ECE1724F Compiler Primer Sept. 18, 2002.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Chapter 8 . Sequence Control
SMIILE Finaly COBOL! and what else is new Gordana Rakić, Zoran Budimac.
Chapter 2 A Simple Compiler
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Advanced Compiler Design An Introduction to the Javali Compiler Framework Zoltán Majó 1.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Gordana Rakić, Zoran Budimac
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Control Flow Analysis Compiler Baojian Hua
LLVM IR, File - Praakrit Pradhan. Overview The LLVM bitcode has essentially two things A bitstream container format Encoding of LLVM IR.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 9 Symbol Table and Attributed Grammars
Simone Campanoni CFA Simone Campanoni
Basic Program Analysis
Names and Attributes Names are a key programming language feature
Profile-Guided Optimization in the LDC D compiler
A Simple Syntax-Directed Translator
Parsing & Context-Free Grammars
Introduction to Parsing (adapted from CS 164 at Berkeley)
Overview of Compilation The Compiler BACK End
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
-by Nisarg Vasavada (Compiled*)
CS 536 / Fall 2017 Introduction to programming languages and compilers
LLVM Pass and Code Instrumentation
Chapter 9 :: Subroutines and Control Abstraction
Basic Program Analysis: AST
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Intermediate Representations Hal Perkins Autumn 2011
Control Flow Analysis CS 4501 Baishakhi Ray.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
CSC 533: Programming Languages Spring 2015
Designing a Predictive Parser
Statement-Level Control Structures
Control Flow Analysis (Chapter 7)
Intermediate Representations Hal Perkins Autumn 2005
Data Flow Analysis Compiler Design
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Code Generation Part II
COP4020 Programming Languages
COP4020 Programming Languages
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Easy IR generation Kai Nacke 3 February 2019 LLVM dev room @ FOSDEM‘19 Roll your own compiler Easy IR generation Kai Nacke 3 February 2019 LLVM dev room @ FOSDEM‘19 Hello! I am Kai.

What are the obstacles of IR generation? 03.02.2018 Roll your own compiler | Kai Nacke

Roll your own compiler | Kai Nacke Modula-2 First implementation in 1979 for the PDP-11 Complete language Carefully designed syntax Module concept Low-level facilities and procedure types Large code base available The Lilith operating system The GMD compiler toolbox (“cocktail”) Later standardized as ISO 10514 (* Taken from PIM4, page 25. *) MODULE gcdlcm; FROM InOut IMPORT ReadInt, WriteLn, WriteString, WriteInt; VAR x, y, u, v: INTEGER; BEGIN WriteString("x = "); ReadInt(x); WriteLn; WriteString("y = "); ReadInt(y); u := x; v := y; WHILE x # y DO IF x > y THEN x := x - y; u := u + v ELSE y := y - x; v := v + u END END; WriteInt(x, 6); WriteInt((u+v) DIV 2, 6); WriteLn END gcdlcm. 03.02.2018 Roll your own compiler | Kai Nacke

m2lang – The LLVM-based Modula-2 compiler ANTLR Semantic Codegen .m2 Source AST Decorated AST LLVM IR Modula-2 grammar provided by ANTLR Semantic phase and IR generation hand-coded Semantic phase uses hand-coded AST Goal: replace ANTLR with RD-parser Source will be published here: https://github.com/redstar/m2lang 03.02.2018 Roll your own compiler | Kai Nacke

Roll your own compiler | Kai Nacke Basic blocks IR instructions go into a basic block A basic block is a single entry single exit section of code Entry is with first instruction, usually marked with a label Ends with a terminating instruction, e.g. conditionally/unconditionally branch, return Optimization is usually applied to basic blocks All basic blocks of a function form a control flow graph (CFG) if: %6 = load i32, i32* %3, align 4 %7 = load i32, i32* %4, align 4 %8 = icmp ne i32 %6, %7 br i1 %8, label %then, label %else 03.02.2018 Roll your own compiler | Kai Nacke

The naive approach to IR generation Define a visitor holding pointer to current basic block Traverse the AST and generate IR Create a new basic block if needed 03.02.2018 Roll your own compiler | Kai Nacke

The trouble with naive approach Naive approach works well with simple arithmetic Now consider nested IF-THEN-ELSE-END structures Current block can be empty (“END”) Generates blocks with branch instruction only IF a > 0 THEN IF b > c THEN (* ... *) ELSE END ; … br label %end4 end4: br label %end5 ;… end5: 03.02.2018 Roll your own compiler | Kai Nacke

The gap between AST and LLVM IR The AST is more closely to the textual representation The basic blocks form a control flow graph Generation of „branch only“ basic blocks is result of this mismatch AST CFG of basic blocks 03.02.2018 Roll your own compiler | Kai Nacke

Roll your own compiler | Kai Nacke How to resolve Do not care – let LLVM optimize it away Simple Induce the CFG on the AST Just adds a pointer to the AST („next basic block“) Can be constructed very fast with recursive visitor Explicitly construct the CFG Costly if only done for construction of IR 03.02.2018 Roll your own compiler | Kai Nacke

Transform AST into high-level CFG Goal is to transform the AST into a representation closer to a CFG Lower high-level constructs in low-level constructs Replace FOR, WHILE, REPEAT with LOOP/EXIT Replace AND/OR with nested IF Introduce GOTO Lowering every implicit jump into a GOTO creates a CFG Think how to preserve debug metadata! You now have created your own IR! WHILE a > b DO (* Stmts *) END LOOP IF a > b THEN EXIT END; (* Stmts *) END 03.02.2018 Roll your own compiler | Kai Nacke

When is another IR needed? Creating another IR can be helpful Elaborate type checking Scope checking Generating synthetic code (e.g. cleanup handlers) Do only when needed Modula-2 seems to be simple enough to go without new IR Be careful Do not replicate LLVM functionality at a higher level Consider adding a new LLVM pass instead 03.02.2018 Roll your own compiler | Kai Nacke

Roll your own compiler | Kai Nacke Thank you! 03.02.2018 Roll your own compiler | Kai Nacke