# # Copyright (C)  Futurewei Technologies, Inc. All rights reverved. # # Licensed under the Mulan Permissive Software License v2. # You can use this software according to the terms and conditions of the MulanPSL - 2.0. # You may obtain a copy of MulanPSL - 2.0 at: # # https://opensource.org/licenses/MulanPSL-2.0 # # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR # FIT FOR A PARTICULAR PURPOSE. # See the MulanPSL - 2.0 for more details. #
Copyright (C) 2019, 2020 XC5 Limited, Inc. All Rights Reserved.
The purpose of this work is to provide translation tools from the open64 compiler and clang to the open ark (Maple) compiler, so it can benefit by having a stable C and C++ front-end.
This repository is based on https://github.com/open64-compiler/open64. The master branch will be kept in sync with the base repository at github. Changes related to Maple are contained in these branches: ark, ark-aarch64 and ark-riscv.
In order to able to generate Maple IR, the following 4 folders contain code copied from corresponding folders in https://gitee.com/openarkcompiler-incubator/mapleall:
osprey/maple_ir osprey/maple_util osprey/mempool ("mempool" inside file names substituted by "MP") osprey/huawei_secure_c
The open64 compiler in the current form only supports the x86_64 target. The ark branch contains the above additions plus the implementation of whirl2mpl and mpl2whirl in osprey/ir_tools.
The ark-aarch64 branch is based on the ark branch but contains additional changes required to support the aarch64 CPU target. This includes code that makes the open64 GNU4.2 front-end targets aarch64.
The ark-riscv branch is based on the ark branch but contains additional changes required to support the riscv64 CPU target.
In addition to this repository, it is necessary to download the llvm and clang source tarballs from https://llvm.org. The latest 11.0.0 version is recommended. The following build steps assume starting in a work folder that contains this repository (open64) and the downloaded llvm-11.0.0.src.tar.xz and clang-11.0.0.src.tar.xz.
tar xf llvm-11.0.0.src.tar.xz
tar xf clang-11.0.0.src.tar.xz
ln -sf `pwd`/clang-11.0.0.src llvm-11.0.0.src/tools/clang
mkdir build && cd build
At this point, the 11.0.0 folder contains the built clang/llvm binaries. The remaining steps are for building open64. The clang2whirl part of open64 needs to link with the built clang/llvm libraries. The environment variable $CLANG_HOME needs to be set to give the path to the built clang and llvm binaries.
mkdir ../obj; cd ../obj
../open64/configure --disable-fortran --build=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-optimize=DEBUG --disable-multilib
After this last step, the built open64 components have been installed under the specified installation directory. In particular, under the bin sub-directory are the opencc, openCC, clangfe and whirl2mpl executables. Any of the 4 branches (master, ark, ark-aarch64, ark-riscv) can be used in the build.
The above build steps can be avoided by downloading and installing the pre-built installation tarballs at https://gitee.com/open64ark/open64_prebuilt.
The open64 GNU front-end supports only the x86_64 target. If the master or ark branch is used, the built compiler can be used for x86_64 compilation:
% opencc foo.c
will compile using the gnu4.2 front-end and generate x86_64 binaries.
% opencc -clang foo.c
will compile using clang as front-end but output open64's Whirl IR (.B) instead of llvm IR. The compilation will continue using open64 components to generate x86_64 binaries. The advantage of using clang is that it supports the new language features in the latest C and C++ standards. If the compilation has problem finding some system header files, adding
can solve the problem, assuming the system has the llvm compiler installed.
Compiling for the aarch64 and riscv64 CPU targets is where the ark compiler comes into the picture. In fact, the main purpose of this repository is to provide a stable C and C++ front-end for the ark compiler. We always need to generate the Whirl IR file (.B) first.
When built using the ark-aarch64 or ark-riscv branch, the built compilers similarly provide 2 different front-ends for generating .B files:
Because open64's GNU4.2 front-end assumes the x86_64 target, the generated Whirl is hacked to be usable for aarch64. But because the GNU4.2 front-end is dated and not actively maintained over the years, this choice should only be used with this in mind. We only need opencc/openCC to generate the whirl file, so we give the -fe flag so it only runs the front-end. We also need to give the -keep flag to make it save the .B file:
% opencc -fe -keep foo.c
Using clang is advantageous not just because it is up-to-date with respect to the language standards, but also because it supports all the main-stream CPU targets. The target it generates code for is controlled using the -triple command line option. This is the most stable C/C++ front-end for the ark compiler targeting aarch64 and riscv64.
For aarch64 C compilation, we recommend invoking clangfe as follows:
% clangfe -cc1 -emit-llvm -triple aarch64-linux-gnu -D__clang__ -D__BLOCKS__ -isystem /usr/aarch64-linux-gnu/include -isystem /usr/lib/gcc-cross/aarch64-linux-gnu/5/include
And for aarch64 C++ compilation:
% clangfe -cc1 -emit-llvm -triple aarch64-linux-gnu -D__clang__ -D__BLOCKS__ -fcxx-exceptions -isystem /usr/aarch64-linux-gnu/include/c++/5 -isystem /usr/aarch64-linux-gnu/include/c++/5/aarch64-linux-gnu -isystem /usr/aarch64-linux-gnu/include -isystem /usr/lib/gcc-cross/aarch64-linux-gnu/5/include -xc++
The above assumes the gnu cross compiler for aarch64 has been installed.
For riscv64 C compilation:
% clangfe -cc1 -emit-llvm -triple riscv64-linux-gnu -D__clang__ -D__BLOCKS__ -D__riscv_xlen=64 -U __riscv_float_abi_soft -D__riscv_float_abi_double -isystem /usr/riscv64-linux-gnu/lib/gcc/riscv64-unknown-linux-gnu/10.1.0/include -isystem /usr/riscv64-linux-gnu/sysroot/usr/include
And for riscv64 C++ compilation:
% clangfe -cc1 -emit-llvm -triple riscv64-linux-gnu -D__clang__ -D__BLOCKS__ -D__riscv_xlen=64 -U __riscv_float_abi_soft -D__riscv_float_abi_double -fcxx-exceptions -isystem /usr/riscv64-linux-gnu/riscv64-unknown-linux-gnu/include/c++/10.1.0 -isystem /usr/riscv64-linux-gnu/riscv64-unknown-linux-gnu/include/c++/10.1.0/riscv64-unknown-linux-gnu -isystem /usr/riscv64-linux-gnu/lib/gcc/riscv64-unknown-linux-gnu/10.1.0/include -isystem /usr/riscv64-linux-gnu/sysroot/usr/include -xc++
The above assumes the gnu cross compiler for riscv64 has been installed.
can be added if needed.
After the .B file has been generated, we use whirl2mpl to convert the .B file to Maple IR:
% whirl2mpl foo.B
will produce foo.bpl, while
% whirl2mpl -a foo.B
will produce foo.mpl.
With the .bpl or .mpl Maple IR files, the ark compiler can be used to finish the compilation. The ark compiler that supports C and C++ is at https://gitee.com/openarkcompiler-incubator/mapleall.