Menu

#1015 Feature request: disable codegeneration for atomics

None
closed-fixed
None
5
13 hours ago
2026-03-09
No

Please add possibility to disable atomic routines codegen
For this add:

  • #pragma noatomics. This pragma should have source code file scope. If it used in the file with main function declaration additional assembler code for atomic operations support should not be generated.
  • compiler flag --noatomics. All source code files scope. Assembler code for atomic operations support should not be generated

When pragma or complier flag is used, sdcc compiler should check than user source code dont use atomics C-language features. Otherwise sdcc compiler should raise a warning "atomic operations support disabled".

Discussion

  • Philipp Klaus Krause

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,6 @@
     Please add possibility to disable atomic routines codegen 
     For this add: 
    -* #pragma noatomics. This pragma should have source code  file scope. If it used in the file with main function declaration additional assembler code for atomic operations support should not be generated.
    -* compiler flag --noatomics. All source code files scope. Assembler code for atomic operations support should not be generated
    +* `#pragma noatomics`. This pragma should have source code  file scope. If it used in the file with main function declaration additional assembler code for atomic operations support should not be generated.
    +* compiler flag `--noatomics`. All source code files scope. Assembler code for atomic operations support should not be generated
    
     When pragma or complier flag is used, sdcc compiler should check than user source code dont use atomics C-language features.   Otherwise sdcc compiler should raise a warning "atomic operations support disabled".
    
    • Group: -->
     
  • Oleg Endo

    Oleg Endo - 2026-04-04

    The option to disable atomics has also been mentioned in the original atomics issue https://sourceforge.net/p/sdcc/feature-requests/624/

     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    The option --norestartseqatomics has been implemented in th eatomics branch, and can be tested by users.

    Currently it only affects the mcs51 and ds390 ports, and mostly disables atomics support for these two ports.

    I did not call it --noatomics, and did not make it disable all atomics support; it just disables the restartable sequence support routines for atomics; depending on the target architecture and how the user uses atomics, stuff might work or not; basically, one could just try to compile with --norestartseqatomics, and the atomics will either work, or there will be a diagnostic from the linker.

    P.S.: E.g. without restartable sequence support routines mcs51 and ds390 loose virtually all atomics support except volatile sig_atomic_t, but AFAIK all other targets can still do atomic_flag and once we have support for more atomic types and improve atomics support, there will be even more differences (e.g. mcs51 hardware could handle an atomic_flag in __idata without the restartable sequence support routines, but not in __xdata).

     

    Last edit: Philipp Klaus Krause 5 days ago
  • Michael Golovanov

    Ok, i built sdcc from atomics branch and make smoke tests for mcs51.

    sdcc --version
    SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/f8/f8l TD- 4.5.24 #16459 (Linux)
    published under GNU General Public License (GPL)

    Wow At first look its worked.

    Details:
    First test is try to build main.c with content
    void main(void) {}

    If no --norestartseqatomics compiler flag the size of firmware is
    Name Start End Size Max


    ROM/EPROM/FLASH 0x0000 0x00ae 175 65536
    main.asm contains atomic related code like
    ; restartable atomic support routines
    .ds 5
    sdcc_atomic_exchange_rollback_start::
    nop
    nop

    If add --norestartseqatomics compiler flag
    Name Start End Size Max


    ROM/EPROM/FLASH 0x0000 0x0068 105 65536

    Second test on main.c

    #include <stdatomic.h>
    
    volatile atomic_flag lock = ATOMIC_FLAG_INIT;
    
    void critical_section(void) {
        // Spin until we acquire the lock
        while (atomic_flag_test_and_set(&lock)) {
            // In a real application, you might do something here
            // to avoid tight spinning, or just wait for the lock.
        }
    
        // --- CRITICAL SECTION START ---
        // Example: Protected access to a shared resource
        // (e.g., updating a 16-bit variable, sending serial data)
        __critical {
            // Even within this lock, you might need __critical 
            // if interrupts can also try to grab this same lock.
        }
        // --- CRITICAL SECTION END ---
    
        // Release the lock
        atomic_flag_clear(&lock);    
    }
    
    
    void main(void) {
        while (1) {
            critical_section();
        }
    }
    

    Without --norestartseqatomics

    Other memory:
    Name Start End Size Max


    PAGED EXT. RAM 0 256
    EXTERNAL RAM 0 65536
    ROM/EPROM/FLASH 0x0000 0x00d6 215 65536

    Try to compile with --norestartseqatomics is failed (and there is expected behavior)
    ?ASlink-Warning-Undefined Global sdcc_atomic_exchange_gptr_impl referenced by module

     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    The feature is now in trunk.

     

Log in to post a comment.

MongoDB Logo MongoDB