From bbad4454bf50875562aed41f27ac9ea8079efde9 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sun, 22 Sep 2024 08:06:43 -0400 Subject: [PATCH] asahi: fix UB affecting x86 builds dEQP-VK.robustness.image_robustness.bind.notemplate.r32f.unroll.nonvolatile.sampled_image.no_fmt_qual.img.samples_1.2d_array.comp Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/lib/agx_nir_lower_texture.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/asahi/lib/agx_nir_lower_texture.c b/src/asahi/lib/agx_nir_lower_texture.c index ea071b682d4..dbc0564d4ee 100644 --- a/src/asahi/lib/agx_nir_lower_texture.c +++ b/src/asahi/lib/agx_nir_lower_texture.c @@ -240,8 +240,15 @@ lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) coord = nir_trim_vector(b, coord, lidx); /* Round layer to nearest even */ - if (!is_txf) - unclamped_layer = nir_f2u32(b, nir_fround_even(b, unclamped_layer)); + if (!is_txf) { + unclamped_layer = nir_fround_even(b, unclamped_layer); + + /* Explicitly round negative to avoid undefined behaviour when constant + * folding. This is load bearing on x86 builds. + */ + unclamped_layer = + nir_f2u32(b, nir_fmax(b, unclamped_layer, nir_imm_float(b, 0.0f))); + } /* For a cube array, the layer is zero-indexed component 3 of the * coordinate but the number of layers is component 2 of the txs result.