Skip to content

Commit 7c67577

Browse files
committed
Respond to feedback
1 parent 8ce1a11 commit 7c67577

File tree

7 files changed

+75
-24
lines changed

7 files changed

+75
-24
lines changed

llvm/docs/SPIRVUsage.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,28 @@ representing an ``OpConstant`` id operand, a ``spirv.Literal`` type, representin
286286
literal operand, or any other type, representing the id of that type as an operand.
287287
``spirv.IntegralConstant`` and ``spirv.Literal`` may not be used outside of this context.
288288

289+
For example, ``OpTypeArray`` (opcode 28) takes an id for the element type and an id for the element
290+
length, so an array of 16 integers could be declared as:
291+
292+
``target("spirv.Type", i32, target("spirv.IntegralConstant", i32, 16), 28, 64, 32)``
293+
294+
This will be lowered to:
295+
296+
``OpTypeArray %int %int_16``
297+
298+
``OpTypeVector`` takes an id for the component type and a literal for the component count, so a
299+
4-integer vector could be declared as:
300+
301+
``target("spirv.Type", i32, target("spirv.Literal", 4), 23, 16, 32)``
302+
303+
This will be lowered to:
304+
305+
``OpTypeVector %int 4``
306+
307+
See `Target Extension Types for Inline SPIR-V and Decorated Types`_ for further details.
308+
309+
.. _Target Extension Types for Inline SPIR-V and Decorated Types: https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md
310+
289311
.. _spirv-intrinsics:
290312

291313
Target Intrinsics

llvm/lib/IR/Type.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,13 +975,16 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
975975
auto Size = Ty->getIntParameter(1);
976976
auto Alignment = Ty->getIntParameter(2);
977977

978-
// LLVM expects variables that can be allocated to have an alignment and
979-
// size. Default to using a 32-bit int as the layout type if none are
980-
// present.
981-
llvm::Type *LayoutType = Type::getInt32Ty(C);
982-
if (Size > 0 && Alignment > 0)
978+
llvm::Type *LayoutType;
979+
if (Size > 0 && Alignment > 0) {
983980
LayoutType =
984981
ArrayType::get(Type::getIntNTy(C, Alignment), Size * 8 / Alignment);
982+
} else {
983+
// LLVM expects variables that can be allocated to have an alignment and
984+
// size. Default to using a 32-bit int as the layout type if none are
985+
// present.
986+
LayoutType = Type::getInt32Ty(C);
987+
}
985988

986989
return TargetTypeInfo(LayoutType, TargetExtType::CanBeGlobal,
987990
TargetExtType::CanBeLocal);

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@ void SPIRVInstPrinter::printUnknownType(const MCInst *MI, raw_ostream &O) {
324324
const auto Enumerant = EnumOperand.getImm();
325325
const auto NumOps = MI->getNumOperands();
326326

327-
// Encode the instruction enumerant and word count into the opcode
328-
const auto OpCode = (0xFF & NumOps) << 16 | (0xFF & Enumerant);
329-
330327
// Print the opcode using the spirv-as unknown opcode syntax
331328
O << "OpUnknown(" << Enumerant << ", " << NumOps << ") ";
332329

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVMCCodeEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ void SPIRVMCCodeEmitter::encodeUnknownType(const MCInst &MI,
111111
SmallVectorImpl<char> &CB,
112112
SmallVectorImpl<MCFixup> &Fixups,
113113
const MCSubtargetInfo &STI) const {
114-
// Encode the first 32 SPIR-V bytes with the number of args and the opcode.
114+
// Encode the first 32 SPIR-V bits with the number of args and the opcode.
115115
const uint64_t OpCode = MI.getOperand(1).getImm();
116116
const uint32_t NumWords = MI.getNumOperands();
117-
const uint32_t FirstWord = (0xFF & NumWords) << 16 | (0xFF & OpCode);
117+
const uint32_t FirstWord = (0xFFFF & NumWords) << 16 | (0xFFFF & OpCode);
118118

119119
// encoding: <opcode+len> <result type> [<operand0> <operand1> ...]
120120
support::endian::write(CB, FirstWord, llvm::endianness::little);

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,18 +1414,22 @@ SPIRVType *SPIRVGlobalRegistry::getOrCreateUnknownType(
14141414
return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(ResVReg);
14151415
ResVReg = createTypeVReg(MIRBuilder);
14161416

1417-
MachineInstrBuilder MIB =
1418-
MIRBuilder.buildInstr(SPIRV::UNKNOWN_type).addDef(ResVReg).addImm(Opcode);
1419-
for (MCOperand Operand : Operands) {
1420-
if (Operand.isReg()) {
1421-
MIB.addUse(Operand.getReg());
1422-
} else if (Operand.isImm()) {
1423-
MIB.addImm(Operand.getImm());
1417+
DT.add(Ty, &MIRBuilder.getMF(), ResVReg);
1418+
1419+
return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1420+
MachineInstrBuilder MIB = MIRBuilder.buildInstr(SPIRV::UNKNOWN_type)
1421+
.addDef(ResVReg)
1422+
.addImm(Opcode);
1423+
for (MCOperand Operand : Operands) {
1424+
if (Operand.isReg()) {
1425+
MIB.addUse(Operand.getReg());
1426+
} else if (Operand.isImm()) {
1427+
MIB.addImm(Operand.getImm());
1428+
}
14241429
}
1425-
}
14261430

1427-
DT.add(Ty, &MIRBuilder.getMF(), ResVReg);
1428-
return MIB;
1431+
return MIB;
1432+
});
14291433
}
14301434

14311435
const MachineInstr *
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
2+
3+
; CHECK: [[float_t:%[0-9]+]] = OpTypeFloat 32
4+
; CHECK: [[uint32_t:%[0-9]+]] = OpTypeInt 32 0
5+
6+
; CHECK: [[uint32_2:%[0-9]+]] = OpConstant [[uint32_t]] 2
7+
%scope = type target("spirv.IntegralConstant", i32, 2) ; Workgroup
8+
; CHECK: [[uint32_4:%[0-9]+]] = OpConstant [[uint32_t]] 4
9+
%cols = type target("spirv.IntegralConstant", i32, 4)
10+
%rows = type target("spirv.IntegralConstant", i32, 4)
11+
; CHECK: [[uint32_0:%[0-9]+]] = OpConstant [[uint32_t]] 0
12+
%use = type target("spirv.IntegralConstant", i32, 0) ; MatrixAKHR
13+
14+
; CHECK: OpUnknown(4456, 7) [[coop_t:%[0-9]+]] [[float_t]] [[uint32_2]] [[uint32_4]] [[uint32_4]] [[uint32_0]]
15+
%coop_t = type target("spirv.Type", float, %scope, %rows, %cols, %use, 4456, 0, 0)
16+
17+
; CHECK: [[getCooperativeMatrix_t:%[0-9]+]] = OpTypeFunction [[coop_t]]
18+
19+
; CHECK: [[getCooperativeMatrix:%[0-9]+]] = OpFunction [[coop_t]] None [[getCooperativeMatrix_t]]
20+
declare %coop_t @getCooperativeMatrix()
21+
22+
define void @main() #1 {
23+
entry:
24+
; CHECK: {{%[0-9]+}} = OpFunctionCall [[coop_t]] [[getCooperativeMatrix]]
25+
%coop = call %coop_t @getCooperativeMatrix()
26+
27+
ret void
28+
}

llvm/test/CodeGen/SPIRV/inline/type.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
; CHECK: OpUnknown(28, 4) [[array_t:%[0-9]+]] [[image_t]] [[uint32_4]]
1717
%ArrayTex2D = type target("spirv.Type", %type_2d_image, %integral_constant_4, 28, 0, 0)
1818

19-
; CHECK: [[getTexArray_t:%[0-9]+]] = OpTypeFunction [[array_t]]
20-
2119
; CHECK: OpUnknown(21, 4) [[int_t:%[0-9]+]] 32 1
2220
%int_t = type target("spirv.Type", %literal_32, %literal_true, 21, 0, 0)
2321

22+
; CHECK: [[getTexArray_t:%[0-9]+]] = OpTypeFunction [[array_t]]
2423
; CHECK: [[getInt_t:%[0-9]+]] = OpTypeFunction [[int_t]]
2524

2625
; CHECK: [[getTexArray:%[0-9]+]] = OpFunction [[array_t]] None [[getTexArray_t]]
@@ -31,8 +30,6 @@ declare %int_t @getInt()
3130

3231
define void @main() #1 {
3332
entry:
34-
%images = alloca %ArrayTex2D
35-
3633
; CHECK: {{%[0-9]+}} = OpFunctionCall [[array_t]] [[getTexArray]]
3734
%retTex = call %ArrayTex2D @getTexArray()
3835

0 commit comments

Comments
 (0)