From 71d0905fb09b4eef5a2bb36287b99c04b3c00f03 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Tue, 21 Nov 2023 17:31:37 +0100
Subject: [PATCH] testsuite: Add 'only_for_offload_target' wrapper for
 'scan-offload-tree-dump' etc.

This allows restricting scans to one specific offload target only.

	gcc/
	* doc/sourcebuild.texi (Final Actions): Document
	'only_for_offload_target' wrapper.
	gcc/testsuite/
	* lib/scanoffload.exp (only_for_offload_target): New 'proc'.
---
 gcc/doc/sourcebuild.texi          | 37 +++++++++++++++++++++++++++++--
 gcc/testsuite/lib/scanoffload.exp | 21 ++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index eaa75f00f5c..bf8f26a080e 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -3431,8 +3431,8 @@ stands for zero or more unmatched lines; the whitespace after
 @subsubsection Scan optimization dump files
 
 These commands are available for @var{kind} of @code{tree}, @code{ltrans-tree},
-@code{offload-tree}, @code{rtl}, @code{offload-rtl}, @code{ipa}, and
-@code{wpa-ipa}.
+@code{offload-tree}, @code{rtl}, @code{offload-rtl}, @code{ipa},
+@code{offload-ipa}, and @code{wpa-ipa}.
 
 @table @code
 @item scan-@var{kind}-dump @var{regex} @var{suffix} [@{ target/xfail @var{selector} @}]
@@ -3467,6 +3467,39 @@ occurrences of the string ``code has been optimized'', use:
 /* @{ dg-final @{ scan-tree-dump "code has been optimized" "mypass\[1-3\]" @} @} */
 @end smallexample
 
+The @code{offload-@dots{}} ones by default separately scan the dump
+file of each enabled offload target.
+You may use the @code{only_for_offload_target} wrapper to restrict the
+scanning to one specific offload target:
+
+@smallexample
+/* @{ dg-do link @{ target offload_target_amdgcn @} @} */
+/* @{ dg-additional-options -foffload-options=-fdump-ipa-simdclone-details @} */
+/* @{ dg-final @{ only_for_offload_target amdgcn-amdhsa scan-offload-ipa-dump @var{regex_amdgcn} simdclone @} @} */
+@end smallexample
+
+This test case is active if GCN offload compilation is enabled (but
+potentially also additional offload targets).
+The @code{simdclone} IPA dump file is (potentially) produced for all
+offload targets, but only the GCN offload one is scanned.
+
+If a test case doesn't have a @samp{@{ target @var{selector} @}}, and
+you need to scan, for example, for different @var{regex}es for each of
+host and potentially several offload targets, use a pattern like this:
+
+@smallexample
+/* @{ dg-final @{ scan-tree-dump @var{regex_host} optimized @} @}
+   @{ dg-final @{ only_for_offload_target amdgcn-amdhsa scan-offload-tree-dump @var{regex_amdgcn} optimized @{ target offload_target_amdgcn @} @} @}
+   @{ dg-final @{ only_for_offload_target nvptx-none scan-offload-tree-dump @var{regex_nvptx} optimized @{ target offload_target_nvptx @} @} @} */
+@end smallexample
+
+Here, unconditionally @var{regex_host} is scanned for in the host dump
+file.
+If GCN offloading compilation is actually enabled, @var{regex_amdgcn}
+is scanned for in the GCN offload compilation dump file.
+If nvptx offloading compilation is actually enabled, @var{regex_nvptx}
+is scanned for in the nvptx offload compilation dump file.
+
 
 @subsubsection Check for output files
 
diff --git a/gcc/testsuite/lib/scanoffload.exp b/gcc/testsuite/lib/scanoffload.exp
index 8315820d44a..c1997985528 100644
--- a/gcc/testsuite/lib/scanoffload.exp
+++ b/gcc/testsuite/lib/scanoffload.exp
@@ -38,6 +38,8 @@ proc scoff-adjust { args idx target } {
 # Wrapper for scan procs.
 # Argument 0 is the index of the argument to replace when calling
 # argument 1 with the remaining arguments.  Use end-1 or end or so.
+# If set, the 'global offload_target' specifies one specific offload target to
+# test, otherwise iterate over all 'global offload_targets'.
 proc scoff { args } {
     set idx [lindex $args 0]
     set prc [lindex $args 1]
@@ -59,3 +61,22 @@ proc scoff { args } {
 	}
     }
 }
+
+# Wrapper so that only for a specific offload target (first argument) we
+# execute a 'dg-final' command (remaining arguments).
+proc only_for_offload_target { args } {
+    set override_offload_target [lindex $args 0]
+    set cmd [lreplace $args 0 0]
+
+    global offload_target
+    if [info exists offload_target] {
+	set original_offload_target $offload_target
+    }
+    set offload_target $override_offload_target
+    eval $cmd
+    if [info exists original_offload_target] {
+	set offload_target $original_offload_target
+    } else {
+	unset offload_target
+    }
+}
-- 
2.34.1

