Skip to content

Conversation

@ds5678
Copy link
Contributor

@ds5678 ds5678 commented May 6, 2025

Problem

#3468

@ds5678 ds5678 marked this pull request as draft May 6, 2025 05:19
@ds5678 ds5678 mentioned this pull request May 6, 2025
@ds5678 ds5678 force-pushed the preincrement-attempt branch from d511e76 to f72319e Compare July 31, 2025 23:14
@ds5678
Copy link
Contributor Author

ds5678 commented Aug 1, 2025

Hi @siegfriedpammer, I'm running into a small problem with this pull request. My tests pass, except for Roslyn 1.3, which compiles the pre-increment like this:

{Block IL_0000 (incoming: 1) {
	stloc V_0(binary.add.i4(ldloc value, ldc.i4 1))
	stloc value(ldloc V_0)
	leave IL_0000 (call ToString(ldloca V_0))
} at IL_0000}

I want to write a separate transform to make the IL into this:

{Block IL_0000 (incoming: 1) {
	stloc value(binary.add.i4(ldloc value, ldc.i4 1))
	stloc V_0(ldloc value)
	leave IL_0000 (call ToString(ldloca V_0))
} at IL_0000}

In other words, I want the second transform to switch the position of value and V_0, so that other transforms can reduce it further:

{Block IL_0000 (incoming: 1) {
	stloc V_0(stloc value(binary.add.i4(ldloc value, ldc.i4 1)))
	leave IL_0000 (call ToString(ldloca V_0))
} at IL_0000}

My current pattern matching would work on this final transformed IL.

However, I'm concerned about unintended consequences and unsure of how this should be implemented. In particular:

  • What rules should I use when deciding whether or not to do this transformation?
  • When should the second transform be run?
  • Does the second transform violate semantics in some way that might affect the use of ILSpy in other tools like Visual Studio?

In regards to rules, my idea was:

  • There are two instructions, stloc x1(expression) followed by stloc x2(ldloc x1).
  • expression references x2 but not x1.
    • Or maybe it references only x2 and no other variables?
  • x1 and x2 have the same type.

What are your thoughts?

@dgrunwald
Copy link
Member

I think your order-swapping only makes sense if you can guarantee that it'll be combined into a preincrement.
If this doesn't happen, it's confusing to show the code in a different order than it's actually executed. If there's multiple statements in the "wrong" order, that might also be problematic for the generated sequence points. But if the preincrement transform can combine it into an inline assignment, then there's only one statement and the sequence points won't be an issue.

So I think this doesn't make sense as a separate statement-swapping transform, but should be a transform that directly creates the inline assignment.

@dgrunwald
Copy link
Member

But if it only impacts Roslyn 1.3, then maybe we don't need to support preincrements for that particular version?
You could just use #if in the testcase to avoid testing with that Roslyn version.

@ds5678 ds5678 force-pushed the preincrement-attempt branch from 0a992a6 to 20d93e2 Compare August 1, 2025 09:27
@ds5678
Copy link
Contributor Author

ds5678 commented Aug 1, 2025

But if it only impacts Roslyn 1.3, then maybe we don't need to support preincrements for that particular version? You could just use #if in the testcase to avoid testing with that Roslyn version.

I ended up doing this. I'll come back and implement a second pull request if I discover that 1.3 support is necessary for my use case.

@ds5678 ds5678 marked this pull request as ready for review August 1, 2025 10:14
@christophwille
Copy link
Member

@ds5678 so it is no longer WIP (PR title)?

@ds5678 ds5678 changed the title WIP preincrement improvements Preincrement improvements Aug 3, 2025
@ds5678
Copy link
Contributor Author

ds5678 commented Aug 3, 2025

Fixed

@ds5678 ds5678 force-pushed the preincrement-attempt branch from 20d93e2 to 2c80dc9 Compare August 10, 2025 03:23
@ds5678 ds5678 force-pushed the preincrement-attempt branch from 2c80dc9 to f860777 Compare August 16, 2025 23:45
@siegfriedpammer siegfriedpammer merged commit f4d746e into icsharpcode:master Aug 17, 2025
5 checks passed
@ds5678 ds5678 deleted the preincrement-attempt branch August 17, 2025 16:21
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.

4 participants