[lld] [RFC][lld][SPIRV] Add support for SPIR-V LTO (PR #178749)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 17 09:11:11 PST 2026


================
@@ -0,0 +1,122 @@
+//===- Driver.cpp ---------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Driver for SPIR-V linking
+//
+//===----------------------------------------------------------------------===//
+
+#include "lld/Common/Driver.h"
+#include "Config.h"
+#include "LTO.h"
+#include "lld/Common/CommonLinkerContext.h"
+#include "lld/Common/ErrorHandler.h"
+#include "lld/Common/Memory.h"
+#include "lld/Common/Version.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+namespace lld::spirv {
+
+Config config;
+
+static void initLLVM() {
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+}
+
+static void printVersion(llvm::raw_ostream &os) {
+  os << getLLDVersion() << "\n";
+}
+
+static void printHelp() {
+  llvm::outs() << "USAGE: spirv-lld [options] file...\n\n"
+               << "OPTIONS:\n"
+               << "  -o <path>     Output file path\n"
+               << "  --version     Print version information\n"
+               << "  --help        Print this help\n";
+}
+
+bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
+          llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
+  auto *context = new CommonLinkerContext;
+  context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
+  context->e.cleanupCallback = []() { config = Config(); };
+
+  initLLVM();
+
+  // Handle --version and --help
+  for (size_t i = 1; i < args.size(); ++i) {
+    StringRef arg = args[i];
+    if (arg == "--version") {
+      printVersion(stdoutOS);
+      return true;
+    }
+    if (arg == "--help") {
+      printHelp();
+      return true;
+    }
+  }
+
+  // Parse args: -o output, rest are inputs
+  std::string outputFile = "a.out.spv";
+  std::vector<MemoryBufferRef> inputs;
----------------
jhuber6 wrote:

Small vector? I'm guessing this is copied from other lld sources so perhaps there's a reason for not using them I'm not aware of.

https://github.com/llvm/llvm-project/pull/178749


More information about the llvm-commits mailing list