Walk-through flang – Part 1

10 篇文章 1 订阅

Flang is an open  source project to create a Fortran compiler for LLVM. It is based on NVIDIA/PGI Fortran and it has been released under Apache License 2.0. In this series we will do a walk-through the code of this compiler and how it has been integrated in the existing LLVM infrastructure.

Introduction

Fortran is a very old programming language. Invented in 1956 by a team in IBM led by John Backus, Fortran represented the first successful high-level programming language system which generated code that was realistically efficient. Much has happened since then, Fortran was standardized in Fortran 77 and then in Fortran 90, Fortran 95, Fortran 2003 and Fortran 2008. Most commercial offerings nowadays support Fortran 2003 and some subset of Fortran 2008. Today Fortran is a niche language which is mostly used for numerical computing and high-performance computing (HPC) though newer languages like Julia aim at replacing it.

The GNU project in his GNU Compiler Collection (GCC), has for long had a Fortran compiler: first g77 (Fortran 77 only) and then gfortran (Fortran 95 and later versions). The LLVM project, being a newer project, never had such a compiler. I believe that at some point the flang project will fill this gap although this may not happen in the short-term.

The LLVM project

The LLVM project is an umbrella project for the development of compilers using open-source modular components. In contrast to GCC, LLVM uses a permissive license which, in principle, is more appealing for companies that develop commercial products on top of those components. The LLVM infrastructure lies around the LLVM IR which is basically a common representation for the middle-end of the compiler, where most target-independent transformations happen, but the project also includes a C/C++ compiler (clang), an implementation of the C++ Standard Library (libcxx), a linker (lld), a debugger (lldb) and other compilation-related components.

Installation of flang

Before we dig in the code of flang, we will want to install it. Fortunately this is documented in the upstream README.md but I will repeat the steps here.

First choose a path where you will install LLVM, clang and flang. Let's call this path the INSTALLDIRdirectory and put it in an environment variable. Make sure it is an absolute path.

$ export INSTALLDIR=/some/path/to/install/flang

Also choose a directory where you will fetch the source code and build the components, it should be diferent to INSTALLDIR. I will call this directory STAGEDIR and inside of it I will create STAGEDIR/build, which I will call the BUILDDIR.

$ cd $STAGEDIR
$ mkdir build
$ export BUILDDIR=STAGEDIR/build

Inside STAGEDIR, let's fetch first the code of LLVM 4.0, the command below will create a directory STAGEDIR/llvm.

$ cd $STAGEDIR
$ git clone --depth 1 -b release_40 https://github.com/llvm-mirror/llvm.git

Now, inside STAGEDIR/llvm/tools, check out the code of a clang that has been modified to be able to invoke the flang components. This will create a directory STAGEDIR/llvm/tools/clang

$ cd $STAGEDIR/llvm/tools
$ git clone --depth 1 -b flang_release_40 https://github.com/flang-compiler/clang.git

One dependence of flang is the Intel OpenMP Runtime Library (OpenMP RTL), so we have to check it out inside STAGEDIR/llvm/projects. This will create a directory STAGEDIR/llvm/projects.

$ cd $STAGEDIR/llvm/projects
$ git clone -b release_40 https://github.com/llvm-mirror/openmp.git

At this point we can already build llvm, clang and the OpenMP RTL. You can also remove -G Ninjaif you prefer to use the Makefile generator instead of the ninja one. We are passing the option -DBUILD_SHARED_LIBS=ON because we are building the Debug version of llvm/clang, otherwise, because of debug information, the static binaries are huge and take a lot of time and memory of the system to link, this should not make any difference in the final setup. If you do not want to build with debug information you can pass the option -DCMAKE_BUILD_TYPE=Release in all invocations of cmakebelow.

$ cd $BUILDDIR
$ cmake ../llvm/ \
   -G Ninja \
   -DBUILD_SHARED_LIBS=ON \
   -DCMAKE_INSTALL_PREFIX=$INSTALLDIR
<... lots of output ...>
$ ninja
<... takes some time ...>
$ ninja install

The ninja build step will take several minutes depending on your machine. Be patient. A few warnings may appear in particular when building the OpenMP RTL. Once this step completes now we have llvm and clang but not flang yet.

Now let's checkout flang code inside STAGEDIR. This will create a directory STAGEDIR/flang.

$ cd $STAGEDIR
$ git clone https://github.com/flang-compiler/flang.git

Now create a build-flang directory where we will build flang only.

$ cd $STAGEDIR
$ mkdir build-flang

Building flang has to be done using the just compiled clang so we have to pass more flags to cmake. Unfortunately the ninja generator of cmake still does not support Fortran, so we will have to use the regular Makefile generator. We cannot use -DBUILD_SHARED_LIBS=ON either due to some problems in the layering of the code. The option -DCMAKE_INSTALL_RPATH=$INSTALLDIR/lib works around a problem when linking Fortran programs in flang. Also compiling in parallel may fail, if it happens, simply invoking again make without the -j option should do.

$ cd $STAGEDIR/build-flang
$ cmake ../flang \
    -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
    -DCMAKE_CXX_COMPILER=$INSTALLDIR/bin/clang++ \
    -DCMAKE_C_COMPILER=$INSTALLDIR/bin/clang \
    -DCMAKE_Fortran_COMPILER=$INSTALLDIR/bin/flang \
    -DCMAKE_INSTALL_RPATH=$INSTALLDIR/lib
$ make -j$(nproc)
<... takes some time and prints lots of things ...>
$ make install

Once this process completes we should have flang installed. Let's make some smoke test. Create a file test.f90 with the following contents.

test.f90

PROGRAM MAIN
  IMPLICIT NONE
  INTEGER :: X

  PRINT *, "Please, enter a number"
  READ (*, *) X
  PRINT *, "The square root of ", X, " is ", SQRT(X)
END PROGRAM MAIN

Now build it using flang.

$ $INSTALLDIR/bin/flang -o test test.f90

If you try to run it, however, you will see this error.

$ ./test
./test: error while loading shared libraries: libflang.so: cannot open shared object file: No such file or directory

This is kind of expected, the program is not finding the libraries of the Fortran runtime. There are several solutions to this problem, but the simpler one is the following.

$ export LD_LIBRARY_PATH=$INSTALLDIR/lib:$LD_LIBRARY_PATH

Now it should work (type a number and press Enter, in the example below I used 17).

$ ./test
 Please, enter a number
17
 The square root of     17.00000      is     4.123106    

Environment script

It may be convenient to create a small script to configure the environment correctly before using flang.

$ echo "export PATH=$INSTALLDIR/bin:\$PATH" > flang-env.sh
$ echo "export LD_LIBRARY_PATH=$INSTALLDIR/lib:\$LD_LIBRARY_PATH" >> flang-env.sh

Before using flang simply loading this script will do.

$ source flang-env.sh

Installation script

I made a simple installation script that does the steps shown above. This script should be run inside an empty directory and it will use the current directory as STAGEDIR and STAGEDIR/install as INSTALLDIR. Also note that this script does not check if you have already cloned the git repositories, so if you want to replay part of the process you may need to comment parts of it.

Ok, that's enough for today. In the next chapter we will see the high level workflow when compiling with flang.

 

 

 

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值