Skip to content

clang: [[gnu::pure]] should mark ptr args as readonly #157693

@qchateau

Description

@qchateau

For small enough return types (not sret), ptr arguments are readonly because the function is annotated memory(read):

struct NotSret {
    double x[2];
};

[[gnu::pure]] NotSret make_not_sret(void*);
; Function Attrs: mustprogress nofree nounwind willreturn memory(read)
declare dso_local { double, double } @_Z13make_not_sretPv(ptr noundef) local_unnamed_addr #39500 

For larger return types, if the function generates a sret argument, then the function is annotated memory(read, argmem: readwrite) and the optimizer cannot assume arguments other than the sret pointer cannot be written to. To leads to missed optimizations.

struct Sret {
    double x[3];
};

[[gnu::pure]] Sret make_sret(void*);
; Function Attrs: mustprogress nounwind willreturn memory(read, argmem: readwrite)
declare dso_local void @_Z9make_sretPv(ptr dead_on_unwind writable sret(%struct.Sret) align 8, ptr noundef) local_unnamed_addr #1
                                                                                                  ^ could add "readonly" here

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions