-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: WasmImportLinkageAttribute
to control wasm module names
#93824
Comments
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsBackground and motivationThis PR proposes a new attribute to be used to control the Wasm import module and function names. In Wasm when a function is imported it can be specified as coming from a named module using this WAT
The first use of this would be in the experimental NativeAOT-LLVM backend, but the desire is to have something that works for Mono and NativeAOT-LLVM. Something that we can use to proceed with Wasm Components and WIT binding via source gen for both Mono and NativeAOT-LLVM.
Please see dotnet/runtimelab#2414 for how we got here and in particular, dotnet/runtimelab#2414 (comment). Other issues and PRs of note on this subject: dotnet/runtimelab#1390 API Proposalnamespace System.Runtime.InteropServices;
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class WasmImportAttribute : Attribute
{
public WasmImportAttribute() { }
} API Usage[WasmImport]
[DllImport("library")]
static extern void Func(); Alternative DesignsIt has been noted (dotnet/runtimelab#2414 (comment)) that
RisksNo response
|
/cc @yowl |
/cc @lambdageek @pavelsavara @dotnet/nativeaot-llvm |
My preference would be |
2 thoughts: If (as mentioned in the runtimelab issue and comments) this attribute is more similar to DllImportSearchPathsAttrbute, would we want to consider a form that could be applied at a type/module/assembly level as well where the module name is specified in the attribute? Not really API Review related, but would we want to have LibraryImport automatically propagate this attribute to the inner DllImport? My initial thought is yes, but just wanted to cover my bases. |
We don't have a concrete need for that right now, I suppose. One of the uses for the attribute would be in generated source code, where I don't think the assembly-wide version could be used due to its potential impact on other PI declarations.
Correct, this would need to be propagated. I don't have preferences on naming aside from "WASM import" staying as that terminology is used by the WASM specification and related tooling. |
I do not think we need this. Similar interop attributes (e.g. |
I agree it is one of the better suggestions. |
+1 |
Unless there is any other discussion or thoughts, I am going to move this to ready for approval? Any concerns? |
Semantics LGTM. Are we unified on the name - (My preference is towards the latter) |
Same. I updated the proposal and bolded this alternative. I will be in the API review and push in that direction unless there is concern here. Also I am happy to update the proposal after I hear from @yowl. |
All good here, |
WasmImportAttribute
to control wasm module namesWasmImportLinkageAttribute
to control wasm module names
namespace System.Runtime.InteropServices;
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class WasmImportLinkageAttribute : Attribute
{
public WasmImportLinkageAttribute();
} |
Unresolved WASM imports in a binary will lead to a fail fast on startup on some WASM hosts. It means that a dynamically unreachable but unresolvable (say, because it is for a different platform) PInvoke in some NuGet library would lead to a fail fast by default. On the other hand, PNSEs are lazy, and so do not have this problem. That is the essential difference between |
So this is dependent on what WASM host is being used? Can you share an example of each? |
Correct.
|
Also, the P/Invokes can be ambiguous. We can have situations when there is both native library and wasm module of the same name. We would not be able to tell whether the PInvoke is trying to call the native library or the wasm module. |
On the actual API for a sec, concerns about future linkage types arising do seem pertinent and I wonder whether it's just better to have e.g. |
Background and motivation
This PR proposes a new attribute to be used to control the Wasm import module and function names. In Wasm when a function is imported it can be specified as coming from a named module using this WAT
The first use of this would be in the experimental NativeAOT-LLVM backend, but the desire is to have something that works for Mono and NativeAOT-LLVM. Something that we can use to proceed with Wasm Components and WIT binding via source gen for both Mono and NativeAOT-LLVM.
DllImportAttribute
has aValue
andEntryPoint
which can be used to define the names, but to preserve existing semantics including static linking forDllImport("*")
andDirectPInvoke
we cannot simply say that we should always create a Wasm import if theDllImport
Value
is not*
. The presence of this newWasmImportLinkage
will distinguish these use cases and the attribute has no properties.Please see dotnet/runtimelab#2414 for how we got here and in particular, dotnet/runtimelab#2414 (comment).
Other issues and PRs of note on this subject:
dotnet/runtimelab#1390
dotnet/runtimelab#1845
dotnet/runtimelab#2410
dotnet/runtimelab#2383
Proposed PR #93823
API Proposal
API Usage
Alternative Designs
It has been noted (dotnet/runtimelab#2414 (comment)) that the original
WasmImport
suggestion was a potentially confusing name as theImport
suffix can infer it's use should be as an alternative toDllImport
but this is not the case, so some other alternatives that I could think of:WasmDynamicLinkAttribute
WasmImportLinkageAttribute
- Updated this proposalWasmModuleLinkAttribute
LlvmWasmImportAttribute
(not good for Mono interpreter perhaps)WasmNamedModuleAttribute
Risks
No response
The text was updated successfully, but these errors were encountered: