Skip to content

[Wasm RyuJIT] Fixes for two codegen asserts#126514

Open
kg wants to merge 10 commits intodotnet:mainfrom
kg:wasm-emit-nyi
Open

[Wasm RyuJIT] Fixes for two codegen asserts#126514
kg wants to merge 10 commits intodotnet:mainfrom
kg:wasm-emit-nyi

Conversation

@kg
Copy link
Copy Markdown
Member

@kg kg commented Apr 3, 2026

Fixes BitConverter.ToBFloat16 and Vector`1.op_Multiply

@kg kg added the arch-wasm WebAssembly architecture label Apr 3, 2026
Copilot AI review requested due to automatic review settings April 3, 2026 20:14
@kg kg added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 3, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes two WebAssembly RyuJIT codegen assertion failures by handling edge cases around struct locals whose effective “register type” is not directly representable as TYP_STRUCT in wasm codegen.

Changes:

  • Ignore leftover GT_LCL_VAR nodes whose computed register type is TYP_UNDEF when they are marked as IsUnusedValue() (e.g., after block-store pruning in LIR).
  • Ensure GT_STORE_LCL_VAR for enregistered struct locals uses the struct layout’s register type when determining the local.set operand size.

@kg kg marked this pull request as ready for review April 3, 2026 21:49
@kg
Copy link
Copy Markdown
Member Author

kg commented Apr 3, 2026

@dotnet/jit-contrib PTAL

Copilot AI review requested due to automatic review settings April 3, 2026 23:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

}
}
LclVarDsc& varDsc = m_compiler->lvaTable[lclAddr->AsLclVarCommon()->GetLclNum()];
isDeadStore = varDsc.lvTracked && !VarSetOps::IsMember(m_compiler, life, varDsc.lvVarIndex);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakobbotsch Do you have thoughts on how to do this correctly? Ideally we would call ComputeLifeLocal here to get the exact same true/false value as the old analysis, but doing that will introduce liveness tracking bugs because ComputeLifeLocal will get called again later on the same node. Should I rework ComputeLifeLocal so it has a read-only mode that can be used here?

For more context the problem is that on wasm we get the LIR order LCL_ADDR, LCL_VAR, STORE_BLK while other targets have LCL_VAR, LCL_ADDR, STORE_BLK. This means that the existing approach of pruning the block store from inside of the lcl_addr doesn't work anymore - it leaves a stray lcl_var behind.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it leaves a stray lcl_var behind.

Why is that a problem? As a principle the backends should handle unused values whether or not they get removed by this liveness pass.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lcl_var is a struct, so we have no way to push it onto the evaluation stack. I think Andy said that other backends don't like this either (stray lcl_vars that don't fit into a register)

Copy link
Copy Markdown
Member

@jakobbotsch jakobbotsch Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like WASM is not marking the LCL_VAR as contained then. I think other backends will do that in lowering for STORE_BLK and I would expect that to make their respective codegens handle the stray LCL_VAR fine.

Copy link
Copy Markdown
Contributor

@SingleAccretion SingleAccretion Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like WASM is not marking the LCL_VAR as contained then. I think other backends will do that in lowering for STORE_BLK and I would expect that to make their respective codegens handle the stray LCL_VAR fine.

We don't have special handling for stay unused TYP_STRUCT locals in codegen (see e. g.

GetEmitter()->emitIns_R_S(ins_Load(type, m_compiler->isSIMDTypeLocalAligned(tree->GetLclNum())),
).

I don't think it would be especially useful to make that (TYP_STRUCT local legal if unused) a new contract, since it won't really solve the problem with WASM where we can't get away with treating LCL_ADDR as the location of the def.

For me the question here is what do we need to do to support async's use of liveness, since for LIR purposes it would be sufficient to only handle assorted 'blessed' IR shapes for indirect stores to tracked (or promoted) locals.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for diving into this, you two. I think what I'll do is make sure we handle unused nodes properly in codegen + teach liveness to remove the stray LCL_VAR when it removes the STORE_BLK.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remove the stray lcl_var trivially without breaking liveness, will keep digging into it but I'd kind of prefer to just move on and fix that part later, if that's okay with you two?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a way to do this that doesn't break anything, updated the PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like an "ad-hoc" kind of limitation if we phrase it the they way we'be been looking at it here. I think it's justifiable if we consider it to just be a subset of the "no TYP_STRUCT values allowed in IR post-lower" (modulo the multi-reg special case) rule that has deeper reasons for existing than just codegen not having some ifs.

My general opinion is that it should be possible to remove IR with BlockRange().Remove(node, true), without having to depend on some optional pass (like liveness) to come in and clean up after you. Otherwise we are making it too hard to manipulate our own IR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My general opinion is that it should be possible to remove IR with BlockRange().Remove(node, true), without having to depend on some optional pass (like liveness) to come in and clean up after you. Otherwise we are making it too hard to manipulate our own IR.

It is a tradeoff, isn't it. Without first-class struct values after lowering, you either get this restriction we have with struct values today, or you need to contort codegen to support these struct values, with the assumption they're unused. The latter seems like a muddier contract to me.

There is a somewhat separate question of whether it is a good restriction to have before lowering. I can see it not being the case and lowering being responsible for transforming such nodes (as it already is partially today).

Copilot AI review requested due to automatic review settings April 8, 2026 23:18
@kg kg force-pushed the wasm-emit-nyi branch from 2126523 to 50cde09 Compare April 8, 2026 23:18
@kg kg marked this pull request as ready for review April 8, 2026 23:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

{
Lowering::TransformUnusedIndirection(data->AsIndir(), m_compiler, block);
}
else if (data == mostRecentLocalVarOrField)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use LIR::LastNode here and remove mostRecentLocalVarOrField to make this work robustly.

Copy link
Copy Markdown
Member

@jakobbotsch jakobbotsch Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am surprised you need this at all. What was the problem with just removing stray local nodes as the data unconditionally?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would explode when visiting the removed node a second time and trying to remove it a second time. I worked around that by preventing it from removing it twice, but then got random crashes in the x64 emitter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds odd. How did we end up visiting the removed node? We shouldn't see it since it has been removed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't make sense to me either. I checked and its gtPrev and gtNext were both nullptr since it had been removed, so I think removing it was leaving a dangling reference elsewhere somehow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use LIR::LastNode here and remove mostRecentLocalVarOrField to make this work robustly.

I've been looking at this and it's not clear how I would substitute LIR::LastNode for my variable here. Is it guaranteed that the range will only contain the store and its dependencies?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't make sense to me either. I checked and its gtPrev and gtNext were both nullptr since it had been removed, so I think removing it was leaving a dangling reference elsewhere somehow?

I looked over the code again and realized it's because ComputeLifeLIR does its iteration custom so it was iterating over dead nodes. This seems like a defect in how it's designed and could potentially happen for any node we remove inside of this method. I found a workaround, so let me know if you think what I did is acceptable. I'm willing to rework the method to not be vulnerable to this.

Copilot AI review requested due to automatic review settings April 10, 2026 20:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +2479 to +2481
next = data->gtPrev;
if (end == data)
end = data->gtNext;
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end == data check looks unreachable here: end is initialized to firstNode->gtPrev (a node outside the block’s range), while data is a node inside the range. Removing this branch (and adding braces for the two if statements for consistency with surrounding code) would reduce confusion and make the iterator-adjustment logic clearer.

Suggested change
next = data->gtPrev;
if (end == data)
end = data->gtNext;
{
next = data->gtPrev;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants