Skip to content

Ensure that embedded mask handling is covering the right scenarios #116201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/coreclr/jit/codegeninterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class CodeGenInterface

static unsigned instInputSize(instruction ins);
static unsigned instKMaskBaseSize(instruction ins);

bool IsEmbeddedBroadcastEnabled(instruction ins, GenTree* op);
#endif // TARGET_XARCH
//-------------------------------------------------------------------------
// Liveness-related fields & methods
Expand Down Expand Up @@ -825,10 +827,6 @@ class CodeGenInterface

virtual const char* siStackVarName(size_t offs, size_t size, unsigned reg, unsigned stkOffs) = 0;
#endif // LATE_DISASM

#if defined(TARGET_XARCH)
bool IsEmbeddedBroadcastEnabled(instruction ins, GenTree* op);
#endif
};

#if !defined(TARGET_LOONGARCH64) && !defined(TARGET_RISCV64)
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2205,11 +2205,6 @@ emitter::code_t emitter::AddEvexPrefix(const instrDesc* id, code_t code, emitAtt
if (aaaContext != 0)
{
maskReg = static_cast<regNumber>(aaaContext + KBASE);

if (id->idIsEvexZContextSet())
{
code |= ZBIT_IN_BYTE_EVEX_PREFIX;
}
}
break;
}
Expand All @@ -2218,6 +2213,11 @@ emitter::code_t emitter::AddEvexPrefix(const instrDesc* id, code_t code, emitAtt
if (isMaskReg(maskReg))
{
code |= (static_cast<code_t>(maskReg - KBASE) << 32);

if (id->idIsEvexZContextSet())
{
code |= ZBIT_IN_BYTE_EVEX_PREFIX;
}
}
return code;
}
Expand Down
13 changes: 5 additions & 8 deletions src/coreclr/jit/emitxarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,12 @@ void SetEvexEmbMaskIfNeeded(instrDesc* id, insOpts instOptions)
{
assert(UseEvexEncoding());
id->idSetEvexAaaContext(instOptions);

if ((instOptions & INS_OPTS_EVEX_z_MASK) == INS_OPTS_EVEX_em_zero)
{
id->idSetEvexZContext();
}
}
else

if ((instOptions & INS_OPTS_EVEX_z_MASK) == INS_OPTS_EVEX_em_zero)
{
assert((instOptions & INS_OPTS_EVEX_z_MASK) == 0);
assert(UseEvexEncoding());
id->idSetEvexZContext();
}
}

Expand Down Expand Up @@ -1290,7 +1287,7 @@ inline bool HasEmbeddedBroadcast(const instrDesc* id) const
//
inline bool HasEmbeddedMask(const instrDesc* id) const
{
return id->idIsEvexAaaContextSet();
return id->idIsEvexAaaContextSet() || id->idIsEvexZContextSet();
}

inline bool HasHighSIMDReg(const instrDesc* id) const;
Expand Down
20 changes: 20 additions & 0 deletions src/coreclr/jit/hwintrinsic.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,26 @@ struct HWIntrinsicInfo
FloatComparisonMode comparison,
var_types simdBaseType,
unsigned simdSize);

//------------------------------------------------------------------------
// genIsTableDrivenHWIntrinsic:
//
// Arguments:
// intrinsicId - The identifier for the hwintrinsic to check
// category - The category of intrinsicId
//
// Return Value:
// returns true if this category can be table-driven in CodeGen
//
static bool genIsTableDrivenHWIntrinsic(NamedIntrinsic intrinsicId, HWIntrinsicCategory category)
{
// TODO - make more categories to the table-driven framework
// HW_Category_Helper and HW_Flag_SpecialCodeGen usually need manual codegen
const bool tableDrivenCategory =
(category != HW_Category_Special) && (category != HW_Category_Scalar) && (category != HW_Category_Helper);
const bool tableDrivenFlag = !HWIntrinsicInfo::HasSpecialCodegen(intrinsicId);
return tableDrivenCategory && tableDrivenFlag;
}
#endif

// Member lookup
Expand Down
44 changes: 20 additions & 24 deletions src/coreclr/jit/hwintrinsiccodegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,6 @@ static void assertIsContainableHWIntrinsicOp(Lowering* lowering,
#endif // DEBUG
}

