From 46abf2045476a32b6f4dd939679c0fbc7a639133 Mon Sep 17 00:00:00 2001 From: Xavier Bouchoux Date: Fri, 28 Jul 2023 19:41:21 +0200 Subject: [PATCH] llvm: partial fix of store undefined to packed result location prefer marking too few undefined bits, rather than too many that may overwrite nearby values. partially resolves https://github.com/ziglang/zig/issues/15337 --- src/codegen/llvm.zig | 9 +++++++++ test/behavior/packed-struct.zig | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 0d314aebdf..4af42bfa9b 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -8753,6 +8753,15 @@ pub const FuncGen = struct { const val_is_undef = if (try self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep(mod) else false; if (val_is_undef) { + const ptr_info = ptr_ty.ptrInfo(mod); + const needs_bitmask = (ptr_info.packed_offset.host_size != 0); + if (needs_bitmask) { + // TODO: only some bits are to be undef, we cannot write with a simple memset. + // meanwhile, ignore the write rather than stomping over valid bits. + // https://github.com/ziglang/zig/issues/15337 + return .none; + } + // Even if safety is disabled, we still emit a memset to undefined since it conveys // extra information to LLVM. However, safety makes the difference between using // 0xaa or actual undefined for the fill byte. diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig index d3d761c3db..6621197e55 100644 --- a/test/behavior/packed-struct.zig +++ b/test/behavior/packed-struct.zig @@ -632,7 +632,6 @@ test "pointer to container level packed struct field" { test "store undefined to packed result location" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;