From patchwork Tue Apr 9 13:55:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Sandoe X-Patchwork-Id: 1921411 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=VHo8HZsW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VDSDT3jRxz1yZg for ; Tue, 9 Apr 2024 23:56:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC835385842C for ; Tue, 9 Apr 2024 13:56:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id 3DC963858C39 for ; Tue, 9 Apr 2024 13:55:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DC963858C39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3DC963858C39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712670957; cv=none; b=CyFnOal4EFyf79SPawqL7TdHOIowTHZnqK9JxfT+oU8RkUaTP43LwWJQkKNQCtuNh6mj71mD/ZwPSLREJ2EB3JU6pTmH1BlTVKBcv4vshoP6ER1YYMQQGLP9Cr+z1mLd4XlBNV8hxe512pPefQWiB/6omLrlhQAoV/VW+vG04g4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712670957; c=relaxed/simple; bh=mUrFI3pDvDMcnOU29xsP4QudiG6wXJ7lVGK4N7+w3H8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=BZZyTXPirf0fpZofCudhirSHE2hYfCSOgSvvMWRxW7hwBt9tvrv5OMBX9YYkJQHTP0bSxht+Ar6lm8SUd2WGoaIMrzXmTxcMse4aaDVjD9hOCT8tJ+Mv/m3/AHJc+h3/i5gAyuFPq8kQABypc9tT9t29O+yTEwXXMJnatXB/vlQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-416b66163a9so3969295e9.2 for ; Tue, 09 Apr 2024 06:55:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712670954; x=1713275754; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=AIvMcfcIqOTjvzMcSNce5OizxdOe13lLQBho4eUj5V0=; b=VHo8HZsW1RVnhvXc8LqntNUmzjZkSJ72U7bc2MytiGZDlw/a0Je2E/KXxwhr/RnB8L 6CgPfBoBePzv23DnQeOEfE7rj3RTM09or041YRnyOKI61MUDDoz3+WM4doX7+KWCuM7o 9oqbdhQNDLyxyhLT4w/n2ks1q4PSeEQp4pNUMhOYsBq71AIU3EjTwkTuFaxELaTyeBnS hywFzWXOfAbMbCdsVIZy7vzjccnDpRtNOqbrzl8ips/e4zvYJ8mn+ccKFR8/1uitS3MB 9V275Eq1jH+/8MG8kULjKb2CkNxHIGOrwYVVzFmwtpfmp3SPmlDbNO6XjblVy3G1TSZV ByXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712670954; x=1713275754; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AIvMcfcIqOTjvzMcSNce5OizxdOe13lLQBho4eUj5V0=; b=M8yeErYc/GAv+Dq0A7VEjflVkBVhPjx7f22vda2sEJYj43xSoqsy0XQoiPWDP8vilY Cs1PBQk+Z5pm7IiDAeXpAFWmX9F4iJiiB9+BfYgfqBra0TgZxiqv1W4DahhMuFkzYa7U Cm4Vwd8UxYI65fHi8HXt5J69ofz9s2/skFe4UUJSeOff/G4K7s59nQp0sZvx0nIvUtum PZbN2kc02epdkBFTz1vhkGdBtWLCnqfd79Ms9zUk5jcBP93jFs7owVwTqNTvaR4oGN+C kH1EeJD0Urd0q8Ysm73KqOx6OhRIJMWJEYaD5MSljsR0l8OfmzkfMt8ss6zEZjWXoWbV AHGQ== X-Gm-Message-State: AOJu0YwhRQ+xS68yTmk2fHrRI/Sb374dlxRPrgjhpAgh0/nzJPdZcj/l D+C6YEPLx/JuYI1Io4WmaA5zcTyh3vEp0hNYBdd/FyeX03ST43rEHvSafHsa X-Google-Smtp-Source: AGHT+IFCx3Q0gsQbktEicRqAQuRRjEI2RZUGmeOXRGw0XHgXGI1lFQ5RVyMEbW0QxgxfxWvgP1Y7xA== X-Received: by 2002:a05:600c:470d:b0:416:b8da:c998 with SMTP id v13-20020a05600c470d00b00416b8dac998mr692476wmo.24.1712670953545; Tue, 09 Apr 2024 06:55:53 -0700 (PDT) Received: from localhost.localdomain (host81-138-1-83.in-addr.btopenworld.com. [81.138.1.83]) by smtp.gmail.com with ESMTPSA id b3-20020a05600c4e0300b00416920ca56csm4868701wmq.42.2024.04.09.06.55.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Apr 2024 06:55:53 -0700 (PDT) From: Iain Sandoe X-Google-Original-From: Iain Sandoe To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH/RFC] target, hooks: Allow a target to trap on unreachable [PR109267]. Date: Tue, 9 Apr 2024 14:55:52 +0100 Message-Id: <20240409135552.79103-1-iain@sandoe.co.uk> X-Mailer: git-send-email 2.39.2 (Apple Git-143) MIME-Version: 1.0 X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: iain@sandoe.co.uk Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org So far, tested lightly on aarch64-darwin; if this is acceptable then it will be possible to back out of the ad hoc fixes used on x86 and powerpc darwin. Comments welcome, thanks, Iain --- 8< --- In the PR cited case a target linker cannot handle enpty FDEs, arguably this is a linker bug - but in some cases we might still wish to work around it. In the case of Darwin, the ABI does not allow two global symbols to have the same address, so that emitting empty functions has potential (almost guarantee) to break ABI. This patch allows a target to ask that __builtin_unreachable is expanded in the same way as __builtin_trap (either to a trap instruction or to abort() if there is no such insn). This means that the middle end's use of unreachability for optimisation should not be altered. __builtin_unreachble is currently expanded to a barrier and __builtin_trap is expanded to a trap insn + a barrier so that it seems we should not be unduly affecting RTL optimisations. For Darwin, we enable this by default, but allow it to be disabled per TU using -mno-unreachable-traps. PR middle-end/109267 gcc/ChangeLog: * builtins.cc (expand_builtin_unreachable): Allow for a target to expand this as a trap. * config/darwin-protos.h (darwin_unreachable_traps_p): New. * config/darwin.cc (darwin_unreachable_traps_p): New. * config/darwin.h (TARGET_UNREACHABLE_SHOULD_TRAP): New. * config/darwin.opt (munreachable-traps): New. * doc/invoke.texi: Document -munreachable-traps. * doc/tm.texi: Regenerate. * doc/tm.texi.in: Document TARGET_UNREACHABLE_SHOULD_TRAP. * target.def (TARGET_UNREACHABLE_SHOULD_TRAP): New hook. Signed-off-by: Iain Sandoe --- gcc/builtins.cc | 7 +++++++ gcc/config/darwin-protos.h | 1 + gcc/config/darwin.cc | 7 +++++++ gcc/config/darwin.h | 4 ++++ gcc/config/darwin.opt | 4 ++++ gcc/doc/invoke.texi | 7 ++++++- gcc/doc/tm.texi | 5 +++++ gcc/doc/tm.texi.in | 2 ++ gcc/target.def | 10 ++++++++++ 9 files changed, 46 insertions(+), 1 deletion(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index f8d94c4b435..13f321b6be6 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -5929,6 +5929,13 @@ expand_builtin_trap (void) static void expand_builtin_unreachable (void) { + /* If the target wants a trap in place of the fall-through, use that. */ + if (targetm.unreachable_should_trap ()) + { + expand_builtin_trap (); + return; + } + /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable to avoid this. */ gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE)); diff --git a/gcc/config/darwin-protos.h b/gcc/config/darwin-protos.h index b67e05264e1..48a32b2ccc2 100644 --- a/gcc/config/darwin-protos.h +++ b/gcc/config/darwin-protos.h @@ -124,6 +124,7 @@ extern void darwin_enter_string_into_cfstring_table (tree); extern void darwin_asm_output_anchor (rtx symbol); extern bool darwin_use_anchors_for_symbol_p (const_rtx symbol); extern bool darwin_kextabi_p (void); +extern bool darwin_unreachable_traps_p (void); extern void darwin_override_options (void); extern void darwin_patch_builtins (void); extern void darwin_rename_builtins (void); diff --git a/gcc/config/darwin.cc b/gcc/config/darwin.cc index dcfccb4952a..018547d09c6 100644 --- a/gcc/config/darwin.cc +++ b/gcc/config/darwin.cc @@ -3339,6 +3339,13 @@ darwin_kextabi_p (void) { return flag_apple_kext; } +/* True, iff we want to map __builtin_unreachable to a trap. */ + +bool +darwin_unreachable_traps_p (void) { + return darwin_unreachable_traps; +} + void darwin_override_options (void) { diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h index d335ffe7345..17f41cf30ef 100644 --- a/gcc/config/darwin.h +++ b/gcc/config/darwin.h @@ -1225,6 +1225,10 @@ void add_framework_path (char *); #define TARGET_N_FORMAT_TYPES 1 #define TARGET_FORMAT_TYPES darwin_additional_format_types +/* We want __builtin_unreachable to be expanded as a trap instruction. */ +#undef TARGET_UNREACHABLE_SHOULD_TRAP +#define TARGET_UNREACHABLE_SHOULD_TRAP darwin_unreachable_traps_p + #ifndef USED_FOR_TARGET extern void darwin_driver_init (unsigned int *,struct cl_decoded_option **); #define GCC_DRIVER_HOST_INITIALIZATION \ diff --git a/gcc/config/darwin.opt b/gcc/config/darwin.opt index 119faf7b385..f96f3de8a3e 100644 --- a/gcc/config/darwin.opt +++ b/gcc/config/darwin.opt @@ -91,6 +91,10 @@ mtarget-linker Target RejectNegative Joined Separate Var(darwin_target_linker) Init(LD64_VERSION) -mtarget-linker Specify that ld64 is the toolchain linker for the current invocation. +munreachable-traps +Target Var(darwin_unreachable_traps) Init(1) +When set (the default) this makes __builtin_unreachable render as a trap. + ; Driver options. all_load diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d75fc87cdda..06b8eb02508 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -954,7 +954,7 @@ Objective-C and Objective-C++ Dialects}. -twolevel_namespace -umbrella -undefined -unexported_symbols_list -weak_reference_mismatches -whatsloaded -F -gused -gfull -mmacosx-version-min=@var{version} --mkernel -mone-byte-bool} +-mkernel -mone-byte-bool -munreachable-traps} @emph{DEC Alpha Options} @gccoptlist{-mno-fp-regs -msoft-float @@ -25136,6 +25136,11 @@ without that switch. Using this switch may require recompiling all other modules in a program, including system libraries. Use this switch to conform to a non-default data model. +@opindex munreachable-traps +@item -munreachable-traps +Causes @code{__builtin_unreachable} to be rendered as a trap. This is the +default for all Darwin architectures. + @opindex mfix-and-continue @opindex ffix-and-continue @opindex findirect-data diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 551463d3a2d..ce1d393ec70 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -12725,6 +12725,11 @@ This target hook can be used to generate a target-specific code If selftests are enabled, run any selftests for this target. @end deftypefn +@deftypefn {Target Hook} bool TARGET_UNREACHABLE_SHOULD_TRAP (void) +This hook should return @code{true} if the target wants @code{__builtin_unreachable} to expand to a trap or @code{abort ()}. + The default value is false. +@end deftypefn + @deftypefn {Target Hook} bool TARGET_MEMTAG_CAN_TAG_ADDRESSES () True if the backend architecture naturally supports ignoring some region of pointers. This feature means that @option{-fsanitize=hwaddress} can diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index a9ff86d4216..a2156ec99ff 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -8103,6 +8103,8 @@ maintainer is familiar with. @hook TARGET_RUN_TARGET_SELFTESTS +@hook TARGET_UNREACHABLE_SHOULD_TRAP + @hook TARGET_MEMTAG_CAN_TAG_ADDRESSES @hook TARGET_MEMTAG_TAG_SIZE diff --git a/gcc/target.def b/gcc/target.def index 05722801c95..e2f7d107e49 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -7409,6 +7409,16 @@ DEFHOOKPOD libatomic. The default value is false.", bool, false) +/* This value represents whether __builtin_unreachable should be expanded + as a trap instruction (or an abort() if the trap is not available). */ +DEFHOOK +(unreachable_should_trap, + "This hook should return @code{true} if the target wants \ + @code{__builtin_unreachable} to expand to a trap or @code{abort ()}.\n\ + The default value is false.", + bool, (void), + hook_bool_void_false) + /* Close the 'struct gcc_target' definition. */ HOOK_VECTOR_END (C90_EMPTY_HACK)