-
Notifications
You must be signed in to change notification settings - Fork 196
/
LLVMObjectWriter.cs
1159 lines (965 loc) · 53.1 KB
/
LLVMObjectWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using ILCompiler.DependencyAnalysisFramework;
using Internal.IL;
using Internal.JitInterface;
using Internal.Text;
using Internal.TypeSystem;
using LLVMSharp.Interop;
using ObjectData = ILCompiler.DependencyAnalysis.ObjectNode.ObjectData;
namespace ILCompiler.DependencyAnalysis
{
/// <summary>
/// Object writer that emits LLVM (bitcode).
/// </summary>
internal sealed unsafe class LLVMObjectWriter
{
// Module with ILC-generated code and data.
private readonly LLVMModuleRef _module;
// Module with the external functions and getters for them. Must be separate from the main module so as to avoid
// direct calls with mismatched signatures at the LLVM level.
private readonly LLVMModuleRef _moduleWithExternalFunctions;
private LLVMTypeRef _ptrType;
private LLVMTypeRef _intPtrType;
// Node factory and compilation for which ObjectWriter was instantiated.
private readonly LLVMCodegenCompilation _compilation;
private readonly NodeFactory _nodeFactory;
// Path to the bitcode file we're emitting.
private readonly string _objectFilePath;
// Used for writing mangled names together with Utf8Name.
private readonly Utf8StringBuilder _utf8StringBuilder = new Utf8StringBuilder();
// List of global values to be kept alive via @llvm.used.
private readonly List<LLVMValueRef> _keepAliveList = new();
// Data emitted for the current object node. Initial capacity chosen to be the size of the largest node in a small program.
private LLVMValueRef[] _currentObjectData = new LLVMValueRef[100_000];
private LLVMTypeRef[] _currentObjectTypes = new LLVMTypeRef[100_000];
#if DEBUG
private static readonly Dictionary<string, ISymbolNode> _previouslyWrittenNodeNames = new();
#endif
private LLVMObjectWriter(string objectFilePath, LLVMCodegenCompilation compilation)
{
_module = LLVMModuleRef.CreateWithName("data");
_module.Target = compilation.Options.Target;
_module.DataLayout = compilation.Options.DataLayout;
_moduleWithExternalFunctions = LLVMModuleRef.CreateWithName("external");
_moduleWithExternalFunctions.Target = compilation.Options.Target;
_moduleWithExternalFunctions.DataLayout = compilation.Options.DataLayout;
_ptrType = LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0);
_intPtrType = compilation.NodeFactory.Target.PointerSize == 4 ? LLVMTypeRef.Int32 : LLVMTypeRef.Int64;
_compilation = compilation;
_nodeFactory = compilation.NodeFactory;
_objectFilePath = objectFilePath;
}
public static void EmitObject(string objectFilePath, IEnumerable<DependencyNode> nodes, LLVMCodegenCompilation compilation, IObjectDumper dumper)
{
LLVMObjectWriter objectWriter = new LLVMObjectWriter(objectFilePath, compilation);
NodeFactory factory = compilation.NodeFactory;
try
{
foreach (DependencyNode depNode in nodes)
{
if (depNode is LLVMMethodCodeNode runtimeExportNode && runtimeExportNode.Method.HasCustomAttribute("System.Runtime", "RuntimeExportAttribute"))
{
objectWriter.EmitRuntimeExportThunk(runtimeExportNode);
continue;
}
ObjectNode node = depNode as ObjectNode;
if (node == null)
continue;
if (node.ShouldSkipEmittingObjectNode(factory))
continue;
switch (node)
{
case ReadyToRunGenericHelperNode readyToRunGenericHelperNode:
objectWriter.GetCodeForReadyToRunGenericHelper(readyToRunGenericHelperNode, factory);
continue;
case ReadyToRunHelperNode readyToRunHelperNode:
objectWriter.GetCodeForReadyToRunHelper(readyToRunHelperNode, factory);
continue;
case TentativeMethodNode tentativeMethodNode:
objectWriter.GetCodeForTentativeMethod(tentativeMethodNode, factory);
continue;
case UnboxingStubNode unboxStubNode:
objectWriter.GetCodeForUnboxThunkMethod(unboxStubNode);
continue;
case ExternMethodAccessorNode accessorNode:
objectWriter.GetCodeForExternMethodAccessor(accessorNode);
continue;
default:
break;
}
ObjectData nodeContents = node.GetData(factory);
dumper?.DumpObjectNode(factory, node, nodeContents);
#if DEBUG
foreach (ISymbolNode definedSymbol in nodeContents.DefinedSymbols)
{
try
{
_previouslyWrittenNodeNames.Add(definedSymbol.GetMangledName(factory.NameMangler), definedSymbol);
}
catch (ArgumentException)
{
ISymbolNode alreadyWrittenSymbol = _previouslyWrittenNodeNames[definedSymbol.GetMangledName(factory.NameMangler)];
Debug.Fail("Duplicate node name emitted to file",
$"Symbol {definedSymbol.GetMangledName(factory.NameMangler)} has already been written to the output object file {objectFilePath} with symbol {alreadyWrittenSymbol}");
}
}
#endif
objectWriter.EmitObjectNode(node, nodeContents);
}
objectWriter.FinishObjWriter();
// Make sure we have released all memory used for mangling names.
Debug.Assert(objectWriter._utf8StringBuilder.Length == 0);
}
catch
{
// If there was an exception while generating the OBJ file, make sure we don't leave the unfinished object file around.
// TODO-LLVM: we also emit the results file, etc, should delete those too.
try
{
File.Delete(objectFilePath);
}
catch { }
// Continue with the original exception.
throw;
}
}
private void EmitObjectNode(ObjectNode node, ObjectData nodeContents)
{
NodeFactory factory = _nodeFactory;
int pointerSize = factory.Target.PointerSize;
Span<LLVMTypeRef> typeElements = default;
// All references to this symbol are through "ordinarily named" aliases. Thus, we need to suffix the real definition.
ISymbolNode symbolNode = (ISymbolNode)node;
using Utf8Name dataSymbolName = GetMangledUtf8Name(symbolNode, "__DATA");
// Calculate the size of this object node.
int dataSizeInBytes = nodeContents.Data.Length;
int dataSizeInBytesAligned = dataSizeInBytes.AlignUp(pointerSize); // TODO-LLVM: do not pad out the data.
int dataSizeInElements = dataSizeInBytesAligned / pointerSize;
// If we need to create unaligned relocs then use a struct for the symbol.
// Start with the premise that most nodes will just contain aligned relocs.
bool useStruct = false;
if (_currentObjectData.Length < dataSizeInElements)
{
Array.Resize(ref _currentObjectData, Math.Max(_currentObjectData.Length * 2, dataSizeInElements));
}
Span<LLVMValueRef> dataElements = _currentObjectData.AsSpan(0, dataSizeInElements);
ReadOnlySpan<byte> data = nodeContents.Data.AsSpan();
// Indicies in byte units to allow us to "zip" the binary data with the relocs
int dataOffset = 0;
int relocIndex = 0;
int elementOffset = 0;
int relocLength = nodeContents.Relocs.Length;
int nextRelocOffset = 0;
bool nextRelocValid = relocIndex < relocLength;
if (nextRelocValid)
{
nextRelocOffset = nodeContents.Relocs[0].Offset;
}
while (dataOffset < dataSizeInBytes)
{
if (!useStruct && nextRelocValid && nextRelocOffset % pointerSize != 0)
{
// Switch from array to struct. This will need more elements because binary data is output byte-by-byte.
useStruct = true;
dataSizeInElements = nodeContents.Relocs.Length + dataSizeInBytes - (nodeContents.Relocs.Length * pointerSize);
if (_currentObjectData.Length < dataSizeInElements)
{
Array.Resize(ref _currentObjectData, Math.Max(_currentObjectData.Length * 2, dataSizeInElements));
}
if (_currentObjectTypes.Length < dataSizeInElements)
{
Array.Resize(ref _currentObjectTypes, Math.Max(_currentObjectTypes.Length * 2, dataSizeInElements));
}
dataElements = _currentObjectData.AsSpan(0, dataSizeInElements);
typeElements = _currentObjectTypes.AsSpan(0, dataSizeInElements);
// Restart zipping while loop.
dataOffset = 0;
elementOffset = 0;
relocIndex = 0;
continue;
}
// Emit binary data until next reloc. As some large nodes only contain binary data this is a small optimization.
while (dataOffset < dataSizeInBytes && (!nextRelocValid || dataOffset < nextRelocOffset))
{
if (useStruct)
{
typeElements[elementOffset] = LLVMTypeRef.Int8;
dataElements[elementOffset] = LLVMValueRef.CreateConstInt(LLVMTypeRef.Int8, data[dataOffset]);
dataOffset++;
}
else
{
ulong value = 0;
int size = Math.Min(dataSizeInBytes - dataOffset, pointerSize);
data.Slice(dataOffset, size).CopyTo(new Span<byte>(&value, size));
dataElements[elementOffset] = LLVMValueRef.CreateConstIntToPtr(LLVMValueRef.CreateConstInt(_intPtrType, value), _ptrType);
dataOffset += pointerSize;
}
elementOffset++;
}
if (nextRelocValid)
{
Relocation reloc = nodeContents.Relocs[relocIndex];
long delta;
fixed (void* location = &data[reloc.Offset])
{
delta = Relocation.ReadValue(reloc.RelocType, location);
}
ISymbolNode symbolRefNode = reloc.Target;
if (symbolRefNode is EETypeNode eeTypeNode && eeTypeNode.ShouldSkipEmittingObjectNode(factory))
{
symbolRefNode = factory.ConstructedTypeSymbol(eeTypeNode.Type);
}
dataElements[elementOffset] = GetSymbolReferenceValue(symbolRefNode, checked((int)delta));
if (useStruct)
{
typeElements[elementOffset] = _ptrType;
}
relocIndex++;
nextRelocValid = relocIndex < relocLength;
if (nextRelocValid)
{
nextRelocOffset = nodeContents.Relocs[relocIndex].Offset;
}
dataOffset += pointerSize;
elementOffset++;
}
}
Debug.Assert(relocIndex == relocLength);
// Create and initialize the LLVM global value.
LLVMTypeRef dataSymbolType = useStruct
? LLVMTypeRef.CreateStruct(typeElements, true)
: LLVMTypeRef.CreateArray(_ptrType, (uint)dataSizeInElements);
LLVMValueRef dataSymbol = _module.AddGlobal(dataSymbolName, dataSymbolType);
dataSymbol.Section = node.GetSection(_nodeFactory).Name;
dataSymbol.Alignment = (uint)nodeContents.Alignment;
dataSymbol.Initializer = useStruct
? LLVMValueRef.CreateConstStruct(dataElements, true)
: LLVMValueRef.CreateConstArray(_ptrType, dataElements);
foreach (ISymbolDefinitionNode definedSymbol in nodeContents.DefinedSymbols)
{
using Utf8Name definedSymbolName = GetMangledUtf8Name(definedSymbol);
int definedSymbolOffset = definedSymbol.Offset;
EmitSymbolDef(dataSymbol, definedSymbolName, definedSymbolOffset);
string alternateDefinedSymbolName = factory.GetSymbolAlternateName(definedSymbol);
if (alternateDefinedSymbolName != null)
{
using Utf8Name alternateDefinedSymbolUtf8Name = GetUtf8Name(alternateDefinedSymbolName);
EmitSymbolDef(dataSymbol, alternateDefinedSymbolUtf8Name, definedSymbolOffset);
}
}
if (ObjectNodeMustBeArtificiallyKeptAlive(node))
{
_keepAliveList.Add(dataSymbol);
}
}
private void EmitSymbolDef(LLVMValueRef baseSymbol, ReadOnlySpan<byte> symbolIdentifier, int offsetFromBaseSymbol)
{
LLVMValueRef symbolAddress = baseSymbol;
if (offsetFromBaseSymbol != 0)
{
LLVMValueRef offsetValue = CreateConst(LLVMTypeRef.Int32, offsetFromBaseSymbol);
symbolAddress = LLVMValueRef.CreateConstGEP2(LLVMTypeRef.Int8, symbolAddress, stackalloc[] { offsetValue });
}
LLVMValueRef symbolDef = _module.GetNamedAlias(symbolIdentifier);
if (symbolDef.Handle == IntPtr.Zero)
{
_module.AddAlias(symbolIdentifier, LLVMTypeRef.Int8, symbolAddress);
}
else
{
// Set the aliasee.
LLVM.AliasSetAliasee(symbolDef, symbolAddress);
}
}
private static bool ObjectNodeMustBeArtificiallyKeptAlive(ObjectNode node)
{
// The modules section is referenced through the special __start/__stop
// symbols, which don't cause the linker to consider it alive by default.
return node is ModulesSectionNode;
}
private void FinishObjWriter()
{
EmitKeepAliveList();
#if DEBUG
_module.PrintToFile(Path.ChangeExtension(_objectFilePath, ".txt"));
_module.Verify(LLVMVerifierFailureAction.LLVMAbortProcessAction);
_moduleWithExternalFunctions.PrintToFile(Path.ChangeExtension(_objectFilePath, "external.txt"));
_moduleWithExternalFunctions.Verify(LLVMVerifierFailureAction.LLVMAbortProcessAction);
#endif
string dataLlvmObjectPath = _objectFilePath;
_module.WriteBitcodeToFile(dataLlvmObjectPath);
string externalLlvmObjectPath = Path.ChangeExtension(_objectFilePath, "external.bc");
_moduleWithExternalFunctions.WriteBitcodeToFile(externalLlvmObjectPath);
LLVMCompilationResults compilationResults = _compilation.GetCompilationResults();
compilationResults.Add(dataLlvmObjectPath);
compilationResults.Add(externalLlvmObjectPath);
compilationResults.SerializeToFile(Path.ChangeExtension(_objectFilePath, "results.txt"));
}
private void EmitKeepAliveList()
{
// See https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable.
ReadOnlySpan<LLVMValueRef> llvmUsedSymbols = CollectionsMarshal.AsSpan(_keepAliveList);
if (llvmUsedSymbols.Length != 0)
{
LLVMTypeRef llvmUsedType = LLVMTypeRef.CreateArray(_ptrType, (uint)llvmUsedSymbols.Length);
LLVMValueRef llvmUsedGlobal = _module.AddGlobal(llvmUsedType, "llvm.used");
llvmUsedGlobal.Linkage = LLVMLinkage.LLVMAppendingLinkage;
llvmUsedGlobal.Section = "llvm.metadata";
llvmUsedGlobal.Initializer = LLVMValueRef.CreateConstArray(_ptrType, llvmUsedSymbols);
}
}
private void EmitRuntimeExportThunk(LLVMMethodCodeNode methodNode)
{
MethodDesc method = methodNode.Method;
Debug.Assert(method.HasCustomAttribute("System.Runtime", "RuntimeExportAttribute") && methodNode.CompilationCompleted);
LLVMValueRef managedFunc = GetOrCreateLLVMFunction(methodNode);
LLVMTypeRef managedFuncType = managedFunc.GetValueType();
// Native signature: managed minus the shadow stack.
LLVMTypeRef[] managedFuncParamTypes = managedFuncType.ParamTypes;
LLVMTypeRef[] nativeFuncParamTypes = new LLVMTypeRef[managedFuncParamTypes.Length - 1];
Array.Copy(managedFuncParamTypes, 1, nativeFuncParamTypes, 0, nativeFuncParamTypes.Length);
LLVMTypeRef nativeFuncType = LLVMTypeRef.CreateFunction(managedFuncType.ReturnType, nativeFuncParamTypes, false);
using Utf8Name nativeFuncName = GetUtf8Name(_compilation.NodeFactory.GetSymbolAlternateName(methodNode));
LLVMValueRef nativeFunc = GetOrCreateLLVMFunction(nativeFuncName, nativeFuncType);
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
LLVMBasicBlockRef block = nativeFunc.AppendBasicBlock("ManagedCallBlock");
builder.PositionAtEnd(block);
// Get the shadow stack. Since we are wrapping a runtime export, the caller is (by definition) managed, so we must have set up the shadow
// stack already and can bypass the init check.
LLVMTypeRef getShadowStackFuncSig = LLVMTypeRef.CreateFunction(_ptrType, Array.Empty<LLVMTypeRef>());
LLVMValueRef getShadowStackFunc = GetOrCreateLLVMFunction("RhpGetShadowStackTop"u8, getShadowStackFuncSig);
LLVMValueRef shadowStack = builder.BuildCall2(getShadowStackFuncSig, getShadowStackFunc, Array.Empty<LLVMValueRef>());
int argsCount = managedFuncParamTypes.Length;
Span<LLVMValueRef> args = argsCount > 100 ? new LLVMValueRef[argsCount] : stackalloc LLVMValueRef[argsCount];
args[0] = shadowStack;
for (int i = 0; i < nativeFuncParamTypes.Length; i++)
{
args[i + 1] = nativeFunc.GetParam((uint)i);
}
LLVMValueRef returnValue = CreateCall(builder, managedFunc, args, "");
if (method.Signature.ReturnType.IsVoid)
{
builder.BuildRetVoid();
}
else
{
builder.BuildRet(returnValue);
}
}
private void GetCodeForReadyToRunGenericHelper(ReadyToRunGenericHelperNode node, NodeFactory factory)
{
if (node.Id == ReadyToRunHelperId.DelegateCtor)
{
GetCodeForDelegateCtorHelper(node);
return;
}
using Utf8Name helperFuncName = GetMangledUtf8Name(node);
LLVMTypeRef helperFuncType = LLVMTypeRef.CreateFunction(
_ptrType, stackalloc[] { _ptrType /* shadow stack */, _ptrType /* generic context */ }, IsVarArg: false);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(helperFuncName, helperFuncType);
LLVMBuilderRef builder = _module.Context.CreateBuilder();
builder.PositionAtEnd(helperFunc.AppendBasicBlock("GenericReadyToRunHelper"));
LLVMValueRef dictionary = OutputCodeForGetGenericDictionary(builder, helperFunc.GetParam(1), node);
LLVMValueRef result = OutputCodeForDictionaryLookup(builder, factory, node, node.LookupSignature, dictionary);
switch (node.Id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
{
MetadataType target = (MetadataType)node.Target;
if (TriggersLazyStaticConstructor(factory, target))
{
OutputCodeForTriggerCctor(builder, result);
}
}
break;
case ReadyToRunHelperId.GetGCStaticBase:
{
MetadataType target = (MetadataType)node.Target;
if (TriggersLazyStaticConstructor(factory, target))
{
GenericLookupResult nonGcBaseLookup = factory.GenericLookup.TypeNonGCStaticBase(target);
LLVMValueRef nonGcStaticsBase = OutputCodeForDictionaryLookup(builder, factory, node, nonGcBaseLookup, dictionary);
OutputCodeForTriggerCctor(builder, nonGcStaticsBase);
}
result = builder.BuildLoad2(_ptrType, result, "gcBase");
}
break;
case ReadyToRunHelperId.GetThreadStaticBase:
{
MetadataType target = (MetadataType)node.Target;
LLVMValueRef nonGcStaticsBase = default;
if (TriggersLazyStaticConstructor(factory, target))
{
GenericLookupResult nonGcBaseLookup = factory.GenericLookup.TypeNonGCStaticBase(target);
nonGcStaticsBase = OutputCodeForDictionaryLookup(builder, factory, node, nonGcBaseLookup, dictionary);
}
result = OutputCodeForGetThreadStaticBase(builder, result, nonGcStaticsBase);
}
break;
// These are all simple: just get the thing from the dictionary and we're done
case ReadyToRunHelperId.TypeHandle:
case ReadyToRunHelperId.TypeHandleForCasting:
case ReadyToRunHelperId.MethodHandle:
case ReadyToRunHelperId.FieldHandle:
case ReadyToRunHelperId.MethodDictionary:
case ReadyToRunHelperId.MethodEntry:
case ReadyToRunHelperId.VirtualDispatchCell:
case ReadyToRunHelperId.DefaultConstructor:
case ReadyToRunHelperId.ObjectAllocator:
case ReadyToRunHelperId.ConstrainedDirectCall:
break;
default:
throw new NotImplementedException();
}
builder.BuildRet(result);
}
private static bool TriggersLazyStaticConstructor(NodeFactory factory, TypeDesc type)
{
return factory.PreinitializationManager.HasLazyStaticConstructor(type.ConvertToCanonForm(CanonicalFormKind.Specific));
}
private void GetCodeForReadyToRunHelper(ReadyToRunHelperNode node, NodeFactory factory)
{
if (node.Id == ReadyToRunHelperId.VirtualCall)
{
GetCodeForVirtualCallHelper(node);
return;
}
if (node.Id == ReadyToRunHelperId.DelegateCtor)
{
GetCodeForDelegateCtorHelper(node);
return;
}
using Utf8Name helperFuncName = GetMangledUtf8Name(node);
LLVMTypeRef helperFuncType = LLVMTypeRef.CreateFunction(
_ptrType, stackalloc[] { _ptrType /* shadow stack or "this" */ }, IsVarArg: false);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(helperFuncName, helperFuncType);
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
builder.PositionAtEnd(helperFunc.AppendBasicBlock("ReadyToRunHelper"));
LLVMValueRef result;
switch (node.Id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
{
MetadataType target = (MetadataType)node.Target;
result = GetSymbolReferenceValue(factory.TypeNonGCStaticsSymbol(target));
if (_compilation.HasLazyStaticConstructor(target))
{
OutputCodeForTriggerCctor(builder, result);
}
}
break;
case ReadyToRunHelperId.GetGCStaticBase:
{
MetadataType target = (MetadataType)node.Target;
if (_compilation.HasLazyStaticConstructor(target))
{
LLVMValueRef nonGcBase = GetSymbolReferenceValue(factory.TypeNonGCStaticsSymbol(target));
OutputCodeForTriggerCctor(builder, nonGcBase);
}
result = GetSymbolReferenceValue(factory.TypeGCStaticsSymbol(target));
result = builder.BuildLoad2(_ptrType, result, "gcBase");
}
break;
case ReadyToRunHelperId.GetThreadStaticBase:
{
MetadataType target = (MetadataType)node.Target;
LLVMValueRef nonGcBase = default;
if (_compilation.HasLazyStaticConstructor(target))
{
nonGcBase = GetSymbolReferenceValue(factory.TypeNonGCStaticsSymbol(target));
}
LLVMValueRef pModuleDataSlot = GetSymbolReferenceValue(factory.TypeThreadStaticIndex(target));
result = OutputCodeForGetThreadStaticBase(builder, pModuleDataSlot, nonGcBase);
}
break;
case ReadyToRunHelperId.ResolveVirtualFunction:
{
MethodDesc targetMethod = (MethodDesc)node.Target;
if (targetMethod.OwningType.IsInterface)
{
// TODO-LLVM: would be nice to use pointers instead of IntPtr in "RhpResolveInterfaceMethod".
LLVMTypeRef resolveFuncType = LLVMTypeRef.CreateFunction(
_intPtrType, stackalloc[] { _ptrType, _intPtrType }, IsVarArg: false);
LLVMValueRef resolveFunc = GetOrCreateLLVMFunction("RhpResolveInterfaceMethod"u8, resolveFuncType);
LLVMValueRef cell = GetSymbolReferenceValue(factory.InterfaceDispatchCell(targetMethod));
LLVMValueRef cellArg = builder.BuildPtrToInt(cell, _intPtrType, "cellArg");
result = builder.BuildCall2(resolveFuncType, resolveFunc, stackalloc[] { helperFunc.GetParam(0), cellArg }, "");
result = builder.BuildIntToPtr(result, _ptrType, "pInterfaceFunc");
}
else
{
Debug.Assert(!targetMethod.CanMethodBeInSealedVTable());
result = OutputCodeForVTableLookup(builder, helperFunc.GetParam(0), targetMethod);
}
}
break;
default:
throw new NotImplementedException();
}
builder.BuildRet(result);
}
private void GetCodeForVirtualCallHelper(ReadyToRunHelperNode node)
{
MethodDesc targetMethod = (MethodDesc)node.Target;
Debug.Assert(!targetMethod.OwningType.IsInterface);
Debug.Assert(!targetMethod.CanMethodBeInSealedVTable());
Debug.Assert(!targetMethod.RequiresInstArg());
using Utf8Name helperFuncName = GetMangledUtf8Name(node);
LLVMTypeRef funcType = _compilation.GetLLVMSignatureForMethod(targetMethod.Signature, hasHiddenParam: false);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(helperFuncName, funcType);
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
builder.PositionAtEnd(helperFunc.AppendBasicBlock("VirtualCall"));
int argsCount = (int)helperFunc.ParamsCount;
Span<LLVMValueRef> args = argsCount > 100 ? new LLVMValueRef[argsCount] : stackalloc LLVMValueRef[argsCount];
for (int i = 0; i < args.Length; i++)
{
args[i] = helperFunc.GetParam((uint)i);
}
LLVMValueRef pTargetMethod = OutputCodeForVTableLookup(builder, helperFunc.GetParam(1), targetMethod);
LLVMValueRef callTarget = builder.BuildCall2(funcType, pTargetMethod, args, "");
if (funcType.ReturnType != LLVMTypeRef.Void)
{
builder.BuildRet(callTarget);
}
else
{
builder.BuildRetVoid();
}
}
private void GetCodeForDelegateCtorHelper(AssemblyStubNode node)
{
NodeFactory factory = _nodeFactory;
ReadyToRunGenericHelperNode genericNode = node as ReadyToRunGenericHelperNode;
DelegateCreationInfo delegateCreationInfo;
scoped Span<LLVMTypeRef> helperFuncParams;
if (genericNode != null)
{
delegateCreationInfo = (DelegateCreationInfo)genericNode.Target;
helperFuncParams = stackalloc[] { _ptrType, _ptrType, _ptrType, _ptrType };
}
else
{
delegateCreationInfo = (DelegateCreationInfo)((ReadyToRunHelperNode)node).Target;
helperFuncParams = stackalloc[] { _ptrType, _ptrType, _ptrType };
}
using Utf8Name helperFuncName = GetMangledUtf8Name(node);
LLVMTypeRef helperFuncType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, helperFuncParams, IsVarArg: false);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(helperFuncName, helperFuncType);
// Incoming parameters are: (SS, this, targetObj, [GenericContext]).
// Outgoing parameters are: (SS, this, targetObj, pTarget, [InvokeThunk]).
// Thus, our main responsibility is to computate the target.
//
LLVMValueRef initializerFunc = GetOrCreateLLVMFunction(delegateCreationInfo.Constructor);
LLVMTypeRef[] initializerFuncParamTypes = initializerFunc.GetValueType().ParamTypes;
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
builder.PositionAtEnd(helperFunc.AppendBasicBlock(genericNode != null ? "GenericDelegateCtor" : "DelegateCtor"));
LLVMValueRef targetObj = helperFunc.GetParam(2);
LLVMTypeRef targetValueType = initializerFuncParamTypes[3];
LLVMValueRef targetValue;
if (genericNode != null)
{
LLVMValueRef dictionary = OutputCodeForGetGenericDictionary(builder, helperFunc.GetParam(3), genericNode);
targetValue = OutputCodeForDictionaryLookup(builder, factory, genericNode, genericNode.LookupSignature, dictionary);
}
else if (delegateCreationInfo.TargetNeedsVTableLookup)
{
targetValue = OutputCodeForVTableLookup(builder, targetObj, delegateCreationInfo.TargetMethod);
}
else
{
targetValue = GetSymbolReferenceValue(delegateCreationInfo.GetTargetNode(factory));
}
if (targetValue.TypeOf != targetValueType)
{
targetValue = builder.BuildPointerCast(targetValue, targetValueType, "pTarget");
}
Span<LLVMValueRef> initializerArgs = stackalloc LLVMValueRef[initializerFuncParamTypes.Length];
initializerArgs[0] = helperFunc.GetParam(0);
initializerArgs[1] = helperFunc.GetParam(1);
initializerArgs[2] = targetObj;
initializerArgs[3] = targetValue;
if (delegateCreationInfo.Thunk != null)
{
LLVMValueRef thunkValue = GetOrCreateLLVMFunction(delegateCreationInfo.Thunk);
initializerArgs[4] = builder.BuildPointerCast(thunkValue, initializerFuncParamTypes[4]);
}
CreateCall(builder, initializerFunc, initializerArgs);
builder.BuildRetVoid();
}
private void GetCodeForTentativeMethod(TentativeMethodNode node, NodeFactory factory)
{
LLVMValueRef tentativeStub = GetOrCreateLLVMFunction(node);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(node.GetTarget(factory));
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
LLVMBasicBlockRef block = tentativeStub.AppendBasicBlock("TentativeStub");
builder.PositionAtEnd(block);
CreateCall(builder, helperFunc, stackalloc[] { tentativeStub.GetParam(0) });
CreateUnreachedAfterAlwaysThrowCall(builder);
}
private void GetCodeForUnboxThunkMethod(UnboxingStubNode node)
{
NodeFactory factory = _compilation.NodeFactory;
// This is the regular unboxing thunk that just does "Target(ref @this.Data, args...)".
// Note how we perform a shadow tail call here while simultaneously overwriting "this".
//
LLVMValueRef unboxingLlvmFunc = GetOrCreateLLVMFunction(node);
LLVMValueRef unboxedLlvmFunc = GetOrCreateLLVMFunction(node.GetUnderlyingMethodEntrypoint(factory));
Debug.Assert(unboxingLlvmFunc.ParamsCount == unboxedLlvmFunc.ParamsCount);
using LLVMBuilderRef builder = _module.Context.CreateBuilder();
builder.PositionAtEnd(unboxingLlvmFunc.AppendBasicBlock("SimpleUnboxingThunk"));
int argsCount = (int)unboxingLlvmFunc.ParamsCount;
Span<LLVMValueRef> args = argsCount > 100 ? new LLVMValueRef[argsCount] : stackalloc LLVMValueRef[argsCount];
for (int i = 0; i < args.Length; i++)
{
LLVMValueRef arg = unboxingLlvmFunc.GetParam((uint)i);
if (i == 1)
{
// Adjust "this" by the method table offset.
arg = CreateAddOffset(builder, arg, factory.Target.PointerSize, "dataThis");
}
args[i] = arg;
}
LLVMValueRef unboxedCall = CreateCall(builder, unboxedLlvmFunc, args);
if (unboxedCall.TypeOf != LLVMTypeRef.Void)
{
builder.BuildRet(unboxedCall);
}
else
{
builder.BuildRetVoid();
}
}
private void GetCodeForExternMethodAccessor(ExternMethodAccessorNode node)
{
// TODO-LLVM: use the Utf8 string directly here.
string externFuncName = node.ExternMethodName.ToString();
LLVMTypeRef externFuncType;
if (node.Signature != null)
{
static LLVMTypeRef GetLlvmType(TargetAbiType abiType) => abiType switch
{
TargetAbiType.Void => LLVMTypeRef.Void,
TargetAbiType.Int32 => LLVMTypeRef.Int32,
TargetAbiType.Int64 => LLVMTypeRef.Int64,
TargetAbiType.Float => LLVMTypeRef.Float,
TargetAbiType.Double => LLVMTypeRef.Double,
_ => throw new UnreachableException()
};
TargetAbiType[] sig = node.Signature;
LLVMTypeRef returnType = GetLlvmType(sig[0]);
LLVMTypeRef[] paramTypes = new LLVMTypeRef[sig.Length - 1];
for (int i = 0; i < paramTypes.Length; i++)
{
paramTypes[i] = GetLlvmType(sig[i + 1]);
}
externFuncType = LLVMTypeRef.CreateFunction(returnType, paramTypes);
}
else // Report the signature mismatch warning, use a placeholder signature.
{
string text = $"Signature mismatch detected: '{externFuncName}' will not be imported from the host environment";
foreach (MethodDesc method in node.EnumerateMethods())
{
text += $"\n Defined as: {method.Signature.ReturnType} {method}";
}
// Error code is just below the "AOT analysis" namespace.
_compilation.Logger.LogWarning(text, 3049, Path.GetFileNameWithoutExtension(_objectFilePath));
externFuncType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, Array.Empty<LLVMTypeRef>());
}
LLVMModuleRef externFuncModule = _moduleWithExternalFunctions;
Debug.Assert(externFuncModule.GetNamedFunction(externFuncName).Handle == IntPtr.Zero);
LLVMValueRef externFunc = externFuncModule.AddFunction(externFuncName, externFuncType);
// Add import attributes if specified.
if (_compilation.ConfigurableWasmImportPolicy.TryGetWasmModule(externFuncName, out string wasmModuleName))
{
externFunc.AddFunctionAttribute("wasm-import-name", externFuncName);
externFunc.AddFunctionAttribute("wasm-import-module", wasmModuleName);
}
// Define the accessor function.
string accessorFuncName = node.GetMangledName(_nodeFactory.NameMangler);
LLVMTypeRef accessorFuncType = LLVMTypeRef.CreateFunction(externFunc.TypeOf, Array.Empty<LLVMTypeRef>());
LLVMValueRef accessorFunc = externFuncModule.AddFunction(accessorFuncName, accessorFuncType);
using LLVMBuilderRef builder = LLVMBuilderRef.Create(externFuncModule.Context);
LLVMBasicBlockRef block = accessorFunc.AppendBasicBlock("GetExternFunc");
builder.PositionAtEnd(block);
builder.BuildRet(externFunc);
}
private LLVMValueRef OutputCodeForGetGenericDictionary(LLVMBuilderRef builder, LLVMValueRef context, ReadyToRunGenericHelperNode node)
{
LLVMValueRef dictionary;
if (node is ReadyToRunGenericLookupFromTypeNode)
{
// Locate the VTable slot that points to the dictionary
int vtableSlot = VirtualMethodSlotHelper.GetGenericDictionarySlot(_nodeFactory, (TypeDesc)node.DictionaryOwner);
// Load the dictionary pointer from the VTable
int pointerSize = _nodeFactory.Target.PointerSize;
int slotOffset = EETypeNode.GetVTableOffset(pointerSize) + (vtableSlot * pointerSize);
var slotAddr = CreateAddOffset(builder, context, slotOffset, "dictionarySlotAddr");
dictionary = builder.BuildLoad2(_ptrType, slotAddr, "dictionary");
}
else
{
dictionary = context;
}
return dictionary;
}
private LLVMValueRef OutputCodeForDictionaryLookup(LLVMBuilderRef builder, NodeFactory factory,
ReadyToRunGenericHelperNode node, GenericLookupResult lookup, LLVMValueRef dictionary)
{
// Find the generic dictionary slot
if (!factory.GenericDictionaryLayout(node.DictionaryOwner).TryGetSlotForEntry(lookup, out int dictionarySlot))
{
return LLVMValueRef.CreateConstPointerNull(_ptrType);
}
int offset = dictionarySlot * factory.Target.PointerSize;
// Load the generic dictionary cell
LLVMValueRef slotAddr = CreateAddOffset(builder, dictionary, offset, "dictionarySlotAddr");
LLVMValueRef lookupResult = builder.BuildLoad2(_ptrType, slotAddr, "slotValue");
if (node.HandlesInvalidEntries(factory))
{
LLVMBasicBlockRef currentBlock = builder.InsertBlock;
LLVMValueRef currentFunc = currentBlock.Parent;
LLVMBasicBlockRef slotNotAvailableBlock = currentFunc.AppendBasicBlock("DictionarySlotNotAvailable");
LLVMBasicBlockRef slotAvailableBlock = currentFunc.AppendBasicBlock("DictionarySlotAvailable");
slotAvailableBlock.MoveAfter(currentBlock);
LLVMValueRef nullValue = LLVMValueRef.CreateConstPointerNull(_ptrType);
LLVMValueRef isSlotNotAvailable = builder.BuildICmp(LLVMIntPredicate.LLVMIntEQ, lookupResult, nullValue, "isSlotNotAvailable");
builder.BuildCondBr(isSlotNotAvailable, slotNotAvailableBlock, slotAvailableBlock);
LLVMValueRef slotNotAvailableFunc = GetOrCreateLLVMFunction(ReadyToRunGenericHelperNode.GetBadSlotHelper(factory));
builder.PositionAtEnd(slotNotAvailableBlock);
CreateCall(builder, slotNotAvailableFunc, stackalloc[] { currentFunc.GetParam(0) });
CreateUnreachedAfterAlwaysThrowCall(builder);
builder.PositionAtEnd(slotAvailableBlock);
}
switch (lookup.LookupResultReferenceType(factory))
{
case GenericLookupResultReferenceType.Indirect:
lookupResult = builder.BuildLoad2(_ptrType, lookupResult, "actualSlotValue");
break;
case GenericLookupResultReferenceType.ConditionalIndirect:
throw new NotImplementedException();
default:
break;
}
return lookupResult;
}
private LLVMValueRef OutputCodeForVTableLookup(LLVMBuilderRef builder, LLVMValueRef objThis, MethodDesc method)
{
LLVMValueRef pMethodTable = builder.BuildLoad2(_ptrType, objThis, "pMethodTable");
int slot = VirtualMethodSlotHelper.GetVirtualMethodSlot(_nodeFactory, method, method.OwningType);
int slotOffset = EETypeNode.GetVTableOffset(_nodeFactory.Target.PointerSize) + (slot * _nodeFactory.Target.PointerSize);
LLVMValueRef pSlot = CreateAddOffset(builder, pMethodTable, slotOffset, "pSlot");
LLVMValueRef slotValue = builder.BuildLoad2(_ptrType, pSlot, "pTarget");
return slotValue;
}
private LLVMValueRef OutputCodeForGetCctorContext(LLVMBuilderRef builder, LLVMValueRef nonGcStaticBaseValue)
{
// We need to trigger the cctor before returning the base. It is stored at the beginning of the non-GC statics region.
int pContextOffset = -NonGCStaticsNode.GetClassConstructorContextSize(_nodeFactory.Target);
LLVMValueRef pContext = CreateAddOffset(builder, nonGcStaticBaseValue, pContextOffset, "pContext");
return pContext;
}
private void OutputCodeForTriggerCctor(LLVMBuilderRef builder, LLVMValueRef nonGcStaticBaseValue)
{
LLVMValueRef pContext = OutputCodeForGetCctorContext(builder, nonGcStaticBaseValue);
LLVMValueRef initialized = builder.BuildLoad2(_intPtrType, pContext, "initialized");
LLVMValueRef isInitialized = builder.BuildICmp(LLVMIntPredicate.LLVMIntEQ, initialized, CreateConst(_intPtrType, 0), "isInitialized");
LLVMValueRef func = builder.InsertBlock.Parent;
LLVMBasicBlockRef callHelperBlock = func.AppendBasicBlock("CallHelper");
LLVMBasicBlockRef returnBlock = func.AppendBasicBlock("Return");
builder.BuildCondBr(isInitialized, returnBlock, callHelperBlock);
builder.PositionAtEnd(callHelperBlock);
IMethodNode helperFuncNode = (IMethodNode)_nodeFactory.HelperEntrypoint(HelperEntrypoint.EnsureClassConstructorRunAndReturnNonGCStaticBase);
LLVMValueRef helperFunc = GetOrCreateLLVMFunction(helperFuncNode);
LLVMValueRef nonGcStaticBaseValueArg = builder.BuildPtrToInt(nonGcStaticBaseValue, _intPtrType);
Span<LLVMValueRef> helperCallArgs = stackalloc[] { func.GetParam(0), pContext, nonGcStaticBaseValueArg };
CreateCall(builder, helperFunc, helperCallArgs);
builder.BuildBr(returnBlock);
builder.PositionAtEnd(returnBlock);
}
private LLVMValueRef OutputCodeForGetThreadStaticBase(LLVMBuilderRef builder, LLVMValueRef pModuleDataSlot, LLVMValueRef nonGcStaticBaseValue)
{
LLVMValueRef shadowStack = builder.InsertBlock.Parent.GetParam(0);
// First arg: address of the TypeManager slot that provides the helper with information about
// module index and the type manager instance (which is used for initialization on first access).
LLVMValueRef pModuleData = builder.BuildLoad2(_ptrType, pModuleDataSlot, "pModuleData");
// Second arg: index of the type in the ThreadStatic section of the modules.
LLVMValueRef pTypeTlsIndex = CreateAddOffset(builder, pModuleDataSlot, _nodeFactory.Target.PointerSize, "pTypeTlsIndex");
LLVMValueRef typeTlsIndex = builder.BuildLoad2(LLVMTypeRef.Int32, pTypeTlsIndex, "typeTlsIndex");
HelperEntrypoint helper;
scoped Span<LLVMValueRef> getBaseHelperArgs;
if (nonGcStaticBaseValue.Handle != IntPtr.Zero)
{
LLVMValueRef pContext = OutputCodeForGetCctorContext(builder, nonGcStaticBaseValue);
helper = HelperEntrypoint.EnsureClassConstructorRunAndReturnThreadStaticBase;
getBaseHelperArgs = stackalloc[] { shadowStack, pModuleData, typeTlsIndex, pContext };
}
else
{
helper = HelperEntrypoint.GetThreadStaticBaseForType;
getBaseHelperArgs = stackalloc[] { shadowStack, pModuleData, typeTlsIndex };
}
IMethodNode getBaseHelperNode = (IMethodNode)_nodeFactory.HelperEntrypoint(helper);
LLVMValueRef getBaseHelperFunc = GetOrCreateLLVMFunction(getBaseHelperNode);
LLVMValueRef getBaseHelperCall = CreateCall(builder, getBaseHelperFunc, getBaseHelperArgs);
return getBaseHelperCall;
}
private LLVMValueRef GetOrCreateSymbol(ISymbolNode symbolRef)
{
LLVMValueRef symbolDefValue;
if (symbolRef is IMethodNode { Offset: 0 } methodNode)
{
symbolDefValue = GetOrCreateLLVMFunction(methodNode);
}
else if (symbolRef is ExternSymbolNode)
{
// We assume extenal symbol nodes are functions. This is rather fragile, but handling this precisely
// would require modifying producers of these nodes to provide more information (namely, the signature
// for function symbols).
//
using Utf8Name externFuncName = GetMangledUtf8Name(symbolRef);
LLVMValueRef externFunc = _module.GetNamedFunction(externFuncName);
if (externFunc.Handle == IntPtr.Zero)
{
LLVMTypeRef funcType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, Array.Empty<LLVMTypeRef>());
Debug.Assert(_module.GetNamedGlobal(externFuncName).Handle == IntPtr.Zero);