Skip to content

Commit 91d5d85

Browse files
jtschusterCopilot
andauthored
Add WasmRelocatableObjectWriter to ILCompiler.Compiler (dotnet#132428)
Adds a new WasmRelocatableObjectWriter class to ILCompiler to emit WASM modules that can be linked with the runtime WASM module. The implementation is very basic and doesn't fully implement all relocs, and creates the same imports as the WebCilObjectWriter. It does not yet emit .reloc custom wasm sections. Fixes style and code analysis warnings in WASM files that come from ILCompiler projects. This is unused until the PR stacked on top of this one. --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent eca7d33 commit 91d5d85

15 files changed

Lines changed: 353 additions & 116 deletions

File tree

‎src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using Internal.TypeSystem;
1313
using Internal.TypeSystem.Ecma;
14+
using Internal.TypeSystem.Interop;
1415

1516
namespace ILCompiler
1617
{
@@ -365,6 +366,37 @@ private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peR
365366

366367
return reader;
367368
}
369+
370+
/// <summary>
371+
/// Managed implementation of CEEInfo::getClassAlignmentRequirementStatic
372+
/// </summary>
373+
public static int GetClassAlignmentRequirementStatic(DefType type)
374+
{
375+
int alignment = type.Context.Target.PointerSize;
376+
377+
if (type is MetadataType metadataType && !metadataType.IsAutoLayout)
378+
{
379+
if (metadataType.IsSequentialLayout ||
380+
MarshalUtils.IsBlittableType(metadataType))
381+
{
382+
alignment = metadataType.InstanceFieldAlignment.AsInt;
383+
}
384+
}
385+
if (type.Context.Target.SupportsAlign8 &&
386+
alignment < 8 && type.RequiresAlign8())
387+
{
388+
// If the structure contains 64-bit primitive fields and the platform requires 8-byte alignment for
389+
// such fields then make sure we return at least 8-byte alignment. Note that it's technically possible
390+
// to create unmanaged APIs that take unaligned structures containing such fields and this
391+
// unconditional alignment bump would cause us to get the calling convention wrong on platforms such
392+
// as ARM. If we see such cases in the future we'd need to add another control (such as an alignment
393+
// property for the StructLayout attribute or a marshaling directive attribute for p/invoke arguments)
394+
// that allows more precise control. For now we'll go with the likely scenario.
395+
alignment = 8;
396+
}
397+
398+
return alignment;
399+
}
368400
}
369401

370402
/// <summary>

‎src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -706,23 +706,6 @@ private void EmitChecksums(Stream outputFileStream, List<ChecksumsToCalculate> c
706706

707707
partial void EmitUnwindInfoForNode(ObjectNode node, Utf8String currentSymbolName, SectionWriter sectionWriter);
708708

709-
public static void EmitObject(string objectFilePath, IReadOnlyCollection<DependencyNode> nodes, NodeFactory factory, ObjectWritingOptions options, IObjectDumper dumper, Logger logger)
710-
{
711-
var stopwatch = Stopwatch.StartNew();
712-
713-
ObjectWriter objectWriter =
714-
factory.Target.IsApplePlatform ? new MachObjectWriter(factory, options) :
715-
factory.Target.OperatingSystem == TargetOS.Windows ? new CoffObjectWriter(factory, options) :
716-
new ElfObjectWriter(factory, options);
717-
718-
using Stream outputFileStream = new FileStream(objectFilePath, FileMode.Create);
719-
objectWriter.EmitObject(outputFileStream, nodes, dumper, logger);
720-
721-
stopwatch.Stop();
722-
if (logger.IsVerbose)
723-
logger.LogMessage($"Done writing object file in {stopwatch.Elapsed}");
724-
}
725-
726709
protected static ReadOnlySpan<byte> FormatUtf8Int(Span<byte> buffer, int number)
727710
{
728711
bool b = number.TryFormat(buffer, out int bytesWritten);

‎src/coreclr/tools/Common/Compiler/ObjectWriter/Wasm/WasmSection.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ILCompiler.ObjectWriter
1212
/// <summary>
1313
/// Interface for section-like objects that can be emitted to a wasm module stream.
1414
/// </summary>
15-
interface IWasmEmittable
15+
internal interface IWasmEmittable
1616
{
1717
int EmitToStream(Stream outputFileStream);
1818
int EncodeSize();
@@ -21,7 +21,7 @@ interface IWasmEmittable
2121
/// <summary>
2222
/// Interface for types that represent a WebAssembly section.
2323
/// </summary>
24-
interface IWasmSection : IWasmEmittable
24+
internal interface IWasmSection : IWasmEmittable
2525
{
2626
WasmSectionType Type { get; }
2727
}
@@ -140,7 +140,7 @@ protected WasmVectorSection(WasmSectionType type, Stream stream, Utf8String name
140140
// the WasmSection abstraction.
141141
internal sealed class WasmExternallyCountedSection : WasmVectorSection
142142
{
143-
private bool _entryCountSet = false;
143+
private bool _entryCountSet;
144144

145145
public WasmExternallyCountedSection(WasmSectionType type, Stream stream, Utf8String name, int sectionIndex)
146146
: base(type, stream, name, sectionIndex)

0 commit comments

Comments
 (0)