This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/CCSTUDIO-C2000: Variable declared with keyword "volatile" still got optimized

Part Number: CCSTUDIO-C2000

Tool/software: TI C/C++ Compiler

Hi,

I compile my project with C2000 and flags set to:

-v28 -ml -mt --float_support=fpu32 --tmu_support=tmu0 --cla_support=cla1 --vcu_support=vcu2 -O2 --opt_for_speed=3 ...

The question is that I define a global variable with volatile, but it seems that I never reference it explicitly in my code so it's optimized and I can't find it in .map file.

.cpp:

volatile const int __attribute__ ((section(".init_proc"), used)) test = 99;

.map:

...

.init_proc

* 0 00082780 00000000 UNINITIALIZED

.text:lib

...

Thanks,

Shaoyu

  • Unfortunately, I am unable to reproduce the problem.  Is your code organized as a CCS project?  If so, I'd appreciate if you would package it up as described in the article Project Sharing, then attach that zip file to your next post.

    Thanks and regards,

    -George

  • "The question is that I define a global variable with volatile, but it seems that I never reference it explicitly in my code so it's optimized"

    Sounds OK to me. if it doesn't reference the variable in the code, why keep it around? Volatile does not mean to treat this variable as sacrosanct, it simply means that any time the variable is accessed it must be accessed from memory and not a stashed register.
  • Note that the example also uses attribute "used." The user has a reasonable expectation that this requires the compile to keep the variable. This is the subject of CODEGEN-1928, which is an enhancement request to treat attribute "used" as if it were pragma "RETAIN." This request is still under investigation.

    See also
    e2e.ti.com/.../400654
  • Keith Barkley,

    Yeah, it makes sense, but in fact I reference those variables in .init_proc section with pointer pointing to the linker script symbols which specify the position of that section.

    So I try to tell the compiler with the keyword and attribute option that those variables may be referenced and even modified implicitly, but just as you can see, it doesn't work.

    anyway thanks for your reply :))

  • George,

    I work on the project  with Code Composer Studio Version: 6.2.0.00050, and I need to ask permission for uploading the project and it may take some time.

    By the way, I solve the problem by adding the keyword extern to the declaration making it in global scope, and it works fine, but that still not explains why C2000 compiler ignores the "used" attribute. Is there any compiler options allowing it to do that?

    I'll upload the project as soon as possible, and I'm really appreciate for your quick reply :)))

    Shaoyu

     

  • Archaeologist,

    Yeah, I did try

    #pragma RETAIN

    volatile const int __attribute__ ((section(".init_proc"))) test = 99;

    but still it doesn't work.

    It seems that const implying the file scope overwrites the compiler directive. (--keep_unneeded_statics is not checked)

    Thanks for your reply :]]]

    Shaoyu

  • Shaoyu Chen said:
    I solve the problem by adding the keyword extern to the declaration making it in global scope

    I suspect you are compiling in C++ mode (option --cpp_default, also known as -fg).  In C++, global variables declared "const" are treated as true constants, and can be removed by the compiler.  If you want to keep the variable, you must declare it "extern const".  Alternately, you can compile the file as a C file, not a C++ file.

    This does not address the problem with used, which is a separate issue.