New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use unsafe.Add
everywhere
#3503
Conversation
I have a semgrep rule that covers this:
We can run this over the codebase and tweak it to make sure it hits all the different cases. I was planning on this doing after my reflect work settled down to avoid the conflicts in reflect/* . Edit: Trying to get my rule to work and I'm running into issues with semgrep stealing |
It won't catch all of them though, especially the ones in src/runtime/hashmap.go (it would have to look over multiple lines, never used semgrep so I don't know whether it's capable to do that). What I did was that I modified the compiler a bit to output an error instead of applying the optimization that's in the second commit. Then, compiling all smoke tests will catch all instances of this optimization.
I see. This is just some code I already had in a local branch but didn't push because I wanted to wait for the #2640 refactor (which is now merged). I figured I could make a PR now before it starts drifting again. |
My goal for the rule was to catch places where we could use
As I said, this currently doesn't work because of a known bug in semgrep. I'm going to see if I can write something to handle all the fixups of the missing parens. I tried |
Seems like this PR introduces a problem with the MaixBiT https://github.com/tinygo-org/tinygo/pull/3503/checks?check_run_id=11716348330 I am able to flash it with code from other branches. |
We have an optimization for this specific pattern, but it's really just a hack. With the addition of unsafe.Add in Go 1.17 we can directly specify the intent instead and eventually remove this special case. The code is also easier to read.
I have checked this conversion is not needed anymore after the previous commit, by running various smoke tests of which none triggered this optimization. The only case where the optimization would have kicked in is in syscall/syscall_windows.go:76 of the Go standard library. Therefore, I prefer to remove it to reduce code complexity.
@deadprogram apparently I tried to also merge runtime_tinygoriscv.go and runtime_tinygoriscv64.go in the same commit, but didn't do so correctly. I've undone that change - it could still be a nice improvement but it's better to do unrelated to
Is it? Are there any cases it finds that aren't in this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good change, thanks @aykevl
Running my auto-convert semgrep rules on
|
Interesting, those are mostly constant integer addition which would indeed not be caught by me. Can you make a PR to fix those remaining bits? |
unsafe.Pointer(uintptr(v) + idx)
instances with justunsafe.Add(v, idx)
.