Skip to content

Commit 44a14a6

Browse files
committed
Add stub DirectX backend
This is the bare minimum needed to get the DirectX target compiling, but does not actually do anything. Reviewed By: pete, rnk, arsenm, jaebaek Differential Revision: https://reviews.llvm.org/D122080
1 parent a29fd4d commit 44a14a6

13 files changed

+489
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
add_llvm_component_group(DirectX)
2+
3+
set(LLVM_TARGET_DEFINITIONS DirectX.td)
4+
5+
tablegen(LLVM DirectXGenSubtargetInfo.inc -gen-subtarget)
6+
7+
add_public_tablegen_target(DirectXCommonTableGen)
8+
9+
add_llvm_target(DirectXCodeGen
10+
DirectXSubtarget.cpp
11+
DirectXTargetMachine.cpp
12+
13+
LINK_COMPONENTS
14+
Bitwriter
15+
Core
16+
Support
17+
DirectXInfo
18+
19+
ADD_TO_COMPONENT
20+
DirectX
21+
)
22+
23+
add_subdirectory(MCTargetDesc)
24+
add_subdirectory(TargetInfo)

llvm/lib/Target/DirectX/DirectX.td

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//- DirectX.td - Describe the DirectX Target Machine ----------*- tablegen -*-//
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+
/// \file
10+
/// This is a target description file for the DirectX target
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
//===----------------------------------------------------------------------===//
15+
// Target-independent interfaces which we are implementing
16+
//===----------------------------------------------------------------------===//
17+
18+
include "llvm/Target/Target.td"
19+
20+
//===----------------------------------------------------------------------===//
21+
// DirectX Subtarget features.
22+
//===----------------------------------------------------------------------===//
23+
24+
def DirectXInstrInfo : InstrInfo;
25+
26+
//===----------------------------------------------------------------------===//
27+
// DirectX Processors supported.
28+
//===----------------------------------------------------------------------===//
29+
30+
def : ProcessorModel<"generic", NoSchedModel, []>;
31+
32+
33+
//===----------------------------------------------------------------------===//
34+
// Target Declaration
35+
//===----------------------------------------------------------------------===//
36+
37+
def DirectXAsmParser : AsmParser {
38+
// The physical register names are not in the binary format or asm text
39+
let ShouldEmitMatchRegisterName = 0;
40+
}
41+
42+
def DirectXAsmWriter : AsmWriter {
43+
string AsmWriterClassName = "InstPrinter";
44+
int PassSubtarget = 0;
45+
int Variant = 0;
46+
bit isMCAsmWriter = 1;
47+
}
48+
49+
def DirectX : Target {
50+
let InstructionSet = DirectXInstrInfo;
51+
let AssemblyParsers = [DirectXAsmParser];
52+
let AssemblyWriters = [DirectXAsmWriter];
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- DirectXSubtarget.cpp - DirectX Subtarget Information --------------===//
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+
/// \file
10+
/// This file implements the DirectX-specific subclass of TargetSubtarget.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "DirectXSubtarget.h"
15+
#include "DirectXTargetLowering.h"
16+
17+
using namespace llvm;
18+
19+
#define DEBUG_TYPE "directx-subtarget"
20+
21+
#define GET_SUBTARGETINFO_CTOR
22+
#define GET_SUBTARGETINFO_TARGET_DESC
23+
#include "DirectXGenSubtargetInfo.inc"
24+
25+
DirectXSubtarget::DirectXSubtarget(const Triple &TT, StringRef CPU,
26+
StringRef FS, const DirectXTargetMachine &TM)
27+
: DirectXGenSubtargetInfo(TT, CPU, CPU, FS), TL(TM, *this) {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- DirectXSubtarget.h - Define Subtarget for DirectX -------*- 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 DirectX specific subclass of TargetSubtargetInfo.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_DIRECTX_DIRECTXSUBTARGET_H
14+
#define LLVM_DIRECTX_DIRECTXSUBTARGET_H
15+
16+
#include "DirectXTargetLowering.h"
17+
#include "llvm/CodeGen/TargetSubtargetInfo.h"
18+
#include "llvm/IR/DataLayout.h"
19+
#include "llvm/Target/TargetMachine.h"
20+
21+
#define GET_SUBTARGETINFO_HEADER
22+
#include "DirectXGenSubtargetInfo.inc"
23+
24+
namespace llvm {
25+
26+
class DirectXTargetMachine;
27+
28+
class DirectXSubtarget : public DirectXGenSubtargetInfo {
29+
DirectXTargetLowering TL;
30+
31+
public:
32+
DirectXSubtarget(const Triple &TT, StringRef CPU, StringRef FS,
33+
const DirectXTargetMachine &TM);
34+
35+
/// Parses a subtarget feature string, setting appropriate options.
36+
/// \note Definition of function is auto generated by `tblgen`.
37+
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
38+
39+
const DirectXTargetLowering *getTargetLowering() const override {
40+
return &TL;
41+
}
42+
};
43+
44+
} // end namespace llvm
45+
46+
#endif // LLVM_DIRECTX_DIRECTXSUBTARGET_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- DirectXTargetLowering.h - Define DX TargetLowering -----*- 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 DirectX specific subclass of TargetLowering.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_DIRECTX_DIRECTXTARGETLOWERING_H
14+
#define LLVM_DIRECTX_DIRECTXTARGETLOWERING_H
15+
16+
#include "llvm/CodeGen/TargetLowering.h"
17+
18+
namespace llvm {
19+
20+
class DirectXSubtarget;
21+
class DirectXTargetMachine;
22+
23+
class DirectXTargetLowering : public TargetLowering {
24+
public:
25+
explicit DirectXTargetLowering(const DirectXTargetMachine &TM,
26+
const DirectXSubtarget &STI);
27+
};
28+
29+
} // end namespace llvm
30+
31+
#endif // LLVM_DIRECTX_DIRECTXTARGETLOWERING_H
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//===- DirectXTargetMachine.cpp - DirectX 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+
/// \file
10+
/// This file contains DirectX target initializer.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "DirectXTargetMachine.h"
15+
#include "DirectXSubtarget.h"
16+
#include "DirectXTargetTransformInfo.h"
17+
#include "TargetInfo/DirectXTargetInfo.h"
18+
#include "llvm/Bitcode/BitcodeWriterPass.h"
19+
#include "llvm/CodeGen/Passes.h"
20+
#include "llvm/CodeGen/TargetPassConfig.h"
21+
#include "llvm/IR/IRPrintingPasses.h"
22+
#include "llvm/IR/LegacyPassManager.h"
23+
#include "llvm/MC/SectionKind.h"
24+
#include "llvm/MC/TargetRegistry.h"
25+
#include "llvm/Support/CodeGen.h"
26+
#include "llvm/Support/Compiler.h"
27+
#include "llvm/Support/ErrorHandling.h"
28+
#include "llvm/Target/TargetLoweringObjectFile.h"
29+
30+
using namespace llvm;
31+
32+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
33+
RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget());
34+
}
35+
36+
class DXILTargetObjectFile : public TargetLoweringObjectFile {
37+
public:
38+
DXILTargetObjectFile() = default;
39+
40+
MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
41+
const TargetMachine &TM) const override {
42+
llvm_unreachable("Not supported!");
43+
}
44+
45+
protected:
46+
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
47+
const TargetMachine &TM) const override {
48+
llvm_unreachable("Not supported!");
49+
}
50+
};
51+
52+
class DirectXPassConfig : public TargetPassConfig {
53+
public:
54+
DirectXPassConfig(DirectXTargetMachine &TM, PassManagerBase &PM)
55+
: TargetPassConfig(TM, PM) {}
56+
57+
DirectXTargetMachine &getDirectXTargetMachine() const {
58+
return getTM<DirectXTargetMachine>();
59+
}
60+
61+
FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
62+
};
63+
64+
DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
65+
StringRef CPU, StringRef FS,
66+
const TargetOptions &Options,
67+
Optional<Reloc::Model> RM,
68+
Optional<CodeModel::Model> CM,
69+
CodeGenOpt::Level OL, bool JIT)
70+
: LLVMTargetMachine(T,
71+
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
72+
"f32:32-f64:64-n8:16:32:64",
73+
TT, CPU, FS, Options, Reloc::Static, CodeModel::Small,
74+
OL),
75+
TLOF(std::make_unique<DXILTargetObjectFile>()),
76+
Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {}
77+
78+
DirectXTargetMachine::~DirectXTargetMachine() {}
79+
80+
bool DirectXTargetMachine::addPassesToEmitFile(
81+
PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
82+
CodeGenFileType FileType, bool DisableVerify,
83+
MachineModuleInfoWrapperPass *MMIWP) {
84+
switch (FileType) {
85+
case CGFT_AssemblyFile:
86+
PM.add(createPrintModulePass(Out, "", true));
87+
break;
88+
case CGFT_ObjectFile:
89+
// TODO: Write DXIL instead of bitcode
90+
PM.add(createBitcodeWriterPass(Out, true, false, false));
91+
break;
92+
case CGFT_Null:
93+
break;
94+
}
95+
return false;
96+
}
97+
98+
bool DirectXTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
99+
MCContext *&Ctx,
100+
raw_pwrite_stream &Out,
101+
bool DisableVerify) {
102+
return true;
103+
}
104+
105+
TargetPassConfig *DirectXTargetMachine::createPassConfig(PassManagerBase &PM) {
106+
return new DirectXPassConfig(*this, PM);
107+
}
108+
109+
const DirectXSubtarget *
110+
DirectXTargetMachine::getSubtargetImpl(const Function &) const {
111+
return Subtarget.get();
112+
}
113+
114+
TargetTransformInfo
115+
DirectXTargetMachine::getTargetTransformInfo(const Function &F) const {
116+
return TargetTransformInfo(DirectXTTIImpl(this, F));
117+
}
118+
119+
DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM,
120+
const DirectXSubtarget &STI)
121+
: TargetLowering(TM) {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===- DirectXTargetMachine.h - DirectX 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+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef LLVM_DIRECTX_DIRECTXTARGETMACHINE_H
12+
#define LLVM_DIRECTX_DIRECTXTARGETMACHINE_H
13+
14+
#include "DirectXSubtarget.h"
15+
#include "llvm/Target/TargetMachine.h"
16+
17+
namespace llvm {
18+
class Function;
19+
class DirectXTargetMachine : public LLVMTargetMachine {
20+
std::unique_ptr<TargetLoweringObjectFile> TLOF;
21+
std::unique_ptr<DirectXSubtarget> Subtarget;
22+
23+
public:
24+
DirectXTargetMachine(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+
~DirectXTargetMachine() override;
30+
31+
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
32+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
33+
bool DisableVerify,
34+
MachineModuleInfoWrapperPass *MMIWP) override;
35+
36+
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
37+
raw_pwrite_stream &Out, bool DisableVerify) override;
38+
39+
const DirectXSubtarget *getSubtargetImpl(const Function &) const override;
40+
41+
TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
42+
43+
TargetLoweringObjectFile *getObjFileLowering() const override {
44+
return TLOF.get();
45+
}
46+
47+
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
48+
};
49+
} // namespace llvm
50+
51+
#endif // LLVM_DIRECTX_DIRECTXTARGETMACHINE_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===- DirectXTargetTransformInfo.h - DirectX TTI ---------------*- 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+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef LLVM_DIRECTX_DIRECTXTARGETTRANSFORMINFO_H
12+
#define LLVM_DIRECTX_DIRECTXTARGETTRANSFORMINFO_H
13+
14+
#include "DirectXSubtarget.h"
15+
#include "DirectXTargetMachine.h"
16+
#include "llvm/CodeGen/BasicTTIImpl.h"
17+
#include "llvm/IR/Function.h"
18+
19+
namespace llvm {
20+
class DirectXTTIImpl : public BasicTTIImplBase<DirectXTTIImpl> {
21+
using BaseT = BasicTTIImplBase<DirectXTTIImpl>;
22+
using TTI = TargetTransformInfo;
23+
24+
friend BaseT;
25+
26+
const DirectXSubtarget *ST;
27+
const DirectXTargetLowering *TLI;
28+
29+
const DirectXSubtarget *getST() const { return ST; }
30+
const DirectXTargetLowering *getTLI() const { return TLI; }
31+
32+
public:
33+
explicit DirectXTTIImpl(const DirectXTargetMachine *TM, const Function &F)
34+
: BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
35+
TLI(ST->getTargetLowering()) {}
36+
};
37+
} // namespace llvm
38+
39+
#endif // LLVM_DIRECTX_DIRECTXTARGETTRANSFORMINFO_H

0 commit comments

Comments
 (0)