DieGoldeneEnte created this revision.
DieGoldeneEnte added a reviewer: yaxunl.
Herald added subscribers: cfe-commits, mgorny.
Herald added a project: clang.

The previous code constructed the executable paths for llvm-link, opt, lld, llc 
and clang-offload-bundler for the path to the compiler. This change uses cmake 
to find the directory containing llvm and lld and searches them using the 
GetProgramPath method in addition to the compiler directory.

The test completed without error (except one which failed even without my 
patch) and I was able to confirm successful compilation using the hip toolchain 
by compiling a simple program with clang, lld, and llvm executables in 
different directories.


Repository:
  rC Clang

https://reviews.llvm.org/D72806

Files:
  clang/CMakeLists.txt
  clang/lib/Driver/ToolChains/HIP.cpp

Index: clang/lib/Driver/ToolChains/HIP.cpp
===================================================================
--- clang/lib/Driver/ToolChains/HIP.cpp
+++ clang/lib/Driver/ToolChains/HIP.cpp
@@ -105,9 +105,8 @@
   CmdArgs.push_back("-o");
   auto OutputFileName = getOutputFileName(C, OutputFilePrefix, "-linked", "bc");
   CmdArgs.push_back(OutputFileName);
-  SmallString<128> ExecPath(C.getDriver().Dir);
-  llvm::sys::path::append(ExecPath, "llvm-link");
-  const char *Exec = Args.MakeArgString(ExecPath);
+  const char *Exec =
+      Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
   return OutputFileName;
 }
@@ -133,9 +132,8 @@
   auto OutputFileName =
       getOutputFileName(C, OutputFilePrefix, "-optimized", "bc");
   OptArgs.push_back(OutputFileName);
-  SmallString<128> OptPath(C.getDriver().Dir);
-  llvm::sys::path::append(OptPath, "opt");
-  const char *OptExec = Args.MakeArgString(OptPath);
+  const char *OptExec =
+      Args.MakeArgString(getToolChain().GetProgramPath("opt"));
   C.addCommand(std::make_unique<Command>(JA, *this, OptExec, OptArgs, Inputs));
   return OutputFileName;
 }
@@ -180,9 +178,7 @@
   auto LlcOutputFile =
       getOutputFileName(C, OutputFilePrefix, "", OutputIsAsm ? "s" : "o");
   LlcArgs.push_back(LlcOutputFile);
-  SmallString<128> LlcPath(C.getDriver().Dir);
-  llvm::sys::path::append(LlcPath, "llc");
-  const char *Llc = Args.MakeArgString(LlcPath);
+  const char *Llc = Args.MakeArgString(getToolChain().GetProgramPath("llc"));
   C.addCommand(std::make_unique<Command>(JA, *this, Llc, LlcArgs, Inputs));
   return LlcOutputFile;
 }
@@ -196,9 +192,7 @@
   // The output from ld.lld is an HSA code object file.
   ArgStringList LldArgs{
       "-flavor", "gnu", "-shared", "-o", Output.getFilename(), InputFileName};
-  SmallString<128> LldPath(C.getDriver().Dir);
-  llvm::sys::path::append(LldPath, "lld");
-  const char *Lld = Args.MakeArgString(LldPath);
+  const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
   C.addCommand(std::make_unique<Command>(JA, *this, Lld, LldArgs, Inputs));
 }
 
@@ -230,9 +224,8 @@
       Args.MakeArgString(std::string("-outputs=").append(OutputFileName));
   BundlerArgs.push_back(BundlerOutputArg);
 
-  SmallString<128> BundlerPath(C.getDriver().Dir);
-  llvm::sys::path::append(BundlerPath, "clang-offload-bundler");
-  const char *Bundler = Args.MakeArgString(BundlerPath);
+  const char *Bundler = Args.MakeArgString(
+      T.getToolChain().GetProgramPath("clang-offload-bundler"));
   C.addCommand(std::make_unique<Command>(JA, T, Bundler, BundlerArgs, Inputs));
 }
 
@@ -277,6 +270,16 @@
   // Lookup binaries into the driver directory, this is used to
   // discover the clang-offload-bundler executable.
   getProgramPaths().push_back(getDriver().Dir);
+
+// add llvm binaries in case they are not in the driver directory
+#if defined(LLVM_TOOLS_BINARY_DIR)
+  getProgramPaths().push_back(LLVM_TOOLS_BINARY_DIR);
+#endif
+
+// add lld binary in case they are not in the driver directory
+#if defined(LLD_BINARY_DIR)
+  getProgramPaths().push_back(LLD_BINARY_DIR);
+#endif
 }
 
 void HIPToolChain::addClangTargetOptions(
Index: clang/CMakeLists.txt
===================================================================
--- clang/CMakeLists.txt
+++ clang/CMakeLists.txt
@@ -866,6 +866,22 @@
   llvm_distribution_add_targets()
 endif()
 
+# add LLVM_TOOLS_BINARY_DIR to Defines to make llc, llvm-link and opt available
+# for HIP toolchain
+add_definitions( -DLLVM_TOOLS_BINARY_DIR="${TOOLS_BINARY_DIR} " )
+message(STATUS "found llvm executable dir: ${TOOLS_BINARY_DIR}")
+
+# add LLD_BINARY_DIR to Defines to make lld available for HIP toolchain
+find_program(LLD_BINARY NAMES lld)
+if(LLD_BINARY MATCHES "-NOTFOUND")
+  message(SEND_ERROR "Could not find lld executable")
+else()
+  message(STATUS "found lld executable: ${LLD_BINARY}")
+endif()
+get_filename_component(LLD_BINARY_DIR ${LLD_BINARY} DIRECTORY)
+add_definitions( -DLLD_BINARY_DIR="${LLD_BINARY_DIR} " )
+
+
 configure_file(
   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to