Skip to content

Commit b2e5472

Browse files
committed
[RISCV] Add stub backend
This contains just enough for lib/Target/RISCV to compile. Notably a basic RISCVTargetMachine and RISCVTargetInfo. At this point you can attempt llc -march=riscv32 myinput.ll and will find it fails due to the lack of MCAsmInfo. See http://lists.llvm.org/pipermail/llvm-dev/2016-August/103748.html for further discussion Differential Revision: https://reviews.llvm.org/D23560 llvm-svn: 285712
1 parent 9677b60 commit b2e5472

File tree

11 files changed

+206
-0
lines changed

11 files changed

+206
-0
lines changed

llvm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ set(LLVM_ALL_TARGETS
279279
MSP430
280280
NVPTX
281281
PowerPC
282+
RISCV
282283
Sparc
283284
SystemZ
284285
X86

llvm/CODE_OWNERS.TXT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ E: mail@justinbogner.com
1717
D: InstrProfiling and related parts of ProfileData
1818
D: SelectionDAG (lib/CodeGen/SelectionDAG/*)
1919

20+
N: Alex Bradbury
21+
E: asb@lowrisc.org
22+
D: RISC-V backend (lib/Target/RISCV/*)
23+
2024
N: Chandler Carruth
2125
E: chandlerc@gmail.com
2226
E: chandlerc@google.com

llvm/docs/CompilerWriterInfo.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ AMDGPU
8383
* `AMD Compute Resources <http://developer.amd.com/tools/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/documentation/>`_
8484
* `AMDGPU Compute Application Binary Interface <https://github.com/RadeonOpenCompute/ROCm-ComputeABI-Doc/blob/master/AMDGPU-ABI.md>`__
8585

86+
RISC-V
87+
------
88+
* `RISC-V User-Level ISA Specification <https://riscv.org/specifications/>`_
89+
8690
SPARC
8791
-----
8892

llvm/lib/Target/LLVMBuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ subdirectories =
3030
NVPTX
3131
Mips
3232
PowerPC
33+
RISCV
3334
Sparc
3435
SystemZ
3536
WebAssembly

llvm/lib/Target/RISCV/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_llvm_target(RISCVCodeGen
2+
RISCVTargetMachine.cpp
3+
)
4+
5+
add_subdirectory(TargetInfo)

llvm/lib/Target/RISCV/LLVMBuild.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
;===- ./lib/Target/RISCV/LLVMBuild.txt -------------------------*- Conf -*--===;
2+
;
3+
; The LLVM Compiler Infrastructure
4+
;
5+
; This file is distributed under the University of Illinois Open Source
6+
; License. See LICENSE.TXT for details.
7+
;
8+
;===------------------------------------------------------------------------===;
9+
;
10+
; This is an LLVMBuild description file for the components in this subdirectory.
11+
;
12+
; For more information on the LLVMBuild system, please see:
13+
;
14+
; http://llvm.org/docs/LLVMBuild.html
15+
;
16+
;===------------------------------------------------------------------------===;
17+
18+
[common]
19+
subdirectories = TargetInfo
20+
21+
[component_0]
22+
type = TargetGroup
23+
name = RISCV
24+
parent = Target
25+
26+
[component_1]
27+
type = Library
28+
name = RISCVCodeGen
29+
parent = RISCV
30+
required_libraries = Core CodeGen RISCVInfo Support Target
31+
add_to_library_groups = RISCV
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===-- RISCVTargetMachine.cpp - Define TargetMachine for RISCV -----------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// Implements the info about RISCV target spec.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "RISCVTargetMachine.h"
15+
#include "llvm/ADT/STLExtras.h"
16+
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17+
#include "llvm/CodeGen/TargetPassConfig.h"
18+
#include "llvm/IR/LegacyPassManager.h"
19+
#include "llvm/CodeGen/Passes.h"
20+
#include "llvm/Support/FormattedStream.h"
21+
#include "llvm/Support/TargetRegistry.h"
22+
#include "llvm/Target/TargetOptions.h"
23+
using namespace llvm;
24+
25+
extern "C" void LLVMInitializeRISCVTarget() {
26+
RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
27+
RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
28+
}
29+
30+
static std::string computeDataLayout(const Triple &TT) {
31+
if (TT.isArch64Bit()) {
32+
return "e-m:e-i64:64-n32:64-S128";
33+
} else {
34+
assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
35+
return "e-m:e-i64:64-n32-S128";
36+
}
37+
}
38+
39+
static Reloc::Model getEffectiveRelocModel(const Triple &TT,
40+
Optional<Reloc::Model> RM) {
41+
if (!RM.hasValue())
42+
return Reloc::Static;
43+
return *RM;
44+
}
45+
46+
RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
47+
StringRef CPU, StringRef FS,
48+
const TargetOptions &Options,
49+
Optional<Reloc::Model> RM,
50+
CodeModel::Model CM,
51+
CodeGenOpt::Level OL)
52+
: LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
53+
getEffectiveRelocModel(TT, RM), CM, OL),
54+
TLOF(make_unique<TargetLoweringObjectFileELF>()) {}
55+
56+
TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) {
57+
return new TargetPassConfig(this, PM);
58+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===-- RISCVTargetMachine.h - Define TargetMachine for RISCV ---*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file declares the RISCV specific subclass of TargetMachine.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_RISCV_RISCVTARGETMACHINE_H
15+
#define LLVM_LIB_TARGET_RISCV_RISCVTARGETMACHINE_H
16+
17+
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
18+
#include "llvm/Target/TargetMachine.h"
19+
#include "llvm/IR/DataLayout.h"
20+
21+
namespace llvm {
22+
class RISCVTargetMachine : public LLVMTargetMachine {
23+
std::unique_ptr<TargetLoweringObjectFile> TLOF;
24+
25+
public:
26+
RISCVTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
27+
StringRef FS, const TargetOptions &Options,
28+
Optional<Reloc::Model> RM, CodeModel::Model CM,
29+
CodeGenOpt::Level OL);
30+
31+
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
32+
33+
TargetLoweringObjectFile *getObjFileLowering() const override {
34+
return TLOF.get();
35+
}
36+
};
37+
Target &getTheRISCV32Target();
38+
Target &getTheRISCV64Target();
39+
}
40+
41+
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_llvm_library(LLVMRISCVInfo
2+
RISCVTargetInfo.cpp
3+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
;===- ./lib/Target/RISCV/TargetInfo/LLVMBuild.txt --------------*- Conf -*--===;
2+
;
3+
; The LLVM Compiler Infrastructure
4+
;
5+
; This file is distributed under the University of Illinois Open Source
6+
; License. See LICENSE.TXT for details.
7+
;
8+
;===------------------------------------------------------------------------===;
9+
;
10+
; This is an LLVMBuild description file for the components in this subdirectory.
11+
;
12+
; For more information on the LLVMBuild system, please see:
13+
;
14+
; http://llvm.org/docs/LLVMBuild.html
15+
;
16+
;===------------------------------------------------------------------------===;
17+
18+
[component_0]
19+
type = Library
20+
name = RISCVInfo
21+
parent = RISCV
22+
required_libraries = Support
23+
add_to_library_groups = RISCV
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- RISCVTargetInfo.cpp - RISCV Target Implementation -----------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/Support/TargetRegistry.h"
11+
using namespace llvm;
12+
13+
namespace llvm {
14+
Target &getTheRISCV32Target() {
15+
static Target TheRISCV32Target;
16+
return TheRISCV32Target;
17+
}
18+
19+
Target &getTheRISCV64Target() {
20+
static Target TheRISCV64Target;
21+
return TheRISCV64Target;
22+
}
23+
}
24+
25+
extern "C" void LLVMInitializeRISCVTargetInfo() {
26+
RegisterTarget<Triple::riscv32> X(getTheRISCV32Target(), "riscv32",
27+
"32-bit RISC-V");
28+
RegisterTarget<Triple::riscv64> Y(getTheRISCV64Target(), "riscv64",
29+
"64-bit RISC-V");
30+
}
31+
32+
// FIXME: Temporary stub - this function must be defined for linking
33+
// to succeed and will be called unconditionally by llc, so must be a no-op.
34+
// Remove once this function is properly implemented.
35+
extern "C" void LLVMInitializeRISCVTargetMC() {}

0 commit comments

Comments
 (0)