[clang] [Feature]: support for the BC library file into the compile dependencies (PR #119513)

via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 16 23:36:51 PST 2025


https://github.com/zhouronghua updated https://github.com/llvm/llvm-project/pull/119513

>From 9fd14fb47e226f5475fdf9bbcad904ea97b809de Mon Sep 17 00:00:00 2001
From: "ronghua.zhou" <ronghua.zhou at enflame-tech.com>
Date: Fri, 14 Feb 2025 01:04:51 +0000
Subject: [PATCH] [Feature]: support for the BC library file into the compile
 dependencies

---
 clang/lib/Driver/ToolChains/Clang.cpp   | 101 +++++++++++++++++++++++-
 clang/lib/Frontend/CompilerInstance.cpp |  10 ++-
 clang/lib/Frontend/DependencyFile.cpp   |  91 +++++++++++++++++++++
 3 files changed, 197 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 55ec3db0ee994..8167fde9c695e 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1041,6 +1041,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
   else
     ArgM = ArgMD;
 
+  // Determine the output location.
+  const char *DepFile = nullptr;
   if (ArgM) {
     if (!JA.isDeviceOffloading(Action::OFK_HIP)) {
       // Determine the output location.
@@ -1056,8 +1058,44 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
         DepFile = getDependencyFileName(Args, Inputs);
         C.addFailureResultFile(DepFile, &JA);
       }
-      CmdArgs.push_back("-dependency-file");
-      CmdArgs.push_back(DepFile);
+      // mv to triple select
+      // CmdArgs.push_back("-dependency-file");
+      // CmdArgs.push_back(DepFile);
+
+      if (getToolChain().getTriple().isNVPTX() ||
+          getToolChain().getTriple().isAMDGCN()) {
+        //  When we set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <DEP_TARGET> -MF
+        //  <DEP_FILE>.host") in cmake during heterogeneous compilation,
+        // we really gererate *.d.host (for host) and *.d (for GPU target),
+        // the content of *.d  = *.d.host + builtin.bc (i.e. libdevice.10.bc or
+        // some
+        // files in --hip-device-lib)
+        // so when libdevice.10.bc or hip-device-lib is updated, the incremental
+        // build rule will be triggered.
+        if (DepFile) {
+          SmallString<128> NewDepFile(DepFile);
+          llvm::StringRef SubStr = ".host";
+          size_t Pos = NewDepFile.find(SubStr);
+          CmdArgs.push_back("-dependency-file");
+          // for tops target, trim .host in dep file
+          if (Pos != llvm::StringRef::npos) {
+            // erase substr
+            auto ndf = NewDepFile.substr(0, Pos);
+            CmdArgs.push_back(Args.MakeArgString(ndf));
+          } else {
+            // if not set dep file with .host extend, remain depfile not touched
+            CmdArgs.push_back(Args.MakeArgString(DepFile));
+          }
+        }
+      }
+      // Host side remain depfile not touched
+      else {
+        // for host compile, we generate orginal dep file
+        if (DepFile) {
+          CmdArgs.push_back("-dependency-file");
+          CmdArgs.push_back(DepFile);
+        }
+      }
     }
 
     bool HasTarget = false;
@@ -1149,6 +1187,64 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
     CmdArgs.push_back("__clang_openmp_device_functions.h");
   }
 
