{"id":2223276,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2223276/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260414231905.191063-10-arthur.cohen@embecosm.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<20260414231905.191063-10-arthur.cohen@embecosm.com>","date":"2026-04-14T23:18:31","name":"[COMMITTED,10/43] gccrs: intrinsic: Add ctlz and ctlz_nonzero intrinsics","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"4e61697f67713b6c7da0d03bd6c4edd1a1011ae6","submitter":{"id":83476,"url":"http://patchwork.ozlabs.org/api/1.1/people/83476/?format=json","name":"Arthur Cohen","email":"arthur.cohen@embecosm.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260414231905.191063-10-arthur.cohen@embecosm.com/mbox/","series":[{"id":499900,"url":"http://patchwork.ozlabs.org/api/1.1/series/499900/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=499900","date":"2026-04-14T23:18:24","name":"[COMMITTED,01/43] gccrs: testsuite:Add a testcase for setup_associated_types","version":1,"mbox":"http://patchwork.ozlabs.org/series/499900/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2223276/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2223276/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256\n header.s=google header.b=TVBm48vC;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256\n header.s=google header.b=TVBm48vC","sourceware.org;\n dmarc=none (p=none dis=none) header.from=embecosm.com","sourceware.org; spf=pass smtp.mailfrom=embecosm.com","server2.sourceware.org;\n arc=none smtp.remote-ip=209.85.128.67"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fwKw92Q44z1yHc\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 15 Apr 2026 09:18:49 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 5E9BF4BA23EA\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 14 Apr 2026 23:18:47 +0000 (GMT)","from mail-wm1-f67.google.com (mail-wm1-f67.google.com\n [209.85.128.67])\n by sourceware.org (Postfix) with ESMTPS id 89DC24BA23EF\n for <gcc-patches@gcc.gnu.org>; Tue, 14 Apr 2026 23:14:24 +0000 (GMT)","by mail-wm1-f67.google.com with SMTP id\n 5b1f17b1804b1-488a14c31eeso50218905e9.0\n for <gcc-patches@gcc.gnu.org>; Tue, 14 Apr 2026 16:14:24 -0700 (PDT)","from platypus.localdomain (176-147-231-59.abo.bbox.fr.\n [176.147.231.59]) by smtp.gmail.com with ESMTPSA id\n 5b1f17b1804b1-488f1dd8806sm3325945e9.3.2026.04.14.16.14.21\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Tue, 14 Apr 2026 16:14:21 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 5E9BF4BA23EA","OpenDKIM Filter v2.11.0 sourceware.org 89DC24BA23EF"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 89DC24BA23EF","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 89DC24BA23EF","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776208464; cv=none;\n b=mH0dgJwtviSsD6msSKD8LCm/VUlTWvIzzJ34xU1qvfFyzblOK/2U3ue6sGFp/Vb4jz7gOlTos1DHkglrc8JYE9WpTE1VRR1i1/IQuRaABU/Qad5ir4svDxhaC5hfBxUukGVR/eZYIYsZTIvL7z0XiF1ejVImrmwpxHbMVySdwkc=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776208464; c=relaxed/simple;\n bh=rTrozMreYtIrCSQcQpRYSh8/57+gWUuKOWOLAVTK+Tw=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=jy97K/NIt1ucrCsHkJqAA1QNr8Nmp4QF7xxBJGjVzqJG2iJtqBRvNNq61+byxzR5do76LOPXUc0MeailkG/J9Cqgw2XdhK4ufxWQrBOGQbdp3378BUueYUKsiBr9y+zH6Pt2GT8brtH6xoqsjeeO1Ijl+D/MsaO+tr+coRip7DU=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=embecosm.com; s=google; t=1776208463; x=1776813263; darn=gcc.gnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=Pj2/vs48ZPrS7c1MOAY+cYVihxafwxRCLHSlGMCflkQ=;\n b=TVBm48vC93czRmaY5/o5vfmfFAyOknwR3gS37g2PcMTgMpq9b38Rhi5+yZTM9M120A\n coKL3bnG73hKhwTKl6kgkZFGrDFk7zh+x9GiPWuosIRBT5azOwjoU6eFJVG/BUOkoj6z\n cqgponcV6w7MIbVFvQsSiGWdgiJB8ImnqdlsLfYuNq3XMY7himf9dE16I17rfF218CQB\n iqHpBshNtrVYup9MKmoTMuT0UrUTe1vUoZGlZHcFciONIatw+J/bkBdMfOmDb1q0bd9x\n QPWkJqMctiWxBBteIl3/FptJC+nU6GzooT9PRIVCKCOkD9JtcwUqXJjokPnDcOoOqNOM\n o8YQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776208463; x=1776813263;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=Pj2/vs48ZPrS7c1MOAY+cYVihxafwxRCLHSlGMCflkQ=;\n b=hGMlk8sk0HC/y21J6YXdaOv/oGprcVPyyVsHP2dwNTyCbSo72aCqnLZOu87KEFxCVo\n JWJYjZqtJ71EnZydpl4fSxb3TLw1DZ3lHdLV2Iv2jhy51wZO8Ma0hy9EtbZv3MhGHGC3\n stpAIQ+Tk/rr7+mDblU5wyu061rgCwIYp76XOE2gxYkpceDKMJ8/r2UqTlFFaxLWsOY8\n KsIYYJd5uJTwXekzQ5IqIlbei/bahbf60iFFU6WPpS/jLgWPCgJoDwzT19BjtW+HTytI\n NsuLtWSiYODFK5jlIayGYVrielX+VaHVidgH7phnGXEU+IV2Asptf3Tjnt5/sRZ2Bj9F\n y3tQ==","X-Gm-Message-State":"AOJu0Yx8J1KbwvcIvxtbUtXOVOpM7hf7eyZAlTviN8Dto5ui/Zfndfaq\n IjwpDZ8uTMTZPPizreSCC1sa8ayFFVDC8UKVHvyBMGQI5F7nZ6i0+fnTE74qysz7nXbNiqxKaqI\n ucJddkf/w","X-Gm-Gg":"AeBDieu2RKMeTEBnmpg0DDTZR7nrTQ1+J+X3Ec7pyfuHSKjLnyq2VVYCsGsW7tRysR9\n 7mi+IVX7r085lPrVi+bid9JGu8kMiyIEeeY7cx2Uz5C7JqjBkC2BV62+Viks62B9A85mG1/wAhT\n HzH5IKImzmIHx8ncoXMkTMvHemqblECeK6Il1of7nv5iDV6GsfdDepddtQgrS5dMqL6MBPA8d2x\n KwBeBOTcuZZKPwbh76Nvn1VPRvQb+gKyVgrkcxFCcT5XytXSFpYY9o3X+q1mPIH+Av2dxW+PyQc\n N7uSEdTtNT+PwVtHh/Ty7fJqIXO8rpFTLVcQ62VlRtSCHSY/qbFsSA05rPGy/VlYZOqIE6bsDwn\n csCN1oqk2NWSXpd2VbrWKDYRjDVhiBBvlDwOe0RxEAk8q2ggz8CA/Icwj4aeAmwAX4sNTxIFq1y\n qyIcAlW9bLCAP2LMtEYA4/On/vxBBJprzuNLnn8cZUd+UVOr1fo/ctleU/hpTr/wuX1hr8KkgtE\n qELNQ==","X-Received":"by 2002:a05:600c:64cf:b0:485:b6dd:5066 with SMTP id\n 5b1f17b1804b1-488d67ce664mr257009515e9.7.1776208463141;\n Tue, 14 Apr 2026 16:14:23 -0700 (PDT)","From":"arthur.cohen@embecosm.com","To":"gcc-patches@gcc.gnu.org","Cc":"gcc-rust@gcc.gnu.org,\n\tMohamed Ali <mohmedali1462005@gmail.com>","Subject":"[COMMITTED 10/43] gccrs: intrinsic: Add ctlz and ctlz_nonzero\n intrinsics","Date":"Wed, 15 Apr 2026 01:18:31 +0200","Message-ID":"<20260414231905.191063-10-arthur.cohen@embecosm.com>","X-Mailer":"git-send-email 2.50.1","In-Reply-To":"<20260414231905.191063-1-arthur.cohen@embecosm.com>","References":"<20260414231905.191063-1-arthur.cohen@embecosm.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"From: Mohamed Ali <mohmedali1462005@gmail.com>\n\nThis commit introduces the `ctlz` and `ctlz_nonzero` intrinsics for counting leading zeros\nin integer types.\nThe implementation includes handlers for both intrinsics, which validate input types and utilize\nGCC built-ins for the actual computation.\n\nAddresses: Rust-GCC#658\n\ngcc/rust/ChangeLog:\n\n\t* backend/rust-compile-intrinsic.cc: Register ctlz and ctlz_nonzero intrinsics.\n\t* backend/rust-intrinsic-handlers.cc (ctlz_handler): Implement ctlz_handler.\n\t(ctlz_nonzero_handler): Implement ctlz_nonzero_handler.\n\t* backend/rust-intrinsic-handlers.h (ctlz_handler): Add decl for ctlz_handler.\n\t(ctlz_nonzero_handler): Add decl for ctlz_nonzero_handler.\n\t* util/rust-intrinsic-values.h: map ctlz_nonzero to its name.\n\ngcc/testsuite/ChangeLog:\n\n\t* rust/compile/ctlz.rs: New test.\n\t* rust/compile/ctlz_nonzero.rs: New test.\n\t* rust/execute/torture/ctlz.rs: New test.\n\t* rust/execute/torture/ctlz_nonzero.rs: New test.\n\nSigned-off-by: Mohamed Ali <mohmedali1462005@gmail.com>\n---\n gcc/rust/backend/rust-compile-intrinsic.cc    |   4 +-\n gcc/rust/backend/rust-intrinsic-handlers.cc   | 157 ++++++++++++++++++\n gcc/rust/backend/rust-intrinsic-handlers.h    |   2 +\n gcc/rust/util/rust-intrinsic-values.h         |   1 +\n gcc/testsuite/rust/compile/ctlz.rs            |  17 ++\n gcc/testsuite/rust/compile/ctlz_nonzero.rs    |  19 +++\n gcc/testsuite/rust/execute/torture/ctlz.rs    |  56 +++++++\n .../rust/execute/torture/ctlz_nonzero.rs      |  46 +++++\n 8 files changed, 301 insertions(+), 1 deletion(-)\n create mode 100644 gcc/testsuite/rust/compile/ctlz.rs\n create mode 100644 gcc/testsuite/rust/compile/ctlz_nonzero.rs\n create mode 100644 gcc/testsuite/rust/execute/torture/ctlz.rs\n create mode 100644 gcc/testsuite/rust/execute/torture/ctlz_nonzero.rs","diff":"diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc\nindex a4924542da3..9263883e9a5 100644\n--- a/gcc/rust/backend/rust-compile-intrinsic.cc\n+++ b/gcc/rust/backend/rust-compile-intrinsic.cc\n@@ -68,7 +68,9 @@ static const std::map<std::string, handlers::HandlerBuilder> generic_intrinsics\n      {IValue::CATCH_UNWIND, handlers::try_handler (true)},\n      {IValue::DISCRIMINANT_VALUE, handlers::discriminant_value},\n      {IValue::VARIANT_COUNT, handlers::variant_count},\n-     {IValue::BSWAP, handlers::bswap_handler}};\n+     {IValue::BSWAP, handlers::bswap_handler},\n+     {IValue::CTLZ, handlers::ctlz_handler},\n+     {IValue::CTLZ_NONZERO, handlers::ctlz_nonzero_handler}};\n \n Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}\n \ndiff --git a/gcc/rust/backend/rust-intrinsic-handlers.cc b/gcc/rust/backend/rust-intrinsic-handlers.cc\nindex 5c7854ce8de..eb86c8ad959 100644\n--- a/gcc/rust/backend/rust-intrinsic-handlers.cc\n+++ b/gcc/rust/backend/rust-intrinsic-handlers.cc\n@@ -748,6 +748,151 @@ atomic_load (Context *ctx, TyTy::FnType *fntype, int ordering)\n   return fndecl;\n }\n \n+// Shared inner implementation for ctlz and ctlz_nonzero.\n+//\n+// nonzero=false → ctlz: ctlz(0) is well-defined in Rust and must return\n+//   bit_size, but __builtin_clz*(0) is undefined behaviour in C, so an\n+//   explicit arg==0 guard is emitted.\n+//\n+// nonzero=true → ctlz_nonzero: the caller guarantees arg != 0 (passing 0\n+//   is immediate UB in Rust), so the zero guard is omitted entirely.\n+static tree\n+ctlz_handler (Context *ctx, TyTy::FnType *fntype, bool nonzero)\n+{\n+  rust_assert (fntype->get_params ().size () == 1);\n+\n+  tree lookup = NULL_TREE;\n+  if (check_for_cached_intrinsic (ctx, fntype, &lookup))\n+    return lookup;\n+\n+  auto fndecl = compile_intrinsic_function (ctx, fntype);\n+\n+  std::vector<Bvariable *> param_vars;\n+  compile_fn_params (ctx, fntype, fndecl, &param_vars);\n+\n+  auto arg_param = param_vars.at (0);\n+  if (!Backend::function_set_parameters (fndecl, param_vars))\n+    return error_mark_node;\n+\n+  rust_assert (fntype->get_num_substitutions () == 1);\n+  auto *monomorphized_type\n+    = fntype->get_substs ().at (0).get_param_ty ()->resolve ();\n+  if (!check_for_basic_integer_type (\"ctlz\", fntype->get_locus (),\n+\t\t\t\t     monomorphized_type))\n+    return error_mark_node;\n+\n+  enter_intrinsic_block (ctx, fndecl);\n+\n+  // BUILTIN ctlz FN BODY BEGIN\n+  auto locus = fntype->get_locus ();\n+  auto arg_expr = Backend::var_expression (arg_param, locus);\n+  tree arg_type = TREE_TYPE (arg_expr);\n+  unsigned bit_size = TYPE_PRECISION (arg_type);\n+\n+  // Convert signed types to their same-width unsigned equivalent before\n+  // widening.  Without this, widening a signed type sign-extends it.\n+  // For example, i8(-1) = 0xFF widened to u32 gives 0xFFFFFFFF, so\n+  // __builtin_clz(0xFFFFFFFF) = 0, then 0 - diff(24) = -24.\n+  // Converting to u8 first gives 0xFF → 0x000000FF (zero-extended), so\n+  // __builtin_clz(0x000000FF) = 24, then 24 - 24 = 0.\n+  tree unsigned_type\n+    = !TYPE_UNSIGNED (arg_type) ? unsigned_type_for (arg_type) : arg_type;\n+  tree unsigned_arg = fold_convert (unsigned_type, arg_expr);\n+\n+  // Pick the narrowest GCC clz builtin whose operand type is wide enough to\n+  // hold bit_size bits.  diff records how many extra leading zeros the builtin\n+  // will count due to the width difference and is subtracted from the result.\n+  //\n+  // Example: ctlz(1u8) bit_size=8, int_prec=32, diff=24.\n+  //   __builtin_clz(1u) returns 31 (counts from bit 31 down to bit 0).\n+  //   31 - 24 = 7, which is the correct answer for an 8-bit value.\n+  //\n+  // TODO: 128-bit integers are not yet handled.\n+  unsigned int_prec = TYPE_PRECISION (unsigned_type_node);\n+  unsigned long_prec = TYPE_PRECISION (long_unsigned_type_node);\n+  unsigned longlong_prec = TYPE_PRECISION (long_long_unsigned_type_node);\n+\n+  const char *builtin_name = nullptr;\n+  tree cast_type = NULL_TREE;\n+  int diff = 0;\n+\n+  if (bit_size <= int_prec)\n+    {\n+      // Fits in unsigned int: covers 8/16/32-bit integers on most targets.\n+      builtin_name = \"__builtin_clz\";\n+      cast_type = unsigned_type_node;\n+      diff = static_cast<int> (int_prec - bit_size);\n+    }\n+  else if (bit_size <= long_prec)\n+    {\n+      // Fits in unsigned long but not unsigned int.\n+      builtin_name = \"__builtin_clzl\";\n+      cast_type = long_unsigned_type_node;\n+      diff = static_cast<int> (long_prec - bit_size);\n+    }\n+  else if (bit_size <= longlong_prec)\n+    {\n+      // Fits in unsigned long long but not unsigned long.\n+      builtin_name = \"__builtin_clzll\";\n+      cast_type = long_long_unsigned_type_node;\n+      diff = static_cast<int> (longlong_prec - bit_size);\n+    }\n+  else\n+    {\n+      rust_sorry_at (locus, \"ctlz for %u-bit integers is not yet implemented\",\n+\t\t     bit_size);\n+      return error_mark_node;\n+    }\n+\n+  // Widen the unsigned arg to the chosen builtin's operand type, call it,\n+  // then subtract the padding bits.  diff == 0 means the Rust type exactly\n+  // matches the builtin's operand width, so the subtraction is skipped.\n+  tree call_arg = fold_convert (cast_type, unsigned_arg);\n+\n+  tree builtin_decl = error_mark_node;\n+  BuiltinsContext::get ().lookup_simple_builtin (builtin_name, &builtin_decl);\n+  rust_assert (builtin_decl != error_mark_node);\n+\n+  tree builtin_fn = build_fold_addr_expr_loc (locus, builtin_decl);\n+  tree clz_expr\n+    = Backend::call_expression (builtin_fn, {call_arg}, nullptr, locus);\n+\n+  if (diff > 0)\n+    {\n+      tree diff_cst = build_int_cst (integer_type_node, diff);\n+      clz_expr\n+\t= fold_build2 (MINUS_EXPR, integer_type_node, clz_expr, diff_cst);\n+    }\n+\n+  clz_expr = fold_convert (uint32_type_node, clz_expr);\n+\n+  tree final_expr;\n+  if (!nonzero)\n+    {\n+      // ctlz(0) must return bit_size per the Rust reference.\n+      // We cannot pass 0 to __builtin_clz* (UB), so emit:\n+      //   arg == 0 ? bit_size : clz_expr\n+      tree zero = build_int_cst (arg_type, 0);\n+      tree cmp = fold_build2 (EQ_EXPR, boolean_type_node, arg_expr, zero);\n+      tree width_cst = build_int_cst (uint32_type_node, bit_size);\n+      final_expr\n+\t= fold_build3 (COND_EXPR, uint32_type_node, cmp, width_cst, clz_expr);\n+    }\n+  else\n+    {\n+      // ctlz_nonzero: arg != 0 is guaranteed by the caller, no guard needed.\n+      final_expr = clz_expr;\n+    }\n+\n+  tree result = fold_convert (TREE_TYPE (DECL_RESULT (fndecl)), final_expr);\n+  auto return_stmt = Backend::return_statement (fndecl, result, locus);\n+  ctx->add_statement (return_stmt);\n+  // BUILTIN ctlz FN BODY END\n+\n+  finalize_intrinsic_block (ctx, fndecl);\n+  return fndecl;\n+}\n+\n } // namespace inner\n \n const HandlerBuilder\n@@ -1530,6 +1675,18 @@ bswap_handler (Context *ctx, TyTy::FnType *fntype)\n   return fndecl;\n }\n \n+tree\n+ctlz_handler (Context *ctx, TyTy::FnType *fntype)\n+{\n+  return inner::ctlz_handler (ctx, fntype, false);\n+}\n+\n+tree\n+ctlz_nonzero_handler (Context *ctx, TyTy::FnType *fntype)\n+{\n+  return inner::ctlz_handler (ctx, fntype, true);\n+}\n+\n } // namespace handlers\n } // namespace Compile\n } // namespace Rust\ndiff --git a/gcc/rust/backend/rust-intrinsic-handlers.h b/gcc/rust/backend/rust-intrinsic-handlers.h\nindex 06174a53574..52462407f82 100644\n--- a/gcc/rust/backend/rust-intrinsic-handlers.h\n+++ b/gcc/rust/backend/rust-intrinsic-handlers.h\n@@ -65,6 +65,8 @@ tree assume (Context *ctx, TyTy::FnType *fntype);\n tree discriminant_value (Context *ctx, TyTy::FnType *fntype);\n tree variant_count (Context *ctx, TyTy::FnType *fntype);\n tree bswap_handler (Context *ctx, TyTy::FnType *fntype);\n+tree ctlz_handler (Context *ctx, TyTy::FnType *fntype);\n+tree ctlz_nonzero_handler (Context *ctx, TyTy::FnType *fntype);\n \n tree prefetch_data (Context *ctx, TyTy::FnType *fntype, Prefetch kind);\n \ndiff --git a/gcc/rust/util/rust-intrinsic-values.h b/gcc/rust/util/rust-intrinsic-values.h\nindex 6754392a063..1b2d968b908 100644\n--- a/gcc/rust/util/rust-intrinsic-values.h\n+++ b/gcc/rust/util/rust-intrinsic-values.h\n@@ -81,6 +81,7 @@ public:\n   static constexpr auto &CALLER_LOCATION = \"caller_location\";\n   static constexpr auto &CTPOP = \"ctpop\";\n   static constexpr auto &CTLZ = \"ctlz\";\n+  static constexpr auto &CTLZ_NONZERO = \"ctlz_nonzero\";\n   static constexpr auto &CTTZ = \"cttz\";\n   static constexpr auto &BSWAP = \"bswap\";\n   static constexpr auto &BITREVERSE = \"bitreverse\";\ndiff --git a/gcc/testsuite/rust/compile/ctlz.rs b/gcc/testsuite/rust/compile/ctlz.rs\nnew file mode 100644\nindex 00000000000..b886be774ea\n--- /dev/null\n+++ b/gcc/testsuite/rust/compile/ctlz.rs\n@@ -0,0 +1,17 @@\n+// { dg-do compile }\n+#![feature(intrinsics, lang_items, no_core)]\n+#![no_core]\n+\n+#[lang = \"sized\"]\n+pub trait Sized {}\n+\n+#[lang = \"copy\"]\n+pub trait Copy {}\n+\n+extern \"rust-intrinsic\" {\n+    pub fn ctlz<T>(x: T) -> u32; // { dg-error \"ctlz intrinsics can only be used with basic integer types .got 'bool'.\" }\n+}\n+\n+fn main() {\n+    let _ = ctlz(true);\n+}\ndiff --git a/gcc/testsuite/rust/compile/ctlz_nonzero.rs b/gcc/testsuite/rust/compile/ctlz_nonzero.rs\nnew file mode 100644\nindex 00000000000..bf12728d215\n--- /dev/null\n+++ b/gcc/testsuite/rust/compile/ctlz_nonzero.rs\n@@ -0,0 +1,19 @@\n+// { dg-do compile }\n+#![feature(intrinsics, lang_items, no_core)]\n+#![no_core]\n+\n+#[lang = \"sized\"]\n+pub trait Sized {}\n+\n+#[lang = \"copy\"]\n+pub trait Copy {}\n+\n+extern \"rust-intrinsic\" {\n+    pub fn ctlz_nonzero<T>(x: T) -> u32; // { dg-error \"ctlz intrinsics can only be used with basic integer types .got 'bool'.\" }\n+}\n+\n+fn main() {\n+    unsafe {\n+        let _ = ctlz_nonzero(true);\n+    }\n+}\ndiff --git a/gcc/testsuite/rust/execute/torture/ctlz.rs b/gcc/testsuite/rust/execute/torture/ctlz.rs\nnew file mode 100644\nindex 00000000000..bd8afba223f\n--- /dev/null\n+++ b/gcc/testsuite/rust/execute/torture/ctlz.rs\n@@ -0,0 +1,56 @@\n+#![feature(no_core)]\n+#![no_core]\n+#![feature(intrinsics)]\n+#![feature(lang_items)]\n+\n+#[lang = \"sized\"]\n+pub trait Sized {}\n+\n+extern \"rust-intrinsic\" {\n+    pub fn ctlz<T>(x: T) -> u32;\n+    pub fn abort() -> !;\n+}\n+\n+fn main() -> i32 {\n+    if ctlz(0u8) != 8 {\n+        abort();\n+    }\n+    if ctlz(1u8) != 7 {\n+        abort();\n+    }\n+    if ctlz(255u8) != 0 {\n+        abort();\n+    }\n+\n+    if ctlz(0u16) != 16 {\n+        abort();\n+    }\n+    if ctlz(1u16) != 15 {\n+        abort();\n+    }\n+    if ctlz(0xFFFFu16) != 0 {\n+        abort();\n+    }\n+\n+    if ctlz(0u32) != 32 {\n+        abort();\n+    }\n+    if ctlz(1u32) != 31 {\n+        abort();\n+    }\n+    if ctlz(0xFFFFFFFFu32) != 0 {\n+        abort();\n+    }\n+\n+    if ctlz(0u64) != 64 {\n+        abort();\n+    }\n+    if ctlz(1u64) != 63 {\n+        abort();\n+    }\n+    if ctlz(!0u64) != 0 {\n+        abort();\n+    }\n+\n+    0\n+}\ndiff --git a/gcc/testsuite/rust/execute/torture/ctlz_nonzero.rs b/gcc/testsuite/rust/execute/torture/ctlz_nonzero.rs\nnew file mode 100644\nindex 00000000000..3476ab2b9a6\n--- /dev/null\n+++ b/gcc/testsuite/rust/execute/torture/ctlz_nonzero.rs\n@@ -0,0 +1,46 @@\n+#![feature(no_core)]\n+#![no_core]\n+#![feature(intrinsics)]\n+#![feature(lang_items)]\n+\n+#[lang = \"sized\"]\n+pub trait Sized {}\n+\n+extern \"rust-intrinsic\" {\n+    pub fn ctlz_nonzero<T>(x: T) -> u32;\n+    pub fn abort() -> !;\n+}\n+\n+fn main() -> i32 {\n+    unsafe {\n+        if ctlz_nonzero(1u8) != 7 {\n+            abort();\n+        }\n+        if ctlz_nonzero(255u8) != 0 {\n+            abort();\n+        }\n+\n+        if ctlz_nonzero(1u16) != 15 {\n+            abort();\n+        }\n+        if ctlz_nonzero(0xFFFFu16) != 0 {\n+            abort();\n+        }\n+\n+        if ctlz_nonzero(1u32) != 31 {\n+            abort();\n+        }\n+        if ctlz_nonzero(0xFFFFFFFFu32) != 0 {\n+            abort();\n+        }\n+\n+        if ctlz_nonzero(1u64) != 63 {\n+            abort();\n+        }\n+        if ctlz_nonzero(!0u64) != 0 {\n+            abort();\n+        }\n+    }\n+\n+    0\n+}\n","prefixes":["COMMITTED","10/43"]}