From 3624f054f2dfef7b36a6f6795e0e3faa6c6c9f8c Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 31 Jul 2025 09:19:31 -0400 Subject: [PATCH] nir: add nir_def_as_* helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to get rid of nir_def::parent_instr eventually, requiring an accessor function instead nir_def_parent_instr(def), so to mitigate the hit to NIR ergonomics, let's add helpers for common patterns using parent_instr. This gets us an immediate win for NIR ergonomics and then reduces the surface area for the later flag day hiding parent_instr. This commit starts us off by adding compositions for nir_instr_as_* with parent_instr's, which are common. Signed-off-by: Alyssa Rosenzweig Reviewed-by: Emma Anholt Reviewed-by: Marek Olšák Acked-by: Karol Herbst Reviewed-by: Konstantin Seurer Part-of: --- src/compiler/nir/nir.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 99e8f392a30..e24c58bcdec 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -2853,6 +2853,21 @@ NIR_DEFINE_SRC_AS_CONST(double, float) #undef NIR_DEFINE_SRC_AS_CONST +#define NIR_DEFINE_DEF_AS_INSTR(type, suffix) \ + static inline type *nir_def_as_##suffix(const nir_def *def) \ + { \ + return nir_instr_as_##suffix(def->parent_instr); \ + } + +NIR_DEFINE_DEF_AS_INSTR(nir_alu_instr, alu) +NIR_DEFINE_DEF_AS_INSTR(nir_intrinsic_instr, intrinsic) +NIR_DEFINE_DEF_AS_INSTR(nir_tex_instr, tex) +NIR_DEFINE_DEF_AS_INSTR(nir_phi_instr, phi) +NIR_DEFINE_DEF_AS_INSTR(nir_deref_instr, deref) +NIR_DEFINE_DEF_AS_INSTR(nir_load_const_instr, load_const) + +#undef NIR_DEFINE_DEF_AS_INSTR + typedef struct nir_scalar { nir_def *def; unsigned comp;