Tk Source Code

View Ticket
Login
Ticket UUID: d93c8175fdfe0871668ee1daee31d03384c46e71
Title: Tk 8.6.13 RC0 - Win10 - -delta -button -count -focus -place option errors (VC++ compiler bug)
Type: Bug Version: 8.6.13 RC0
Submitter: apnadkarni Created on: 2022-10-15 13:56:23
Subsystem: 69. Events Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-05-21 18:09:11
Resolution: Fixed Closed By: fvogel
    Closed on: 2023-05-21 18:09:11
Description:

On Win10, several tests fail with unknown or unsupported options errors. Samples:

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: <Expose> event doesn't accept "-count" option
    while executing
"event generate .t.f <Expose> -count 20"
    ("uplevel" body line 3)
    invoked from within
"uplevel 1 $script"
---- errorCode: TK EVENT BAD_OPTION
==== bind-22.32 FAILED

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: <Enter> event doesn't accept "-focus" option
    while executing
"event generate .t.f <Enter> -focus 1"
    ("uplevel" body line 3)
    invoked from within
"uplevel 1 $script"
---- errorCode: TK EVENT BAD_OPTION
==== bind-22.39 FAILED

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: <Circulate> event doesn't accept "-place" option
    while executing
"event generate .t.f <Circulate> -place PlaceOnTop"
    ("uplevel" body line 3)
    invoked from within
"uplevel 1 $script"
---- errorCode: TK EVENT BAD_OPTION
==== bind-22.61 FAILED

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: <ButtonPress> event doesn't accept "-button" option
    while executing
"event generate .t.f.s <ButtonPress> -button 1 -x [expr {[winfo width .t.f.s] / 2}] -y 5"
    ("uplevel" body line 15)
    invoked from within
"uplevel 1 $script"
---- errorCode: TK EVENT BAD_OPTION
==== scrollbar-8.1 FAILED

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: <MouseWheel> event doesn't accept "-delta" option
    while executing
"event generate .s <MouseWheel> -delta -120"
    ("uplevel" body line 7)
    invoked from within
"uplevel 1 $script"
---- errorCode: TK EVENT BAD_OPTION
==== scrollbar-10.1.1 FAILED
User Comments: fvogel added on 2023-05-21 18:09:11:

For the records, Microsoft has now released VS 2022 in a version containing the fix for this issue.

See https://developercommunity.visualstudio.com/t/Bad-code-generation-with-optimization-en/10175413


