Presentation is loading. Please wait.

Presentation is loading. Please wait.

Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1.

Similar presentations


Presentation on theme: "Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1."— Presentation transcript:

1 Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1

2 Background: Fortran-C Interoperability Scientific programs supported by the large systems at NCAR are often written in a combination of Fortran and C Fortran 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. 2

3 Background: Fortran-C Interoperability 3

4 Introduction: Automating Fortran Project What is the object of this project? – Build a source to source translation tool – Translate 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 laboring – Easy for maintenance and update 4

5 Introduction: Automating Fortran Project 5

6 Approaches: Finding the Right Tool ANTLR – Lexer and parser 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 APIs Clang LLVM – Clang AST – modular design, abundant APIs 6

7 Abstract Syntax Tree (AST) A tree representati on of the abstract syntactic structure of code widely used in compilers as intermediate representation Language- independent A Simple Abstract Syntax Tree Example, tokens of the source code become tree nodes. 7

8 Steps to Automate Source-to-Source Translation 1.Lexical analysis and parse C to AST 2.Traversing through each node of AST recursively i.Identify the node: cast to specific declaration, type or expression ii.Tree structure. May expand to more nodes. (e.g. FunctionDecl expands to return type and multiple VarDecl) iii.Create a object for each node with all needed info for translation 3.Unparse the content of each node to Fortran code by calling the “formatASString()” method of this object to dump the Fortran code 8

9 Source-to-Source Translation Workflow 9

10 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 variables – Statement (function body, unlikely to be seen in headers) – Type (inside declaration) Iso_c_binding—convert intrinsic C types to Fortran types Type defined identifiers Types can be more complicated 10

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

12 Implementation—Declarations Function Declaration – Expand to return 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 types 12

13 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 13

14 Implementation— Variable Declarations VarDecl without an init value – Support 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 14

15 Implementation—Macros are Different Macros are not part of AST but preprocessor Preprocessor functions only return the macro kind (object or function) and the source text Unlike dealing with object-oriented AST, values or types have to be manually parsed Limited types (only intrinsic types) are supported 15

16 Implementation—Macros Object Macro – Literal value declaration (e.g. #define INT_VARIABLE 128 )->variable declaration – Alias 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 declaration 16

17 Example: PTHREAD_CANCELED is commented out due to the value’s type is non-trivial PTHREAD_MUTEX_DEFAUL T is commented out because there is no type information for this declaration 17

18 Overall Performance About 40% of the system headers in the /usr/include can be correctly compiled Another 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 18

19 Future Work Potential improvements for the rest 20% headers – Non-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 undeclared variables errors 19

20 Brief Demo 20 Shell script of using the tool to generate pthread module A mini Fortran hello world program that uses Fortran pthread module

21 Acknowledgement Many thanks to: Mentors: Dan Nagle and Davide Del Vento NCAR and SIParCS staff 21

22 References H2m tool repository: – https://github.com/garnetliu/llvm-clang-autoFortranTool https://github.com/garnetliu/llvm-clang-autoFortranTool Building instruction: – https://github.com/garnetliu/llvm-clang- autoFortranTool/blob/master/Tool%20Building%20Instruct ion.pdf https://github.com/garnetliu/llvm-clang- autoFortranTool/blob/master/Tool%20Building%20Instruct ion.pdf Figure reference: – Performing Source-to-Source Transformations with Clang by Olaf Krzikalla http://llvm.org/devmtg/2013-04/krzikalla-slides.pdf 22


Download ppt "Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1."

Similar presentations


Ads by Google