Skip to content

Add warning for non-portable pragmas #389

Open

Description

@niemela

Give a warning if example submissions in C/C++ use non-portable pragmas such as e.g.:

#pragma GCC target("bmi,bmi2,lzcnt,popcnt")
#pragma GCC target("movbe")
#pragma GCC target("aes,pclmul,rdrnd")
#pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2")

It is of course true that problemtools can't check for every possible footgun one can set up, but this particular one is very common, and I think a warning would be useful.

Activity

  1. Matistjati commented on Apr 5, 2026

    @Matistjati
    Contributor

    I've already discussed this with Fredrik and I'm kinda torn about it.

    On one hand, yeah, this is a huge source of failing installs and forward incompatability.

    On the other hand, when we design a problem, it's mainly for the contest at hand, where we want to ensure that our test data and time limit do not allow participants to trivially cheese the problem with the SIMD available on the judge. To do this, we write "trivial" SIMD solutions and ensure they do not get more points than they should.

    So I would personally be slightly annoyed to get warnings, when I have proper tooling to check whether we have done the necessary workarounds to avoid compile errors for Kattis. However, since I've already made the plunge into having a custom fork of Problemtools, I wouldn't mind simply implementing the checks for checking whether it will work in Kattis there.

    So yeah, I would be fine with it. And I do not think that we should bloat problemtools with flags such as --ignore-pragma-warnings, or --stip-pragmas.

    Summoning @Tagl since you care about constant factor optimization and use problemtools a lot. If you're fine with it, I'll probably create a PR.

  2. Tagl commented on Jul 13, 2026

    @Tagl
    Contributor

    Generally the code will compile with these settings but running it will result in a SIGILL (Illegal Instruction).
    We could have recommendations with examples on how to achieve this in different ways for the trivial and advanced cases.
    I think the best thing to do would be to encourage people to write the solutions with function multiversioning or manual runtime dispatch coding.

    See: https://web.archive.org/web/20200804021412/extensa.tech/blog/migrating-multiversioning/

  3. Matistjati commented on Jul 13, 2026

    @Matistjati
    Contributor

    I don't think it's that simple, though. For example, you really want to enable avx2 before including bitset.

    We could encourage this pattern

    #ifdef __AVX2__
    #pragma GCC target("avx2")
    #endif
    

    But even that is not entirely sufficient because Kattis' GCC version has a compiler bug, and will cause a compile error when compiling bits/allocator.h even when avx2 should be supported.

  4. Tagl commented on Jul 13, 2026

    @Tagl
    Contributor

    I think for now, the simplest approach is just to add a warning for each submission that uses these architecture specific targeting.

    Have you tested that pattern out? I'm fairly certain that the target statement is equivalent to the -mavx2 flag which is exactly what will define __AVX2__, therefore meaning you rely on circular logic.

  5. Matistjati commented on Jul 13, 2026

    @Matistjati
    Contributor

    Kattis has actually updated GCC version this week, so bits/allocator.h should no longer be an issue. I will do some testing, but surely there is a way to safely use the #pragma GCC target("avx2")...

  6. Tagl commented on Jul 13, 2026

    @Tagl
    Contributor

    I think the safe way to do it is runtime detection, but that means parts of the standard library won't get the features.
    If you find a way to do it at compile time then I'm all for it.

  7. Tagl commented on Jul 13, 2026

    @Tagl
    Contributor

    Can also use -march=native in problemtools compiler flags, but this will introduce variance for task authors using different machines (maybe the good kind of variance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions