/
Automating Fortran - C Interoperability Automating Fortran - C Interoperability

Automating Fortran - C Interoperability - PowerPoint Presentation

cheryl-pisano
cheryl-pisano . @cheryl-pisano
Follow
357 views
Uploaded On 2018-09-22

Automating Fortran - C Interoperability - PPT Presentation

Prepared by Sisi Liu Garnet Mentor Dan Nagle CoMentor Davide del Vento 1 Background FortranC Interoperability Scientific programs supported by the large systems at NCAR are often written in a combination of Fortran and ID: 675004

type fortran declaration source fortran type source declaration types ast function tool object implementation

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Automating Fortran - C Interoperability" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Automating Fortran - C Interoperability

Prepared by: Sisi Liu (Garnet)Mentor: Dan NagleCo-Mentor: Davide del Vento

1Slide2

Background: Fortran-C Interoperability

Scientific programs supported by the large systems at NCAR are often written in a combination of Fortran and CFortran modules connect C

library functions to the Fortran

program with Fortran compiler’s support

Currently, modules are written manually to express C interfaces. In other words, the translation from C to Fortran is done by hand.

2Slide3

Background: Fortran-C Interoperability

3Slide4

Introduction: Automating Fortran Project

What is the object of this project?Build a source to source translation toolTranslate C headers to Fortran modules to let Fortran programs use C library functions

Why such tool is needed?

Automated module generation helps

avoid redundant work and error-prone manual laboringEasy for maintenance and update

4Slide5

Introduction: Automating Fortran Project

5Slide6

Approaches: Finding the Right Tool

ANTLR

Lexer

and p

arser tool

– manually write the parser, last

resort

GCC

GENERIC – complicated and hard to modify

GIMPLE – unused

variables are optimized (nearly the entire

header is removed in the output)

ROSE Compiler

Intermediate representation

and support both C and Fortran

lack of public

APIsClang LLVMClang AST – modular design, abundant APIs

6Slide7

Abstract Syntax Tree (AST)

A tree representation of the abstract syntactic structure of codewidely used in compilers as intermediate representation

Language-independent

A Simple Abstract

Syntax Tree

Example, tokens of the source code become tree nodes.

7Slide8

Steps to Automate

Source-to-Source TranslationLexical analysis and parse C to AST

Traversing through each node of AST recursively

Identify the node: cast to specific declaration, type or expression

Tree structure. May expand to more nodes. (e.g.

FunctionDecl

expands to return type and multiple

VarDecl

)

Create a object for each node with all needed info for translation

Unparse

the content of each node to Fortran code by calling the “

formatASString

()” method of this object to dump the Fortran code

8Slide9

Source-to-Source Translation Workflow

9Slide10

Implementation—Using Clang Tool to Traverse AST

Clang AST

Node

Structure Overview

Declaration

FunctionDecl

—declares functions, might contain a function body

VarDecl

—declares values or types to variables

RecordDecl

—declares

struct

or union

EnumDecl

—declares enumerations

TypeDefDecl

—defines a type to variablesStatement (function body, unlikely to be seen in headers)Type (inside declaration)Iso_c_binding—convert intrinsic C types to Fortran typesType defined identifiersTypes can be more complicated10Slide11

The source code can expand to very long abstract syntax tree!

11Slide12

Implementation—Declarations

Function Declaration

Expand to

r

eturn and argument type

Support

intrinsic

types: all literal type,

c

ptr

and

funptr

, and type defined identifiers

Function body is preserved as comment

Unnamed arguments are automatically named as arg_1, arg_2

Add import attribute to import derived types12Slide13

Implementation—Declarations

Record Declaration (

struct

)

and

Typedef

Declaration

Translate as derived types in Fortran

Identifier is required in Fortran but not in C

Anonymous

struct

will be preserved as comment

Enumeration

Declaration

Directly translate to Fortran

Enumeration

13Slide14

Implementation— Variable Declarations

VarDecl without an init valueSupport all intrinsic and declared types

Names that are valid in C (“_id”) but not in Fortran are commented out

VarDecl

with an init value

Support literal type, pointers, string and multidimensional array

14Slide15

Implementation—Macros are Different

Macros are not part of AST but preprocessorPreprocessor functions only return the macro kind (object or function) and the source textUnlike dealing with object-oriented AST, values

or types have to be manually

parsed

Limited types (only intrinsic types) are supported

15Slide16

Implementation—Macros

Object MacroLiteral value declaration (e.g.

#define INT_VARIABLE 128

)->variable

declarationAlias type declaration (e.g.

#define long unsigned long

int

)->derived

type

Identifier declaration

(e.g.

#define

id1 id2

) -> no type information, so commented out

Function Macro (e.g.

#define SQUARE(x) (x*x)

)Lack of argument and return types (assume to be integer) ->function declaration16Slide17

Example:

PTHREAD_CANCELED is commented out due to the value’s type is non-trivial

PTHREAD_MUTEX_DEFAULT is commented out because there is no type information for this declaration

17Slide18

Overall Performance

About 40% of the system headers in the /usr/include can be correctly compiledAnother 40% headers contain unavoidable compilation errors (minor errors that need to be manually modified)

Same module (file name) and function name, not allowed in Fortran but allowed in C

Some symbols are valid in C but not in Fortran (e.g. ‘_id’ and ‘id-1’)

Type defined (tag named

)

struct

with an identifier will be declared twice due to they are 2 separate nodes in AST

18Slide19

Future Work

Potential improvements for the rest 20% headersNon-intrinsic undeclared types cannot be recognized such as unrecognized_type

(void (

Tcl_Time

*,

ClientData

)

No

suitable auto

translation for Union in

Fortran

Remove the redundant

struct

that is defined twice

Out of source types that are defined other files can cause

u

ndeclared variables errors19Slide20

Brief Demo

20

Shell

script of using the tool to generate

pthread

module

A mini

Fortran hello world

program that uses Fortran

pthread

module Slide21

Acknowledgement

Many thanks to:Mentors: Dan Nagle and Davide Del Vento

NCAR and

SIParCS

staff

21Slide22

References

H2m tool repository:https://github.com/garnetliu/llvm-clang-autoFortranTool

Building instruction:

https

://

github.com/garnetliu/llvm-clang-autoFortranTool/blob/master/Tool%20Building%20Instruction.pdf

Figure reference:

Performing

Source-to-Source Transformations with Clang by Olaf

Krzikalla

http://llvm.org/devmtg/2013-04/krzikalla-slides.pdf

22