Skip to content

[LTO] Fix Veclib flags correctly pass to LTO flags #78749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,28 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
"-generate-arange-section"));
}

// Pass vector library arguments to LTO.
Arg *ArgVecLib = Args.getLastArg(options::OPT_fveclib);
if (ArgVecLib && ArgVecLib->getNumValues() == 1) {
// Map the vector library names from clang front-end to opt front-end. The
// values are taken from the TargetLibraryInfo class command line options.
std::optional<StringRef> OptVal =
llvm::StringSwitch<std::optional<StringRef>>(ArgVecLib->getValue())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to refactor and reuse existing TargetLibraryInfo code, i.e. create a common static function that maps the values so that it can be called in multiple places?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing code for mapping the strings is a great suggestion, however, TargetLibraryInfo statically creates some cl::opt (command line options) and I'm not sure ATM what would be a clean way to do this.

I would suggest such change (if is doable in a clean way) to be dealt in a separate patch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the TLI belongs to llvm world, while clang driver from what I can see is only linked with following llvm components:
set(LLVM_LINK_COMPONENTS
BinaryFormat
MC
Object
Option
ProfileData
Support
TargetParser
WindowsDriver
)
so I don't think it is possible to do it.

Unfortunately at the early stage when the tools::addLTOOptions function is called the CodeGenOptions are not even created, perhaps we could have a function defined which does the strings checking in include/llvm/Analysis/TargetLibraryInfo.h and include it in the CommonArgs.cpp? a least everything would be in one place? what do you think @david-arm ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think that would work, i.e. having a static function in TargetLibraryInfo.h that can be called in two places and doesn't have a dependency on the component/library. Having said that, I won't hold this patch up for this if it's too difficult!

.Case("Accelerate", "Accelerate")
.Case("LIBMVEC", "LIBMVEC-X86")
.Case("MASSV", "MASSV")
.Case("SVML", "SVML")
.Case("SLEEF", "sleefgnuabi")
.Case("Darwin_libsystem_m", "Darwin_libsystem_m")
.Case("ArmPL", "ArmPL")
.Case("none", "none")
.Default(std::nullopt);

if (OptVal)
CmdArgs.push_back(Args.MakeArgString(
Twine(PluginOptPrefix) + "-vector-library=" + OptVal.value()));
}

// Try to pass driver level flags relevant to LTO code generation down to
// the plugin.

Expand Down
18 changes: 18 additions & 0 deletions clang/test/Driver/fveclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@

// RUN: %clang -fveclib=Accelerate %s -nodefaultlibs -target arm64-apple-ios8.0.0 -### 2>&1 | FileCheck --check-prefix=CHECK-LINK-NODEFAULTLIBS %s
// CHECK-LINK-NODEFAULTLIBS-NOT: "-framework" "Accelerate"


/* Verify that the correct vector library is passed to LTO flags. */

// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=LIBMVEC -flto %s 2>&1 | FileCheck -check-prefix CHECK-LTO-LIBMVEC %s
// CHECK-LTO-LIBMVEC: "-plugin-opt=-vector-library=LIBMVEC-X86"

// RUN: %clang -### --target=powerpc64-unknown-linux-gnu -fveclib=MASSV -flto %s 2>&1 | FileCheck -check-prefix CHECK-LTO-MASSV %s
// CHECK-LTO-MASSV: "-plugin-opt=-vector-library=MASSV"

// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=SVML -flto %s 2>&1 | FileCheck -check-prefix CHECK-LTO-SVML %s
// CHECK-LTO-SVML: "-plugin-opt=-vector-library=SVML"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF -flto %s 2>&1 | FileCheck -check-prefix CHECK-LTO-SLEEF %s
// CHECK-LTO-SLEEF: "-plugin-opt=-vector-library=sleefgnuabi"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -flto %s 2>&1 | FileCheck -check-prefix CHECK-LTO-ARMPL %s
// CHECK-LTO-ARMPL: "-plugin-opt=-vector-library=ArmPL"