dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.41k stars 197 forks source link

[NativeAOT-LLVM] Ilc assert with locals in release config #2682

Open yowl opened 2 weeks ago

yowl commented 2 weeks ago

This IL seems to cause an assert

// [PresentationFramework]System.Windows.FrameworkTemplate.WireRootObjectToParent(object,DependencyObject,DependencyObject,FrameworkElement,INameScope)
.method class [WindowsBase]System.Windows.DependencyObject WireRootObjectToParent(object, class [WindowsBase]System.Windows.DependencyObject, class [WindowsBase]System.Windows.DependencyObject, class System.Windows.FrameworkElement, class [System.Xaml]System.Windows.Markup.INameScope) cil managed
{
  // Code size: 124
  .maxstack 5
  .locals init (class [PresentationCore]System.Windows.UIElement V_0,
      class System.Windows.FrameworkElement V_1,
      class System.Windows.FrameworkContentElement V_2)

  IL_0000:  ldarg.0
  IL_0001:  isinst      [WindowsBase]System.Windows.DependencyObject
  IL_0006:  starg.s     1
  IL_0008:  ldarg.1
  IL_0009:  brfalse.s   IL_007A
  IL_000B:  ldarg.3
  IL_000C:  brfalse.s   IL_0045
  IL_000E:  ldarg.1
  IL_000F:  isinst      [PresentationCore]System.Windows.UIElement
  IL_0014:  stloc.0
  IL_0015:  ldloc.0
  IL_0016:  brtrue.s    IL_003C
  IL_0018:  ldstr       "TemplateMustBeFE"
  IL_001D:  ldc.i4.1
  IL_001E:  newarr      object
  IL_0023:  dup
  IL_0024:  ldc.i4.0
  IL_0025:  ldarg.1
  IL_0026:  callvirt    instance class [System.Private.CoreLib]System.Type [System.Private.CoreLib]System.Object::GetType()
  IL_002B:  callvirt    instance string [System.Private.CoreLib]System.Type::get_FullName()
  IL_0030:  stelem.ref
  IL_0031:  call        string System.Windows.SR::Get(string, object[])
  IL_0036:  newobj      instance void [System.Private.CoreLib]System.InvalidOperationException::.ctor(string)
  IL_003B:  throw
  IL_003C:  ldarg.3
  IL_003D:  ldloc.0
  IL_003E:  callvirt    instance void System.Windows.FrameworkElement::set_TemplateChild(class [PresentationCore]System.Windows.UIElement)
  IL_0043:  br.s        IL_006A
  IL_0045:  ldarg.2
  IL_0046:  brfalse.s   IL_006A
  IL_0048:  ldarg.1
  IL_0049:  ldloca.s    1
  IL_004B:  ldloca.s    2
  IL_004D:  ldc.i4.1
  IL_004E:  call        void MS.Internal.Helper::DowncastToFEorFCE(class [WindowsBase]System.Windows.DependencyObject, class System.Windows.FrameworkElement&, class System.Windows.FrameworkContentElement&, bool)
  IL_0053:  ldarg.2
  IL_0054:  castclass   System.Windows.FrameworkContentElement
  IL_0059:  ldarg.1
  IL_005A:  callvirt    instance class [System.Private.CoreLib]System.Type [System.Private.CoreLib]System.Object::GetType()
  IL_005F:  ldloc.1
  IL_0060:  ldnull
  IL_0061:  cgt.un
  IL_0063:  ldloc.1
  IL_0064:  ldloc.2
  IL_0065:  call        void System.Windows.FrameworkElementFactory::AddNodeToLogicalTree(class [WindowsBase]System.Windows.DependencyObject, class [System.Private.CoreLib]System.Type, bool, class System.Windows.FrameworkElement, class System.Windows.FrameworkContentElement)
  IL_006A:  ldarg.1
  IL_006B:  call        class [System.Xaml]System.Windows.Markup.INameScope [WindowsBase]System.Windows.NameScope::GetNameScope(class [WindowsBase]System.Windows.DependencyObject)
  IL_0070:  brtrue.s    IL_007A
  IL_0072:  ldarg.1
  IL_0073:  ldarg.s     4
  IL_0075:  call        void [WindowsBase]System.Windows.NameScope::SetNameScope(class [WindowsBase]System.Windows.DependencyObject, class [System.Xaml]System.Windows.Markup.INameScope)
  IL_007A:  ldarg.1
  IL_007B:  ret
}

at https://github.com/dotnet/runtimelab/blob/409591fa1363a89d1bbce4daf7ab1a414de8b3df/src/coreclr/jit/llvmcodegen.cpp#L1191

Haven't done any more investigation

image

yowl commented 2 weeks ago

EGitHubruntimelabartifactsbincoreclrwindows.x64.Debugilcnet9.0ilc.exe.txt

Jit dump attached

SingleAccretion commented 2 weeks ago

It looks like we need to add the local to the live-in set of the first block here: https://github.com/dotnet/runtimelab/blob/e80eec3fbb94e44157d16a542f89281eb43cab0d/src/coreclr/jit/llvmlssa.cpp#L984-L990

yowl commented 2 weeks ago
VarSetOps::AddElemD(m_compiler, m_compiler->fgFirstBB->bbLiveIn, varDsc->lvVarIndex);

Sorry, not sure I've got this right, sometimes lvaTrackedCount is 0 which causes an assert?

SingleAccretion commented 2 weeks ago

Yes, that's correct. It should be guarded by varDsc->lvTracked.