User avatar
OneMadGypsy
Posts: 361
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 3:55 pm

I'm playing around with optimization flags as a personal study. When using O3 I get an error saying that a var may be used uninitialized. The part in question is the below snippet (and other similar lines).

Code: Select all

mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);


I'm not confused as to why it is telling me this. I am confused as to how to "fix" it. Before someone goes into details as to why I don't want to use O3 optimization, I'll save you some time. I already know. I've read all about all the optimization levels and the good, bad, ugly of using them. That's not the point of this question. My only real question is: What is the PROPER way to get rid of this error? My guess is to go to all of these spots and add = mp_const_none, but something tells me this is probably junky/wrong/not-optimal...

Actually, I have one more quickie. Is this the best place to put the optimization flag, and if not, where would you recommend?

ports/rp2/CMakeLists.txt

Code: Select all

target_compile_options(${MICROPY_TARGET} PRIVATE
    -Wall
    -Werror
    -O3
)
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

slimhazard
Posts: 76
Joined: Sat Apr 03, 2021 8:47 pm

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 4:39 pm

When using O3 I get an error saying that a var may be used uninitialized.
That's not because of -O3, and has nothing to do with optimization. It's because you've turned on -Wall and -Werror, meaning that all warnings are turned on, and all warnings are errors. That means that -Wmaybe-uninitialized is turned on and counts as an error. The compiler is telling you that as a matter of fact, it can't tell if you're reading from a variable before a value is assigned to it.

