{"id":2215640,"url":"http://patchwork.ozlabs.org/api/patches/2215640/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260324231418.3118-1-gerris.rs@gmail.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/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,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260324231418.3118-1-gerris.rs@gmail.com>","list_archive_url":null,"date":"2026-03-24T23:14:17","name":"[gccrs,COMMIT,1/2] intrinsic: Add ctlz and ctlz_nonzero intrinsics","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"4e61697f67713b6c7da0d03bd6c4edd1a1011ae6","submitter":{"id":92092,"url":"http://patchwork.ozlabs.org/api/people/92092/?format=json","name":null,"email":"gerris.rs@gmail.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260324231418.3118-1-gerris.rs@gmail.com/mbox/","series":[{"id":497359,"url":"http://patchwork.ozlabs.org/api/series/497359/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=497359","date":"2026-03-24T23:14:18","name":"[gccrs,COMMIT,1/2] intrinsic: Add ctlz and ctlz_nonzero intrinsics","version":1,"mbox":"http://patchwork.ozlabs.org/series/497359/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2215640/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2215640/checks/","tags":{},"related":[],"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=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=dB0e1dY9;\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=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=dB0e1dY9","sourceware.org;\n dmarc=pass (p=none dis=none) header.from=gmail.com","sourceware.org; spf=pass smtp.mailfrom=gmail.com","server2.sourceware.org;\n arc=none smtp.remote-ip=74.125.82.177"],"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 4fgQrR746zz1xy3\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 25 Mar 2026 10:15:51 +1100 (AEDT)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 86B5B4BB5886\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 24 Mar 2026 23:15:49 +0000 (GMT)","from mail-dy1-f177.google.com (mail-dy1-f177.google.com\n [74.125.82.177])\n by sourceware.org (Postfix) with ESMTPS id 09CC14BB3BFC\n for <gcc-patches@gcc.gnu.org>; Tue, 24 Mar 2026 23:14:22 +0000 (GMT)","by mail-dy1-f177.google.com with SMTP id\n 5a478bee46e88-2c15849aa2cso995454eec.0\n for <gcc-patches@gcc.gnu.org>; Tue, 24 Mar 2026 16:14:21 -0700 (PDT)","from\n runnervm46oaq.024wrwsfosyuda1nzj1jdj35te.dx.internal.cloudapp.net\n ([172.184.191.164]) by smtp.gmail.com with ESMTPSA id\n 5a478bee46e88-2c10b2d01f7sm16171559eec.23.2026.03.24.16.14.19\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Tue, 24 Mar 2026 16:14:20 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 86B5B4BB5886","OpenDKIM Filter v2.11.0 sourceware.org 09CC14BB3BFC"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 09CC14BB3BFC","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 09CC14BB3BFC","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774394062; cv=none;\n b=hQ4xkspHW3yInvLd6RMVdnHfKW7CWqFHY3FFxWJqQdGEdswKpbZZei6NJR42WVfT0rbtFEzB4KFjK16onmBNehkMeDdRSMHNRoSOBTIvUlLD1nUtcOIa+o06tUyiAiFIfoAmngvY1lDpg+54ICh6gzTGe2m70Ascq/lrn8JI7Vg=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1774394062; c=relaxed/simple;\n bh=2+qPvRBP8UMUcauKG0RhRW6YKNaC9kCAfsJSHZOgI78=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=VfNTv0IFm1qtrtbrhn6nqczBNSGLcFWJ44Fp7gsQv+D8t8yiwk6pqnZoi2vXtHOn9AvfCry/CB/ZmDUeuXLvt/gayifppWXu4cSr9LcQ/4KBXkX0Ou8j9SyMdRMZ9ZKZvl52Mha5JbaWuopQO8akxiL+OypHqSOym/fLluEYm7Y=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1774394061; x=1774998861; darn=gcc.gnu.org;\n h=content-transfer-encoding:mime-version:message-id:date:subject:cc\n :to:from:from:to:cc:subject:date:message-id:reply-to;\n bh=YBgmOr/RlEbzmwp/GBIf9LBZaA5AgilF2PaJnPLrWSc=;\n b=dB0e1dY9/nFVET2/JLFlM3wOklI83k3CvkbSeROdgA4nG8CAUwYo+C2A8OhFJQpdw+\n +sYIl8ADKi+tF3aVeFuuzX9q+U1ThJCLThLuGWBhwmi8kAyJTgYpFwvilumydx4OkM5V\n bYLvRny0ubHPuWYXyBSRZKgtg9boEAb9qREQ8Ac+2jMgWA/ZYvgX1mFoKc1pIR8jXj1V\n bYmIa+GDMb8br9peFH/t9wroD9qBydIyOiH11hUyDJ4xqTNHG0MUIrl+pEKcpAriUJvT\n VoHhaiSgXMbc4CTCsn7kzETe6bccUXRt3+y0zilPEQK/sJcRtLjKlxQGjca/KPw5zjgU\n FDcA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1774394061; x=1774998861;\n h=content-transfer-encoding:mime-version:message-id:date:subject:cc\n :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date\n :message-id:reply-to;\n bh=YBgmOr/RlEbzmwp/GBIf9LBZaA5AgilF2PaJnPLrWSc=;\n b=geqxChaOGS7KWdDugy9om6B2vGknKeMGIYwYzySZCFNoWuEnXfuHQDo6jhW26nSLW7\n FkIzrcwNPy9qGQlBSD+bTYNXY6oQAhG7ALWMrkTv2oPBHojjyDMKeXWMDgCR7Y0RQdXm\n Vx4wk64ityAmterstd1Z6wzIVCoh5xIgJx7ehbqfs/sSFEiV3nN/CUf/aox279aqWGCn\n IJFskGNtb9Z6HKNh4I12C2tIG+OQQ6HJcBF3bZbrLOMYm/2aOwq3AZlTImh2vTGapX2N\n p5/0+DaHRI8qvY9qLzdiD60vCJxjkV8iaKI1I5+hIhBtD1JiKiqQXzxbmJuwS6Blly58\n lIGw==","X-Gm-Message-State":"AOJu0Yxn/lqCltxSzCXiiBkFY+/uS+DwFfIJeNI5BfR43ybogsgfMsTa\n xEjs1mFktv0zBr5QsPPc6tI7xRADXMhudkdIVme+Apxocta6sl09Xs4U9ViWerYX","X-Gm-Gg":"ATEYQzxv1uepsSqWCaKtUP4rDx6QeqyFdju3gNIz7XdAMcEXVo2UzMeccR0izPvCrBj\n ZPYBAMxcskkQ4DSYodRFeyCZSn25f4rxkdUAnGzVeJnYLnK5bUk4ik0i/CstefD5fTmkVLwNEjE\n LQVsJ/AUDv2d5Qw0EridTPs7Exq4y4T4vPNzYlLZlK9VS+THi3Pp2RWWFpkkiPy0p2DnqA4Khjw\n +KPLH6wQxx+Y1Vz0WfvXGEhx9u7FKeO/dfaIi/w5Dy8wdiG52kV323TlwHnpxlYizhjKupWjkhz\n YvlTdEp7MQJdGan3tWxpHKGiuCqSK8LltaBgFxaMgpG4Y+sUbfIIFC3q6F5BjfVc2cRma0ZjcDn\n i/T1g4jQikoFNCXKjs9ifNrJw4MRalTAoYMFOojA2yBdKiDAgOld4og17aBY5MNQC5FRLfhn1ny\n eJlVWesVSaTtak1cibp+344D6dgWPSFlKD1i+VGoTAeOd5r/QMjNTwiTvvzZFUz55slOfE+D1jw\n Hnm/RmbbY7ZqAWSIxAPq7M=","X-Received":"by 2002:a05:693c:23c6:b0:2be:fe8:8b0d with SMTP id\n 5a478bee46e88-2c15d443bf9mr643410eec.22.1774394060698;\n Tue, 24 Mar 2026 16:14:20 -0700 (PDT)","From":"gerris.rs@gmail.com","To":"gcc-patches@gcc.gnu.org,\n\tgcc-rust@gcc.gnu.org","Cc":"pierre-emmanuel.patry@embecosm.com, arthur.cohen@embecosm.com,\n dkm+gccrs@kataplop.net, Mohamed Ali <mohmedali1462005@gmail.com>","Subject":"[gccrs COMMIT 1/2] intrinsic: Add ctlz and ctlz_nonzero intrinsics","Date":"Tue, 24 Mar 2026 23:14:17 +0000","Message-ID":"<20260324231418.3118-1-gerris.rs@gmail.com>","X-Mailer":"git-send-email 2.43.0","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---\nThis change was merged into the gccrs repository and is posted here for\nupstream visibility and potential drive-by review, as requested by GCC\nrelease managers.\nEach commit email contains a link to its details on github from where you can\nfind the Pull-Request and associated discussions.\n\n\nCommit on github: https://github.com/Rust-GCC/gccrs/commit/d3927c9fadd97b76cfa74f13563257992024f128\n\nThe commit has been mentioned in the following pull-request(s):\n - https://github.com/Rust-GCC/gccrs/pull/4467\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\n\n\nbase-commit: 3deeccb2c85b3daab49edbfd957527b9e835001d","diff":"diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc\nindex a4924542d..9263883e9 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 5c7854ce8..eb86c8ad9 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 06174a535..52462407f 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 6754392a0..1b2d968b9 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 000000000..b886be774\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 000000000..bf12728d2\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 000000000..bd8afba22\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 000000000..3476ab2b9\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":["gccrs","COMMIT","1/2"]}