Skip to content

Commit d909152

Browse files
findeevyigcbot
authored andcommitted
Migrate OpenCLVectorTypeHint to ModuleMetaData FuncMD
Move the per-function OpenCL vec_type_hint attribute from the legacy LLVM-MDNode-based MetaDataApi framework to the plain-memory ModuleMetaData::FuncMD structure, stored as its ZEBinary string form. The LLVM-type to string reduction now runs at SPIR-V metadata translation time and the result is stored as a POD string, so code generation reads the string directly without an MDNode round-trip. The underlying VectorTypeHintMetaData class is retained for the SPIR-V input metadata layer.
1 parent bc847d5 commit d909152

9 files changed

Lines changed: 164 additions & 85 deletions

File tree

IGC/Compiler/CISACodeGen/OpenCLKernelCodeGen.cpp

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -533,63 +533,6 @@ uint32_t COpenCLKernel::getMaxPressureForSIMD(llvm::Function &F, unsigned SimdLa
533533
return maxPressure;
534534
}
535535

536-
std::string COpenCLKernel::getVecTypeHintTypeString(const VectorTypeHintMetaDataHandle &vecTypeHintInfo) const {
537-
std::string vecTypeString;
538-
539-
// Get the information about the type
540-
Type *baseType = vecTypeHintInfo->getVecType()->getType();
541-
unsigned int numElements = 1;
542-
if (baseType->isVectorTy()) {
543-
numElements = (unsigned)cast<IGCLLVM::FixedVectorType>(baseType)->getNumElements();
544-
baseType = cast<VectorType>(baseType)->getElementType();
545-
}
546-
547-
// ExecutionModel doesn't differentiate base type in term of signed/unsigned.
548-
if (baseType->isIntegerTy()) {
549-
vecTypeString += "u";
550-
}
551-
552-
switch (baseType->getTypeID()) {
553-
case Type::IntegerTyID:
554-
switch (baseType->getIntegerBitWidth()) {
555-
case 8:
556-
vecTypeString += "char";
557-
break;
558-
case 16:
559-
vecTypeString += "short";
560-
break;
561-
case 32:
562-
vecTypeString += "int";
563-
break;
564-
case 64:
565-
vecTypeString += "long";
566-
break;
567-
default:
568-
IGC_ASSERT_MESSAGE(0, "Unexpected data type in vec_type_hint");
569-
break;
570-
}
571-
break;
572-
case Type::DoubleTyID:
573-
vecTypeString += "double";
574-
break;
575-
case Type::FloatTyID:
576-
vecTypeString += "float";
577-
break;
578-
case Type::HalfTyID:
579-
vecTypeString += "half";
580-
break;
581-
default:
582-
IGC_ASSERT_MESSAGE(0, "Unexpected data type in vec_type_hint");
583-
break;
584-
}
585-
586-
if (numElements != 1) {
587-
vecTypeString += utostr(numElements);
588-
}
589-
590-
return vecTypeString;
591-
}
592-
593536
bool COpenCLKernel::CreateZEPayloadArguments(IGC::KernelArg *kernelArg, uint payloadPosition,
594537
PtrArgsAttrMapType &ptrArgsAttrMap) {
595538
#ifndef DX_ONLY_IGC
@@ -1832,9 +1775,9 @@ void COpenCLKernel::FillZEUserAttributes(IGC::IGCMD::FunctionInfoMetaDataHandle
18321775
}
18331776

18341777
// vec_type_hint
1835-
VectorTypeHintMetaDataHandle vecTypeHintInfo = funcInfoMD->getOpenCLVectorTypeHint();
1836-
if (vecTypeHintInfo->hasValue()) {
1837-
m_kernelInfo.m_zeUserAttributes.vec_type_hint = getVecTypeHintTypeString(vecTypeHintInfo);
1778+
auto itVecTypeHint = m_Context->getModuleMetaData()->FuncMD.find(entry);
1779+
if (itVecTypeHint != m_Context->getModuleMetaData()->FuncMD.end() && !itVecTypeHint->second.vecTypeHint.empty()) {
1780+
m_kernelInfo.m_zeUserAttributes.vec_type_hint = itVecTypeHint->second.vecTypeHint;
18381781
}
18391782

18401783
// work_group_size_hint

IGC/Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ class COpenCLKernel : public CComputeShaderBase {
184184
// Creates annotations for inline sampler_t objects
185185
void CreateZEInlineSamplerAnnotations();
186186

187-
// A helper function to get vector type hint string for filling user attributes
188-
std::string getVecTypeHintTypeString(const IGC::IGCMD::VectorTypeHintMetaDataHandle &vecTypeHintInfo) const;
189-
190187
// Load from MD and return the resource information for argument number argNo
191188
SOpenCLKernelInfo::SResourceInfo getResourceInfo(int argNo);
192189

IGC/Compiler/MetaDataApi/MetaDataApi.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,36 +218,30 @@ FunctionInfoMetaData::FunctionInfoMetaData(const llvm::MDNode *pNode, bool hasId
218218
: _Mybase(pNode, hasId), m_Type(getNamedNode(pNode, "function_type")),
219219
m_ArgInfoList(getNamedNode(pNode, "arg_desc"), true),
220220
m_ImplicitArgInfoList(getNamedNode(pNode, "implicit_arg_desc"), true),
221-
m_SubGroupSize(new SubGroupSizeMetaData(getNamedNode(pNode, "sub_group_size"), true)),
222-
m_OpenCLVectorTypeHint(new VectorTypeHintMetaData(getNamedNode(pNode, "opencl_vec_type_hint"), true)),
223-
m_pNode(pNode) {}
221+
m_SubGroupSize(new SubGroupSizeMetaData(getNamedNode(pNode, "sub_group_size"), true)), m_pNode(pNode) {}
224222

225223
FunctionInfoMetaData::FunctionInfoMetaData()
226224
: m_Type("function_type"), m_ArgInfoList("arg_desc"), m_ImplicitArgInfoList("implicit_arg_desc"),
227-
m_SubGroupSize(new SubGroupSizeMetaDataHandle::ObjectType("sub_group_size")),
228-
m_OpenCLVectorTypeHint(new VectorTypeHintMetaDataHandle::ObjectType("opencl_vec_type_hint")), m_pNode(nullptr) {}
225+
m_SubGroupSize(new SubGroupSizeMetaDataHandle::ObjectType("sub_group_size")), m_pNode(nullptr) {}
229226

230227
FunctionInfoMetaData::FunctionInfoMetaData(const char *name)
231228
: _Mybase(name), m_Type("function_type"), m_ArgInfoList("arg_desc"), m_ImplicitArgInfoList("implicit_arg_desc"),
232-
m_SubGroupSize(new SubGroupSizeMetaDataHandle::ObjectType("sub_group_size")),
233-
m_OpenCLVectorTypeHint(new VectorTypeHintMetaDataHandle::ObjectType("opencl_vec_type_hint")), m_pNode(nullptr) {}
229+
m_SubGroupSize(new SubGroupSizeMetaDataHandle::ObjectType("sub_group_size")), m_pNode(nullptr) {}
234230

235231
bool FunctionInfoMetaData::hasValue() const {
236232
return m_Type.hasValue() || m_ArgInfoList.hasValue() || m_ImplicitArgInfoList.hasValue() ||
237-
m_SubGroupSize->hasValue() || m_OpenCLVectorTypeHint->hasValue() || nullptr != m_pNode || dirty();
233+
m_SubGroupSize->hasValue() || nullptr != m_pNode || dirty();
238234
}
239235

240236
bool FunctionInfoMetaData::dirty() const {
241-
return m_Type.dirty() || m_ArgInfoList.dirty() || m_ImplicitArgInfoList.dirty() || m_SubGroupSize.dirty() ||
242-
m_OpenCLVectorTypeHint.dirty();
237+
return m_Type.dirty() || m_ArgInfoList.dirty() || m_ImplicitArgInfoList.dirty() || m_SubGroupSize.dirty();
243238
}
244239

245240
void FunctionInfoMetaData::discardChanges() {
246241
m_Type.discardChanges();
247242
m_ArgInfoList.discardChanges();
248243
m_ImplicitArgInfoList.discardChanges();
249244
m_SubGroupSize.discardChanges();
250-
m_OpenCLVectorTypeHint.discardChanges();
251245
}
252246

253247
llvm::Metadata *FunctionInfoMetaData::generateNode(llvm::LLVMContext &context) const {
@@ -268,9 +262,6 @@ llvm::Metadata *FunctionInfoMetaData::generateNode(llvm::LLVMContext &context) c
268262
if (m_SubGroupSize->hasValue()) {
269263
args.push_back(m_SubGroupSize.generateNode(context));
270264
}
271-
if (m_OpenCLVectorTypeHint->hasValue()) {
272-
args.push_back(m_OpenCLVectorTypeHint.generateNode(context));
273-
}
274265
return llvm::MDNode::get(context, args);
275266
}
276267

@@ -286,6 +277,5 @@ void FunctionInfoMetaData::save(llvm::LLVMContext &context, llvm::MDNode *pNode)
286277
m_ArgInfoList.save(context, getNamedNode(pNode, "arg_desc"));
287278
m_ImplicitArgInfoList.save(context, getNamedNode(pNode, "implicit_arg_desc"));
288279
m_SubGroupSize.save(context, getNamedNode(pNode, "sub_group_size"));
289-
m_OpenCLVectorTypeHint.save(context, getNamedNode(pNode, "opencl_vec_type_hint"));
290280
}
291281
} // namespace IGC::IGCMD

IGC/Compiler/MetaDataApi/MetaDataApi.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ using ArgInfoMetaDataHandle = MetaObjectHandle<ArgInfoMetaData>;
2020
class SubGroupSizeMetaData;
2121
using SubGroupSizeMetaDataHandle = MetaObjectHandle<SubGroupSizeMetaData>;
2222

23+
// NOTE: VectorTypeHintMetaData is retained solely for the SPIR-V input metadata
24+
// layer (SpirMetaDataApi.h aliases it as SPIRMD::VectorTypeHintMetaData). The IGC
25+
// output side now carries the OpenCL vec_type_hint as ModuleMetaData::FuncMD[F].vecTypeHint.
2326
class VectorTypeHintMetaData;
2427
using VectorTypeHintMetaDataHandle = MetaObjectHandle<VectorTypeHintMetaData>;
2528

@@ -184,6 +187,8 @@ class SubGroupSizeMetaData : public IMetaDataObject {
184187
SIMDSizeType m_SIMDSize;
185188
};
186189

190+
// Retained for the SPIR-V input metadata layer only (see SpirMetaDataApi.h).
191+
// The IGC output side reads/writes ModuleMetaData::FuncMD[F].vecTypeHint instead.
187192
class VectorTypeHintMetaData : public IMetaDataObject {
188193
public:
189194
using _Mybase = IMetaDataObject;
@@ -310,9 +315,6 @@ class FunctionInfoMetaData : public IMetaDataObject {
310315
// SubGroupSize
311316
SubGroupSizeMetaDataHandle getSubGroupSize() { return m_SubGroupSize; }
312317

313-
// OpenCLVectorTypeHint
314-
VectorTypeHintMetaDataHandle getOpenCLVectorTypeHint() { return m_OpenCLVectorTypeHint; }
315-
316318
private:
317319
// parent node
318320
const llvm::MDNode *m_pNode;
@@ -322,7 +324,6 @@ class FunctionInfoMetaData : public IMetaDataObject {
322324
ArgInfoListList m_ArgInfoList;
323325
ImplicitArgInfoListList m_ImplicitArgInfoList;
324326
SubGroupSizeMetaDataHandle m_SubGroupSize;
325-
VectorTypeHintMetaDataHandle m_OpenCLVectorTypeHint;
326327
};
327328

328329
class MetaDataUtils {

IGC/Compiler/SPIRMetaDataTranslation.cpp

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ SPDX-License-Identifier: MIT
1010
#include "Compiler/MetaDataApi/SpirMetaDataApi.h"
1111
#include "Compiler/IGCPassSupport.h"
1212
#include "Compiler/CodeGenPublic.h"
13+
#include "Probe/Assertion.h"
14+
// llvmWrapper/IR/DerivedTypes.h manages its own LLVMWarnings push/pop, so it
15+
// must be included outside the warnings block below (a nested push with no
16+
// matching pop trips the guard in LLVMWarningsPush.hpp).
17+
#include "llvmWrapper/IR/DerivedTypes.h"
1318

1419
#include "common/LLVMWarningsPush.hpp"
1520
#include "llvm/ADT/StringSwitch.h"
21+
#include "llvm/ADT/StringExtras.h"
1622
#include "common/LLVMWarningsPop.hpp"
1723

1824
using namespace llvm;
@@ -45,6 +51,66 @@ bool IGC::isLegalOCLVersion(int major, int minor) {
4551
return false;
4652
}
4753

54+
// Reduce an OpenCL vec_type_hint LLVM type to its ZEBinary string form (e.g. "uchar4").
55+
// Relocated here from COpenCLKernel::getVecTypeHintTypeString as part of migrating the
56+
// opencl_vec_type_hint metadata from MetaDataApi to ModuleMetaData::FuncMD[F].vecTypeHint:
57+
// the string is now computed at SPIR-translate time (where the LLVM type is available)
58+
// and stored as a POD string, so codegen no longer needs the LLVM type.
59+
static std::string getVecTypeHintTypeString(Type *baseType) {
60+
std::string vecTypeString;
61+
62+
unsigned int numElements = 1;
63+
if (baseType->isVectorTy()) {
64+
numElements = (unsigned)cast<IGCLLVM::FixedVectorType>(baseType)->getNumElements();
65+
baseType = cast<VectorType>(baseType)->getElementType();
66+
}
67+
68+
// ExecutionModel doesn't differentiate base type in term of signed/unsigned.
69+
if (baseType->isIntegerTy()) {
70+
vecTypeString += "u";
71+
}
72+
73+
switch (baseType->getTypeID()) {
74+
case Type::IntegerTyID:
75+
switch (baseType->getIntegerBitWidth()) {
76+
case 8:
77+
vecTypeString += "char";
78+
break;
79+
case 16:
80+
vecTypeString += "short";
81+
break;
82+
case 32:
83+
vecTypeString += "int";
84+
break;
85+
case 64:
86+
vecTypeString += "long";
87+
break;
88+
default:
89+
IGC_ASSERT_MESSAGE(0, "Unexpected data type in vec_type_hint");
90+
break;
91+
}
92+
break;
93+
case Type::DoubleTyID:
94+
vecTypeString += "double";
95+
break;
96+
case Type::FloatTyID:
97+
vecTypeString += "float";
98+
break;
99+
case Type::HalfTyID:
100+
vecTypeString += "half";
101+
break;
102+
default:
103+
IGC_ASSERT_MESSAGE(0, "Unexpected data type in vec_type_hint");
104+
break;
105+
}
106+
107+
if (numElements != 1) {
108+
vecTypeString += utostr(numElements);
109+
}
110+
111+
return vecTypeString;
112+
}
113+
48114
// Khronos SPIRV-LLVM Translator attaches kernel metadata directly to LLVM function, example:
49115
// define spir_kernel void @bar() !reqd_work_group_size !0
50116
// ...
@@ -194,9 +260,8 @@ bool SPIRMetaDataTranslation::runOnModule(Module &M) {
194260
// Handling OpenCL Vector Type Hint
195261
SPIRMD::VectorTypeHintMetaDataHandle vectorTypeHint = spirKernel->getVectorTypeHint();
196262
if (vectorTypeHint->hasValue()) {
197-
IGCMD::VectorTypeHintMetaDataHandle vthHandle = fHandle->getOpenCLVectorTypeHint();
198-
vthHandle->setVecType(vectorTypeHint->getVecType());
199-
vthHandle->setSign(vectorTypeHint->getSign());
263+
// Sign is not used downstream (vec_type_hint is type-only), so it is not carried over.
264+
funcMD.vecTypeHint = getVecTypeHintTypeString(vectorTypeHint->getVecType()->getType());
200265
}
201266

202267
// Handling OpenCL Kernel Args Address Spaces
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2026 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; REQUIRES: llvm-14-plus
10+
; RUN: igc_opt --opaque-pointers -igc-serialize-metadata -S < %s | FileCheck %s
11+
; ------------------------------------------------
12+
; Verifies that ModuleMetaData::FuncMD[F].vecTypeHint (the OpenCL vec_type_hint
13+
; kernel attribute, stored as its ZEBinary string form) round-trips through the
14+
; autogen MDNode serialization. The input embeds the field via the standard
15+
; !IGCMetadata MD; igc-serialize-metadata re-emits it.
16+
; ------------------------------------------------
17+
18+
define spir_kernel void @bar(i32 %a) {
19+
entry:
20+
ret void
21+
}
22+
23+
; CHECK-DAG: {!"vecTypeHint", !"uchar4"}
24+
25+
!IGCMetadata = !{!2}
26+
27+
!2 = !{!"ModuleMD", !3}
28+
!3 = !{!"FuncMD", !4, !5}
29+
!4 = !{!"FuncMDMap[0]", ptr @bar}
30+
!5 = !{!"FuncMDValue[0]", !6, !7, !11, !12, !13}
31+
!6 = !{!"localOffsets"}
32+
!7 = !{!"workGroupWalkOrder", !8, !9, !10}
33+
!8 = !{!"dim0", i32 0}
34+
!9 = !{!"dim1", i32 1}
35+
!10 = !{!"dim2", i32 2}
36+
!11 = !{!"vecTypeHint", !"uchar4"}
37+
!12 = !{!"funcArgs"}
38+
!13 = !{!"functionType", !"KernelFunction"}

IGC/Compiler/tests/SPIRMetaDataTranslation/basic.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ declare spir_kernel void @test_spir(i64 addrspace(1)*)
3232
; CHECK-DAG: [[D2]] = !{!"dim2", i32 16}
3333
; CHECK-DAG: {!"threadGroupSizeHint", {{![0-9]+}}, {{![0-9]+}}, [[DH2:![0-9]+]]}
3434
; CHECK-DAG: [[DH2]] = !{!"dim2", i32 4}
35+
; CHECK-DAG: {!"vecTypeHint", !"float4"}
3536
; CHECK-DAG: {!"function_type", i32 0}
3637
; CHECK-DAG: {!"sub_group_size", i32 16}
3738
; CHECK-DAG: {!"m_OpenCLArgAddressSpaces", [[AS_VEC:![0-9]*]]}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2026 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; UNSUPPORTED: llvm-17-plus
10+
; RUN: igc_opt --typed-pointers -igc-spir-metadata-translation -igc-serialize-metadata -S < %s | FileCheck %s
11+
; ------------------------------------------------
12+
; SPIRMetaDataTranslation
13+
; ------------------------------------------------
14+
15+
; Verifies the OpenCL vec_type_hint reduction: SPIRMetaDataTranslation turns the
16+
; SPIR-V !vec_type_hint LLVM type into its ZEBinary string form and stores it in
17+
; ModuleMetaData::FuncMD[F].vecTypeHint (serialized as {!"vecTypeHint", !"<str>"}).
18+
; Each kernel exercises a distinct branch of the type->string reduction:
19+
; <4 x i8> -> "uchar4" (integer 'u' prefix, 8-bit, vector suffix)
20+
; <2 x i32> -> "uint2" (integer 'u' prefix, 32-bit, vector suffix)
21+
; double -> "double" (non-integer scalar, no prefix, no suffix)
22+
; <3 x half>-> "half3" (non-integer, 16-bit float, vector suffix)
23+
24+
declare spir_kernel void @k_uchar4()
25+
declare spir_kernel void @k_uint2()
26+
declare spir_kernel void @k_double()
27+
declare spir_kernel void @k_half3()
28+
29+
; CHECK-DAG: {!"vecTypeHint", !"uchar4"}
30+
; CHECK-DAG: {!"vecTypeHint", !"uint2"}
31+
; CHECK-DAG: {!"vecTypeHint", !"double"}
32+
; CHECK-DAG: {!"vecTypeHint", !"half3"}
33+
34+
!opencl.kernels = !{!0, !2, !4, !6}
35+
36+
!0 = !{void ()* @k_uchar4, !1}
37+
!1 = !{!"vec_type_hint", <4 x i8> undef, i32 0}
38+
!2 = !{void ()* @k_uint2, !3}
39+
!3 = !{!"vec_type_hint", <2 x i32> undef, i32 0}
40+
!4 = !{void ()* @k_double, !5}
41+
!5 = !{!"vec_type_hint", double undef, i32 0}
42+
!6 = !{void ()* @k_half3, !7}
43+
!7 = !{!"vec_type_hint", <3 x half> undef, i32 0}

IGC/common/MDFrameWork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ enum class ShaderTypeMD
360360
WorkGroupWalkOrderMD workGroupWalkOrder{};
361361
ThreadGroupSizeMD threadGroupSize{};
362362
ThreadGroupSizeMD threadGroupSizeHint{};
363+
std::string vecTypeHint = ""; // "" = not specified; OpenCL vec_type_hint as ZEBinary string (e.g. "uchar4")
363364
std::vector<FuncArgMD> funcArgs{};
364365
FunctionTypeMD functionType = KernelFunction;
365366
RayTraceShaderInfo rtInfo{};

0 commit comments

Comments
 (0)