Wholeprogramdevirt pass not working as expected


I’ve been trying to run the -wholeprogramdevirt pass in LLVM 12.0.1. Although my pass runs, it doesn’t devirtualize the calls. My program as shown below should be easily devirtualizable.

Below are the commands I am using to compile and run the pass.

clang++ -O0 -fvisibility=hidden -flto -fwhole-program-vtables -c -emit-llvm virfunc.cpp -o virfunc.bc
opt -wholeprogramdevirt virfunc.bc -o virfunc_opt.bc

When I view the optimized version of the LLVM IR, it still uses a vtable lookup in the main() which means the virtual function call was not converted to a direct call.

Any advice would be great.


Doesn’t -O0 imply the optnone attribute? If you can see those in your intermediate bitcode file, try -O3 -Xclang -disable-llvm-passes instead.

thanks, for the reply. I gave that a shot but it didn’t work. Here’re the commands I used.

clang++ -O3 -Xclang -disable-llvm-passes -fvisibility=hidden -flto -fwhole-program-vtables -c -emit-llvm virfunc.cpp -o virfunc.bc

opt -wholeprogramdevirt virfunc.bc -o virfunc_opt.bc

I noticed there are some ImportSummary and ExportSummary objects in the source file for the pass LLVM: lib/Transforms/IPO/WholeProgramDevirt.cpp Source File

Some optimizations seem to be dependent on these objects and by default these objects are set to null. I’m wondering if I’m missing any flags to turn on these optimizations.