Skip to content

Commit 899a596

Browse files
author
sandreenko
authored
Merge pull request dotnet/coreclr#8858 from sandreenko/CoreRT-PInvoke
Update PInvoke inlining restrictions for CoreRT Commit migrated from dotnet/coreclr@01f8e7d
2 parents 5b7a5be + d51e87d commit 899a596

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/coreclr/src/jit/compiler.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,8 +3294,6 @@ void Compiler::compInitOptions(JitFlags* jitFlags)
32943294
}
32953295
#endif
32963296

3297-
opts.compMustInlinePInvokeCalli = jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB);
3298-
32993297
opts.compScopeInfo = opts.compDbgInfo;
33003298

33013299
#ifdef LATE_DISASM

src/coreclr/src/jit/compiler.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ class Compiler
27852785

27862786
void impImportNewObjArray(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_CALL_INFO* pCallInfo);
27872787

2788-
bool impCanPInvokeInline(BasicBlock* block);
2788+
bool impCanPInvokeInline();
27892789
bool impCanPInvokeInlineCallSite(BasicBlock* block);
27902790
void impCheckForPInvokeCall(
27912791
GenTreePtr call, CORINFO_METHOD_HANDLE methHnd, CORINFO_SIG_INFO* sig, unsigned mflags, BasicBlock* block);
@@ -7625,8 +7625,6 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
76257625
static const bool compNoPInvokeInlineCB;
76267626
#endif
76277627

7628-
bool compMustInlinePInvokeCalli; // Unmanaged CALLI in IL stubs must be inlined
7629-
76307628
#ifdef DEBUG
76317629
bool compGcChecks; // Check arguments and return values to ensure they are sane
76327630
bool compStackCheckOnRet; // Check ESP on return to ensure it is correct

src/coreclr/src/jit/importer.cpp

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5390,29 +5390,23 @@ GenTreePtr Compiler::impTransformThis(GenTreePtr thisPtr,
53905390
}
53915391

53925392
//------------------------------------------------------------------------
5393-
// impCanPInvokeInline: examine information from a call to see if the call
5394-
// qualifies as an inline pinvoke.
5395-
//
5396-
// Arguments:
5397-
// block - block contaning the call, or for inlinees, block
5398-
// containing the call being inlined
5393+
// impCanPInvokeInline: check whether PInvoke inlining should enabled in current method.
53995394
//
54005395
// Return Value:
5401-
// true if this call qualifies as an inline pinvoke, false otherwise
5396+
// true if PInvoke inlining should be enabled in current method, false otherwise
54025397
//
54035398
// Notes:
5404-
// Checks basic legality and then a number of ambient conditions
5405-
// where we could pinvoke but choose not to
5399+
// Checks a number of ambient conditions where we could pinvoke but choose not to
54065400

5407-
bool Compiler::impCanPInvokeInline(BasicBlock* block)
5401+
bool Compiler::impCanPInvokeInline()
54085402
{
5409-
return impCanPInvokeInlineCallSite(block) && getInlinePInvokeEnabled() && (!opts.compDbgCode) &&
5410-
(compCodeOpt() != SMALL_CODE) && (!opts.compNoPInvokeInlineCB) // profiler is preventing inline pinvoke
5403+
return getInlinePInvokeEnabled() && (!opts.compDbgCode) && (compCodeOpt() != SMALL_CODE) &&
5404+
(!opts.compNoPInvokeInlineCB) // profiler is preventing inline pinvoke
54115405
;
54125406
}
54135407

54145408
//------------------------------------------------------------------------
5415-
// impCanPInvokeInlineSallSite: basic legality checks using information
5409+
// impCanPInvokeInlineCallSite: basic legality checks using information
54165410
// from a call to see if the call qualifies as an inline pinvoke.
54175411
//
54185412
// Arguments:
@@ -5441,6 +5435,17 @@ bool Compiler::impCanPInvokeInline(BasicBlock* block)
54415435

54425436
bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)
54435437
{
5438+
if (block->hasHndIndex())
5439+
{
5440+
return false;
5441+
}
5442+
5443+
// The remaining limitations do not apply to CoreRT
5444+
if (IsTargetAbi(CORINFO_CORERT_ABI))
5445+
{
5446+
return true;
5447+
}
5448+
54445449
#ifdef _TARGET_AMD64_
54455450
// On x64, we disable pinvoke inlining inside of try regions.
54465451
// Here is the comment from JIT64 explaining why:
@@ -5462,12 +5467,13 @@ bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)
54625467
//
54635468
// A desktop test case where this seems to matter is
54645469
// jit\jit64\ebvts\mcpp\sources2\ijw\__clrcall\vector_ctor_dtor.02\deldtor_clr.exe
5465-
const bool inX64Try = block->hasTryIndex();
5466-
#else
5467-
const bool inX64Try = false;
5470+
if (block->hasTryIndex())
5471+
{
5472+
return false;
5473+
}
54685474
#endif // _TARGET_AMD64_
54695475

5470-
return !inX64Try && !block->hasHndIndex();
5476+
return true;
54715477
}
54725478

54735479
//------------------------------------------------------------------------
@@ -5533,27 +5539,38 @@ void Compiler::impCheckForPInvokeCall(
55335539
}
55345540
optNativeCallCount++;
55355541

5536-
if (opts.compMustInlinePInvokeCalli && methHnd == nullptr)
5542+
if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && methHnd == nullptr)
55375543
{
5538-
// Always inline pinvoke.
5544+
// PInvoke CALLI in IL stubs must be inlined
55395545
}
55405546
else
55415547
{
5542-
// Check legality and profitability.
5543-
if (!impCanPInvokeInline(block))
5548+
// Check legality
5549+
if (!impCanPInvokeInlineCallSite(block))
55445550
{
55455551
return;
55465552
}
55475553

5548-
if (info.compCompHnd->pInvokeMarshalingRequired(methHnd, sig))
5554+
// PInvoke CALL in IL stubs must be inlined on CoreRT. Skip the ambient conditions checks and
5555+
// profitability checks
5556+
if (!(opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && IsTargetAbi(CORINFO_CORERT_ABI)))
55495557
{
5550-
return;
5558+
if (impCanPInvokeInline())
5559+
{
5560+
return;
5561+
}
5562+
5563+
// Size-speed tradeoff: don't use inline pinvoke at rarely
5564+
// executed call sites. The non-inline version is more
5565+
// compact.
5566+
if (block->isRunRarely())
5567+
{
5568+
return;
5569+
}
55515570
}
55525571

5553-
// Size-speed tradeoff: don't use inline pinvoke at rarely
5554-
// executed call sites. The non-inline version is more
5555-
// compact.
5556-
if (block->isRunRarely())
5572+
// The expensive check should be last
5573+
if (info.compCompHnd->pInvokeMarshalingRequired(methHnd, sig))
55575574
{
55585575
return;
55595576
}
@@ -6220,7 +6237,7 @@ bool Compiler::impIsTailCallILPattern(bool tailPrefixed,
62206237
((nextOpcode == CEE_NOP) || ((nextOpcode == CEE_POP) && (++cntPop == 1)))); // Next opcode = nop or exactly
62216238
// one pop seen so far.
62226239
#else
6223-
nextOpcode = (OPCODE)getU1LittleEndian(codeAddrOfNextOpcode);
6240+
nextOpcode = (OPCODE)getU1LittleEndian(codeAddrOfNextOpcode);
62246241
#endif
62256242

62266243
if (isCallPopAndRet)

0 commit comments

Comments
 (0)