Skip to content
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

Miscompilation of printstring at high optimization levels #14

Closed
aykevl opened this issue Sep 20, 2018 · 6 comments
Closed

Miscompilation of printstring at high optimization levels #14

aykevl opened this issue Sep 20, 2018 · 6 comments

Comments

@aykevl
Copy link
Member

aykevl commented Sep 20, 2018

As reported by @deadprogram in PR #12, this code does not print the string:

package main

import (
	"machine"
	"time"
)

func main() {
	machine.InitADC()

	led := machine.GPIO{machine.LED}
	led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})

	sensor := machine.ADC{machine.ADC2}
	sensor.Configure()

	for {
		val := sensor.Get()
		if val < 512 {
			led.Low()
		} else {
			led.High()
		}

		println("Value is", val) // this prints only the numeric value of val, not the string prefix "Value is"
		time.Sleep(time.Millisecond * 100)
	}
}

So far, I've found it's related to optimization levels (see params to Optimize() in main.go). Size level 0 and 1 compile just fine, but 2 fails. Optimization level 1 and 2 have no difference.

@deadprogram
Copy link
Member

Just confirming this is still an issue with the latest master branch. Was not sure with all of the core changes.

@aykevl
Copy link
Member Author

aykevl commented Sep 28, 2018

I plan to look into it once I'm on a stable LLVM version. It is possible this is a bug in LLVM.

What I've found last time I experimented with it, is that it is a specific combination of a branch depending on a volatile variable, print, and time.Sleep(). Note that something like:

println("foo", uint16(5))

is turned into this:

runtime.printstring("foo")
runtime.printspace()
runtime.printuint16(5)
runtime.printnl()

EDIT: if you manage to reproduce this on a desktop computer, that would make finding the bug much easier (with bugpoint).

@aykevl
Copy link
Member Author

aykevl commented Sep 29, 2018

Tested on stable LLVM, the issue is still present.

@aykevl
Copy link
Member Author

aykevl commented Sep 30, 2018

It looks like I made a mistake while testing. I cannot reproduce it now (on stable LLVM) so it looks like a bug in LLVM that was fixed before the release.
@deadprogram can you try whether it is still present?

@deadprogram
Copy link
Member

Good news here, I also can no longer reproduce on latest LLVM. Seems you are correct, this must have been addressed upstream. Seems like it can ne closed now.

@aykevl
Copy link
Member Author

aykevl commented Sep 30, 2018

Glad to hear, and thanks for testing!
So this is why I wasn't putting much time in it ;) I already suspected a LLVM bug as it happened during an optimization pass.

@aykevl aykevl closed this as completed Sep 30, 2018
omnide pushed a commit to omnide/tinygo that referenced this issue May 7, 2024
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

No branches or pull requests

2 participants