Skip to content

Commit 52cc44e

Browse files
committed
llvm ir bitcast added in SPIRVLegalizePointerCast.cpp does not get translated into MIR.
Instead we will do what `SPIRVEmitIntrinsics::visitBitCastInst` does and emit `spv_bitcast` instead of `BitCastInst`.
1 parent 344e7a1 commit 52cc44e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ class SPIRVLegalizePointerCast : public FunctionPass {
7676
FixedVectorType *TargetType, Value *Source) {
7777
assert(TargetType->getNumElements() <= SourceType->getNumElements());
7878
LoadInst *NewLoad = B.CreateLoad(SourceType, Source);
79-
Value *AssignType = NewLoad;
80-
if (TargetType->getElementType() != SourceType->getElementType())
81-
AssignType = B.CreateBitCast(NewLoad, TargetType);
82-
buildAssignType(B, SourceType, AssignType);
79+
buildAssignType(B, SourceType, NewLoad);
80+
Value *AssignValue = NewLoad;
81+
if (TargetType->getElementType() != SourceType->getElementType()) {
82+
AssignValue = B.CreateIntrinsic(Intrinsic::spv_bitcast,
83+
{TargetType, SourceType}, {NewLoad});
84+
buildAssignType(B, TargetType, AssignValue);
85+
}
8386

8487
SmallVector<int> Mask(/* Size= */ TargetType->getNumElements());
8588
for (unsigned I = 0; I < TargetType->getNumElements(); ++I)
8689
Mask[I] = I;
87-
Value *Output = B.CreateShuffleVector(AssignType, AssignType, Mask);
90+
Value *Output = B.CreateShuffleVector(AssignValue, AssignValue, Mask);
8891
buildAssignType(B, TargetType, Output);
8992
return Output;
9093
}
@@ -136,7 +139,9 @@ class SPIRVLegalizePointerCast : public FunctionPass {
136139
OriginalOperand, LI);
137140
}
138141
// Destination is a smaller vector than source.
142+
// or different vector type.
139143
// - float3 v3 = vector4;
144+
// - float4 v2 = int4;
140145
else if (SVT && DVT)
141146
Output = loadVectorFromVector(B, SVT, DVT, OriginalOperand);
142147
// Destination is the scalar type stored at the start of an aggregate.

llvm/test/CodeGen/SPIRV/hlsl-resources/issue-146942-ptr-cast.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ define void @main() local_unnamed_addr #0 {
1111
; CHECK: %[[#FLOAT:]] = OpTypeFloat 32
1212
; CHECK: %[[#FLOAT4:]] = OpTypeVector %[[#FLOAT]] 4
1313
; CHECK: %[[#BUFFER_LOAD:]] = OpLoad %[[#FLOAT4]] %{{[0-9]+}} Aligned 16
14-
; CHECK: %[[#VEC_SHUFFLE:]] = OpVectorShuffle %[[#INT4]] %[[#BUFFER_LOAD]] %[[#BUFFER_LOAD]] 0 1 2 3
14+
; CHECK: %[[#CAST_LOAD:]] = OpBitcast %[[#INT4]] %[[#BUFFER_LOAD]]
15+
; CHECK: %[[#VEC_SHUFFLE:]] = OpVectorShuffle %[[#INT4]] %[[#CAST_LOAD]] %[[#CAST_LOAD]] 0 1 2 3
1516
%1 = tail call target("spirv.VulkanBuffer", [0 x <4 x float>], 12, 0) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v4f32_12_0t(i32 0, i32 2, i32 1, i32 0, i1 false, ptr nonnull @.str)
1617
%2 = tail call target("spirv.VulkanBuffer", [0 x <4 x i32>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v4i32_12_1t(i32 0, i32 5, i32 1, i32 0, i1 false, ptr nonnull @.str.2)
1718
%3 = tail call noundef align 16 dereferenceable(16) ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v4f32_12_0t(target("spirv.VulkanBuffer", [0 x <4 x float>], 12, 0) %1, i32 0)

0 commit comments

Comments
 (0)