From d786ef6778130ccb65a1e5cade31a94522ab05ba Mon Sep 17 00:00:00 2001 From: EgorBo Date: Sat, 21 Jan 2023 11:59:49 +0100 Subject: [PATCH 1/7] Optimize static fields of gc type on NativeAOT --- src/coreclr/jit/compiler.cpp | 5 +++++ src/coreclr/jit/emit.cpp | 6 ++++++ src/coreclr/jit/gentree.cpp | 3 +++ src/coreclr/jit/gentree.h | 3 ++- src/coreclr/jit/importer.cpp | 16 ++++++++++++---- src/coreclr/jit/valuenum.cpp | 1 + .../JitInterface/CorInfoImpl.RyuJit.cs | 15 ++++++++++++--- 7 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/coreclr/jit/compiler.cpp b/src/coreclr/jit/compiler.cpp index 671d5692ac4576..13036a6766425d 100644 --- a/src/coreclr/jit/compiler.cpp +++ b/src/coreclr/jit/compiler.cpp @@ -9592,6 +9592,11 @@ void cTreeFlags(Compiler* comp, GenTree* tree) chars += printf("[GTF_ICON_STATIC_BOX_PTR]"); break; + case GTF_ICON_STATIC_ADDR_PTR: + + chars += printf("[GTF_ICON_STATIC_ADDR_PTR]"); + break; + default: assert(!"a forgotten handle flag"); break; diff --git a/src/coreclr/jit/emit.cpp b/src/coreclr/jit/emit.cpp index 692eb10d12359e..ed530ab39d29a8 100644 --- a/src/coreclr/jit/emit.cpp +++ b/src/coreclr/jit/emit.cpp @@ -4189,6 +4189,12 @@ void emitter::emitDispCommentForHandle(size_t handle, size_t cookie, GenTreeFlag printf("%s %s for %s", commentPrefix, flag == GTF_ICON_STATIC_HDL ? "data" : "box", fieldName); return; } + + if (flag == GTF_ICON_STATIC_ADDR_PTR) + { + printf("%s static fld addr", commentPrefix); + return; + } } if (handle == 0) diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index dd1dc0e70d371e..9e6c9f4e8cd04f 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -11434,6 +11434,9 @@ void Compiler::gtDispConst(GenTree* tree) case GTF_ICON_STATIC_BOX_PTR: printf(" static box ptr"); break; + case GTF_ICON_STATIC_ADDR_PTR: + printf(" static fld addr"); + break; default: printf(" UNKNOWN"); break; diff --git a/src/coreclr/jit/gentree.h b/src/coreclr/jit/gentree.h index 790d14f2841779..d98f96ee040bea 100644 --- a/src/coreclr/jit/gentree.h +++ b/src/coreclr/jit/gentree.h @@ -559,7 +559,8 @@ enum GenTreeFlags : unsigned int GTF_ICON_CIDMID_HDL = 0x0E000000, // GT_CNS_INT -- constant is a class ID or a module ID GTF_ICON_BBC_PTR = 0x0F000000, // GT_CNS_INT -- constant is a basic block count pointer GTF_ICON_STATIC_BOX_PTR = 0x10000000, // GT_CNS_INT -- constant is an address of the box for a STATIC_IN_HEAP field - GTF_ICON_FIELD_SEQ = 0x11000000, // <--------> -- constant is a FieldSeq* (used only as VNHandle) + GTF_ICON_FIELD_SEQ = 0x11000000, // GT_CNS_INT -- constant is a FieldSeq* (used only as VNHandle) + GTF_ICON_STATIC_ADDR_PTR = 0x13000000, // GT_CNS_INT -- constant is a pointer to a static field address // GTF_ICON_REUSE_REG_VAL = 0x00800000 // GT_CNS_INT -- GTF_REUSE_REG_VAL, defined above GTF_ICON_SIMD_COUNT = 0x00200000, // GT_CNS_INT -- constant is Vector.Count diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index bfecb034e6145c..8a4a6ecf359f76 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -4238,13 +4238,21 @@ GenTree* Compiler::impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedT case CORINFO_FIELD_STATIC_RELOCATABLE: { #ifdef FEATURE_READYTORUN - assert(pFieldInfo->fieldLookup.accessType == InfoAccessType::IAT_VALUE); assert(fieldKind == FieldSeq::FieldKind::SimpleStatic); assert(innerFldSeq != nullptr); - GenTree* baseAddr = gtNewIconHandleNode((size_t)pFieldInfo->fieldLookup.addr, GTF_ICON_STATIC_HDL); - GenTree* offset = gtNewIconNode(pFieldInfo->offset, innerFldSeq); - op1 = gtNewOperNode(GT_ADD, TYP_I_IMPL, baseAddr, offset); + size_t fldAddr = (size_t)pFieldInfo->fieldLookup.addr; + if (pFieldInfo->fieldLookup.accessType == InfoAccessType::IAT_VALUE) + { + op1 = gtNewIconHandleNode(fldAddr, GTF_ICON_STATIC_HDL); + } + else + { + assert(pFieldInfo->fieldLookup.accessType == InfoAccessType::IAT_PVALUE); + op1 = gtNewIndOfIconHandleNode(TYP_BYREF, fldAddr, GTF_ICON_STATIC_ADDR_PTR, true); + } + GenTree* offset = gtNewIconNode(pFieldInfo->offset, innerFldSeq); + op1 = gtNewOperNode(GT_ADD, TYP_I_IMPL, op1, offset); #else unreached(); #endif // FEATURE_READYTORUN diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 16380ac8d1911b..4bbbbf96ef2d8d 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -5360,6 +5360,7 @@ GenTreeFlags ValueNumStore::GetFoldedArithOpResultHandleFlags(ValueNum vn) case GTF_ICON_CIDMID_HDL: case GTF_ICON_TLS_HDL: case GTF_ICON_STATIC_BOX_PTR: + case GTF_ICON_STATIC_ADDR_PTR: return GTF_ICON_CONST_PTR; case GTF_ICON_STATIC_HDL: case GTF_ICON_GLOBAL_PTR: diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index 26fab2dab3d73d..901f6a153c1e32 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -2141,11 +2141,20 @@ private void getFieldInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_MET pResult->helper = CorInfoHelpFunc.CORINFO_HELP_READYTORUN_THREADSTATIC_BASE; helperId = ReadyToRunHelperId.GetThreadStaticBase; } - else if (!_compilation.HasLazyStaticConstructor(field.OwningType) && !field.HasGCStaticBase) + else if (!_compilation.HasLazyStaticConstructor(field.OwningType)) { fieldAccessor = CORINFO_FIELD_ACCESSOR.CORINFO_FIELD_STATIC_RELOCATABLE; - ISymbolNode baseAddress = _compilation.NodeFactory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType); - pResult->fieldLookup.accessType = InfoAccessType.IAT_VALUE; + ISymbolNode baseAddress; + if (field.HasGCStaticBase) + { + pResult->fieldLookup.accessType = InfoAccessType.IAT_PVALUE; + baseAddress = _compilation.NodeFactory.TypeGCStaticsSymbol((MetadataType)field.OwningType); + } + else + { + pResult->fieldLookup.accessType = InfoAccessType.IAT_VALUE; + baseAddress = _compilation.NodeFactory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType); + } pResult->fieldLookup.addr = (void*)ObjectToHandle(baseAddress); } else From e209ecf5570c5e5c74fd88f4e117193270f0ebaa Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 21 Jan 2023 18:23:31 +0100 Subject: [PATCH 2/7] Apply suggestions from code review Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com> --- src/coreclr/jit/gentree.cpp | 2 +- src/coreclr/jit/gentree.h | 2 +- src/coreclr/jit/importer.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 9e6c9f4e8cd04f..9d44d19d3c2f68 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -11435,7 +11435,7 @@ void Compiler::gtDispConst(GenTree* tree) printf(" static box ptr"); break; case GTF_ICON_STATIC_ADDR_PTR: - printf(" static fld addr"); + printf(" static base addr cell"); break; default: printf(" UNKNOWN"); diff --git a/src/coreclr/jit/gentree.h b/src/coreclr/jit/gentree.h index d98f96ee040bea..dfe1549914987a 100644 --- a/src/coreclr/jit/gentree.h +++ b/src/coreclr/jit/gentree.h @@ -560,7 +560,7 @@ enum GenTreeFlags : unsigned int GTF_ICON_BBC_PTR = 0x0F000000, // GT_CNS_INT -- constant is a basic block count pointer GTF_ICON_STATIC_BOX_PTR = 0x10000000, // GT_CNS_INT -- constant is an address of the box for a STATIC_IN_HEAP field GTF_ICON_FIELD_SEQ = 0x11000000, // GT_CNS_INT -- constant is a FieldSeq* (used only as VNHandle) - GTF_ICON_STATIC_ADDR_PTR = 0x13000000, // GT_CNS_INT -- constant is a pointer to a static field address + GTF_ICON_STATIC_ADDR_PTR = 0x13000000, // GT_CNS_INT -- constant is a pointer to a static base address // GTF_ICON_REUSE_REG_VAL = 0x00800000 // GT_CNS_INT -- GTF_REUSE_REG_VAL, defined above GTF_ICON_SIMD_COUNT = 0x00200000, // GT_CNS_INT -- constant is Vector.Count diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 8a4a6ecf359f76..aed72a536e9f3d 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -4242,13 +4242,13 @@ GenTree* Compiler::impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedT assert(innerFldSeq != nullptr); size_t fldAddr = (size_t)pFieldInfo->fieldLookup.addr; - if (pFieldInfo->fieldLookup.accessType == InfoAccessType::IAT_VALUE) + if (pFieldInfo->fieldLookup.accessType == IAT_VALUE) { op1 = gtNewIconHandleNode(fldAddr, GTF_ICON_STATIC_HDL); } else { - assert(pFieldInfo->fieldLookup.accessType == InfoAccessType::IAT_PVALUE); + assert(pFieldInfo->fieldLookup.accessType == IAT_PVALUE); op1 = gtNewIndOfIconHandleNode(TYP_BYREF, fldAddr, GTF_ICON_STATIC_ADDR_PTR, true); } GenTree* offset = gtNewIconNode(pFieldInfo->offset, innerFldSeq); From e34e6fcd029c9db9f2571061fd98da877e2bbefa Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 21 Jan 2023 18:24:12 +0100 Subject: [PATCH 3/7] Update src/coreclr/jit/gentree.h Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com> --- src/coreclr/jit/gentree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/jit/gentree.h b/src/coreclr/jit/gentree.h index dfe1549914987a..d1ebcd0496d530 100644 --- a/src/coreclr/jit/gentree.h +++ b/src/coreclr/jit/gentree.h @@ -559,7 +559,7 @@ enum GenTreeFlags : unsigned int GTF_ICON_CIDMID_HDL = 0x0E000000, // GT_CNS_INT -- constant is a class ID or a module ID GTF_ICON_BBC_PTR = 0x0F000000, // GT_CNS_INT -- constant is a basic block count pointer GTF_ICON_STATIC_BOX_PTR = 0x10000000, // GT_CNS_INT -- constant is an address of the box for a STATIC_IN_HEAP field - GTF_ICON_FIELD_SEQ = 0x11000000, // GT_CNS_INT -- constant is a FieldSeq* (used only as VNHandle) + GTF_ICON_FIELD_SEQ = 0x11000000, // <--------> -- constant is a FieldSeq* (used only as VNHandle) GTF_ICON_STATIC_ADDR_PTR = 0x13000000, // GT_CNS_INT -- constant is a pointer to a static base address // GTF_ICON_REUSE_REG_VAL = 0x00800000 // GT_CNS_INT -- GTF_REUSE_REG_VAL, defined above From 157f15d09416fe93e1177a12c124457051dd9c4e Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 21 Jan 2023 18:29:26 +0100 Subject: [PATCH 4/7] Update CorInfoImpl.RyuJit.cs --- .../aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index 901f6a153c1e32..8f78cbc16c3b64 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -2147,14 +2147,13 @@ private void getFieldInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_MET ISymbolNode baseAddress; if (field.HasGCStaticBase) { - pResult->fieldLookup.accessType = InfoAccessType.IAT_PVALUE; baseAddress = _compilation.NodeFactory.TypeGCStaticsSymbol((MetadataType)field.OwningType); } else { - pResult->fieldLookup.accessType = InfoAccessType.IAT_VALUE; baseAddress = _compilation.NodeFactory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType); } + pResult->fieldLookup.accessType = baseAddress.RepresentsIndirectionCell ? InfoAccessType.IAT_PVALUE : InfoAccessType.IAT_VALUE; pResult->fieldLookup.addr = (void*)ObjectToHandle(baseAddress); } else From a488079be16b520ebb7486d88fac6b1f3cdaa3ff Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 21 Jan 2023 18:30:57 +0100 Subject: [PATCH 5/7] Update src/coreclr/jit/emit.cpp Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com> --- src/coreclr/jit/emit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/jit/emit.cpp b/src/coreclr/jit/emit.cpp index ed530ab39d29a8..8c5adef145de91 100644 --- a/src/coreclr/jit/emit.cpp +++ b/src/coreclr/jit/emit.cpp @@ -4192,7 +4192,7 @@ void emitter::emitDispCommentForHandle(size_t handle, size_t cookie, GenTreeFlag if (flag == GTF_ICON_STATIC_ADDR_PTR) { - printf("%s static fld addr", commentPrefix); + printf("%s static base addr cell", commentPrefix); return; } } From afa3c5b78f583aafa60ba3e276744099ee3822d5 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 21 Jan 2023 19:29:01 +0100 Subject: [PATCH 6/7] Update CorInfoImpl.RyuJit.cs --- .../JitInterface/CorInfoImpl.RyuJit.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index 8f78cbc16c3b64..84513502930dfc 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -2144,17 +2144,18 @@ private void getFieldInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_MET else if (!_compilation.HasLazyStaticConstructor(field.OwningType)) { fieldAccessor = CORINFO_FIELD_ACCESSOR.CORINFO_FIELD_STATIC_RELOCATABLE; - ISymbolNode baseAddress; + ISymbolNode baseAddr; if (field.HasGCStaticBase) { - baseAddress = _compilation.NodeFactory.TypeGCStaticsSymbol((MetadataType)field.OwningType); + pResult->fieldLookup.accessType = InfoAccessType.IAT_PVALUE; + baseAddr = _compilation.NodeFactory.TypeGCStaticsSymbol((MetadataType)field.OwningType); } else { - baseAddress = _compilation.NodeFactory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType); + pResult->fieldLookup.accessType = InfoAccessType.IAT_VALUE; + baseAddr = _compilation.NodeFactory.TypeNonGCStaticsSymbol((MetadataType)field.OwningType); } - pResult->fieldLookup.accessType = baseAddress.RepresentsIndirectionCell ? InfoAccessType.IAT_PVALUE : InfoAccessType.IAT_VALUE; - pResult->fieldLookup.addr = (void*)ObjectToHandle(baseAddress); + pResult->fieldLookup.addr = (void*)ObjectToHandle(baseAddr); } else { From 2b493ca7c42c8bf61ed51855b33887022cf99721 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Mon, 23 Jan 2023 12:30:23 +0100 Subject: [PATCH 7/7] Update importer.cpp --- src/coreclr/jit/importer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index aed72a536e9f3d..822ec09a583ec0 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -4249,7 +4249,7 @@ GenTree* Compiler::impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedT else { assert(pFieldInfo->fieldLookup.accessType == IAT_PVALUE); - op1 = gtNewIndOfIconHandleNode(TYP_BYREF, fldAddr, GTF_ICON_STATIC_ADDR_PTR, true); + op1 = gtNewIndOfIconHandleNode(TYP_I_IMPL, fldAddr, GTF_ICON_STATIC_ADDR_PTR, true); } GenTree* offset = gtNewIconNode(pFieldInfo->offset, innerFldSeq); op1 = gtNewOperNode(GT_ADD, TYP_I_IMPL, op1, offset);