From ab466fd0ed815cbf8445836051c7027cf957b2e3 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Thu, 25 Nov 2021 13:42:27 +0300 Subject: [PATCH 1/5] [mapleall][driver] Add "target" and "isystem" as default options for clang-ast generator These options are needed to correct compile for aarch64. These options were tested with SPECCPU2017 benchmark. --- .../defs/default/O0_options_clang.def | 3 +- .../include/driver_option_common.h | 1 + .../maple_driver/src/clang_compiler.cpp | 52 ++++++++++++++++--- .../maple_driver/src/driver_option_common.cpp | 9 ++++ 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/mapleall/maple_driver/defs/default/O0_options_clang.def b/src/mapleall/maple_driver/defs/default/O0_options_clang.def index a8edd35c27..fb70797f01 100644 --- a/src/mapleall/maple_driver/defs/default/O0_options_clang.def +++ b/src/mapleall/maple_driver/defs/default/O0_options_clang.def @@ -1,2 +1 @@ -{"-emit-ast", "", false}, -{"-o", "", false}, \ No newline at end of file +{"-emit-ast", "", false}, \ No newline at end of file diff --git a/src/mapleall/maple_driver/include/driver_option_common.h b/src/mapleall/maple_driver/include/driver_option_common.h index 5f6f08ee4d..3607a328df 100644 --- a/src/mapleall/maple_driver/include/driver_option_common.h +++ b/src/mapleall/maple_driver/include/driver_option_common.h @@ -71,6 +71,7 @@ enum DriverOptionIndex { kLdLibPath, kClangMacro, kClangInclude, + kClangISystem, kMapleOut, kCommonOptionEnd, }; diff --git a/src/mapleall/maple_driver/src/clang_compiler.cpp b/src/mapleall/maple_driver/src/clang_compiler.cpp index 002994d595..6f44c4e882 100644 --- a/src/mapleall/maple_driver/src/clang_compiler.cpp +++ b/src/mapleall/maple_driver/src/clang_compiler.cpp @@ -29,16 +29,56 @@ const std::string &ClangCompiler::GetBinName() const { return kBinNameClang; } +static uint32_t FillSpecialDefaulOpt(std::unique_ptr &opt, + const Action &action) { + uint32_t additionalLen = 1; // for -o option + + /* TODO: Add check for the target architecture and OS environment. + * Currently it supports only aarch64 and linux-gnu- + */ + if (kOperatingSystem == "linux-gnu-" && kMachine == "aarch64-") { + additionalLen += 3; + opt = std::make_unique(additionalLen); + + opt[0].SetKey("-isystem"); + opt[0].SetValue(FileUtils::SafeGetenv(kMapleRoot) + "/tools/gcc-linaro-7.5.0/aarch64-linux-gnu/libc/usr/include"); + + opt[1].SetKey("-isystem"); + opt[1].SetValue(FileUtils::SafeGetenv(kMapleRoot) + "/tools/gcc-linaro-7.5.0/lib/gcc/aarch64-linux-gnu/7.5.0/include"); + + opt[2].SetKey("-target"); + opt[2].SetValue("aarch64"); + } else { + CHECK_FATAL(false, "Only linux-gnu OS and aarch64 target are supported \n"); + } + + /* Set last option as -o option */ + opt[additionalLen-1].SetKey("-o"); + opt[additionalLen-1].SetValue(action.GetFullOutputName() + ".ast"); + + return additionalLen; +} + DefaultOption ClangCompiler::GetDefaultOptions(const MplOptions &options, const Action &action) const { - uint32_t len = sizeof(kClangDefaultOptions) / sizeof(MplOption); - DefaultOption defaultOptions = { std::make_unique(len), len }; + DefaultOption defaultOptions; + uint32_t fullLen = 0; + uint32_t defaultLen = 0; + uint32_t additionalLen = 0; + std::unique_ptr additionalOptions; + + additionalLen = FillSpecialDefaulOpt(additionalOptions, action); + defaultLen = sizeof(kClangDefaultOptions) / sizeof(MplOption); + fullLen = defaultLen + additionalLen; - for (uint32_t i = 0; i < len; ++i) { + defaultOptions = { std::make_unique(fullLen), fullLen }; + + for (uint32_t i = 0; i < defaultLen; ++i) { defaultOptions.mplOptions[i] = kClangDefaultOptions[i]; } - - CHECK_FATAL((len > 1), "Option is hardcoded in O0_options_clang.def file \n"); - defaultOptions.mplOptions[1].SetValue(action.GetFullOutputName() + ".ast"); + for (uint32_t defInd = defaultLen, additionalInd = 0; + additionalInd < additionalLen; ++additionalInd) { + defaultOptions.mplOptions[defInd++] = additionalOptions[additionalInd]; + } for (uint32_t i = 0; i < defaultOptions.length; ++i) { defaultOptions.mplOptions[i].SetValue( diff --git a/src/mapleall/maple_driver/src/driver_option_common.cpp b/src/mapleall/maple_driver/src/driver_option_common.cpp index 59ba0e46aa..52805c7e13 100644 --- a/src/mapleall/maple_driver/src/driver_option_common.cpp +++ b/src/mapleall/maple_driver/src/driver_option_common.cpp @@ -492,6 +492,15 @@ const mapleOption::Descriptor kUsages[] = { " -I \tAdd directory to include search path\n", "all", {"clang"} }, + { kClangISystem, + 0, + "isystem", + "", + kBuildTypeAll, + kArgCheckPolicyRequired, + " -isystem \tAdd directory to SYSTEM include search path\n", + "all", + {"clang"} }, { kMaplePhaseOnly, kEnable, "", -- Gitee From 34e6a11422cffade5680f760f2c3ec35a7a7e277 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Thu, 25 Nov 2021 13:48:30 +0300 Subject: [PATCH 2/5] [mapleall][driver] Add "-U" option "-U " option is used to undefine macro . --- src/mapleall/maple_driver/include/driver_option_common.h | 1 + src/mapleall/maple_driver/src/driver_option_common.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/mapleall/maple_driver/include/driver_option_common.h b/src/mapleall/maple_driver/include/driver_option_common.h index 3607a328df..238312b263 100644 --- a/src/mapleall/maple_driver/include/driver_option_common.h +++ b/src/mapleall/maple_driver/include/driver_option_common.h @@ -70,6 +70,7 @@ enum DriverOptionIndex { kLdLib, kLdLibPath, kClangMacro, + kClangUMacro, kClangInclude, kClangISystem, kMapleOut, diff --git a/src/mapleall/maple_driver/src/driver_option_common.cpp b/src/mapleall/maple_driver/src/driver_option_common.cpp index 52805c7e13..7d992f0574 100644 --- a/src/mapleall/maple_driver/src/driver_option_common.cpp +++ b/src/mapleall/maple_driver/src/driver_option_common.cpp @@ -483,6 +483,15 @@ const mapleOption::Descriptor kUsages[] = { " -D = \tDefine to (or 1 if omitted)\n", "all", {"clang"} }, + { kClangMacro, + 0, + "U", + "", + kBuildTypeAll, + kArgCheckPolicyRequired, + " -U \tUndefine macro \n", + "all", + {"clang"} }, { kClangInclude, 0, "I", -- Gitee From 51cb307b8d61187d04883049565e705942ae4324 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Thu, 25 Nov 2021 14:26:53 +0300 Subject: [PATCH 3/5] [mapleall][driver] Add enable-variable-array as default option for mplfe --- src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def b/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def index 311847daa5..04bbc03d70 100644 --- a/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def +++ b/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def @@ -1,2 +1 @@ -{} - +{"--enable-variable-array", "", false}, -- Gitee From 0b98e75590b476e3e5c40356ee355a55d78a7070 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Thu, 25 Nov 2021 15:50:39 +0300 Subject: [PATCH 4/5] [mapleall][driver] Do not replace default options Default options must be extended with extra options(not replaced). This commit implements it. To implement it options map was replaced with options vector. Also it allows to have several default options with the same key: For example, several -isystem default options are needed to set system includes. --- src/mapleall/maple_driver/include/compiler.h | 18 ++----- src/mapleall/maple_driver/src/compiler.cpp | 52 +++++++++++++------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/mapleall/maple_driver/include/compiler.h b/src/mapleall/maple_driver/include/compiler.h index 089ba8ea0d..e27316d8fb 100644 --- a/src/mapleall/maple_driver/include/compiler.h +++ b/src/mapleall/maple_driver/include/compiler.h @@ -98,28 +98,20 @@ class Compiler { std::vector MakeOption(const MplOptions &options, const Action &action) const; void AppendDefaultOptions(std::vector &finalOptions, - const std::map &defaultOptions, + const std::vector &defaultOptions, bool isDebug) const; void AppendExtraOptions(std::vector &finalOptions, - std::map defaultOptions, const MplOptions &options, bool isDebug) const; void AppendInputsAsOptions(std::vector &finalOptions, const MplOptions &mplOptions, const Action &action) const; - std::map MakeDefaultOptions(const MplOptions &options, - const Action &action) const; + void ReplaceOrInsertOption(std::vector &finalOptions, + const std::string &key, const std::string &value) const; + std::vector MakeDefaultOptions(const MplOptions &options, + const Action &action) const; int Exe(const MplOptions &mplOptions, const std::vector &options) const; const std::string &GetName() const { return name; } - - void ReplaceOption(std::vector &finalOptions, - const std::string &key, const std::string &value) const { - for (auto &opt : finalOptions) { - if (opt.GetKey() == key) { - opt.SetValue(value); - } - } - } }; class Jbc2MplCompiler : public Compiler { diff --git a/src/mapleall/maple_driver/src/compiler.cpp b/src/mapleall/maple_driver/src/compiler.cpp index ea872fbe49..8fb9f24cb4 100644 --- a/src/mapleall/maple_driver/src/compiler.cpp +++ b/src/mapleall/maple_driver/src/compiler.cpp @@ -64,34 +64,33 @@ ErrorCode Compiler::Compile(MplOptions &options, const Action &action, std::vector Compiler::MakeOption(const MplOptions &options, const Action &action) const { std::vector finalOptions; - std::map defaultOptions = MakeDefaultOptions(options, action); + std::vector defaultOptions = MakeDefaultOptions(options, action); AppendInputsAsOptions(finalOptions, options, action); AppendDefaultOptions(finalOptions, defaultOptions, options.HasSetDebugFlag()); - AppendExtraOptions(finalOptions, defaultOptions, options, options.HasSetDebugFlag()); + AppendExtraOptions(finalOptions, options, options.HasSetDebugFlag()); return finalOptions; } void Compiler::AppendDefaultOptions(std::vector &finalOptions, - const std::map &defaultOptions, + const std::vector &defaultOptions, bool isDebug) const { for (const auto &defaultIt : defaultOptions) { - (void)finalOptions.push_back(defaultIt.second); + (void)finalOptions.push_back(defaultIt); } if (isDebug) { LogInfo::MapleLogger() << Compiler::GetName() << " Default Options: "; for (const auto &defaultIt : defaultOptions) { - LogInfo::MapleLogger() << defaultIt.first << " " - << defaultIt.second.GetValue(); + LogInfo::MapleLogger() << defaultIt.GetKey() << " " + << defaultIt.GetValue(); } LogInfo::MapleLogger() << '\n'; } } void Compiler::AppendExtraOptions(std::vector &finalOptions, - std::map defaultOptions, const MplOptions &options, bool isDebug) const { const std::string &binName = GetTool(); auto exeOption = options.GetExeOptions().find(binName); @@ -107,13 +106,18 @@ void Compiler::AppendExtraOptions(std::vector &finalOptions, prefix = "--"; } - const std::string key = prefix + opt.OptionKey(); + const std::string baseKey = opt.OptionKey(); + const std::string key = prefix + baseKey; const std::string value = opt.Args(); - /* Update option if needed */ - auto it = defaultOptions.find(key); - if (it != defaultOptions.end()) { - ReplaceOption(finalOptions, key, value); + /* Default behaviour: extra options do not replace default options, + * because it can be some additional option with the same key. + * For example: we can have some default -isystem SYSTEM pathes option. + * And if some additional -isystem SYSTEM pathes is added, it's not correct + * to replace them (SYSTEM pathes msut be extended (not replaced)). + * If you need to replace some special option, check and replace it here */ + if (baseKey == "o") { + ReplaceOrInsertOption(finalOptions, key, value); } else { finalOptions.push_back(MplOption(key, value)); } @@ -129,6 +133,21 @@ void Compiler::AppendExtraOptions(std::vector &finalOptions, } } +void Compiler::ReplaceOrInsertOption(std::vector &finalOptions, + const std::string &key, const std::string &value) const { + bool wasFound = false; + for (auto &opt : finalOptions) { + if (opt.GetKey() == key) { + opt.SetValue(value); + wasFound = true; + } + } + + if (!wasFound) { + finalOptions.push_back(MplOption(key, value)); + } +} + void Compiler::AppendInputsAsOptions(std::vector &finalOptions, const MplOptions &mplOptions, const Action &action) const { std::vector splittedInputFileNames; @@ -140,14 +159,13 @@ void Compiler::AppendInputsAsOptions(std::vector &finalOptions, } } -std::map Compiler::MakeDefaultOptions(const MplOptions &options, - const Action &action) const { +std::vector Compiler::MakeDefaultOptions(const MplOptions &options, + const Action &action) const { DefaultOption rawDefaultOptions = GetDefaultOptions(options, action); - std::map defaultOptions; + std::vector defaultOptions; if (rawDefaultOptions.mplOptions != nullptr) { for (uint32_t i = 0; i < rawDefaultOptions.length; ++i) { - (void)defaultOptions.insert(std::make_pair(rawDefaultOptions.mplOptions[i].GetKey(), - rawDefaultOptions.mplOptions[i])); + (void)defaultOptions.push_back(rawDefaultOptions.mplOptions[i]); } } return defaultOptions; -- Gitee From b6da975eb4232148e8566a56d2b1236fb8690dd9 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Mon, 29 Nov 2021 13:18:26 +0300 Subject: [PATCH 5/5] [CI] Add new degradation tests for maple driver * DRIVER0003-macro tests macro options: -D and -U; * DRIVER0004-syslibs tests system library linking with -l option; --- .../driver_test/DRIVER0003-macro/expected.txt | 4 ++ .../driver_test/DRIVER0003-macro/helper.c | 31 +++++++++++++++ .../driver_test/DRIVER0003-macro/main.c | 38 +++++++++++++++++++ .../driver_test/DRIVER0003-macro/test.cfg | 2 + .../DRIVER0004-syslibs/expected.txt | 1 + .../driver_test/DRIVER0004-syslibs/main.c | 29 ++++++++++++++ .../driver_test/DRIVER0004-syslibs/test.cfg | 2 + 7 files changed, 107 insertions(+) create mode 100644 testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt create mode 100644 testsuite/c_test/driver_test/DRIVER0003-macro/helper.c create mode 100644 testsuite/c_test/driver_test/DRIVER0003-macro/main.c create mode 100644 testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg create mode 100644 testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt create mode 100644 testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c create mode 100644 testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt b/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt new file mode 100644 index 0000000000..f57d321a22 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt @@ -0,0 +1,4 @@ +TEST1 +HELPER TEST1 +RES: 40 +HELPER TEST2 10 diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c b/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c new file mode 100644 index 0000000000..9ad6a5b4e3 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c @@ -0,0 +1,31 @@ +/* + * Copyright (C) [2021] 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. + */ + +#include +#include + +int helper(int in) +{ +#ifdef TEST1 + printf("HELPER TEST1\n"); +#endif + +#ifdef TEST2 + printf("HELPER TEST2\n"); +#endif + + return in * 2; +} + diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/main.c b/testsuite/c_test/driver_test/DRIVER0003-macro/main.c new file mode 100644 index 0000000000..c967a6db28 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/main.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) [2021] 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. + */ + +#include +#include + +int helper(int in); + +int main(void) +{ +#ifdef TEST1 + printf("TEST1\n"); +#endif + +#ifdef TEST2 + printf("TEST2\n"); +#endif + + int res = helper(NUM); + printf("RES: %d\n", res); + +#ifdef TEST3 + printf("HELPER TEST2 %d\n", EXTRANUM); +#endif + return 0; +} diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg b/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg new file mode 100644 index 0000000000..ad25f535d3 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg @@ -0,0 +1,2 @@ +compile(APP="main.c helper.c",OPTION="--no-maple-phase --static -DTEST1 -DNUM=20 -DTEST2 -UTEST2 -D TEST3 -D EXTRANUM=10") +run(a) diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt b/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt new file mode 100644 index 0000000000..9ecdb58fc1 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt @@ -0,0 +1 @@ +sqrt(1024.0) = 32.0 diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c b/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c new file mode 100644 index 0000000000..cb346561e8 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) [2021] 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. + */ + +#include +#include + +#define PI 3.14 + +int main() +{ + double param, result; + param = 1024.0; + result = sqrt(param); + + printf ("sqrt(%.1f) = %.1f\n", param, result ); + return 0; +} diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg b/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg new file mode 100644 index 0000000000..d576379fe0 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg @@ -0,0 +1,2 @@ +compile(APP=main.c,OPTION="--no-maple-phase --static -lm") +run(a) -- Gitee