fvogel added on 2023-04-01 11:47:14:
Thanks, all done along your views (which I'm sharing). I'm closing this ticket now.

apnadkarni added on 2023-04-01 10:48:51:

Please report back or let me know if you prefer me to.

Regarding removal of the volatile keyword, I think since it is not performance critical code it is better to leave it there with a comment because there is no way to prevent users from building with a buggy VC++ version. Since VS 2022 has been out a while (I'm not sure when that bug appeared) there might be many people who use that and will run into this. Just my opinion.

/Ashok


fvogel added on 2023-04-01 08:21:21:
I have installed Visual Studio 2022 version 17.6 Preview 2. It installs nicely side-by-side with the current non-preview version.

Then I have removed the (workaround) 'volatile' keyword from tkBind.c:4000, have built Tcl 8.6 and Tk 8.6 from core-8-6-branch in release mode with the preview Visual version. Result: the optimization bug is gone indeed.

Now, should I report this in the Microsoft ticket?

And what do you think about the workaround we have put in tkBind.c (the added 'volatile' keyword) ? Should we keep it in your opinion?

apnadkarni added on 2023-03-27 05:36:07:
I saw that MS update. However, I've too much development going on right now to risk breaking my build environment to test a preview release.

/Ashok

fvogel added on 2023-03-26 20:30:18:
Microsoft reported recently as follows:

"A fix for this issue is now available in preview release. Try out the fix by installing the most recent preview from https://visualstudio.microsoft.com/vs/preview/ ."

THe problem is supposed to be "Fixed In: Visual Studio 2022 version 17.6 Preview 2"

fvogel added on 2022-11-14 07:12:18:
After a few exchanges with them (thank you Ashok!), Microsoft updated the ticket with: "A fix for this issue has been internally implemented and is being prepared for release. We'll update you once it becomes available for download."

apnadkarni added on 2022-10-18 06:30:53:

I've logged a ticket with Microsoft.


fvogel added on 2022-10-17 19:29:28:
> I have refined the Windows workflow file so that the CI runner at Github Actions
> now builds and tests in both release and debug modes on MSVC. I have also
> upgraded the MSVC build environment from windows-2019 to windows-2022,
> so that the bug should show up in the test logs of core-8-6-branch (see
> commit [3133a47b]).

The bug indeed shows up in the test logs of core-8-6-branch for the release build and not for the debug build, as expected.

> Finally, after having merged the second commit into the bugfix branch (see
> commit [141344dc]), I expect the CI runner to show that the issue is worked
> around in the bugfix branch.

The CI runner indeed did this.

> If everything goes well during the next run at Github Actions, I'll merge the
> bugfix branch.

Now done, we have a workaround for the MSVC compiler optimization bug.

Leaving the bugfix branch and the ticket open since a real fix would be needed.

fvogel added on 2022-10-16 20:42:50:

I have created a bugfix branch for this ticket.

First commit in that bugfix branhc is [ea09aa71] introducing the volatile statement for the "badOpt" variable.

Then I have refined the Windows workflow file so that the CI runner at Github Actions now builds and tests in both release and debug modes on MSVC. I have also upgraded the MSVC build environment from windows-2019 to windows-2022, so that the bug should show up in the test logs of core-8-6-branch (see commit [3133a47b]).

Finally, after having merged the second commit into the bugfix branch (see commit [141344dc]), I expect the CI runner to show that the issue is worked around in the bugfix branch.

If everything goes well during the next run at Github Actions, I'll merge the bugfix branch.

Remember however that this is only a workaround for a compiler optimization bug that still needs a fix.


apnadkarni added on 2022-10-16 07:25:50:

Here's what I know so far:

  • Disabling optimization fixes the problem
  • Marking the badOpt variable as volatile also fixes the problem, probably because it also disables optimizations (this may be preferable as a stopgap solution)
  • Tk 8.6.11 also exhibits the same problem when compiled with this compiler (Visual Studio 2022, VC++ 19.33). I don't remember seeing this with VS 2017.
  • Mingw builds do not exhibit the problem

I'm thus increasingly convinced this is a compiler bug. I've looked at the generated code and it simply does not make sense to me. The loop in HandleEventGenerate is unrolled (hardly worthwhile, but that's a different issue) but I cannot fathom why the compiler thinks the badOpt variable can be simply optimized away. I'm told compilers are smarter than me, so that could be it :-)

I'll attach the generated assembly listing if anyone more conversant with compilers and assembler wants to take a look.

/Ashok


fvogel added on 2022-10-15 20:08:48:
As an emergency patch one can surround the HandleEventGenerate() function with  #pragma optimize("", on) / #pragma optimize("", off) statements (with the adequate 
#if defined(_MSC_VER) / #endif of course).

Any better idea?

fvogel added on 2022-10-15 20:01:59:

Minimal script allowing to reproduce:

  pack req Tk
  event generate . <ButtonPress> -button 1 

I had a look at the code. The really looks like an optimization bug in the MSVC compiler. I'm convinced some parts of the code are erroneously optimized out in the switch ((enum field) index) {... in HandleEventGenerate().

Look at what sort of patch allows to make the issue vanish:

Index: generic/tkBind.c
==================================================================
--- generic/tkBind.c
+++ generic/tkBind.c
@@ -4044,10 +4044,11 @@
 		return TCL_ERROR;
 	    }
 	    if (flags & BUTTON) {
 		event.general.xbutton.button = number;
 	    } else {
+printf("hello\n");
 		badOpt = 1;
 	    }
 	    break;
 	case EVENT_COUNT:
 	    if (Tcl_GetIntFromObj(interp, valuePtr, &number) != TCL_OK) {

Wonderful, isn't it? :-)


fvogel added on 2022-10-15 18:11:59:
Failures reproduced with a release build, but not with a debug build.

apnadkarni added on 2022-10-15 16:09:53:
Mea culpa. Sorry about that.

Tk was picking up Tcl 8.6.12 dll from the path. 

package require Tk -> 8.6.13
package require Tcl -> 8.6.12

Removing that installation got rid of the errors.

I don't understand how that happens though. nmake shows the Tcl 8.6.13 build directory first in the path before the 8.6.12 installation. I would have thought that would cause the 8.6.13 dll to be picked. There might be something else I'm missing in my config.

Sorry for the noise.

/Ashok

fvogel added on 2022-10-15 14:34:37:
I'm not able to reproduce this on Windows 10 with core-8-6-13-rc branches (for both Tcl and Tk). Compiler is MSVC, debug build. Puzzling...!

Attachments: