Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing Fortran Standardese within LFortran

F6baf93a0833a98bdc8184c214f4c468?s=47 Rohit Goswami
September 24, 2021

Implementing Fortran Standardese within LFortran

The Fortran standard is a rather imposing document; though with F2018 at around 600 pages it is significantly lighter than its C++ counterpart. The standard itself is rarely cited as a beginner resource, with a cottage industry of books designed to shed light on the dry prose and foreboding technical rigor. However, with LFortran, particularly viewed through the lens of the Abstract Semantic Representation (ASR) of any Fortran code, the standard suddenly gains clarity and rigor. Being as it is a Fortran-first representation, unlike GCC's internal representations (parse trees and lowered GIMPLE) or opaque (proprietary) middle-layers of NAG/Intel; the ASR is meant to concretely represent the syntax in a way to make the Standard seem eminently rational. Implementing the intrinsic functions and standard computing models become pleasant C++ puzzles instead of pidgin nightmares of progressively lower representations. In this presentation, I will discuss the process of implementing Standard intrinsic functions, the lifeline of any applied physical science researcher. LFortran allows for further flexibility at runtime, which will come up briefly, but most of the focus for this short talk will be on the compile time evaluation of intrinsic functions and future directions.

Site Post --> https://rgoswami.me/posts/fortrancon-2021-meta
FortranCon 2021 Link --> https://tcevents.chem.uzh.ch/event/14/contributions/69/

F6baf93a0833a98bdc8184c214f4c468?s=128

Rohit Goswami

September 24, 2021
Tweet

Transcript

  1. IMPLEMENTING FORTRAN STANDARDESE WITHIN LFORTRAN ROHIT GOSWAMI .and. ONDŘEJ ČERTÍK

    Created: 2021-09-24 Fri 01:14
  2. BRIEF INTRODUCTION 3

  3. HELLO! Find me here: Who? Rohit Goswami MInstP Doctoral Researcher,

    University of Iceland, Faculty of Physical Sciences https://rgoswami.me 4
  4. LOGISTICS All contents are Slides are in presentations/fortranCon2021/gsocLFortran hosted on

    GitHub Questions are welcome anytime 5
  5. LFORTRAN 7

  6. INTRODUCTION Language Files Lines Code Comments Blanks C 3 1023

    694 191 138 C Header 57 14237 11416 1041 1780 CMake 11 430 361 16 53 C++ 54 30745 26911 1560 2274 C++ Header 1 8938 8297 348 293 FORTRAN 20 1738 1344 174 220 Python 2 224 191 4 29 Total 148 57335 49214 3334 4787 8
  7. STRUCTURE

  8. FEATURES Runtime Libraries Pure Fortran | Impure ASR Guarantees compilation

    –> Wrappers Parser Currently almost all of F2018; including F77 LLVM Canonical backend, includes compile time evaluated expressions Jupyter Native execution as a kernel 10
  9. REPRESENTATIONS 12

  10. READING CODE I __attribute__((externally_visible)) main (integer(kind=4) argc, character(kind=1) * *

    argv) {integer(kind=4) D.3878; static integer(kind=4) options.1[7] = {2116, 4095, 0, 1, 1, 0, 31}; _gfortran_set_args (argc, argv); _gfortran_set_options (7, &options.1[0]); MAIN__ (); D.3878 = 0; return D.3878;} MAIN__ () {static integer(kind=4) i = 3; {struct __st_parameter_dt dt_parm.0; try {dt_parm.0.common.filename = &"hi.f90"[1]{lb: 1 sz: 1}; dt_parm.0.common.line = 3; dt_parm.0.common.flags = 128; dt_parm.0.common.unit = 6; _gfortran_st_write (&dt_parm.0); _gfortran_transfer_integer_write (&dt_parm.0, &i, 4); _gfortran_transfer_character_write (&dt_parm.0, &"Hello World"[1]{lb: 1 sz: 1}, 1 _gfortran_st_write_done (&dt_parm.0);} finally {dt_parm.0 = {CLOBBER};}}} GIMPLE is an internal gfortran representation… 13
  11. READING CODE II AST on the left ASR on the

    right 14
  12. READING CODE III program main integer :: i=3 print*, i,

    "Hello World" end program lfortran has a nicer intermediate structure conda create -n lf conda activate lf conda install lfortran \ -c conda-forge lfortran --show-asr hi.f90 15
  13. FORTRAN AND STANDARDESE The ASR can be used to implement

    the standard verbatim 17
  14. REAL DEFINITION

  15. REAL ARGUMENTS if (func_name == "real") { if (args.n ==

    1) { ASR::expr_t* real_expr = args[0]; int real_kind = LFortran::ASRUtils::extract_kind_from_ttype_t (func_type); if (LFortran::ASR::is_a<LFortran::ASR::Real_t>(*func_type)) { if (real_kind == 4){ float rr = ASR::down_cast<ASR::ConstantReal_t> (LFortran::ASRUtils::expr_value(real_expr))->m_r; value = ASR::down_cast<ASR::expr_t> (ASR::make_ConstantReal_t(al, x.base.base.loc, rr, func_type)); } else { double rr = ASR::down_cast<ASR::ConstantReal_t> (LFortran::ASRUtils::expr_value(real_expr))->m_r; value = ASR::down_cast<ASR::expr_t> (ASR::make_ConstantReal_t(al, x.base.base.loc, rr, func_type)); } } // TODO: Handle BOZ later } else { throw SemanticError("REAL must have only one argument", x.base.base.loc); } 19
  16. INTEGER ARGUMENTS else if (LFortran::ASR::is_a<LFortran::ASR::Integer_t>(*func_type)) { if (real_kind == 4){

    int64_t rv = ASR::down_cast<ASR::ConstantInteger_t>( LFortran::ASRUtils::expr_value(real_expr))->m_n; float rr = static_cast<float>(rv); value = ASR::down_cast<ASR::expr_t> (ASR::make_ConstantReal_t(al, x.base.base.loc, rr, func_type)); } else { double rr = static_cast<double>(ASR::down_cast <ASR::ConstantInteger_t>(LFortran::ASRUtils:: expr_value(real_expr))->m_n); value = ASR::down_cast<ASR::expr_t> (ASR::make_ConstantReal_t (al, x.base.base.loc, rr, func_type)); } } 20
  17. CONCLUSIONS 22

  18. OMITTED TOPICS Details here

  19. THE END 25

  20. ACKNOWLEDGEMENTS as my supervisor, as my co-supervisor, and my committee

    member at Los Alamos National Laboratory ( , and ) Family, pets, Groupmembers, audience Prof. Hannes Jónsson Prof. Birgir Hrafnkelsson Dr. Elvar Jonsson Dr. Ondřej Čertík Quansight Labs Dr. Ralf Gommers Dr. Melissa Weber Mendonça Dr. Pearu Peterson 26
  21. THANKS! 27