Skip to content

Commit e3afef1

Browse files
gtong-nvcsyonghegithub-actions[bot]
authored
Fix metal segfault by check vectorValue before accessing (#7688)
* Check vectorValue before accessing * Fix metal segfault by using IRElementExtract for general vector handling Address review comments by replacing IRMakeVector-specific code with IRElementExtract to handle any vector instruction type (IRIntCast, etc). This makes the code more robust and fixes cases where float2(1,2) creates IRIntCast instead of IRMakeVector. Co-authored-by: Yong He <csyonghe@users.noreply.github.com> * Fix sign comparison warning in metal legalize Cast originalElementCount->getValue() to UInt to avoid comparison between signed and unsigned integers. Co-authored-by: Yong He <csyonghe@users.noreply.github.com> --------- Co-authored-by: Yong He <yonghe@outlook.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yong He <csyonghe@users.noreply.github.com>
1 parent eaaf019 commit e3afef1

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

source/slang/slang-ir-metal-legalize.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ void legalizeImageStoreValue(IRBuilder& builder, IRImageStore* imageStore)
3232
}
3333
}
3434
elementType = valueVectorType->getElementType();
35-
auto vectorValue = as<IRMakeVector>(originalValue);
36-
for (UInt i = 0; i < vectorValue->getOperandCount(); i++)
35+
36+
// Extract components using IRElementExtract to handle any vector instruction type
37+
if (auto originalElementCount = as<IRIntLit>(valueVectorType->getElementCount()))
3738
{
38-
components.add(vectorValue->getOperand(i));
39+
for (UInt i = 0; i < (UInt)originalElementCount->getValue(); i++)
40+
{
41+
auto elementExtract = builder.emitElementExtract(
42+
elementType,
43+
originalValue,
44+
builder.getIntValue(builder.getIntType(), i));
45+
components.add(elementExtract);
46+
}
3947
}
4048
}
4149
else
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target metal
2+
3+
// Test that we can compile texture store operations with 2-component vectors
4+
// without segfaulting. This reproduces the issue where legalizeImageStoreValue
5+
// would crash when trying to expand a float2 to float4 for Metal texture writes.
6+
7+
RWTexture2D<float2> output;
8+
9+
[shader("compute")]
10+
[numthreads(32, 32, 1)]
11+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
12+
{
13+
// This should expand float2(1,2) to float4(1,2,0,0)
14+
// CHECK: {{.*}}float4(1.0, 2.0,
15+
output.Store(dispatchThreadID.xy, float2(1, 2));
16+
}

0 commit comments

Comments
 (0)