Build PyTorch android binaries with -O0?

I am trying to track down a bug that happens inside a pytorch android binary (speed_benchmark_torch) for one of my models.

When I look at the backtrace on android logcat, it looks like debug symbols are disabled, so I don’t know what line of code is failing. Also, I tried building with -DANDROID_DEBUG_SYMBOLS=1, and I still couldn’t get a stacktrace with debug symbols.

I suspect that I will get a better stacktrace if I can compile with -O0 instead of the current -O2. Towards this end, I changed one of the lines in pytorch/CMakeLists.txt from this:
to this:

I also changed the O2 to O0 in the QNNPACK CMakeLists.

And, after making that change, I recompiled pytorch. When I look at pytorch/build/compile_commands.json, I see we use -O0 in the pytorch build. Great.

Now, I run scripts/, and to my dismay I see that each command in build_android/compile_commands.json has both -O0 and -O2. That’s not good!

Does anyone know…
(a) Where might this stray -O2 be coming from, and
(b) How do I do an “only O0” build of the android binaries, particularly speed_benchmark_torch?

One idea I had is to just manually compile speed_benchmark_torch, since I have its compile command in build_android/compile_commands.json. However, I don’t know the link command, and I imagine there’s a long list of flags on the link command.

Figured it out. The solution is to use -DCMAKE_BUILD_TYPE=Debug in Otherwise, it defaults to Release, which seems to add the -O2 flag.