Skip to content

Commit 614c782

Browse files
[release/6.0] Fix incorrect SIMD temp allocation for Vector256 with AVX2 disabled (#58850)
* Fix incorrect SIMD temp allocation for Vector256 with AVX2 disabled The NI_Vector256_GetElement intrinsic, in some situations, requires a stack temporary. With AVX2 disabled, this temporary was getting allocated as a TYP_SIMD16 instead of a TYP_SIMD32, leading to overwriting the local variable. Add a type argument to the temp variable allocation, and allocate the temp as the largest sized type required by any use. Fixes #58295 * Code review change: improve arm64 SIMD temp creation type Co-authored-by: Bruce Forstall <brucefo@microsoft.com>
1 parent 4c814fa commit 614c782

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/coreclr/jit/compiler.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8915,15 +8915,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
89158915
}
89168916

89178917
private:
8918-
unsigned getSIMDInitTempVarNum()
8919-
{
8920-
if (lvaSIMDInitTempVarNum == BAD_VAR_NUM)
8921-
{
8922-
lvaSIMDInitTempVarNum = lvaGrabTempWithImplicitUse(false DEBUGARG("SIMDInitTempVar"));
8923-
lvaTable[lvaSIMDInitTempVarNum].lvType = getSIMDVectorType();
8924-
}
8925-
return lvaSIMDInitTempVarNum;
8926-
}
8918+
unsigned getSIMDInitTempVarNum(var_types simdType);
89278919

89288920
#else // !FEATURE_SIMD
89298921
bool isOpaqueSIMDLclVar(LclVarDsc* varDsc)

src/coreclr/jit/lsraarm64.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,8 @@ int LinearScan::BuildHWIntrinsic(GenTreeHWIntrinsic* intrinsicTree)
11321132
{
11331133
// If the index is not a constant or op1 is in register,
11341134
// we will use the SIMD temp location to store the vector.
1135-
compiler->getSIMDInitTempVarNum();
1135+
var_types requiredSimdTempType = (intrin.id == NI_Vector64_GetElement) ? TYP_SIMD8 : TYP_SIMD16;
1136+
compiler->getSIMDInitTempVarNum(requiredSimdTempType);
11361137
}
11371138
}
11381139

src/coreclr/jit/lsraxarch.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,8 @@ int LinearScan::BuildHWIntrinsic(GenTreeHWIntrinsic* intrinsicTree)
22012201
{
22022202
// If the index is not a constant or op1 is in register,
22032203
// we will use the SIMD temp location to store the vector.
2204-
compiler->getSIMDInitTempVarNum();
2204+
var_types requiredSimdTempType = (intrinsicId == NI_Vector128_GetElement) ? TYP_SIMD16 : TYP_SIMD32;
2205+
compiler->getSIMDInitTempVarNum(requiredSimdTempType);
22052206
}
22062207
break;
22072208
}

src/coreclr/jit/simd.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ int Compiler::getSIMDTypeAlignment(var_types simdType)
107107
#endif
108108
}
109109

110+
//------------------------------------------------------------------------
111+
// Get, and allocate if necessary, the SIMD temp used for various operations.
112+
// The temp is allocated as the maximum sized type of all operations required.
113+
//
114+
// Arguments:
115+
// simdType - Required SIMD type
116+
//
117+
// Returns:
118+
// The temp number
119+
//
120+
unsigned Compiler::getSIMDInitTempVarNum(var_types simdType)
121+
{
122+
if (lvaSIMDInitTempVarNum == BAD_VAR_NUM)
123+
{
124+
JITDUMP("Allocating SIMDInitTempVar as %s\n", varTypeName(simdType));
125+
lvaSIMDInitTempVarNum = lvaGrabTempWithImplicitUse(false DEBUGARG("SIMDInitTempVar"));
126+
lvaTable[lvaSIMDInitTempVarNum].lvType = simdType;
127+
}
128+
else if (genTypeSize(lvaTable[lvaSIMDInitTempVarNum].lvType) < genTypeSize(simdType))
129+
{
130+
// We want the largest required type size for the temp.
131+
JITDUMP("Increasing SIMDInitTempVar type size from %s to %s\n",
132+
varTypeName(lvaTable[lvaSIMDInitTempVarNum].lvType), varTypeName(simdType));
133+
lvaTable[lvaSIMDInitTempVarNum].lvType = simdType;
134+
}
135+
return lvaSIMDInitTempVarNum;
136+
}
137+
110138
//----------------------------------------------------------------------------------
111139
// Return the base type and size of SIMD vector type given its type handle.
112140
//

0 commit comments

Comments
 (0)