Skip to content

Commit b0be55c

Browse files
committed
[DirectX] initialize registers properties by calling addRegisterClass and computeRegisterProperties
This fixes #126784 for the DirectX backend. This bug was marked critical for DX so it is going to go in first. At least one register class needs to be added via addRegisterClass for RegClassForVT to be valid. Further for costing information used by loop unroll and other optimizations to be valid we need to call computeRegisterProperties. This change does both of these. The test cases confirm that we can fetch costing information off of `getRegisterInfo` and that `DirectXTargetLowering` maps i32 typed registers to DXILClassRegClass.
1 parent 0be3f13 commit b0be55c

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

llvm/lib/Target/DirectX/DirectXTargetMachine.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,7 @@ DirectXTargetMachine::getTargetTransformInfo(const Function &F) const {
187187

188188
DirectXTargetLowering::DirectXTargetLowering(const DirectXTargetMachine &TM,
189189
const DirectXSubtarget &STI)
190-
: TargetLowering(TM) {}
190+
: TargetLowering(TM) {
191+
addRegisterClass(MVT::i32, &dxil::DXILClassRegClass);
192+
computeRegisterProperties(STI.getRegisterInfo());
193+
}

llvm/unittests/Target/DirectX/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ add_llvm_target_unittest(DirectXTests
1616
CBufferDataLayoutTests.cpp
1717
PointerTypeAnalysisTests.cpp
1818
UniqueResourceFromUseTests.cpp
19+
RegisterCostTests.cpp
1920
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===- llvm/unittests/Target/DirectX/RegisterCostTests.cpp ----------------===//
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 "DirectXInstrInfo.h"
10+
#include "DirectXTargetLowering.h"
11+
#include "DirectXTargetMachine.h"
12+
#include "TargetInfo/DirectXTargetInfo.h"
13+
#include "llvm/IR/DerivedTypes.h"
14+
#include "llvm/IR/Function.h"
15+
#include "llvm/MC/MCTargetOptions.h"
16+
#include "llvm/MC/TargetRegistry.h"
17+
#include "llvm/Support/TargetSelect.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
using namespace llvm;
22+
using namespace llvm::dxil;
23+
24+
namespace {
25+
class RegisterCostTests : public testing::Test {
26+
protected:
27+
DirectXInstrInfo DXInstInfo;
28+
DirectXRegisterInfo RI;
29+
DirectXTargetLowering *DL;
30+
31+
virtual void SetUp() {
32+
LLVMInitializeDirectXTargetMC();
33+
Target T = getTheDirectXTarget();
34+
RegisterTargetMachine<DirectXTargetMachine> X(T);
35+
Triple TT("dxil-pc-shadermodel6.3-library");
36+
StringRef CPU = "";
37+
StringRef FS = "";
38+
DirectXTargetMachine TM(T, TT, CPU, FS, TargetOptions(), Reloc::Static,
39+
CodeModel::Small, CodeGenOptLevel::Default, false);
40+
LLVMContext Context;
41+
Function *F =
42+
Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
43+
Function::ExternalLinkage, 0);
44+
DL = new DirectXTargetLowering(TM, *TM.getSubtargetImpl(*F));
45+
delete F;
46+
}
47+
virtual void TearDown() { delete DL; }
48+
};
49+
50+
TEST_F(RegisterCostTests, TestRepRegClassForVTSet) {
51+
const TargetRegisterClass *RC = DL->getRepRegClassFor(MVT::i32);
52+
EXPECT_EQ(&dxil::DXILClassRegClass, RC);
53+
}
54+
55+
TEST_F(RegisterCostTests, TestTrivialCopyCostGetter) {
56+
57+
const TargetRegisterClass *RC = DXInstInfo.getRegisterInfo().getRegClass(0);
58+
unsigned Cost = RC->getCopyCost();
59+
EXPECT_EQ(1u, Cost);
60+
61+
RC = RI.getRegClass(0);
62+
Cost = RC->getCopyCost();
63+
EXPECT_EQ(1u, Cost);
64+
}
65+
} // namespace

0 commit comments

Comments
 (0)