================
@@ -73,44 +97,111 @@ void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(
   tools::constructLLVMLinkCommand(C, *this, JA, Inputs, LinkArgs, Output, Args,
                                   TempFile);
 
-  // Post-link HIP lowering.
+  auto T = getToolChain().getTriple();
 
-  // Run LLVM IR passes to lower/expand/emulate HIP code that does not 
translate
-  // to SPIR-V (E.g. dynamic shared memory).
-  auto PassPluginPath = findPassPlugin(C.getDriver(), Args);
-  if (!PassPluginPath.empty()) {
-    const char *PassPathCStr = C.getArgs().MakeArgString(PassPluginPath);
-    const char *OptOutput = HIP::getTempFile(C, Name + "-lower", "bc");
-    ArgStringList OptArgs{TempFile,     "-load-pass-plugin",
-                          PassPathCStr, "-passes=hip-post-link-passes",
-                          "-o",         OptOutput};
-    const char *Opt = Args.MakeArgString(getToolChain().GetProgramPath("opt"));
+  if (T.getOS() == llvm::Triple::ChipStar) {
+    // chipStar: run HipSpvPasses via opt, then emit SPIR-V either via the
+    // external llvm-spirv translator (if available) or the in-tree SPIR-V
+    // backend (mirrors the fallback in clang-linker-wrapper).
+
+    // Run HipSpvPasses plugin via opt (must run on LLVM IR before
+    // the SPIR-V backend lowers to MIR).
+    TempFile = runHipSpvPasses(C, JA, *this, getToolChain(), Inputs, Output,
+                               Args, Name, TempFile);
+
+    // Prefer the external llvm-spirv translator when it is available next to
+    // the toolchain (or on PATH) - required when LLVM is built without the
+    // in-tree SPIR-V target. Mirror SPIRV::constructTranslateCommand, which
+    // prefers the versioned "llvm-spirv-<major>" over the plain name, so the
+    // translator-vs-backend choice matches the binary that will actually run.
+    // Use findProgramByName (not GetProgramPath) so a missing binary is
+    // reported as not-found rather than being silently substituted by its 
name.
+    std::string LLVMSpirvPath;
+    {
+      StringRef DriverDir(C.getDriver().Dir);
+      std::string Versioned =
+          "llvm-spirv-" + std::to_string(LLVM_VERSION_MAJOR);
+      for (StringRef Name : {StringRef(Versioned), StringRef("llvm-spirv")}) {
+        llvm::ErrorOr<std::string> P =
+            llvm::sys::findProgramByName(Name, {DriverDir});
+        if (!P)
+          P = llvm::sys::findProgramByName(Name);
+        if (P) {
+          LLVMSpirvPath = *P;
+          break;
+        }
+      }
+    }
+
+    if (!LLVMSpirvPath.empty()) {
+      // External translator path: BC -> SPIR-V via llvm-spirv.
+      llvm::opt::ArgStringList TrArgs;
+      // Match clang-linker-wrapper's chipstar version selection.
+      if (T.getSubArch() == llvm::Triple::SPIRVSubArch_v13)
+        TrArgs.push_back("--spirv-max-version=1.3");
+      else
+        TrArgs.push_back("--spirv-max-version=1.2");
+      // Keep this extension list in sync with the in-tree backend fallback
+      // below. SPV_EXT_relaxed_printf_string_address_space is required to
+      // translate modules whose printf format string is not in the constant
+      // address space (e.g. device-side assert / dynamic %s lowering); without
+      // it llvm-spirv aborts with exit code 18.
+      TrArgs.push_back(
+          "--spirv-ext=-all,+SPV_INTEL_function_pointers,+SPV_INTEL_subgroups"
+          ",+SPV_EXT_relaxed_printf_string_address_space"
----------------
linehill wrote:

For improving readability and style consistency:
```suggestion
          "--spirv-ext=-all"
          ",+SPV_INTEL_function_pointers"
          ",+SPV_INTEL_subgroups"
          ",+SPV_EXT_relaxed_printf_string_address_space"
```

https://github.com/llvm/llvm-project/pull/206910
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to