[SPIR-V](1/6) Add stub for SPIRV backend
This patch contains enough for lib/Target/SPIRV to compile: a basic
SPIRVTargetMachine and SPIRVTargetInfo.
Differential Revision: https://reviews.llvm.org/D115009
Authors: Aleksandr Bezzubikov, Lewis Crawford, Ilia Diachkov,
Michal Paszkowski, Andrey Tretyakov, Konrad Trifunovic
Co-authored-by: Aleksandr Bezzubikov <zuban32s@gmail.com>
Co-authored-by: Ilia Diachkov <iliya.diyachkov@intel.com>
Co-authored-by: Michal Paszkowski <michal.paszkowski@outlook.com>
Co-authored-by: Andrey Tretyakov <andrey1.tretyakov@intel.com>
Co-authored-by: Konrad Trifunovic <konrad.trifunovic@intel.com>
GitOrigin-RevId: 7fd4622d4801cc14823ecde678d55b6f3a106eb9
diff --git a/CODE_OWNERS.TXT b/CODE_OWNERS.TXT
index f56c8e5..5e0f8a6 100644
--- a/CODE_OWNERS.TXT
+++ b/CODE_OWNERS.TXT
@@ -248,3 +248,7 @@
N: Zi Xuan Wu (Zeson)
E: zixuan.wu@linux.alibaba.com
D: C-SKY backend (lib/Target/CSKY/*)
+
+N: Ilia Diachkov
+E: iliya.diyachkov@intel.com
+D: SPIR-V backend (lib/Target/SPIRV/*)
diff --git a/docs/CompilerWriterInfo.rst b/docs/CompilerWriterInfo.rst
index d7d181c..b2a873d 100644
--- a/docs/CompilerWriterInfo.rst
+++ b/docs/CompilerWriterInfo.rst
@@ -198,6 +198,11 @@
* `CUDA Documentation <http://docs.nvidia.com/cuda/index.html>`_ includes the PTX
ISA and Driver API documentation
+SPIR-V
+======
+
+* `SPIR-V documentation <https://www.khronos.org/registry/SPIR-V/>`_
+
Miscellaneous Resources
=======================
diff --git a/lib/Target/SPIRV/CMakeLists.txt b/lib/Target/SPIRV/CMakeLists.txt
new file mode 100644
index 0000000..ebda759
--- /dev/null
+++ b/lib/Target/SPIRV/CMakeLists.txt
@@ -0,0 +1,17 @@
+add_llvm_component_group(SPIRV)
+
+add_llvm_target(SPIRVCodeGen
+ SPIRVTargetMachine.cpp
+
+ LINK_COMPONENTS
+ CodeGen
+ Core
+ SPIRVInfo
+ Support
+ Target
+
+ ADD_TO_COMPONENT
+ SPIRV
+ )
+
+add_subdirectory(TargetInfo)
diff --git a/lib/Target/SPIRV/SPIRVTargetMachine.cpp b/lib/Target/SPIRV/SPIRVTargetMachine.cpp
new file mode 100644
index 0000000..baf6d68
--- /dev/null
+++ b/lib/Target/SPIRV/SPIRVTargetMachine.cpp
@@ -0,0 +1,72 @@
+//===- SPIRVTargetMachine.cpp - Define TargetMachine for SPIR-V -*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements the info about SPIR-V target spec.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SPIRVTargetMachine.h"
+#include "TargetInfo/SPIRVTargetInfo.h"
+#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/MC/TargetRegistry.h"
+
+using namespace llvm;
+
+extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
+ // Register the target.
+ RegisterTargetMachine<SPIRVTargetMachine> X(getTheSPIRV32Target());
+ RegisterTargetMachine<SPIRVTargetMachine> Y(getTheSPIRV64Target());
+}
+
+static std::string computeDataLayout(const Triple &TT) {
+ std::string DataLayout = "e-m:e";
+
+ const auto Arch = TT.getArch();
+ if (Arch == Triple::spirv32)
+ DataLayout += "-p:32:32";
+ else if (Arch == Triple::spirv64)
+ DataLayout += "-p:64:64";
+ return DataLayout;
+}
+
+static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
+ if (!RM)
+ return Reloc::PIC_;
+ return *RM;
+}
+
+SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT,
+ StringRef CPU, StringRef FS,
+ const TargetOptions &Options,
+ Optional<Reloc::Model> RM,
+ Optional<CodeModel::Model> CM,
+ CodeGenOpt::Level OL, bool JIT)
+ : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
+ initAsmInfo();
+}
+
+namespace {
+// SPIR-V Code Generator Pass Configuration Options.
+class SPIRVPassConfig : public TargetPassConfig {
+public:
+ SPIRVPassConfig(SPIRVTargetMachine &TM, PassManagerBase &PM)
+ : TargetPassConfig(TM, PM) {}
+
+ SPIRVTargetMachine &getSPIRVTargetMachine() const {
+ return getTM<SPIRVTargetMachine>();
+ }
+};
+} // namespace
+
+TargetPassConfig *SPIRVTargetMachine::createPassConfig(PassManagerBase &PM) {
+ return new SPIRVPassConfig(*this, PM);
+}
diff --git a/lib/Target/SPIRV/SPIRVTargetMachine.h b/lib/Target/SPIRV/SPIRVTargetMachine.h
new file mode 100644
index 0000000..91b294d
--- /dev/null
+++ b/lib/Target/SPIRV/SPIRVTargetMachine.h
@@ -0,0 +1,37 @@
+//===-- SPIRVTargetMachine.h - Define TargetMachine for SPIR-V -*- C++ -*--===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the SPIR-V specific subclass of TargetMachine.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTARGETMACHINE_H
+#define LLVM_LIB_TARGET_SPIRV_SPIRVTARGETMACHINE_H
+
+#include "llvm/IR/DataLayout.h"
+#include "llvm/Target/TargetMachine.h"
+
+namespace llvm {
+class SPIRVTargetMachine : public LLVMTargetMachine {
+ std::unique_ptr<TargetLoweringObjectFile> TLOF;
+
+public:
+ SPIRVTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
+ StringRef FS, const TargetOptions &Options,
+ Optional<Reloc::Model> RM, Optional<CodeModel::Model> CM,
+ CodeGenOpt::Level OL, bool JIT);
+
+ TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
+
+ TargetLoweringObjectFile *getObjFileLowering() const override {
+ return TLOF.get();
+ }
+};
+} // namespace llvm
+
+#endif
diff --git a/lib/Target/SPIRV/TargetInfo/CMakeLists.txt b/lib/Target/SPIRV/TargetInfo/CMakeLists.txt
new file mode 100644
index 0000000..a1f2da9
--- /dev/null
+++ b/lib/Target/SPIRV/TargetInfo/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_llvm_component_library(LLVMSPIRVInfo
+ SPIRVTargetInfo.cpp
+
+ LINK_COMPONENTS
+ MC
+ Support
+
+ ADD_TO_COMPONENT
+ SPIRV
+ )
diff --git a/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.cpp b/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.cpp
new file mode 100644
index 0000000..12ca131
--- /dev/null
+++ b/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.cpp
@@ -0,0 +1,33 @@
+//===-- SPIRVTargetInfo.cpp - SPIR-V Target Implementation ----*- C++ -*---===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "TargetInfo/SPIRVTargetInfo.h"
+#include "llvm/MC/TargetRegistry.h"
+
+using namespace llvm;
+
+Target &llvm::getTheSPIRV32Target() {
+ static Target TheSPIRV32Target;
+ return TheSPIRV32Target;
+}
+Target &llvm::getTheSPIRV64Target() {
+ static Target TheSPIRV64Target;
+ return TheSPIRV64Target;
+}
+
+extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetInfo() {
+ RegisterTarget<Triple::spirv32> X(getTheSPIRV32Target(), "spirv32",
+ "SPIR-V 32-bit", "SPIRV");
+ RegisterTarget<Triple::spirv64> Y(getTheSPIRV64Target(), "spirv64",
+ "SPIR-V 64-bit", "SPIRV");
+}
+
+// FIXME: Temporary stub - this function must be defined for linking
+// to succeed and will be called unconditionally by llc, so must be a no-op.
+// Remove once this function is properly implemented.
+extern "C" void LLVMInitializeSPIRVTargetMC() {}
diff --git a/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.h b/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.h
new file mode 100644
index 0000000..4353258
--- /dev/null
+++ b/lib/Target/SPIRV/TargetInfo/SPIRVTargetInfo.h
@@ -0,0 +1,21 @@
+//===-- SPIRVTargetInfo.h - SPIRV Target Implementation ---------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H
+#define LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H
+
+namespace llvm {
+
+class Target;
+
+Target &getTheSPIRV32Target();
+Target &getTheSPIRV64Target();
+
+} // namespace llvm
+
+#endif // LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H