Skip to content

feat(ssa): Remove all non side effectual instructions from unreachable blocks during DIE#9238

Closed
vezenovm wants to merge 1 commit intomasterfrom
mv/remove-all-safe-instrs-unreachable-block
Closed

feat(ssa): Remove all non side effectual instructions from unreachable blocks during DIE#9238
vezenovm wants to merge 1 commit intomasterfrom
mv/remove-all-safe-instrs-unreachable-block

Conversation

@vezenovm
Copy link
Contributor

@vezenovm vezenovm commented Jul 17, 2025

Description

Problem*

This helps resolve a failure with regression_8994 in https://github.com/noir-lang/noir/actions/runs/16346960365/job/46184088603?pr=9232. I decided to pull it out as it should apply more generally.

No issue but I noticed after #9008 that we can be significantly more aggressive about remove instructions with Unreachable terminators.

Summary*

If we have a block that terminates with unreachable, we can remove all instructions in the block (not just those after the unreachable), if the instruciton:

  • Does not have side effects
  • Is not the trap that triggered the unreachable terminator
  • And does not produce results used in later instructions

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@vezenovm vezenovm requested a review from a team July 17, 2025 15:57
@vezenovm vezenovm changed the title feat(ssa): Remove all non side effectual instructions from unreachable blocks feat(ssa): Remove all non side effectual instructions from unreachable blocks during DIE Jul 17, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Execution Time'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 7e38d85 Previous: c7ce62a Ratio
rollup-root 0.005 s 0.004 s 1.25

This comment was automatically generated by workflow using github-action-benchmark.

CC: @TomAFrench

Comment on lines +1215 to +1217
v0 = allocate -> &mut u1
v2 = make_array [u1 0, v0] : [(u1, &mut u1); 1]
v4 = array_get v2, index u32 2 -> u1
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the current DIE pass (without the changes in this PR) already remove these as unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it would. I can update the test, but after #9232 none of these will be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am going to branch off of #9232 as this is mostly relevant for that case, and I'm not sure if this is the best solution anymore.

return false;
}

self.is_unused(instruction_id, function)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if we can find a way to refactor this so that self.is_unused isn't called twice (self.is_unused(...) || self.can_remove_with_unreachable_terminator(...) where the latter calls the former again).

I must be missing something, but when do we return from this construct anything but what is_unsed would have returned already? All we return is false from the early returns, and then end up in the same case as what the || already checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah good point, I'll refactor this

@vezenovm
Copy link
Contributor Author

I worked on this a little fast. After fixing the logic there are some other bugs I am getting with uninstantiated variables. Converting back to a draft for now.

@vezenovm vezenovm marked this pull request as draft July 17, 2025 17:08
@vezenovm vezenovm closed this Jul 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants