Skip to content

Commit 15e0aa9

Browse files
jcranmer-intelMrSidims
authored andcommitted
[OpaquePointers] Emit target extension types for SPIR-V friendly IR.
The ABI name mangling of target extension types is tweaked to be like a pointer-to-a-struct, which maintains somewhat better compatibility with the typed pointer representation for name mangling.
1 parent 81ffaaf commit 15e0aa9

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "llvm/ADT/SmallSet.h"
5959
#include "llvm/Analysis/LoopInfo.h"
6060
#include "llvm/BinaryFormat/Dwarf.h"
61+
#include "llvm/IR/AttributeMask.h"
6162
#include "llvm/IR/Constants.h"
6263
#include "llvm/IR/DerivedTypes.h"
6364
#include "llvm/IR/Dominators.h"
@@ -2892,8 +2893,11 @@ void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
28922893
auto *BA = BF->getArgument(I->getArgNo());
28932894
mapValue(BA, &(*I));
28942895
setName(&(*I), BA);
2896+
AttributeMask IllegalAttrs = AttributeFuncs::typeIncompatible(I->getType());
28952897
BA->foreachAttr([&](SPIRVFuncParamAttrKind Kind) {
28962898
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind);
2899+
if (IllegalAttrs.contains(LLVMKind))
2900+
return;
28972901
Type *AttrTy = nullptr;
28982902
switch (LLVMKind) {
28992903
case Attribute::AttrKind::ByVal:
@@ -3200,6 +3204,8 @@ SPIRVToLLVM::SPIRVToLLVM(Module *LLVMModule, SPIRVModule *TheSPIRVModule)
32003204
assert(M && "Initialization without an LLVM module is not allowed");
32013205
initialize(*M);
32023206
Context = &M->getContext();
3207+
if (BM->getDesiredBIsRepresentation() == BIsRepresentation::SPIRVFriendlyIR)
3208+
UseTargetTypes = !Context->supportsTypedPointers();
32033209
DbgTran.reset(new SPIRVToLLVMDbgTran(TheSPIRVModule, LLVMModule, this));
32043210
}
32053211

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,14 @@ static SPIR::RefParamType transTypeDesc(Type *Ty,
13501350
}
13511351
if (auto *TargetTy = dyn_cast<TargetExtType>(Ty)) {
13521352
std::string FullName;
1353+
unsigned AS = 0;
13531354
{
13541355
raw_string_ostream OS(FullName);
13551356
StringRef Name = TargetTy->getName();
13561357
if (Name.consume_front(kSPIRVTypeName::PrefixAndDelim)) {
13571358
OS << "__spirv_" << Name;
1359+
AS = getOCLOpaqueTypeAddrSpace(
1360+
SPIRVOpaqueTypeOpCodeMap::map(Name.str()));
13581361
} else {
13591362
OS << Name;
13601363
}
@@ -1365,7 +1368,12 @@ static SPIR::RefParamType transTypeDesc(Type *Ty,
13651368
for (unsigned Param : TargetTy->int_params())
13661369
OS << "_" << Param;
13671370
}
1368-
return SPIR::RefParamType(new SPIR::UserDefinedType(FullName));
1371+
// Translate as if it's a pointer to the named struct.
1372+
auto *Inner = new SPIR::UserDefinedType(FullName);
1373+
auto *PT = new SPIR::PointerType(Inner);
1374+
PT->setAddressSpace(static_cast<SPIR::TypeAttributeEnum>(
1375+
AS + (unsigned)SPIR::ATTR_ADDR_SPACE_FIRST));
1376+
return SPIR::RefParamType(PT);
13691377
}
13701378

13711379
if (auto *TPT = dyn_cast<TypedPointerType>(Ty)) {

test/GroupAndSubgroupInstructions.spvasm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
; CHECK-SPV-IR: declare spir_func i32 @_Z31__spirv_SubgroupShuffleXorINTELij(i32, i32) #[[#Attrs]]
9494
; CHECK-SPV-IR: declare spir_func i32 @_Z35__spirv_SubgroupBlockReadINTEL_RintPU3AS1Kj(ptr addrspace(1)) #[[#Attrs]]
9595
; CHECK-SPV-IR: declare spir_func void @_Z31__spirv_SubgroupBlockWriteINTELPU3AS1jj(ptr addrspace(1), i32) #[[#Attrs]]
96-
; CHECK-SPV-IR: declare spir_func i32 @_Z40__spirv_SubgroupImageBlockReadINTEL_RintPU3AS133__spirv_Image__void_1_0_0_0_0_0_2Dv2_i(ptr addrspace(1), <2 x i32>) #[[#Attrs]]
97-
; CHECK-SPV-IR: declare spir_func void @_Z36__spirv_SubgroupImageBlockWriteINTELPU3AS133__spirv_Image__void_1_0_0_0_0_0_2Dv2_ij(ptr addrspace(1), <2 x i32>, i32) #[[#Attrs]]
96+
; CHECK-SPV-IR: declare spir_func i32 @_Z40__spirv_SubgroupImageBlockReadINTEL_RintPU3AS133__spirv_Image__void_1_0_0_0_0_0_2Dv2_i(target("spirv.Image", void, 1, 0, 0, 0, 0, 0, 2), <2 x i32>) #[[#Attrs]]
97+
; CHECK-SPV-IR: declare spir_func void @_Z36__spirv_SubgroupImageBlockWriteINTELPU3AS133__spirv_Image__void_1_0_0_0_0_0_2Dv2_ij(target("spirv.Image", void, 1, 0, 0, 0, 0, 0, 2), <2 x i32>, i32) #[[#Attrs]]
9898

9999
; CHECK-COMMON: attributes #[[#Attrs]] =
100100
; CHECK-COMMON-SAME: convergent

test/constant-sampler-under-control-flow.spt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@
9898
; CHECK-LLVM: br i1 %[[cond]], label %[[br1:.*]], label %[[br0:.*]]
9999

100100
; CHECK-LLVM: [[br0]]:
101-
; CHECK-LLVM: %[[s0:.*]] = call ptr addrspace(2) @__translate_sampler_initializer(i32 23)
102-
; CHECK-LLVM: call spir_func ptr addrspace(1) @_Z20__spirv_SampledImagePU3AS133__spirv_Image__void_1_0_0_0_0_0_0PU3AS215__spirv_Sampler(ptr addrspace(1) %{{.*}}, ptr addrspace(2) %[[s0]])
101+
; CHECK-LLVM: %[[s0:.*]] = call target("spirv.Sampler") @__translate_sampler_initializer(i32 23)
102+
; CHECK-LLVM: call spir_func target("spirv.SampledImage", void, 1, 0, 0, 0, 0, 0, 0) @_Z20__spirv_SampledImagePU3AS133__spirv_Image__void_1_0_0_0_0_0_0PU3AS215__spirv_Sampler(target("spirv.Image", void, 1, 0, 0, 0, 0, 0, 0) %{{.*}}, target("spirv.Sampler") %[[s0]])
103103

104104
; CHECK-LLVM: [[br1]]:
105-
; CHECK-LLVM: %[[s1:.*]] = call ptr addrspace(2) @__translate_sampler_initializer(i32 23)
106-
; CHECK-LLVM: call spir_func ptr addrspace(1) @_Z20__spirv_SampledImagePU3AS133__spirv_Image__void_1_0_0_0_0_0_0PU3AS215__spirv_Sampler(ptr addrspace(1) %{{.*}}, ptr addrspace(2) %[[s1]])
105+
; CHECK-LLVM: %[[s1:.*]] = call target("spirv.Sampler") @__translate_sampler_initializer(i32 23)
106+
; CHECK-LLVM: call spir_func target("spirv.SampledImage", void, 1, 0, 0, 0, 0, 0, 0) @_Z20__spirv_SampledImagePU3AS133__spirv_Image__void_1_0_0_0_0_0_0PU3AS215__spirv_Sampler(target("spirv.Image", void, 1, 0, 0, 0, 0, 0, 0) %{{.*}}, target("spirv.Sampler") %[[s1]])

0 commit comments

Comments
 (0)