Skip to content

Generation of constant arrays with non-zero elements suboptimal #10807

Open

Description

@lerno

Zig Version

0.9.0

Steps to Reproduce

Run the following code and observe the generated LLVM IR and asm

const std = @import("std");
pub var arr = blk: {
  var x = std.mem.zeroes([10 * 1024]u32);
  x[5] = 2;
  break :blk x; 
}; 

export fn tester(x : usize) u32 {
  return arr[x];
}

Expected Behavior

The following LLVM IR is what Clang generates for similar code:

@arr = dso_local global <{ i32, i32, i32, i32, i32, i32, [10234 x i32] }> <{ i32 0, i32 0, i32 0, i32 0, i32 0, i32 2, [10234 x i32] zeroinitializer }>, align 16

Actual Behavior

The following LLVM IR is generated:

@arr = internal unnamed_addr global [10240 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 2, <omitting about 10000 i32 0> i32 0,i32 0, i32 0], align 4

Activity

  1. added
    bug
    on Feb 5, 2022
  2. lerno commented on Feb 5, 2022

    @lerno
    Author

    Clang uses a merge strategy to create its input.

  3. added
    backend-llvm
    enhancement
    on Feb 5, 2022
  4. added this to the 0.11.0 milestone on Feb 5, 2022
  5. andrewrk commented on Feb 5, 2022

    @andrewrk
    Member

    Leaving as "bug" because of the incorrect alignment (should be 4).

    Oops, that's the "expected" behavior. I disagree with the alignment of 16 on the expected code.

  6. removed
    bug
    on Feb 5, 2022
  7. modified the milestones: 0.11.0, 1.1.0 on Feb 5, 2022
  8. andrewrk commented on Feb 5, 2022

    @andrewrk
    Member

    Alignment issue extracted to #10809.

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

    backend-llvmenhancementoptimization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions