Max
Max

Reputation: 51

Avoid LLVM IR Optimization on certain basic blocks

I am implementing a security related pass and therefore, I am injecting a custom basic block as part of an LLVM IR function pass.

Is there a way, to mark this basic block such that later passes do not optimize this basic block?

Upvotes: 2

Views: 605

Answers (1)

compor
compor

Reputation: 2339

I'm afraid I don't have a very straightforward suggestion. But here's a few things to consider.

Could you apply you basic block injection after all optimizations related with the desired/required optimization level (e.g. opt -O3) are applied? (I suspect not, since you're asking.)

I'm also not sure which optimizations you want to avoid (depending on what you do on your basic block). For example, do you mind the effects of -simplifycfg? In other words, having your injected basic block merged with predecessor/successor blocks.

If the basic block code is fairly generic, maybe you could consider keep that block outlined in a separate function and provide it with any required input as parameters and marking it with __attribute__((noinline)) to avoid inlining.

Having said that, you could declare all memory accesses in that basic block as volatile, which inhibits optimizations. Take a look at this.

Edit: There is also the Attribute::OptimizeNone with which you can adorn a function and have it being skipped during optimization by opt and related passes. You can follow (aka grep) that attribute in LLVM's source.

If that is not enough and since I'm unaware of any general blacklist/whitelist capabilities in place, I can only think of an onerous alternative, but I won't dare posting it yet.

Upvotes: 1

Related Questions