[Openmp-commits] [PATCH] D116906: [OpenMP][AMDGPU] Optimize the linked in math libraries

Johannes Doerfert via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Sun Jan 9 18:20:54 PST 2022

jdoerfert created this revision.
jdoerfert added reviewers: JonChesterfield, jhuber6, ye-luo.
Herald added subscribers: kerbowa, guansong, bollu, t-tye, tpr, dstuttard, yaxunl, nhaehnle, jvesely, kzhuravl.
jdoerfert requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, wdng.
Herald added a project: clang.

Once we linked in math files, potentially even if we link in only other
"system libraries", we want to optimize the code again. This is not only
reasonable but also helps to hide various problems with the missing
attribute annotations in the math libraries.

  rG LLVM Github Monorepo



Index: clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
--- clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
+++ clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
@@ -16,6 +16,7 @@
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/InputInfo.h"
 #include "clang/Driver/Options.h"
+#include "clang/Driver/Tool.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FormatAdapters.h"
@@ -95,9 +96,9 @@
     if (II.isFilename())
+  bool HasLibm = false;
   if (Args.hasArg(options::OPT_l)) {
     auto Lm = Args.getAllArgValues(options::OPT_l);
-    bool HasLibm = false;
     for (auto &Lib : Lm) {
       if (Lib == "m") {
         HasLibm = true;
@@ -143,6 +144,26 @@
       JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs,
       InputInfo(&JA, Args.MakeArgString(OutputFileName))));
+  // If we linked in libm definitions late we run another round of optimizations
+  // to inline the definitions and fold what is foldable.
+  if (HasLibm) {
+    ArgStringList OptCmdArgs;
+    const char *OptOutputFileName =
+        getOutputFileName(C, OutputFilePrefix, "-linked-opt", "bc");
+    addLLCOptArg(Args, OptCmdArgs);
+    OptCmdArgs.push_back(OutputFileName);
+    OptCmdArgs.push_back("-o");
+    OptCmdArgs.push_back(OptOutputFileName);
+    const char *OptExec =
+        Args.MakeArgString(getToolChain().GetProgramPath("opt"));
+    C.addCommand(std::make_unique<Command>(
+        JA, *this, ResponseFileSupport::AtFileCurCP(), OptExec, OptCmdArgs,
+        InputInfo(&JA, Args.MakeArgString(OutputFileName)),
+        InputInfo(&JA, Args.MakeArgString(OptOutputFileName))));
+    OutputFileName = OptOutputFileName;
+  }
   return OutputFileName;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116906.398476.patch
Type: text/x-patch
Size: 1890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20220110/91a3b391/attachment.bin>

More information about the Openmp-commits mailing list