Skip to content

rust slice.as_ptr() should use getelementptr, not bitcast/load #14761

@taralx

Description

@taralx

bytes!(...).as_ptr() is used in freestanding code to produce needed byte arrays with specific content. This causes an entire allocation of the slice data in rustc:

@const = private constant [6 x i8] c"TEXT!\00"
@_ZN4main5BYTES20h0c254b3a0ff92eb8Naa4v0.0E = internal unnamed_addr constant { i8*, i32 } { i8* getelementptr inbounds ([6 x i8]* @const, i32 0, i32 0), i32 6 }

whereas in clang, the structure is elided, as evidenced by compiling this C code with -O4 -S:

static struct {
  char *ptr;
  int size;
} ZN = {"TEXT!", 6};

int main() {
  return (int) ZN.ptr;
}

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