Skip to content

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

@lerno

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.enhancementSolving this issue will likely involve adding new logic or components to the codebase.optimization

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions