From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id 700CE3858C35 for ; Wed, 8 May 2024 19:43:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 700CE3858C35 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 700CE3858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::536 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715197407; cv=none; b=ZTrnWCTs0vbJn14M2y8Qeysp7GIIFK4N65QTNz6J29F+PgJxFrmMEQ3/GwJ/DLWXTp/FAZZmUuqaMxOnTmJNh7gKbW0xMQHVRtDXl+jq6yrYgLawdgWV8/Rd0hLaNaNIQ772EKKBKTDbhxPTfOIlKSFAbNHdJwUZ3cEkknFoT7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715197407; c=relaxed/simple; bh=fFcawOGzTcGBFuGKwFOkJQpPk7IYywnl2yOh8FghVME=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=vOLDGCOGZoqWNQ5gNi4cjfZHCw/3wsqj6/kAu18UrAY/q5bL73VDsJ6aGx76f0EHC0jJtyqBUEaoFi7JIqhF+n8IgAHQWbj6Hnl35gT/h3veZehyVmFWoLJ2DiOK0dXiNiibcM2F0pSHpO7P85pHTYwXMwDUL++rPC4h1ITzmks= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5d8b887bb0cso108516a12.2 for ; Wed, 08 May 2024 12:43:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715197403; x=1715802203; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=n9NoebhFBOVGSS5zqKYSWbV9D7CUfAtGWgXHur1udHM=; b=XZUWy2Q9eUSbIWRckWuHeydPtFWvZmAGGHBnFbwWhJS3+hwNh5bUyJQIDSCURaV6to HIcYn4MeilK4JQUuoQPJ4Lq1zgRgSSwZbm9HMQyNXpdCiHoWJ1pDwGtEvCfeD+nAFrdV IV2kseuWlSf9uI54VKSTI+8hFJ4qin+LIrJREXFUyfJLVAvF0zS/tVAWZxphO7i3M9Pz JI5EVD5/tkpRgk6H078RNyKdsb+TSrSS5seU0GJ63VSu9/ea5x9SeBQNCvkkorsiXhvT +w8D3qjLLsa4Ygd4YGlMJIJEO1r2vCi5VDpiMfU3hwEQSwn8UCNTnKk1qdVDTXqOg/4X mvJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715197403; x=1715802203; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n9NoebhFBOVGSS5zqKYSWbV9D7CUfAtGWgXHur1udHM=; b=mjlwZEqmQo9Pdli9NtYX7JInodfylsyOKALNfrPq4/Zb3OBxM9I2MZN49wqNUlIEtL gQ7AgOdf1Vd5b7d98FDQdX2MvWVuvAAjswb4Z4Mfx4wA0Ct1ctakjnaayv3WpqqJSWVA ahzCJEaTk1onR8h1PrH3o7d6Djr7HvNpp1DlLBZr/cpUQoKysxOiTRbDnYMwG4eykIMz 9MXvmylP3fkxsJw2ECWNiDYOZnnqTkLz6UQ5/Hjs7MbYTL8Exj/RnRTgQxll8QkRmXRi 1GYoK93bWgiunl4dfkxa4+gtFtI+AqzBtUgwCaAhbHAaxpkyhtguryzqNcCZK8Av1Odz 7qkA== X-Gm-Message-State: AOJu0YzzUjBCkcozxwyYV6ByqinzDW1ckYLiQItveFGua4bXwga2nMO7 obhqBU14m/r5y6HJMulWgSFGjo/L14xSOBufG+xkcIJbKg/MzOjpAZLVxGaaKaooAW4YQBgaQOM YviDpEUkE/Cv0zvEGTqvHtkQEHOU= X-Google-Smtp-Source: AGHT+IEgtOXYV+BXAGyhUQ+P7EDa4dgbaUVNhklxvyCIJ4xntaqmKj4dI9TpaD98qbL+wvJFPsNzknAk5AAr/tnXTG0= X-Received: by 2002:a17:90b:4c11:b0:2b2:74c9:192b with SMTP id 98e67ed59e1d1-2b616bf951cmr3649070a91.40.1715197403226; Wed, 08 May 2024 12:43:23 -0700 (PDT) MIME-Version: 1.0 References: <20240409135552.79103-1-iain@sandoe.co.uk> <7C194A88-5AAC-40A6-8E1F-8F88F5447230@sandoe.co.uk> In-Reply-To: <7C194A88-5AAC-40A6-8E1F-8F88F5447230@sandoe.co.uk> From: Andrew Pinski Date: Wed, 8 May 2024 12:43:11 -0700 Message-ID: Subject: Re: Ping [PATCH/RFC] target, hooks: Allow a target to trap on unreachable [PR109267]. To: Iain Sandoe Cc: GCC Patches , Jakub Jelinek , "Andrew Pinski (QUIC)" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.0 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 List-Id: On Wed, May 8, 2024 at 12:37=E2=80=AFPM Iain Sandoe wro= te: > > Hi Folks, > > I=E2=80=99d like to land a viable solution to this issue if possible, (it= is a show- > stopper for the aarch64-darwin development branch). > > > On 9 Apr 2024, at 14:55, Iain Sandoe wrote: > > > > 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, > > @Andrew - you were also (at one stage) talking about some ideas about > how to handle this is in the middle end. > Is that something you are likely to have time to do? > Would it still be reasonable to have a target hook to control the behavio= ur. > (the implementation below allows one to make the effect per TU) I won't be able to implement the idea until July at earliest though. Thanks, Andrew > > > > 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(LD= 64_VERSION) > > -mtarget-linker Specify that ld64 is the toolch= ain 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 tr= ap. > > + > > ; 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=3D@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 re= quire 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 tar= get-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{__buil= tin_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 regio= n > > of pointers. This feature means that @option{-fsanitize=3Dhwaddress} c= an > > 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 expan= ded > > + 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) > > > > -- > > 2.39.2 (Apple Git-143) > > >