+<<<<<<< HEAD
+  if (getToolChain().getTriple().isNVPTX() ||
+      getToolChain().getTriple().isAMDGCN()) {
+    //  When we set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <DEP_TARGET> -MF
+    //  <DEP_FILE>.host") in cmake during heterogeneous compilation,
+    // we really gererate *.d.host (for host) and *.d (for GPU target),
+    // the content of *.d  = *.d.host + builtin.bc (i.e. libdevice.10.bc or some
+    // files in --hip-device-lib)
+    // so when libdevice.10.bc or hip-device-lib is updated, the incremental
+    // build rule will be triggered.
+    if (DepFile) {
+      SmallString<128> NewDepFile(DepFile);
+      llvm::StringRef SubStr = ".host";
+      size_t Pos = NewDepFile.find(SubStr);
+      CmdArgs.push_back("-dependency-file");
+      // for tops target, trim .host in dep file
+      if (Pos != llvm::StringRef::npos) {
+        // erase substr
+        auto ndf = NewDepFile.substr(0, Pos);
+        CmdArgs.push_back(Args.MakeArgString(ndf));
+      } else {
+        // if not set dep file with .host extend, remain depfile not touched
+        CmdArgs.push_back(Args.MakeArgString(DepFile));
+      }
+    }
+=======
+  if (getToolChain().getTriple().isDTU() ||
+      getToolChain().getTriple().isGCU()) {
+    // Add Camelus headers
+    SmallString<128> P(D.ResourceDir);
+    llvm::sys::path::append(P, "include");
+    llvm::sys::path::append(P, "cml");
+    CmdArgs.push_back("-internal-isystem");
+    CmdArgs.push_back(Args.MakeArgString(P));
+    AddExtraMacros(Args, CmdArgs);
+>>>>>>> 7b4cbf8af94e ([Feature](INF-2375) infra: merge host depends to kernel depends)
+  }
+  // Host side remain depfile not touched
+  else {
+    GenerateHostCompilationDeviceArchMacro(Args, CmdArgs);
+  }
+
+  // for host compile, if with dtu/gcu auxtriple, 
+  // dep file will be renamed to *.d.host
+  if (DepFile) {
+    auto at = getToolChain().getAuxTriple();
+    if (!at || (at->isDTU() || at->isGCU())) {
+      SmallString<128> NewDepFile(DepFile);
+      NewDepFile.append(".host");
+      CmdArgs.push_back("-dependency-file");
+      CmdArgs.push_back(Args.MakeArgString(NewDepFile));
+      // else keep the original dep file name
+    } else {
+      CmdArgs.push_back("-dependency-file");
+      CmdArgs.push_back(DepFile);
+    }
+  }
+
   if (Args.hasArg(options::OPT_foffload_via_llvm)) {
     // Add llvm_wrappers/* to our system include path.  This lets us wrap
     // standard library headers and other headers.
@@ -1159,7 +1255,6 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
       CmdArgs.push_back("__llvm_offload_device.h");
     else
       CmdArgs.push_back("__llvm_offload_host.h");
-  }
 
   // Add -i* options, and automatically translate to
   // -include-pch/-include-pth for transparent PCH support. It's
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index c11c857ea0606..60ac343391e18 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -494,8 +494,14 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
 
   // Handle generating dependencies, if requested.
   const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
-  if (!DepOpts.OutputFile.empty())
-    addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));
+  if (!DepOpts.OutputFile.empty()) {
+    auto DFG = std::make_shared<DependencyFileGenerator>(DepOpts);
+    for (auto F : getCodeGenOpts().LinkBitcodeFiles) {
+      DFG->maybeAddDependency(F.Filename, false, false, false, false);
+    }
+    addDependencyCollector(DFG);
+  }
+
   if (!DepOpts.DOTOutputFile.empty())
     AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
                              getHeaderSearchOpts().Sysroot);
diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp
index 15fa7de35df97..ce612d8522ff8 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -10,6 +10,12 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <iostream>
+#include <fstream>
+#include <set>
+#include <string>
+#include <vector>
+
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/DependencyOutputOptions.h"
@@ -357,6 +363,91 @@ void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
   }
 
   outputDependencyFile(OS);
+  OS.flush();
+  OS.close();
+
+  // merge host dependency file (*.d.host)
+  // to kernel dependency file (*.d.host) for tops target
+  llvm::StringRef SubStr = ".host";
+  SmallString<128> OutputFileS(OutputFile);
+  size_t Pos = OutputFileS.find(SubStr);
+  // for tops target, trim .host in dep file
+  if (Pos != llvm::StringRef::npos) {
+    auto ndf = OutputFileS.substr(0, Pos);
+    // dependencies is a set to auto merge duplicate dependencies
+    std::set<std::string> dependencies;
+
+    std::string line;
+    std::string tmpline;
+    std::string KernelStartLine;
+    std::string HostStartLine;
+    std::string Endline;
+    // read kernel dep file
+    std::ifstream KernelDepFile(ndf.str());
+    if (KernelDepFile.is_open()) {
+      while (std::getline(KernelDepFile, tmpline)) {
+        // Remove empty lines and comment lines
+        if (!tmpline.empty() && tmpline[0] != '#') {
+          line = tmpline;
+          if (KernelStartLine.empty()) {
+            KernelStartLine = line;
+          } else {
+            dependencies.insert(line);
+          }
+        }
+      }
+      // Process Endline
+      Endline = line;
+      dependencies.erase(line);
+    }
+    // read host dep file
+    std::ifstream HostDepFile(OutputFile);
+    if (HostDepFile.is_open()) {
+      while (std::getline(HostDepFile, tmpline)) {
+        // Remove empty lines and comment lines
+        if (!tmpline.empty() && tmpline[0] != '#') {
+          line = tmpline;
+          if (HostStartLine.empty()) {
+            HostStartLine = line;
+            // if KernelStartLine is not empty
+            if (KernelStartLine.length() > 0) {
+              if (HostStartLine != KernelStartLine) {
+                Diags.Report(diag::err_fe_error_opening)
+                    << OutputFile
+                    << "host dep file is not match kernel dep file";
+                return;
+              }
+            }
+          } else {
+            dependencies.insert(line);
+          }
+        }
+      }
+      // Process Endline
+      dependencies.erase(line);
+
+      if (Endline.length() > 0) {
+        if (line != Endline) {
+          dependencies.insert(line + " \\");
+        }
+      } else {
+        Endline = line;
+      }
+    }
+    // write merged dep file
+    std::ofstream DepFile(ndf.str());
+    if (DepFile.is_open()) {
+      // Write HostStartLine, KernelStartLine maybe is empty.
+      DepFile << HostStartLine<< std::endl;
+      for (const auto &dep : dependencies) {
+        DepFile << dep << std::endl;
+      }
+      // Write Endline
+      DepFile << Endline<< std::endl;
+    }
+    // unlink host dep file
+    llvm::sys::fs::remove(OutputFile);
+  }
 }
 
 void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {



More information about the cfe-commits mailing list