Skip to content

Commit 7fd4622

Browse files
iliya-diyachkovzuban32michalpaszkowskiandreytr4intelktrifunovic
committed
[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>
1 parent b39d34d commit 7fd4622

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

llvm/CODE_OWNERS.TXT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,7 @@ D: MinGW
248248
N: Zi Xuan Wu (Zeson)
249249
E: zixuan.wu@linux.alibaba.com
250250
D: C-SKY backend (lib/Target/CSKY/*)
251+
252+
N: Ilia Diachkov
253+
E: iliya.diyachkov@intel.com
254+
D: SPIR-V backend (lib/Target/SPIRV/*)

llvm/docs/CompilerWriterInfo.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ NVPTX
198198
* `CUDA Documentation <http://docs.nvidia.com/cuda/index.html>`_ includes the PTX
199199
ISA and Driver API documentation
200200

201+
SPIR-V
202+
======
203+
204+
* `SPIR-V documentation <https://www.khronos.org/registry/SPIR-V/>`_
205+
201206
Miscellaneous Resources
202207
=======================
203208

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_llvm_component_group(SPIRV)
2+
3+
add_llvm_target(SPIRVCodeGen
4+
SPIRVTargetMachine.cpp
5+
6+
LINK_COMPONENTS
7+
CodeGen
8+
Core
9+
SPIRVInfo
10+
Support
11+
Target
12+
13+
ADD_TO_COMPONENT
14+
SPIRV
15+
)
16+
17+
add_subdirectory(TargetInfo)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===- SPIRVTargetMachine.cpp - Define TargetMachine for SPIR-V -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implements the info about SPIR-V target spec.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SPIRVTargetMachine.h"
14+
#include "TargetInfo/SPIRVTargetInfo.h"
15+
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
16+
#include "llvm/CodeGen/TargetPassConfig.h"
17+
#include "llvm/MC/TargetRegistry.h"
18+
19+
using namespace llvm;
20+
21+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
22+
// Register the target.
23+
RegisterTargetMachine<SPIRVTargetMachine> X(getTheSPIRV32Target());
24+
RegisterTargetMachine<SPIRVTargetMachine> Y(getTheSPIRV64Target());
25+
}
26+
27+
static std::string computeDataLayout(const Triple &TT) {
28+
std::string DataLayout = "e-m:e";
29+
30+
const auto Arch = TT.getArch();
31+
if (Arch == Triple::spirv32)
32+
DataLayout += "-p:32:32";
33+
else if (Arch == Triple::spirv64)
34+
DataLayout += "-p:64:64";
35+
return DataLayout;
36+
}
37+
38+
static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
39+
if (!RM)
40+
return Reloc::PIC_;
41+
return *RM;
42+
}
43+
44+
SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT,
45+
StringRef CPU, StringRef FS,
46+
const TargetOptions &Options,
47+
Optional<Reloc::Model> RM,
48+
Optional<CodeModel::Model> CM,
49+
CodeGenOpt::Level OL, bool JIT)
50+
: LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
51+
getEffectiveRelocModel(RM),
52+
getEffectiveCodeModel(CM, CodeModel::Small), OL),
53+
TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
54+
initAsmInfo();
55+
}
56+
57+
namespace {
58+
// SPIR-V Code Generator Pass Configuration Options.
59+
class SPIRVPassConfig : public TargetPassConfig {
60+
public:
61+
SPIRVPassConfig(SPIRVTargetMachine &TM, PassManagerBase &PM)
62+
: TargetPassConfig(TM, PM) {}
63+
64+
SPIRVTargetMachine &getSPIRVTargetMachine() const {
65+
return getTM<SPIRVTargetMachine>();
66+
}
67+
};
68+
} // namespace
69+
70+
TargetPassConfig *SPIRVTargetMachine::createPassConfig(PassManagerBase &PM) {
71+
return new SPIRVPassConfig(*this, PM);
72+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- SPIRVTargetMachine.h - Define TargetMachine for SPIR-V -*- C++ -*--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the SPIR-V specific subclass of TargetMachine.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTARGETMACHINE_H
14+
#define LLVM_LIB_TARGET_SPIRV_SPIRVTARGETMACHINE_H
15+
16+
#include "llvm/IR/DataLayout.h"
17+
#include "llvm/Target/TargetMachine.h"
18+
19+
namespace llvm {
20+
class SPIRVTargetMachine : public LLVMTargetMachine {
21+
std::unique_ptr<TargetLoweringObjectFile> TLOF;
22+
23+
public:
24+
SPIRVTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
25+
StringRef FS, const TargetOptions &Options,
26+
Optional<Reloc::Model> RM, Optional<CodeModel::Model> CM,
27+
CodeGenOpt::Level OL, bool JIT);
28+
29+
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
30+
31+
TargetLoweringObjectFile *getObjFileLowering() const override {
32+
return TLOF.get();
33+
}
34+
};
35+
} // namespace llvm
36+
37+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_llvm_component_library(LLVMSPIRVInfo
2+
SPIRVTargetInfo.cpp
3+
4+
LINK_COMPONENTS
5+
MC
6+
Support
7+
8+
ADD_TO_COMPONENT
9+
SPIRV
10+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- SPIRVTargetInfo.cpp - SPIR-V Target Implementation ----*- C++ -*---===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TargetInfo/SPIRVTargetInfo.h"
10+
#include "llvm/MC/TargetRegistry.h"
11+
12+
using namespace llvm;
13+
14+
Target &llvm::getTheSPIRV32Target() {
15+
static Target TheSPIRV32Target;
16+
return TheSPIRV32Target;
17+
}
18+
Target &llvm::getTheSPIRV64Target() {
19+
static Target TheSPIRV64Target;
20+
return TheSPIRV64Target;
21+
}
22+
23+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetInfo() {
24+
RegisterTarget<Triple::spirv32> X(getTheSPIRV32Target(), "spirv32",
25+
"SPIR-V 32-bit", "SPIRV");
26+
RegisterTarget<Triple::spirv64> Y(getTheSPIRV64Target(), "spirv64",
27+
"SPIR-V 64-bit", "SPIRV");
28+
}
29+
30+
// FIXME: Temporary stub - this function must be defined for linking
31+
// to succeed and will be called unconditionally by llc, so must be a no-op.
32+
// Remove once this function is properly implemented.
33+
extern "C" void LLVMInitializeSPIRVTargetMC() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- SPIRVTargetInfo.h - SPIRV Target Implementation ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H
10+
#define LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H
11+
12+
namespace llvm {
13+
14+
class Target;
15+
16+
Target &getTheSPIRV32Target();
17+
Target &getTheSPIRV64Target();
18+
19+
} // namespace llvm
20+
21+
#endif // LLVM_LIB_TARGET_SPIRV_TARGETINFO_SPIRVTARGETINFO_H

0 commit comments

Comments
 (0)