Hard to tell from your snippet which variable is meant (doesn't the warning message say so?), because we can't see what has happened with path_in before you got there. Did you assign it a value before this snippet? path_out is in fact uninitialized, although I wouldn't have thought that passing its address into a function would cause the warning. But maybe I'm wrong.

I for one think that -Wall -Werror is excellent practice, since I've found that it discovers many potential errors that I might have missed before there's a chance to compile the code, greatly improving quality. Like running lint back in the day, except nobody runs lint any more, since we have -Wall -Werror. It's required for everything I do at my day job, and it's worth it to do some things you ordinarily wouldn't have bothered doing to get all of the warnings silenced.

In your case, I'd assign a value to whichever variable is provoking the warning, even if you're pretty sure that you don't necessarily need to. If it's path_out, assign it NULL or the empty string or whatever is appropriate for the type. That sort of thing can feel a bit silly, but getting your code to compile warning-free usually pays off well in the long run.

User avatar
OneMadGypsy
Posts: 361
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Thu Sep 23, 2021 4:47 pm

That's not because of -O3, and has nothing to do with optimization.


But Wall and Werror were already there and the error does not show up unless I specifically use -O3, and a actually, if I use -O2 I get a completely different error, and any other O (or no O) doesn't throw any errors. Here's an image for proof (I have no reason to fake this. I'm trying to learn). I can make images with various O's if you want to see them all.

Image

Here's at least "with -O3" for comparison.
Image

@hard to tell cause I didn't post error ...

It's not my code. It's the stock vfs code that is part of micropython. I also already knew why the error occurred, and wasn't worried about why ~ they didn't assign the variable before referencing it. I was just curious what the best way to fix it was. The immediate reflex was to simply give it a value, but as I stated before, I am still getting good at this language, and immediate reflexes may not necessarily be a good choice. For instance maybe there is some other compiler flag that I should use in tandem which allows using uninitiated vars, and continuing that thought, maybe that flag exists because uninitiated vars in certain circumstances are no big deal. In contrast, maybe there is a flag that assigns uninitiated vars a "safe" value, essentially fixing the code instead of treating it as if t is broken.
Last edited by OneMadGypsy on Thu Sep 23, 2021 5:21 pm, edited 1 time in total.
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

slimhazard
Posts: 76
Joined: Sat Apr 03, 2021 8:47 pm

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 5:19 pm

Don't take it personally, the warning you're getting is in fact -Wmaybe-uninitialized, -Werror makes it an error, and nothing in your original post gave reason to suppose that optimization had anything to do with it. Apparently -O3 has removed a previous initialization, which has led to the warning. So now I know.

It would be more helpful if you would share the actual text of the warning. I don't ever recall seeing it without the name of the variable that the compiler thinks is uninitialized, and that's the crux of your problem. Just copy over the text of outputs like warnings, that's better than a screenshot.

Have your tried assigning a value to one or both of the two variables in your code snippet, before the function call?

You could add -Wno-maybe-initialized to your compile flags, to turn off the warning. For every warning -Wfoo. -Wno-foo turns it off. But then the compile won't warn you if your code ever forgets to ensure that a variable has value before reading from it.

User avatar
OneMadGypsy
Posts: 361
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 5:25 pm

Don't take it personally,

You misread me. I have very thick skin. My "I have no reason to fake this" comment was based on "you know what you are doing and I don't, but what you are saying is not congruent with my results.". Let there be no doubt, you're the big dog and I'm the puppy. I'm just trying to get paper trained, over here. :D

I don't know if you can identify with this, but I am much better at talking to computers than I am at talking to people. If you can keep that in mind regarding any of my responses you'll always realize that I just suck at human communication. I've been told a number of times "It's all black and white with you, there is no gray." and my response is generally something like: "What comes between True and False? Maybe? So, there is no 'maybe' with me."
Last edited by OneMadGypsy on Thu Sep 23, 2021 5:35 pm, edited 1 time in total.
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

trejan
Posts: 3856
Joined: Tue Jul 02, 2019 2:28 pm

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 5:34 pm

One of the additional optimisations is changing the intermediate representation enough that its triggering the heuristic for detecting a potentially uninitialised variable. -O3 turns on these additional optimisations to O2. Find which one is doing it.

Code: Select all

-fgcse-after-reload 
-fipa-cp-clone
-floop-interchange 
-floop-unroll-and-jam 
-fpeel-loops 
-fpredictive-commoning 
-fsplit-loops 
-fsplit-paths 
-ftree-loop-distribution 
-ftree-loop-vectorize 
-ftree-partial-pre 
-ftree-slp-vectorize 
-funswitch-loops 
-fvect-cost-model 
-fvect-cost-model=dynamic 
-fversion-loops-for-strides

lurk101
Posts: 1061
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 5:39 pm

OneMadGypsy wrote:
Thu Sep 23, 2021 5:25 pm
Don't take it personally,
You misread me. I have very thick skin. My "I have no reason to fake this" comment was based on "you know what you are doing and I don't, but what you are saying is not congruent with my results.". Let there be no doubt, you're the big dog and I'm the puppy. I'm just trying to get paper trained, over here. :D
The compiler needs to do far more static analysis when optimization is enabled, allowing it to detect things like uninitialized variables.

The following

Code: Select all

int main() {
    int j;
    for(int i = 0; j < 10; i++)
        j += i; 
    printf("%d\n", j); 
}
gives no warrnings with gcc -Wall test.c, but does with gcc -Wall -O3 test.c. Fix your code by addressing the warnings if you must use -Werror

Not as expected, but that's how it is.

ejolson
Posts: 8612
Joined: Tue Mar 18, 2014 11:47 am

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 5:58 pm

lurk101 wrote:
Thu Sep 23, 2021 5:39 pm
OneMadGypsy wrote:
Thu Sep 23, 2021 5:25 pm
Don't take it personally,
You misread me. I have very thick skin. My "I have no reason to fake this" comment was based on "you know what you are doing and I don't, but what you are saying is not congruent with my results.". Let there be no doubt, you're the big dog and I'm the puppy. I'm just trying to get paper trained, over here. :D
The compiler needs to do far more static analysis when optimization is enabled, allowing it to detect things like uninitialized variables.

The following

Code: Select all

int main() {
    int j;
    for(int i = 0; j < 10; i++)
        j += i; 
    printf("%d\n", j); 
}
gives no warrnings with gcc -Wall test.c, but does with gcc -Wall -O3 test.c. Fix your code by addressing the warnings if you must use -Werror

Not as expected, but that's how it is.
With about 50 percent probability that code has an infinite loop.

hippy
Posts: 10936
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 6:08 pm

OneMadGypsy wrote:
Thu Sep 23, 2021 3:55 pm
What is the PROPER way to get rid of this error?
That's an interesting question. I suspect the authoritative answer is to initialise it with NULL or anything compatible with its type, as 'slimhazard' suggested.

But, as that's not your code, you would need to jump through hoops to keep such a fix in place. The easier way is to hack 'CMakeLists.txt' so that specific warning is not treated as an error. It will still be shown but won't cause the build to fail.

It's what MicroPython itself already does for files in TinyUSB which produce warnings which would otherwise prevent the build from succeeding. This worked for me -

Code: Select all

set_source_files_properties(
    ${MICROPY_DIR}/extmod/vfs.c
    PROPERTIES COMPILE_OPTIONS "-Wno-error=maybe-uninitialized"
)
You will probably also need to add -

Code: Select all

set_source_files_properties(
    ${PICO_TINYUSB_PATH}/src/common/tusb_fifo.c
    PROPERTIES COMPILE_OPTIONS "-Wno-error=strict-overflow"
)
Where you put your "-O3" is where I would have put it, assuming you want it to apply to all files. If you want it to apply to individual files, use 'set_source_files_properties' as above to set it for those particular files. If a group of files you probably need to start looking at adding additional targets.

slimhazard
Posts: 76
Joined: Sat Apr 03, 2021 8:47 pm

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 6:26 pm

The following

Code: Select all

int main() {
    int j;
    for(int i = 0; j < 10; i++)
        j += i; 
    printf("%d\n", j); 
}
gives no warrnings with gcc -Wall test.c, but does with gcc -Wall -O3 test.c.
Do what now?

That's pretty whack. -O3 really is your friend here. Makes me wonder if the OP has a real problem rather than just s spurious warning.

User avatar
OneMadGypsy
Posts: 361
Joined: Wed Apr 28, 2021 1:57 am
Location: New Orleans, Louisiana
Contact: Website

Re: Confused as to how to fix an O3 level optimization error

Thu Sep 23, 2021 6:54 pm

Those snippets did the trick. I tend to be a nazi about even the most benign warning, but just like you said, that is not my code and maintaining variable assignments is not feasible. Everybody gave very helpful answers/information, but I think your answer is probably the best solution for this case. I've tested all of the optimization flags and none of them seem to make a difference where I was hoping they would. -O1 shaved 1ms off of drawing 1000 lines, but didn't seem to shave anything off of any of my other draw methods, and that 1ms might really only be a fraction of 1ms because my resolution on time is only 3 decimal places. Welp, back to work for me. I think it's finally time to write an algorithm for filling polygons. Unfortunately, I read something that ended up telling me too much, so this is probably not going to be too hard. It sucks, I guarantee you I could have figured it out. However, whenever I'm done with the scanline approach, I'm going to play with finding all rects and treating the difference as a polygons, maybe change the pixel by pixel scanline into just draw a line, too. I'm not saying that either of those ideas are good, they're just my ideas and I'll never know if they are good if I don't try them. Who knows, maybe I'll even skip pixels based on Bayesian averages or some other crazy idea that's probably going to draw garbage. Active edge table, here I come.

Thank you to everyone for your answers. I earnestly appreciate it.
"Focus is a matter of deciding what things you're not going to do." ~ John Carmack

User avatar
jojopi
Posts: 3565
Joined: Tue Oct 11, 2011 8:38 pm

Re: Confused as to how to fix an O3 level optimization error

Fri Sep 24, 2021 2:26 am

ejolson wrote:
Thu Sep 23, 2021 5:58 pm
With about 50 percent probability that code has an infinite loop.
It has undefined behavior, so at least five percent probability of nasal demons.

I do not see the infinite loop, however. No matter the initial value of j, we are adding all possible values of i to it. How could it remain forever less than ten?

lurk101
Posts: 1061
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: Confused as to how to fix an O3 level optimization error

Fri Sep 24, 2021 3:41 am

jojopi wrote:
Fri Sep 24, 2021 2:26 am
ejolson wrote:
Thu Sep 23, 2021 5:58 pm
With about 50 percent probability that code has an infinite loop.
It has undefined behavior, so at least five percent probability of nasal demons.

I do not see the infinite loop, however. No matter the initial value of j, we are adding all possible values of i to it. How could it remain forever less than ten?
j is used incorrectly in the for statements second term.

ejolson
Posts: 8612
Joined: Tue Mar 18, 2014 11:47 am

Re: Confused as to how to fix an O3 level optimization error

Fri Sep 24, 2021 5:47 am

jojopi wrote:
Fri Sep 24, 2021 2:26 am
ejolson wrote:
Thu Sep 23, 2021 5:58 pm
With about 50 percent probability that code has an infinite loop.
It has undefined behavior, so at least five percent probability of nasal demons.

I do not see the infinite loop, however. No matter the initial value of j, we are adding all possible values of i to it. How could it remain forever less than ten?
Yikes! You are right. I made a mistake imagining that if j randomly started out negative then it would stay that way. I see now that it wouldn't.

Return to “SDK”