Skip to content

Certain floating-point constants and flags cause nominally constant initializers to be executed at runtime #174340

@tpwrules

Description

@tpwrules

I have a program which needs constant static data tables to be constant-initialized so they are available for global constructors and external parsing of the binary. This turns out to break if all of the following are true:

  • The initialization also involves an expression which is not constexpr
  • The flag -ftrapping-math is passed
  • A double precision float literal is assigned to a single precision float member

In this case, Clang decides to initialize the data tables with zero in the binary and fill them in at runtime, which is a problem.

I don't know if this is a bug, but it's unexpected and not something GCC does. I've prepared a godbolt example which demonstrates the problem and explains how to mitigate the problem by removing one of the above factors: https://godbolt.org/z/fs8v7h1hx . But I don't think we should have to apply any mitigation and there may be an issue in the compiler.

In the broken case, the table is zero and there's a bunch of extra code:

Container::data:
        .zero   64

In the working case, the table is filled out:

Container::data:
        .quad   .str
        .quad   0
        .long   0x3ed70a3d
        .zero   4
        .zero   40

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions