[llvm-exegesis] Improve test flexibility by using available_features
authorPavel Kosov <kpdev42@gmail.com>
Mon, 27 Mar 2023 11:43:08 +0000 (14:43 +0300)
committerPavel Kosov <kpdev42@gmail.com>
Mon, 27 Mar 2023 11:43:08 +0000 (14:43 +0300)
Currently, all llvm-exegesis regression tests are target-specific and
they are organized so that the entire subdirectories of
llvm/test/tools/llvm-exegesis are enabled or disabled as a whole.

After the commit e0ad2af691 ("Skip codegen" dry-run mode), at least two
kinds of tests are possible:
1) ensure that a snippet can be *generated* for the particular opcode
2) tests involving actual snippet execution inside the llvm-exegesis
   process

Thus, for the particular target subdirectory, some tests should run only
on the particular host architecture and other tests only need the target
being among LLVM_TARGETS_TO_BUILD.

This commit defines a bunch of exegesis-specific features that can be
referenced in REQUIRES, UNSUPPORTED, etc. on a test-by-test basis.

~~

Huawei RRI

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D146302

llvm/test/tools/llvm-exegesis/AArch64/latency-by-opcode-name.s
llvm/test/tools/llvm-exegesis/AArch64/lit.local.cfg
llvm/test/tools/llvm-exegesis/Mips/latency-GPR64.s
llvm/test/tools/llvm-exegesis/Mips/latency-by-opcode-name.s
llvm/test/tools/llvm-exegesis/Mips/lit.local.cfg
llvm/test/tools/llvm-exegesis/X86/lbr/lit.local.cfg [deleted file]
llvm/test/tools/llvm-exegesis/X86/lbr/mov-add.s
llvm/test/tools/llvm-exegesis/lit.local.cfg [new file with mode: 0644]

index 1d719ce..653f544 100644 (file)
@@ -1,4 +1,5 @@
 # RUN: llvm-exegesis -mode=latency -opcode-name=ADDXrr | FileCheck %s
+# REQUIRES: exegesis-can-execute-aarch64, exegesis-can-measure-latency
 
 CHECK:      ---
 CHECK-NEXT: mode: latency
index a768323..2c80c84 100644 (file)
@@ -1,32 +1,3 @@
-import subprocess
-import lit.util
-
-if 'native' not in config.available_features:
-    config.unsupported = True
-
-elif not ('AArch64' in config.root.targets):
+if not ('AArch64' in config.root.targets):
     # We need support for AArch64.
     config.unsupported = True
-
-elif not ('aarch64' in config.root.host_triple):
-    # We need to be running on an AArch64 host.
-    config.unsupported = True
-
-else:
-    # We need libpfm to be installed and allow reading perf counters. We can
-    # only know that at runtime, so we try to measure the latency of an empty
-    # code snippet and bail out on error.
-    llvm_exegesis_exe = lit.util.which('llvm-exegesis', config.llvm_tools_dir)
-    if not llvm_exegesis_exe:
-        print('llvm-exegesis not found')
-        config.unsupported = True
-    else:
-      try:
-          with open(os.devnull, 'w') as quiet:
-              check_llvm_exegesis_result = subprocess.call(
-                [llvm_exegesis_exe, '-mode', 'latency', '-snippets-file', '/dev/null'], stdout=quiet, stderr=quiet)
-      except OSError:
-          print('could not exec llvm-exegesis')
-          config.unsupported = True
-      if not check_llvm_exegesis_result == 0:
-        config.unsupported = True
index 3be9658..f9b4860 100644 (file)
@@ -1,4 +1,5 @@
 # RUN: llvm-exegesis -mode=latency -opcode-name=AND64 | FileCheck %s
+# REQUIRES: exegesis-can-execute-mips, exegesis-can-measure-latency
 
 CHECK:      ---
 CHECK-NEXT: mode: latency
index 365608a..f3853ea 100644 (file)
@@ -1,4 +1,5 @@
 # RUN: llvm-exegesis -mode=latency -opcode-name=ADD | FileCheck %s
+# REQUIRES: exegesis-can-execute-mips, exegesis-can-measure-latency
 
 CHECK:      ---
 CHECK-NEXT: mode: latency
index 766ae9c..9e3b402 100644 (file)
@@ -1,32 +1,3 @@
-import subprocess
-import lit.util
-
-if 'native' not in config.available_features:
-    config.unsupported = True
-
-elif not ('Mips' in config.root.targets):
+if not ('Mips' in config.root.targets):
     # We need support for Mips.
     config.unsupported = True
-
-elif not ('mips' in config.root.host_triple):
-    # We need to be running on an Mips host.
-    config.unsupported = True
-
-else:
-    # We need libpfm to be installed and allow reading perf counters. We can
-    # only know that at runtime, so we try to measure the latency of an empty
-    # code snippet and bail out on error.
-    llvm_exegesis_exe = lit.util.which('llvm-exegesis', config.llvm_tools_dir)
-    if not llvm_exegesis_exe:
-        print('llvm-exegesis not found')
-        config.unsupported = True
-    else:
-      try:
-          with open(os.devnull, 'w') as quiet:
-              check_llvm_exegesis_result = subprocess.call(
-                [llvm_exegesis_exe, '-mode', 'latency', '-snippets-file', '/dev/null'], stdout=quiet, stderr=quiet)
-      except OSError:
-          print('could not exec llvm-exegesis')
-          config.unsupported = True
-      if not check_llvm_exegesis_result == 0:
-        config.unsupported = True
diff --git a/llvm/test/tools/llvm-exegesis/X86/lbr/lit.local.cfg b/llvm/test/tools/llvm-exegesis/X86/lbr/lit.local.cfg
deleted file mode 100644 (file)
index a188cb2..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-import subprocess
-import lit.util
-
-if 'native' not in config.available_features:
-    config.unsupported = True
-
-elif not ('X86' in config.root.targets):
-    # We need support for X86.
-    config.unsupported = True
-
-elif not ('x86_64' in config.root.host_triple):
-    # We need to be running on an X86 host.
-    config.unsupported = True
-
-else:
-    # We need libpfm to be installed and the host to be support LBR format with cycles.
-    llvm_exegesis_exe = lit.util.which('llvm-exegesis', config.llvm_tools_dir)
-    if not llvm_exegesis_exe:
-        print('llvm-exegesis not found')
-        config.unsupported = True
-    else:
-      try:
-          with open(os.devnull, 'w') as quiet:
-              check_llvm_exegesis_latency_result = subprocess.call(
-                [llvm_exegesis_exe, '-mode', 'latency', '-x86-lbr-sample-period', '123', '-repetition-mode', 'loop', '-snippets-file', '/dev/null'], stdout=quiet, stderr=quiet)
-      except OSError:
-          print('could not exec llvm-exegesis')
-          config.unsupported = True
-      if not check_llvm_exegesis_latency_result == 0:
-        config.unsupported = True
index 6f0762b..af1662d 100644 (file)
@@ -1,4 +1,5 @@
 # RUN: llvm-exegesis -mtriple=x86_64-unknown-unknown -mcpu=x86-64 -mode=latency --repetition-mode=loop --x86-lbr-sample-period=521 --snippets-file=%p/Inputs/mov_add.att
+# REQUIRES: exegesis-can-execute-x86_64, exegesis-can-measure-latency-lbr
 
 
 CHECK:      ---
diff --git a/llvm/test/tools/llvm-exegesis/lit.local.cfg b/llvm/test/tools/llvm-exegesis/lit.local.cfg
new file mode 100644 (file)
index 0000000..a932f32
--- /dev/null
@@ -0,0 +1,40 @@
+import subprocess
+import lit.util
+
+
+def can_execute_generated_snippets(arch):
+    is_host_arch = arch in config.root.host_triple
+    # 'native' feature is defined as "host arch == default triple arch"
+    is_native_codegen = 'native' in config.available_features
+    return is_host_arch and is_native_codegen
+
+
+def can_use_perf_counters(mode, extra_options=[]):
+    # We need libpfm to be installed and allow reading perf counters. We can
+    # only know that at runtime, so we try to measure an empty code snippet
+    # and bail out on error.
+    llvm_exegesis_exe = lit.util.which('llvm-exegesis', config.llvm_tools_dir)
+    try:
+        return_code = subprocess.call(
+            [llvm_exegesis_exe, '-mode', mode, '-snippets-file', '/dev/null'] + extra_options,
+            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+        return return_code == 0
+    except OSError:
+        print('could not exec llvm-exegesis')
+        return False
+
+
+for arch in ['aarch64', 'mips', 'powerpc', 'x86_64']:
+    if can_execute_generated_snippets(arch):
+        config.available_features.add('exegesis-can-execute-%s' % arch)
+
+if can_use_perf_counters('latency'):
+    config.available_features.add('exegesis-can-measure-latency')
+
+if can_use_perf_counters('uops'):
+    config.available_features.add('exegesis-can-measure-uops')
+
+if can_execute_generated_snippets('x86_64'):
+    # Check for support of LBR format with cycles.
+    if can_use_perf_counters('latency', ['-x86-lbr-sample-period', '123', '-repetition-mode', 'loop']):
+        config.available_features.add('exegesis-can-measure-latency-lbr')