Allow inlining for ldsfld + value-type (#78736)
authorEgor Bogatov <egorbo@gmail.com>
Thu, 2 Feb 2023 22:11:15 +0000 (23:11 +0100)
committerGitHub <noreply@github.com>
Thu, 2 Feb 2023 22:11:15 +0000 (23:11 +0100)
src/coreclr/jit/importer.cpp

index 34db37c..53e5104 100644 (file)
@@ -9236,11 +9236,20 @@ void Compiler::impImportBlockCode(BasicBlock* block)
                         {
                             // Loading a static valuetype field usually will cause a JitHelper to be called
                             // for the static base. This will bloat the code.
-                            compInlineResult->Note(InlineObservation::CALLEE_LDFLD_STATIC_VALUECLASS);
 
-                            if (compInlineResult->IsFailure())
+                            // Make an exception - small getters (6 bytes of IL) returning initialized fields, e.g.:
+                            //
+                            //  static DateTime Foo { get; } = DateTime.Now;
+                            //
+                            bool isInitedFld = (opcode == CEE_LDSFLD) && (info.compILCodeSize <= 6) &&
+                                               (fieldInfo.fieldFlags & CORINFO_FLG_FIELD_FINAL);
+                            if (!isInitedFld)
                             {
-                                return;
+                                compInlineResult->Note(InlineObservation::CALLEE_LDFLD_STATIC_VALUECLASS);
+                                if (compInlineResult->IsFailure())
+                                {
+                                    return;
+                                }
                             }
                         }
                     }