Skip to content

Commit

Permalink
[NativeAOT-LLVM] Migrate the last LLVM helper to WASM and delete `LLV…
Browse files Browse the repository at this point in the history
…MObjectWriter` (#2655)

* Migrate external accessors to raw WASM

* Delete all LLVM interop from ILC

* Comment fix
  • Loading branch information
SingleAccretion authored Aug 20, 2024
1 parent d04118c commit 87eee57
Show file tree
Hide file tree
Showing 25 changed files with 315 additions and 1,373 deletions.
392 changes: 0 additions & 392 deletions src/coreclr/jit/llvm.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly)
}

#if !READYTORUN
public abstract Wasm.WasmFunctionType GetWasmFunctionType(NodeFactory factory);
protected abstract void EmitCode(NodeFactory factory, ref Wasm.WasmEmitter instructionEncoder, bool relocsOnly);
#endif

protected abstract void EmitCode(NodeFactory factory, ref X64.X64Emitter instructionEncoder, bool relocsOnly);
protected abstract void EmitCode(NodeFactory factory, ref X86.X86Emitter instructionEncoder, bool relocsOnly);
protected abstract void EmitCode(NodeFactory factory, ref ARM.ARMEmitter instructionEncoder, bool relocsOnly);
Expand Down
65 changes: 1 addition & 64 deletions src/coreclr/tools/Common/JitInterface/CorJitApiId.Shared.cspp
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,7 @@ public
#endif
enum CorJitApiId
{
CJAI_LLVMInterop_LLVMContext_Create,

CJAI_LLVMInterop_LLVMModule_Create,
CJAI_LLVMInterop_LLVMModule_GetNamedAlias,
CJAI_LLVMInterop_LLVMModule_GetNamedFunction,
CJAI_LLVMInterop_LLVMModule_GetNamedGlobal,
CJAI_LLVMInterop_LLVMModule_AddAlias,
CJAI_LLVMInterop_LLVMModule_AddFunction,
CJAI_LLVMInterop_LLVMModule_AddGlobal,
CJAI_LLVMInterop_LLVMModule_PrintToFile,
CJAI_LLVMInterop_LLVMModule_Verify,
CJAI_LLVMInterop_LLVMModule_WriteBitcodeToFile,

CJAI_LLVMInterop_LLVMType_GetContext,
CJAI_LLVMInterop_LLVMType_GetReturnType,
CJAI_LLVMInterop_LLVMType_GetParamTypes,
CJAI_LLVMInterop_LLVMType_GetPointer,
CJAI_LLVMInterop_LLVMType_GetInt,
CJAI_LLVMInterop_LLVMType_GetFloat,
CJAI_LLVMInterop_LLVMType_GetDouble,
CJAI_LLVMInterop_LLVMType_GetVoid,
CJAI_LLVMInterop_LLVMType_CreateFunction,
CJAI_LLVMInterop_LLVMType_CreateStruct,
CJAI_LLVMInterop_LLVMType_CreateArray,

CJAI_LLVMInterop_LLVMValue_TypeOf,
CJAI_LLVMInterop_LLVMValue_AppendBasicBlock,
CJAI_LLVMInterop_LLVMValue_AddAttributeAtIndex,
CJAI_LLVMInterop_LLVMValue_GetParam,
CJAI_LLVMInterop_LLVMValue_GetParamCount,
CJAI_LLVMInterop_LLVMValue_GetValueType,
CJAI_LLVMInterop_LLVMValue_SetAlignment,
CJAI_LLVMInterop_LLVMValue_SetSection,
CJAI_LLVMInterop_LLVMValue_SetLinkage,
CJAI_LLVMInterop_LLVMValue_SetAliasee,
CJAI_LLVMInterop_LLVMValue_CreateConstNull,
CJAI_LLVMInterop_LLVMValue_CreateConstInt,
CJAI_LLVMInterop_LLVMValue_CreateConstIntToPtr,
CJAI_LLVMInterop_LLVMValue_CreateConstGEP,
CJAI_LLVMInterop_LLVMValue_CreateConstStruct,
CJAI_LLVMInterop_LLVMValue_CreateConstArray,

CJAI_LLVMInterop_LLVMBasicBlock_GetParent,
CJAI_LLVMInterop_LLVMBasicBlock_MoveAfter,

CJAI_LLVMInterop_LLVMBuilder_Create,
CJAI_LLVMInterop_LLVMBuilder_GetInsertBlock,
CJAI_LLVMInterop_LLVMBuilder_BuildICmp,
CJAI_LLVMInterop_LLVMBuilder_BuildCondBr,
CJAI_LLVMInterop_LLVMBuilder_BuildBr,
CJAI_LLVMInterop_LLVMBuilder_BuildGEP,
CJAI_LLVMInterop_LLVMBuilder_BuildPtrToInt,
CJAI_LLVMInterop_LLVMBuilder_BuildIntToPtr,
CJAI_LLVMInterop_LLVMBuilder_BuildPointerCast,
CJAI_LLVMInterop_LLVMBuilder_BuildCall,
CJAI_LLVMInterop_LLVMBuilder_BuildLoad,
CJAI_LLVMInterop_LLVMBuilder_BuildRet,
CJAI_LLVMInterop_LLVMBuilder_BuildUnreachable,
CJAI_LLVMInterop_LLVMBuilder_PositionAtEnd,
CJAI_LLVMInterop_LLVMBuilder_Dispose,

CJAI_LLVMInterop_LLVMAttribute_Create,

CJAI_StartSingleThreadedCompilation,
CJAI_FinishSingleThreadedCompilation,
CJAI_Count,
CJAI_Count
};
30 changes: 0 additions & 30 deletions src/coreclr/tools/Common/JitInterface/LLVMInterop.Shared.cspp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ public abstract class PInvokeILEmitterConfiguration
{
public abstract bool GenerateDirectCall(MethodDesc method, out string externName);

public virtual bool GenerateWasmImportCall(MethodDesc method, out string externName, out string moduleName)
{
throw new NotImplementedException();
}
public virtual bool GenerateWasmImportCall(MethodDesc method, out string externName, out string moduleName) => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace ILCompiler.DependencyAnalysis.Wasm
{
public interface IWasmFunctionNode
{
public WasmFunctionType GetWasmFunctionType(NodeFactory factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ WasmValueType GetWasmArgTypeForArg(TypeDesc argSigType)
maxWasmSigLength > 100 ? new WasmValueType[maxWasmSigLength] : stackalloc WasmValueType[maxWasmSigLength];

int index = 0;
signatureTypes[index++] = wasmPointerType;
signatureTypes[index++] = wasmPointerType; // Shadow stack.

if (!signature.IsStatic) // TODO-LLVM-Bug: doesn't handle explicit 'this'.
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override int GetHashCode()
return hash.ToHashCode();
}

public static bool IsFunction(ISymbolNode symbol) => symbol is ExternSymbolNode or AssemblyStubNode or IMethodNode { Offset: 0 };
public static bool IsFunction(ISymbolNode symbol) => symbol is ExternSymbolNode or IWasmFunctionNode or IMethodNode { Offset: 0 };

public static bool operator ==(WasmFunctionType left, WasmFunctionType right) => left.Equals(right);
public static bool operator !=(WasmFunctionType left, WasmFunctionType right) => !(left == right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,5 @@ protected override void EmitCode(NodeFactory factory, ref WasmEmitter encoder, b
// This node is currently not used on WASM.
throw new NotImplementedException();
}

public override WasmFunctionType GetWasmFunctionType(NodeFactory factory)
{
// This node is currently not used on WASM.
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,35 @@

namespace ILCompiler.DependencyAnalysis
{
public partial class ReadyToRunGenericHelperNode
public partial class ReadyToRunGenericHelperNode : IWasmFunctionNode
{
public WasmFunctionType GetWasmFunctionType(NodeFactory factory)
{
WasmValueType wasmPointerType = WasmAbi.GetNaturalIntType(factory.Target);
switch (_id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
case ReadyToRunHelperId.GetGCStaticBase:
case ReadyToRunHelperId.GetThreadStaticBase:
case ReadyToRunHelperId.TypeHandle:
case ReadyToRunHelperId.MethodHandle:
case ReadyToRunHelperId.FieldHandle:
case ReadyToRunHelperId.MethodDictionary:
case ReadyToRunHelperId.MethodEntry:
case ReadyToRunHelperId.VirtualDispatchCell:
case ReadyToRunHelperId.DefaultConstructor:
case ReadyToRunHelperId.ObjectAllocator:
case ReadyToRunHelperId.TypeHandleForCasting:
case ReadyToRunHelperId.ConstrainedDirectCall:
return new WasmFunctionType(wasmPointerType, [wasmPointerType, wasmPointerType]);
case ReadyToRunHelperId.DelegateCtor:
// (Shadow stack, this, targetObj, GenericContext).
return new WasmFunctionType(WasmValueType.Invalid, [wasmPointerType, wasmPointerType, wasmPointerType, wasmPointerType]);
default:
throw new NotImplementedException();
}
}

protected sealed override unsafe void EmitCode(NodeFactory factory, ref WasmEmitter encoder, bool relocsOnly)
{
uint InitializeLocals(uint argCount, uint* pHandlesInvalidEntriesLocal, uint* pNonGcStaticBaseLocal = null, uint* pCctorContextLocal = null)
Expand Down Expand Up @@ -159,33 +186,6 @@ uint InitializeLocals(uint argCount, uint* pHandlesInvalidEntriesLocal, uint* pN
}
}

public override WasmFunctionType GetWasmFunctionType(NodeFactory factory)
{
WasmValueType wasmPointerType = WasmAbi.GetNaturalIntType(factory.Target);
switch (_id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
case ReadyToRunHelperId.GetGCStaticBase:
case ReadyToRunHelperId.GetThreadStaticBase:
case ReadyToRunHelperId.TypeHandle:
case ReadyToRunHelperId.MethodHandle:
case ReadyToRunHelperId.FieldHandle:
case ReadyToRunHelperId.MethodDictionary:
case ReadyToRunHelperId.MethodEntry:
case ReadyToRunHelperId.VirtualDispatchCell:
case ReadyToRunHelperId.DefaultConstructor:
case ReadyToRunHelperId.ObjectAllocator:
case ReadyToRunHelperId.TypeHandleForCasting:
case ReadyToRunHelperId.ConstrainedDirectCall:
return new WasmFunctionType(wasmPointerType, [wasmPointerType, wasmPointerType]);
case ReadyToRunHelperId.DelegateCtor:
// (Shadow stack, this, targetObj, GenericContext).
return new WasmFunctionType(WasmValueType.Invalid, [wasmPointerType, wasmPointerType, wasmPointerType, wasmPointerType]);
default:
throw new NotImplementedException();
}
}

protected virtual void EmitLoadGenericContext(NodeFactory factory, ref WasmEmitter encoder, uint contextLocal, bool saveIntoContextLocal, bool relocsOnly)
{
encoder.EmitLocalGet(contextLocal); // The context local is already a dictionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,27 @@

namespace ILCompiler.DependencyAnalysis
{
public partial class ReadyToRunHelperNode
public partial class ReadyToRunHelperNode : IWasmFunctionNode
{
public WasmFunctionType GetWasmFunctionType(NodeFactory factory)
{
WasmValueType wasmPointerType = WasmAbi.GetNaturalIntType(factory.Target);
switch (_id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
case ReadyToRunHelperId.GetThreadStaticBase:
case ReadyToRunHelperId.GetGCStaticBase:
return new WasmFunctionType(wasmPointerType, [wasmPointerType]);
case ReadyToRunHelperId.DelegateCtor:
// (Shadow stack, this, targetObj).
return new WasmFunctionType(WasmValueType.Invalid, [wasmPointerType, wasmPointerType, wasmPointerType]);
case ReadyToRunHelperId.ResolveVirtualFunction:
return new WasmFunctionType(wasmPointerType, [wasmPointerType, wasmPointerType]);
default:
throw new NotImplementedException();
}
}

protected override void EmitCode(NodeFactory factory, ref WasmEmitter encoder, bool relocsOnly)
{
WasmValueType wasmPointerType = encoder.GetNaturalIntType();
Expand Down Expand Up @@ -160,25 +179,6 @@ protected override void EmitCode(NodeFactory factory, ref WasmEmitter encoder, b
}
}

public override WasmFunctionType GetWasmFunctionType(NodeFactory factory)
{
WasmValueType wasmPointerType = WasmAbi.GetNaturalIntType(factory.Target);
switch (_id)
{
case ReadyToRunHelperId.GetNonGCStaticBase:
case ReadyToRunHelperId.GetThreadStaticBase:
case ReadyToRunHelperId.GetGCStaticBase:
return new WasmFunctionType(wasmPointerType, [wasmPointerType]);
case ReadyToRunHelperId.DelegateCtor:
// (Shadow stack, this, targetObj).
return new WasmFunctionType(WasmValueType.Invalid, [wasmPointerType, wasmPointerType, wasmPointerType]);
case ReadyToRunHelperId.ResolveVirtualFunction:
return new WasmFunctionType(wasmPointerType, [wasmPointerType, wasmPointerType]);
default:
throw new NotImplementedException();
}
}

private static void EmitVTableLookup(NodeFactory factory, ref WasmEmitter encoder, uint thisObjLocal, MethodDesc method)
{
Debug.Assert(!method.CanMethodBeInSealedVTable(factory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ protected override void EmitCode(NodeFactory factory, ref WasmEmitter encoder, b
Debug.Assert(calleeReturnType == WasmAbi.GetWasmReturnType(target.Method, out _));
encoder.EmitReturnAfterAlwaysThrowCall(callerReturnType, calleeReturnType, isEnd: true);
}

public override WasmFunctionType GetWasmFunctionType(NodeFactory factory) => WasmAbi.GetWasmFunctionType(Method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ protected override void EmitCode(NodeFactory factory, ref WasmEmitter encoder, b
encoder.EmitCall(target);
encoder.EmitEnd();
}

public override WasmFunctionType GetWasmFunctionType(NodeFactory factory) => WasmAbi.GetWasmFunctionType(Method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@
<Compile Include="Compiler\DependencyAnalysis\StructMarshallingDataNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_ARM64\ARM64TentativeMethodNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_ARM\ARMTentativeMethodNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_Wasm\IWasmFunctionNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_Wasm\WasmAbi.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_Wasm\WasmValueType.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_Wasm\WasmFunctionType.cs" />
Expand Down

This file was deleted.

Loading

0 comments on commit 87eee57

Please sign in to comment.