Skip to content
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

Unsafe.Unlikely (Assume) draft implementation #33043

Closed
wants to merge 6 commits into from
Closed
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
21 changes: 21 additions & 0 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,19 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
break;
}

case NI_System_Runtime_CompilerServices_Unsafe_Unlikely:
{
if (compCurBB->bbJumpKind == BBJ_COND)
{
// I guess actually we need to change weight for a specific edge
// rather than for the whole block (it can be non-cold for other conditions)
// however, bbPreds are not calcaluted at this moment...
compCurBB->bbNext->bbFlags |= BBF_RUN_RARELY;
compCurBB->bbNext->setBBWeight(BB_ZERO_WEIGHT);
}
break;
}

#ifdef FEATURE_HW_INTRINSICS
case NI_System_Math_FusedMultiplyAdd:
case NI_System_MathF_FusedMultiplyAdd:
Expand Down Expand Up @@ -4430,6 +4443,14 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
}
}
}
else if ((strcmp(namespaceName, "Internal.Runtime.CompilerServices") == 0) ||
(strcmp(namespaceName, "System.Runtime.CompilerServices") == 0))
{
if ((strcmp(className, "Unsafe") == 0) && (strcmp(methodName, "Unlikely") == 0))
{
result = NI_System_Runtime_CompilerServices_Unsafe_Unlikely;
}
}
#if defined(TARGET_XARCH) // We currently only support BSWAP on x86
else if (strcmp(namespaceName, "System.Buffers.Binary") == 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum NamedIntrinsic : unsigned short
NI_System_GC_KeepAlive,
NI_System_Type_get_IsValueType,
NI_System_Type_IsAssignableFrom,
NI_System_Runtime_CompilerServices_Unsafe_Unlikely,

#ifdef FEATURE_HW_INTRINSICS
NI_IsSupported_True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,13 @@ public static void SkipInit<T>(out T value)

// ret
}

[Intrinsic]
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Unlikely(bool condition)
{
return condition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@
ret
} // end of method Unsafe::SkipInit

//.method public hidebysig static bool Unlikely (bool condition) cil managed aggressiveinlining
//{
// .custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
// .maxstack 1
// ldarg.0
// ret
//} // end of method Unsafe::Unlikely

.method public hidebysig static int32 SizeOf<T>() cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
Expand Down
12 changes: 10 additions & 2 deletions src/mono/mono/mini/intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,19 @@ emit_unsafe_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignatu
MONO_INST_NEW (cfg, ins, OP_NOP);
MONO_ADD_INS (cfg->cbb, ins);
return ins;
}
else if (!strcmp (cmethod->name, "SkipInit")) {
} else if (!strcmp (cmethod->name, "SkipInit")) {
MONO_INST_NEW (cfg, ins, OP_NOP);
MONO_ADD_INS (cfg->cbb, ins);
return ins;
} else if (COMPILE_LLVM (cfg) && !strcmp (cmethod->name, "Unlikely")) {
g_assert (fsig->param_count == 1);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
// LLVM should expand it as @llvm.expect.i32
MONO_INST_NEW (cfg, ins, OP_UNLIKELY32);
ins->type = STACK_I4;
ins->dreg = mono_alloc_ireg (cfg);
ins->sreg1 = args [0]->dreg;
MONO_ADD_INS (cfg->cbb, ins);
return ins;
}
#endif

Expand Down
11 changes: 11 additions & 0 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ typedef enum {
INTRINS_TRUNCF,
INTRINS_COPYSIGN,
INTRINS_COPYSIGNF,
INTRINS_EXPECT_I32,
INTRINS_EXPECT_I8,
INTRINS_EXPECT_I1,
INTRINS_CTPOP_I32,
Expand Down Expand Up @@ -8057,6 +8058,12 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb)
#endif /* ENABLE_NETCORE */
#endif /* SIMD */

case OP_UNLIKELY32: {
LLVMValueRef args [] = { lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE) };
values [ins->dreg] = LLVMBuildCall (builder, get_intrins (ctx, INTRINS_EXPECT_I32), args, 2, "");
break;
}

case OP_DUMMY_USE:
break;

Expand Down Expand Up @@ -9365,6 +9372,7 @@ static IntrinsicDesc intrinsics[] = {
{INTRINS_TRUNCF, "llvm.trunc.f32"},
{INTRINS_COPYSIGN, "llvm.copysign.f64"},
{INTRINS_COPYSIGNF, "llvm.copysign.f32"},
{INTRINS_EXPECT_I32, "llvm.expect.i32"},
{INTRINS_EXPECT_I8, "llvm.expect.i8"},
{INTRINS_EXPECT_I1, "llvm.expect.i1"},
{INTRINS_CTPOP_I32, "llvm.ctpop.i32"},
Expand Down Expand Up @@ -9590,6 +9598,9 @@ add_intrinsic (LLVMModuleRef module, int id)
case INTRINS_POW:
AddFunc2 (module, name, LLVMDoubleType (), LLVMDoubleType (), LLVMDoubleType ());
break;
case INTRINS_EXPECT_I32:
AddFunc2 (module, name, LLVMInt32Type (), LLVMInt32Type (), LLVMInt32Type ());
break;
case INTRINS_EXPECT_I8:
AddFunc2 (module, name, LLVMInt8Type (), LLVMInt8Type (), LLVMInt8Type ());
break;
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/mini-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -1456,4 +1456,4 @@ MINI_OP(OP_LZCNT64, "lzcnt64", LREG, LREG, NONE)
MINI_OP(OP_POPCNT32, "popcnt32", IREG, IREG, NONE)
MINI_OP(OP_POPCNT64, "popcnt64", LREG, LREG, NONE)


MINI_OP(OP_UNLIKELY32, "unlikely", IREG, IREG, NONE)