authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2022-12-13 21:58:03-05:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-01-01 16:44:28-05:00
log9a77743cc71008d7186614212e542eb2d930af57
treee4cbed6b07e7e8bd25efde7700ad9fd07ddcd384
parentb86a8b4a5b862ce5a6a1c7ca5a755b90c16693db

cbe: add doNotOptimizeAwayC to handle not having __asm support in msvc


1 files changed, 23 insertions(+), 7 deletions(-)

lib/std/mem.zig+23-7
......@@ -3771,7 +3771,7 @@ pub fn doNotOptimizeAway(val: anytype) void {
37713771 .Bool => doNotOptimizeAway(@boolToInt(val)),
37723772 .Int => {
37733773 const bits = t.Int.bits;
3774 if (bits <= max_gp_register_bits) {
3774 if (bits <= max_gp_register_bits and builtin.zig_backend != .stage2_c) {
37753775 const val2 = @as(
37763776 std.meta.Int(t.Int.signedness, @max(8, std.math.ceilPowerOfTwoAssert(u16, bits))),
37773777 val,
......@@ -3783,18 +3783,24 @@ pub fn doNotOptimizeAway(val: anytype) void {
37833783 } else doNotOptimizeAway(&val);
37843784 },
37853785 .Float => {
3786 if (t.Float.bits == 32 or t.Float.bits == 64) {
3786 if ((t.Float.bits == 32 or t.Float.bits == 64) and builtin.zig_backend != .stage2_c) {
37873787 asm volatile (""
37883788 :
37893789 : [val] "rm" (val),
37903790 );
37913791 } else doNotOptimizeAway(&val);
37923792 },
3793 .Pointer => asm volatile (""
3794 :
3795 : [val] "m" (val),
3796 : "memory"
3797 ),
3793 .Pointer => {
3794 if (builtin.zig_backend == .stage2_c) {
3795 doNotOptimizeAwayC(val);
3796 } else {
3797 asm volatile (""
3798 :
3799 : [val] "m" (val),
3800 : "memory"
3801 );
3802 }
3803 },
37983804 .Array => {
37993805 if (t.Array.len * @sizeOf(t.Array.child) <= 64) {
38003806 for (val) |v| doNotOptimizeAway(v);
......@@ -3804,6 +3810,16 @@ pub fn doNotOptimizeAway(val: anytype) void {
38043810 }
38053811}
38063812
3813/// .stage2_c doesn't support asm blocks yet, so use volatile stores instead
3814var deopt_target: if (builtin.zig_backend == .stage2_c) u8 else void = undefined;
3815fn doNotOptimizeAwayC(ptr: anytype) void {
3816 const dest = @ptrCast(*volatile u8, &deopt_target);
3817 for (asBytes(ptr)) |b| {
3818 dest.* = b;
3819 }
3820 dest.* = 0;
3821}
3822
38073823test "doNotOptimizeAway" {
38083824 comptime doNotOptimizeAway("test");
38093825