[mono][interp] Fix broken code attempting to reapply superinstruction optimization#116069
Conversation
… optimization For each instruction in a basic block we check for patterns. In a certain case, once we replaced the instruction with a new one, we were attempting to do a loop reiteration by setting `ins = ins->prev` so after the loop reincrements with `ins = ins->next` we check super instruction patterns again for the current instruction. This is broken if `ins` was the first instruction in a basic block, aka `ins->prev` is NULL. This used to be impossible before SSA optimizations, since super instruction pass was applying optimizations in a single basic block only.
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the superinstruction reprocessing logic by eliminating an unsafe pointer dereference when inspecting the previous instruction.
- Removed the use of ins = ins->prev to avoid null pointer access
- Introduced a goto retry_ins label to re-read the opcode of the current instruction
| @@ -3804,9 +3806,7 @@ interp_super_instructions (TransformData *td) | |||
| g_print ("superins: "); | |||
| interp_dump_ins (ins, td->data_items); | |||
| } | |||
There was a problem hiding this comment.
Replacing 'ins = ins->prev' with 'goto retry_ins' avoids a potential null pointer dereference, but consider adding a comment to clarify the control flow and ensure readability for future maintainers.
| } | |
| } | |
| // Retry processing the current instruction after modifying its opcode and operands. |
|
Tagging subscribers to this area: @BrzVlad, @kotlarmilos |
| int opcode; | ||
| if (bb->dfs_index >= td->bblocks_count_no_eh || bb->dfs_index == -1 || (ins->flags & INTERP_INST_FLAG_LIVENESS_MARKER)) | ||
| current_liveness.ins_index++; | ||
| retry_ins: | ||
| opcode = ins->opcode; |
There was a problem hiding this comment.
| int opcode; | |
| if (bb->dfs_index >= td->bblocks_count_no_eh || bb->dfs_index == -1 || (ins->flags & INTERP_INST_FLAG_LIVENESS_MARKER)) | |
| current_liveness.ins_index++; | |
| retry_ins: | |
| opcode = ins->opcode; | |
| if (bb->dfs_index >= td->bblocks_count_no_eh || bb->dfs_index == -1 || (ins->flags & INTERP_INST_FLAG_LIVENESS_MARKER)) | |
| current_liveness.ins_index++; | |
| retry_ins: | |
| int opcode = ins->opcode; |
There was a problem hiding this comment.
that would lead to compilation failures because opcode needs to be declared before the label.
|
so could you backport to release/v9.0 ? |
… optimization (dotnet#116069) For each instruction in a basic block we check for patterns. In a certain case, once we replaced the instruction with a new one, we were attempting to do a loop reiteration by setting `ins = ins->prev` so after the loop reincrements with `ins = ins->next` we check super instruction patterns again for the current instruction. This is broken if `ins` was the first instruction in a basic block, aka `ins->prev` is NULL. This used to be impossible before SSA optimizations, since super instruction pass was applying optimizations in a single basic block only.
* [mono][interp] Add possibility to configure interp options from env var * [mono][interp] Update var definition when inserting new instructions during cprop (#116179) The definition was not updated, leading to invalid optimizations later on. * [mono][interp] Fix broken code attempting to reapply superinstruction optimization (#116069) For each instruction in a basic block we check for patterns. In a certain case, once we replaced the instruction with a new one, we were attempting to do a loop reiteration by setting `ins = ins->prev` so after the loop reincrements with `ins = ins->next` we check super instruction patterns again for the current instruction. This is broken if `ins` was the first instruction in a basic block, aka `ins->prev` is NULL. This used to be impossible before SSA optimizations, since super instruction pass was applying optimizations in a single basic block only.
For each instruction in a basic block we check for patterns. In a certain case, once we replaced the instruction with a new one, we were attempting to do a loop reiteration by setting
ins = ins->prevso after the loop reincrements withins = ins->nextwe check super instruction patterns again for the current instruction. This is broken ifinswas the first instruction in a basic block, akains->previs NULL. This used to be impossible before SSA optimizations, since super instruction pass was applying optimizations in a single basic block only.Bug reported in xoofx/markdig#873