Skip to content

Commit 6e08094

Browse files
[SPIR-V] Promote arbitrary width ints to regular width
1 parent 9ad277b commit 6e08094

6 files changed

Lines changed: 90 additions & 12 deletions

File tree

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeBool(MachineIRBuilder &MIRBuilder) {
8080
SPIRVType *SPIRVGlobalRegistry::getOpTypeInt(uint32_t Width,
8181
MachineIRBuilder &MIRBuilder,
8282
bool IsSigned) {
83+
assert(Width <= 64 && "Unsupported integer width!");
84+
if (Width <= 8)
85+
Width = 8;
86+
else if (Width <= 16)
87+
Width = 16;
88+
else if (Width <= 32)
89+
Width = 32;
90+
else if (Width <= 64)
91+
Width = 64;
92+
8393
auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeInt)
8494
.addDef(createTypeVReg(MIRBuilder))
8595
.addImm(Width)

llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ unsigned SPIRVTargetLowering::getNumRegistersForCallingConv(
2727
(VT.getVectorElementType() == MVT::i1 ||
2828
VT.getVectorElementType() == MVT::i8))
2929
return 1;
30+
if (!VT.isVector() && VT.isInteger() && VT.getSizeInBits() <= 64)
31+
return 1;
3032
return getNumRegisters(Context, VT);
3133
}
3234

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ bool SPIRVInstructionSelector::selectConst(Register ResVReg,
11891189
.addUse(GR.getSPIRVTypeID(ResType))
11901190
.constrainAllUses(TII, TRI, RBI);
11911191
if (TyOpcode == SPIRV::OpTypeInt) {
1192+
assert(Imm.getBitWidth() <= 64 && "Unsupported integer width!");
11921193
Register Reg = GR.getOrCreateConstInt(Imm.getZExtValue(), I, ResType, TII);
11931194
if (Reg == ResVReg)
11941195
return true;

llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,19 @@ std::string getStringImm(const MachineInstr &MI, unsigned StartIndex) {
7777

7878
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB) {
7979
const auto Bitwidth = Imm.getBitWidth();
80-
switch (Bitwidth) {
81-
case 1:
82-
break; // Already handled.
83-
case 8:
84-
case 16:
85-
case 32:
80+
if (Bitwidth == 1)
81+
return; // Already handled
82+
else if (Bitwidth <= 32) {
8683
MIB.addImm(Imm.getZExtValue());
87-
break;
88-
case 64: {
84+
return;
85+
} else if (Bitwidth <= 64) {
8986
uint64_t FullImm = Imm.getZExtValue();
9087
uint32_t LowBits = FullImm & 0xffffffff;
9188
uint32_t HighBits = (FullImm >> 32) & 0xffffffff;
9289
MIB.addImm(LowBits).addImm(HighBits);
93-
break;
94-
}
95-
default:
96-
report_fatal_error("Unsupported constant bitwidth");
90+
return;
9791
}
92+
report_fatal_error("Unsupported constant bitwidth");
9893
}
9994

10095
void buildOpName(Register Target, const StringRef &Name,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
2+
3+
define i4 @getConstantI4() {
4+
ret i4 2 ; i4 => OpTypeInt 8
5+
}
6+
7+
define i11 @getConstantI11() {
8+
ret i11 7 ; i11 => OpTypeInt 16
9+
}
10+
11+
define i24 @getConstantI24() {
12+
ret i24 42 ; i24 => OpTypeInt 32
13+
}
14+
15+
define i63 @getConstantI63() {
16+
ret i63 5705 ; i63 => OpTypeInt 64
17+
}
18+
19+
;; Capabilities:
20+
; CHECK-DAG: OpCapability Int8
21+
; CHECK-DAG: OpCapability Int16
22+
; CHECK-DAG: OpCapability Int64
23+
24+
; CHECK-NOT: DAG-FENCE
25+
26+
;; Names:
27+
; CHECK-DAG: OpName %[[#GET_I4:]] "getConstantI4"
28+
; CHECK-DAG: OpName %[[#GET_I11:]] "getConstantI11"
29+
; CHECK-DAG: OpName %[[#GET_I24:]] "getConstantI24"
30+
; CHECK-DAG: OpName %[[#GET_I63:]] "getConstantI63"
31+
32+
; CHECK-NOT: DAG-FENCE
33+
34+
;; Types and Constants:
35+
; CHECK-DAG: %[[#I8:]] = OpTypeInt 8 0
36+
; CHECK-DAG: %[[#I16:]] = OpTypeInt 16 0
37+
; CHECK-DAG: %[[#I32:]] = OpTypeInt 32 0
38+
; CHECK-DAG: %[[#I64:]] = OpTypeInt 64 0
39+
; CHECK-DAG: %[[#CST_I8:]] = OpConstant %[[#I8]] 2
40+
; CHECK-DAG: %[[#CST_I16:]] = OpConstant %[[#I16]] 7
41+
; CHECK-DAG: %[[#CST_I32:]] = OpConstant %[[#I32]] 42
42+
; CHECK-DAG: %[[#CST_I64:]] = OpConstant %[[#I64]] 5705
43+
44+
; CHECK: %[[#GET_I4]] = OpFunction %[[#I8]]
45+
; CHECK: OpReturnValue %[[#CST_I8]]
46+
; CHECK: OpFunctionEnd
47+
48+
; CHECK: %[[#GET_I11]] = OpFunction %[[#I16]]
49+
; CHECK: OpReturnValue %[[#CST_I16]]
50+
; CHECK: OpFunctionEnd
51+
52+
; CHECK: %[[#GET_I24]] = OpFunction %[[#I32]]
53+
; CHECK: OpReturnValue %[[#CST_I32]]
54+
; CHECK: OpFunctionEnd
55+
56+
; CHECK: %[[#GET_I63]] = OpFunction %[[#I64]]
57+
; CHECK: OpReturnValue %[[#CST_I64]]
58+
; CHECK: OpFunctionEnd

llvm/test/CodeGen/SPIRV/constant/local-integers-constants.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
22

3+
define i8 @getConstantI8() {
4+
ret i8 2
5+
}
6+
37
define i16 @getConstantI16() {
48
ret i16 -58
59
}
@@ -17,12 +21,14 @@ define i64 @getLargeConstantI64() {
1721
}
1822

1923
;; Capabilities:
24+
; CHECK-DAG: OpCapability Int8
2025
; CHECK-DAG: OpCapability Int16
2126
; CHECK-DAG: OpCapability Int64
2227

2328
; CHECK-NOT: DAG-FENCE
2429

2530
;; Names:
31+
; CHECK-DAG: OpName %[[#GET_I8:]] "getConstantI8"
2632
; CHECK-DAG: OpName %[[#GET_I16:]] "getConstantI16"
2733
; CHECK-DAG: OpName %[[#GET_I32:]] "getConstantI32"
2834
; CHECK-DAG: OpName %[[#GET_I64:]] "getConstantI64"
@@ -31,14 +37,20 @@ define i64 @getLargeConstantI64() {
3137
; CHECK-NOT: DAG-FENCE
3238

3339
;; Types and Constants:
40+
; CHECK-DAG: %[[#I8:]] = OpTypeInt 8 0
3441
; CHECK-DAG: %[[#I16:]] = OpTypeInt 16 0
3542
; CHECK-DAG: %[[#I32:]] = OpTypeInt 32 0
3643
; CHECK-DAG: %[[#I64:]] = OpTypeInt 64 0
44+
; CHECK-DAG: %[[#CST_I8:]] = OpConstant %[[#I8]] 2
3745
; CHECK-DAG: %[[#CST_I16:]] = OpConstant %[[#I16]] 65478
3846
; CHECK-DAG: %[[#CST_I32:]] = OpConstant %[[#I32]] 42
3947
; CHECK-DAG: %[[#CST_I64:]] = OpConstant %[[#I64]] 123456789 0
4048
; CHECK-DAG: %[[#CST_LARGE_I64:]] = OpConstant %[[#I64]] 0 8
4149

50+
; CHECK: %[[#GET_I8]] = OpFunction %[[#I8]]
51+
; CHECK: OpReturnValue %[[#CST_I8]]
52+
; CHECK: OpFunctionEnd
53+
4254
; CHECK: %[[#GET_I16]] = OpFunction %[[#I16]]
4355
; CHECK: OpReturnValue %[[#CST_I16]]
4456
; CHECK: OpFunctionEnd

0 commit comments

Comments
 (0)