//------------------------------------------------------------------------
// genIsTableDrivenHWIntrinsic:
//
// Arguments:
// category - category of a HW intrinsic
//
// Return Value:
// returns true if this category can be table-driven in CodeGen
//
static bool genIsTableDrivenHWIntrinsic(NamedIntrinsic intrinsicId, HWIntrinsicCategory category)
{
// TODO - make more categories to the table-driven framework
// HW_Category_Helper and HW_Flag_SpecialCodeGen usually need manual codegen
const bool tableDrivenCategory =
(category != HW_Category_Special) && (category != HW_Category_Scalar) && (category != HW_Category_Helper);
const bool tableDrivenFlag = !HWIntrinsicInfo::HasSpecialCodegen(intrinsicId);
return tableDrivenCategory && tableDrivenFlag;
}

//------------------------------------------------------------------------
// AddEmbRoundingMode: Adds the embedded rounding mode to the insOpts
//
Expand Down Expand Up @@ -408,7 +389,7 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
assert(HWIntrinsicInfo::RequiresCodegen(intrinsicId));
assert(!HWIntrinsicInfo::NeedsNormalizeSmallTypeToInt(intrinsicId) || !varTypeIsSmall(node->GetSimdBaseType()));

bool isTableDriven = genIsTableDrivenHWIntrinsic(intrinsicId, category);
bool isTableDriven = HWIntrinsicInfo::genIsTableDrivenHWIntrinsic(intrinsicId, category);
insOpts instOptions = INS_OPTS_NONE;

if (GetEmitter()->UseEvexEncoding())
Expand Down Expand Up @@ -659,6 +640,8 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
{
// Until we improve the handling of addressing modes in the emitter, we'll create a
// temporary GT_IND to generate code with.

assert(instOptions == INS_OPTS_NONE);
GenTreeIndir load = indirForm(node->TypeGet(), op1);
emit->emitInsLoadInd(ins, simdSize, node->GetRegNum(), &load);
}
Expand Down Expand Up @@ -703,6 +686,7 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
// Until we improve the handling of addressing modes in the emitter, we'll create a
// temporary GT_STORE_IND to generate code with.

assert(instOptions == INS_OPTS_NONE);
GenTreeStoreInd store = storeIndirForm(node->TypeGet(), op1, op2);
emit->emitInsStoreInd(ins, simdSize, &store);
break;
Expand Down Expand Up @@ -834,10 +818,9 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
}
else if (category == HW_Category_MemoryStore)
{
assert(instOptions == INS_OPTS_NONE);

// The Mask instructions do not currently support containment of the address.
assert(!op2->isContained());

if (intrinsicId == NI_AVX_MaskStore || intrinsicId == NI_AVX2_MaskStore)
{
emit->emitIns_AR_R_R(ins, simdSize, op2Reg, op3Reg, op1Reg, 0, instOptions);
Expand Down Expand Up @@ -1340,9 +1323,22 @@ void CodeGen::genHWIntrinsic_R_R_RM_R(GenTreeHWIntrinsic* node, instruction ins,
GenTree* op3 = node->Op(3);
emitter* emit = GetEmitter();

regNumber op1Reg = op1->GetRegNum();
regNumber op1Reg = REG_NA;
regNumber op3Reg = op3->GetRegNum();

if (op1->isContained())
{
assert(node->GetHWIntrinsicId() == NI_AVX512_BlendVariableMask);
assert(op1->IsVectorZero());

instOptions = AddEmbMaskingMode(instOptions, REG_K0, true);
op1Reg = targetReg;
}
else
{
op1Reg = op1->GetRegNum();
}

assert(targetReg != REG_NA);
assert(op1Reg != REG_NA);
assert(op3Reg != REG_NA);
Expand Down Expand Up @@ -1693,7 +1689,7 @@ void CodeGen::genNonTableDrivenHWIntrinsicsJumpTableFallback(GenTreeHWIntrinsic*

assert(HWIntrinsicInfo::IsEmbRoundingCompatible(intrinsicId));
assert(!lastOp->isContained());
assert(!genIsTableDrivenHWIntrinsic(intrinsicId, category));
assert(!HWIntrinsicInfo::genIsTableDrivenHWIntrinsic(intrinsicId, category));

var_types baseType = node->GetSimdBaseType();
emitAttr attr = emitActualTypeSize(Compiler::getSIMDTypeForSize(node->GetSimdSize()));
Expand Down
Loading
Loading