From efa9f242a8d4452fbe3c33fd3c9870e050d02e44 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Tue, 7 May 2024 16:58:04 -0400 Subject: [PATCH] agx: fix UB in cursor comparison padding here is implementation-defined, do the cleaner thing. fixes invalid IR generated with gcc but not clang. Signed-off-by: Alyssa Rosenzweig Reported-by: Janne Grunau Part-of: --- src/asahi/compiler/agx_compile.c | 2 +- src/asahi/compiler/agx_compiler.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 53965f2d105..4d5be6b184e 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -1082,7 +1082,7 @@ agx_emit_export(agx_builder *b, unsigned base, nir_src src) agx_export(&b_, chan, base + (c * stride)); } - if (memcmp(&b->cursor, &after_cursor, sizeof(agx_cursor)) == 0) { + if (agx_cursors_equal(b->cursor, after_cursor)) { b->cursor = agx_after_block_logical(b->cursor.block); } diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 6fba5cb429d..c48897b73da 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -801,6 +801,18 @@ typedef struct { }; } agx_cursor; +static inline bool +agx_cursors_equal(agx_cursor a, agx_cursor b) +{ + if (a.option != b.option) + return false; + + if (a.option == agx_cursor_after_block) + return a.block == b.block; + else + return a.instr == b.instr; +} + static inline agx_cursor agx_after_block(agx_block *block) {