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

Implementing Fortran Standardese within LFortran

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/

Rohit Goswami

September 24, 2021
Tweet

More Decks by Rohit Goswami

Other Decks in Programming

Transcript

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

    University of Iceland, Faculty of Physical Sciences https://rgoswami.me 4
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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