llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

bitfields result in really slow code

Open dobbelaj-snps opened this issue 12 years ago • 2 comments

Bugzilla Link 21039
Version trunk
OS Linux
Attachments test_bitfields.c: the testcase, test_bitfiels.s-i386: good code for x86, test_bitfields.s-armv7: code for armv7 - pretty bad, test_bitfields.s-arm64: code for arm64 - pretty bad, test_bitfields.s-sparc: code for sparc - pretty bad
CC @chandlerc,@hfinkel,@rnk

Extended Description

Clang produces code that treats a bitfield as a part of a very large number, consisting of all adjacent bitfields. For structs containing a lot of bitfields, this can result in code loading, storing and doing arithmetic operations on large numbers.

In the provided test example, we clear one specific bitfield. On the x86/x86_64 architecture, this result in good code, but on all the other architectures that I tried, the code results in unnecessary load/store operations.

The problem seems to be that in the legalization and lowering phase, the loading/masking/storing is split up in smaller chunks, but corresponding load/stores are not optimized away. For x86, this is less of an issue as it already knows how to map arithmetic operations onto memory.

Note: maybe the bigger problem is that clang maps bitfields onto such large numbers in the first place ?

dobbelaj-snps avatar Sep 23 '14 11:09 dobbelaj-snps

I'm starting to think we should add a CodeGen-level IR pass (much like the atomic pass) that does load and store slicing and merging. It can do this with direct and explicit hooks into TargetLowering, etc. It seems reasonable (to me) to do this at the IR level so that it can reach across BBs and use dominance and other analyses.

chandlerc avatar Sep 23 '14 19:09 chandlerc

So far, consensus has been that larger loads and stores are "more canonical" in the mid-level. I won't try to rehash why, it's a long discussion.

Given that basically all backends other than x86 fail to handle them, maybe we should start eliminating these kinds of illegal loads during codegenprep. On the other hand, that's a lot like legalization, and we have a precept that Though Shalt Not Legalize In The IR.

rnk avatar Sep 23 '14 19:09 rnk