Skip to content

Commit

Permalink
JIT: Intrinsify UTF16->UTF8 conversion for string literals (Encoding.…
Browse files Browse the repository at this point in the history
…UTF8.TryGetBytes) (#85328)

Co-authored-by: Jakob Botsch Nielsen <[email protected]>
  • Loading branch information
EgorBo and jakobbotsch committed Jul 23, 2023
1 parent 65536eb commit fe03904
Show file tree
Hide file tree
Showing 11 changed files with 695 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4877,6 +4877,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
bool doBranchOpt = true;
bool doCse = true;
bool doAssertionProp = true;
bool doVNBasedIntrinExpansion = true;
bool doRangeAnalysis = true;
bool doVNBasedDeadStoreRemoval = true;
int iterations = 1;
Expand All @@ -4890,6 +4891,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
doBranchOpt = doValueNum && (JitConfig.JitDoRedundantBranchOpts() != 0);
doCse = doValueNum;
doAssertionProp = doValueNum && (JitConfig.JitDoAssertionProp() != 0);
doVNBasedIntrinExpansion = doValueNum;
doRangeAnalysis = doAssertionProp && (JitConfig.JitDoRangeAnalysis() != 0);
doVNBasedDeadStoreRemoval = doValueNum && (JitConfig.JitDoVNBasedDeadStoreRemoval() != 0);

Expand Down Expand Up @@ -4973,6 +4975,13 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
DoPhase(this, PHASE_ASSERTION_PROP_MAIN, &Compiler::optAssertionPropMain);
}

if (doVNBasedIntrinExpansion)
{
// Expand some intrinsics based on VN data
//
DoPhase(this, PHASE_VN_BASED_INTRINSIC_EXPAND, &Compiler::fgVNBasedIntrinsicExpansion);
}

if (doRangeAnalysis)
{
// Bounds check elimination via range analysis
Expand Down
46 changes: 45 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5214,6 +5214,8 @@ class Compiler
}
}

bool GetObjectHandleAndOffset(GenTree* tree, ssize_t* byteOffset, CORINFO_OBJECT_HANDLE* pObj);

// Convert a BYTE which represents the VM's CorInfoGCtype to the JIT's var_types
var_types getJitGCType(BYTE gcType);

Expand Down Expand Up @@ -5357,6 +5359,10 @@ class Compiler
PhaseStatus fgExpandStaticInit();
bool fgExpandStaticInitForCall(BasicBlock** pBlock, Statement* stmt, GenTreeCall* call);

PhaseStatus fgVNBasedIntrinsicExpansion();
bool fgVNBasedIntrinsicExpansionForCall(BasicBlock** pBlock, Statement* stmt, GenTreeCall* call);
bool fgVNBasedIntrinsicExpansionForCall_ReadUtf8(BasicBlock** pBlock, Statement* stmt, GenTreeCall* call);

PhaseStatus fgInsertGCPolls();
BasicBlock* fgCreateGCPoll(GCPollType pollType, BasicBlock* block);

Expand Down Expand Up @@ -7087,6 +7093,7 @@ class Compiler
#define OMF_HAS_MDARRAYREF 0x00004000 // Method contains multi-dimensional intrinsic array element loads or stores.
#define OMF_HAS_STATIC_INIT 0x00008000 // Method has static initializations we might want to partially inline
#define OMF_HAS_TLS_FIELD 0x00010000 // Method contains TLS field access
#define OMF_HAS_SPECIAL_INTRINSICS 0x00020000 // Method contains special intrinsics expanded in late phases

// clang-format on

Expand Down Expand Up @@ -7147,6 +7154,16 @@ class Compiler
optMethodFlags |= OMF_HAS_TLS_FIELD;
}

bool doesMethodHaveSpecialIntrinsics()
{
return (optMethodFlags & OMF_HAS_SPECIAL_INTRINSICS) != 0;
}

void setMethodHasSpecialIntrinsics()
{
optMethodFlags |= OMF_HAS_SPECIAL_INTRINSICS;
}

void pickGDV(GenTreeCall* call,
IL_OFFSET ilOffset,
bool isInterface,
Expand Down Expand Up @@ -8947,7 +8964,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

public:
// Similar to roundUpSIMDSize, but for General Purpose Registers (GPR)
unsigned int roundUpGPRSize(unsigned size)
unsigned roundUpGPRSize(unsigned size)
{
if (size > 4 && (REGSIZE_BYTES == 8))
{
Expand All @@ -8960,6 +8977,33 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
return size; // 2, 1, 0
}

var_types roundDownMaxType(unsigned size)
{
assert(size > 0);
var_types result = TYP_UNDEF;
#ifdef FEATURE_SIMD
if (IsBaselineSimdIsaSupported() && (roundDownSIMDSize(size) > 0))
{
return getSIMDTypeForSize(roundDownSIMDSize(size));
}
#endif
int nearestPow2 = 1 << BitOperations::Log2((unsigned)size);
switch (min(nearestPow2, REGSIZE_BYTES))
{
case 1:
return TYP_UBYTE;
case 2:
return TYP_USHORT;
case 4:
return TYP_INT;
case 8:
assert(REGSIZE_BYTES == 8);
return TYP_LONG;
default:
unreached();
}
}

enum UnrollKind
{
Memset,
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ CompPhaseNameMacro(PHASE_VALUE_NUMBER, "Do value numbering",
CompPhaseNameMacro(PHASE_OPTIMIZE_INDEX_CHECKS, "Optimize index checks", false, -1, false)
CompPhaseNameMacro(PHASE_OPTIMIZE_VALNUM_CSES, "Optimize Valnum CSEs", false, -1, false)
CompPhaseNameMacro(PHASE_VN_COPY_PROP, "VN based copy prop", false, -1, false)
CompPhaseNameMacro(PHASE_VN_BASED_INTRINSIC_EXPAND, "VN based intrinsic expansion", false, -1, false)
CompPhaseNameMacro(PHASE_OPTIMIZE_BRANCHES, "Redundant branch opts", false, -1, false)
CompPhaseNameMacro(PHASE_ASSERTION_PROP_MAIN, "Assertion prop", false, -1, false)
CompPhaseNameMacro(PHASE_IF_CONVERSION, "If conversion", false, -1, false)
Expand Down
Loading

0 comments on commit fe03904

Please sign in to comment.