[llvm-dev] Disabling optimizations in clang/llvm for specified regions in code

Usman Nadeem via llvm-dev llvm-dev at lists.llvm.org
Mon Oct 4 18:57:03 PDT 2021

I don't think there is currently any way to disable an arbitrary transformation for an arbitrary block of code, it may be possible to implement something like that though.

You may be able to mark variables volatile to prevent certain transformations for those variables.
Another way is to insert something like this before and after a block of code to prevent optimizations.
    __asm__ volatile("" : : : "memory");

    __asm__ volatile("" : : "g"(pointer) : "memory");
You can try to experiment and see if it works for you.


From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Noor, Abdul Rafae via llvm-dev
Sent: Monday, October 4, 2021 3:24 PM
To: llvm-dev at lists.llvm.org
Subject: [llvm-dev] Disabling optimizations in clang/llvm for specified regions in code

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.
I wanted to inquire if there is a way within LLVM/Clang to limit (function-level) optimizations to certain contiguous regions within the function. E.g.

void foo(int* A, int n1, int n2){

         // Enable optimizations above this loop
         // (#pragma clang loop nolicm?)
         for(int i = 0; i < n1; ++i){
                 for(int j=0; j< n2; j++){
                          int i_offset = (n2*i); // Do not Hoist to outside current loop
                          int net_offset = i_offset + j;
                          A[net_offset] = ...;

         // Enable optimizations below this loop


In the above case, would it be possible to disable Loop Invariant Code motion just for that loop nest in the function foo, but enable it to be used elsewhere in the function? We would like to disable certain passes like LICM for our use-case (We've defined markers inside the functions specifying the start and end of certain regions and we want to limit certain optimizations as we will be extracting those code regions later on).

The clang pragma at https://bcain-llvm.readthedocs.io/projects/clang/en/latest/LanguageExtensions/#id20 acts at a per function level. Similarly the loop optimizations pragmas https://clang.llvm.org/docs/LanguageExtensions.html#id30 only cover some of the loop specific metadata's available in LLVM (For example, I wasn't able to find a way to add 'llvm.licm.disable' to loops at the source level).

Thank you,
Abdul Rafae Noor
Grad Student,
University of Illinois, Urbana Champaign

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20211005/3f006fb2/attachment.html>

More information about the llvm-dev mailing list