[PATCH] D104743: [UpdateCCTestChecks] Implement --global-hex-value-regex

Joel E. Denny via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 22 13:40:40 PDT 2021


jdenny created this revision.
jdenny added reviewers: arichardson, ggeorgakoudis, jdoerfert, MaskRay, mtrofin, greened.
jdenny requested review of this revision.
Herald added a subscriber: sstefan1.
Herald added projects: clang, LLVM.

For example, in OpenMP offload codegen tests, global variables like
`.offload_maptypes*` are much easier to read in hex.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104743

Files:
  clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c
  clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c.expected
  clang/test/utils/update_cc_test_checks/global-hex-value-regex.test
  llvm/utils/UpdateTestChecks/common.py


Index: llvm/utils/UpdateTestChecks/common.py
===================================================================
--- llvm/utils/UpdateTestChecks/common.py
+++ llvm/utils/UpdateTestChecks/common.py
@@ -38,10 +38,13 @@
                       help='Add a prefix to FileCheck IR value names to avoid conflicts with scripted names')
   parser.add_argument('--global-value-regex', nargs='+', default=[],
                       help='List of regular expressions that a global value declaration must match to generate a check (has no effect if checking globals is not enabled)')
+  parser.add_argument('--global-hex-value-regex', nargs='+', default=[],
+                      help='List of regular expressions such that, for matching global value declarations, literal i32/i64 values should be encoded in hex in the associated FileCheck directives')
   args = parser.parse_args()
-  global _verbose, _global_value_regex
+  global _verbose, _global_value_regex, _global_hex_value_regex
   _verbose = args.verbose
   _global_value_regex = args.global_value_regex
+  _global_hex_value_regex = args.global_hex_value_regex
   return args
 
 
@@ -607,6 +610,12 @@
   for i, line in enumerate(lines):
     # An IR variable named '%.' matches the FileCheck regex string.
     line = line.replace('%.', '%dot')
+    for regex in _global_hex_value_regex:
+      if re.match('^@' + regex + ' = ', line):
+        line = re.sub(r'\bi(32|64) ([0-9]+)',
+            lambda m : 'i' + m.group(1) + ' [[#' + hex(int(m.group(2))) + ']]',
+            line)
+        break
     # Ignore any comments, since the check lines will too.
     scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line)
     lines[i] = scrubbed_line
Index: clang/test/utils/update_cc_test_checks/global-hex-value-regex.test
===================================================================
--- /dev/null
+++ clang/test/utils/update_cc_test_checks/global-hex-value-regex.test
@@ -0,0 +1,18 @@
+RUN: rm -rf %t && mkdir %t
+
+# Check --global-hex-value-regex.
+RUN: cp %S/Inputs/global-hex-value-regex.c %t/test.c
+RUN: %update_cc_test_checks %t/test.c --check-globals \
+RUN:     --global-value-regex "foo\..*" "bar\..*" \
+RUN:     --global-hex-value-regex ".*\.hex"
+RUN: diff -u %S/Inputs/global-hex-value-regex.c.expected %t/test.c
+
+# Check that the generated directives actually work correctly.
+RUN: cp %S/Inputs/lit.cfg.example %t/lit.cfg
+# Show lit failures while avoiding confusing FileCheck input dump nesting.
+RUN: %lit %t
+# Lit was successful.  Sanity-check the results.
+RUN: %lit %t 2>&1 | FileCheck %s
+
+CHECK: Testing: 1 tests
+CHECK: PASS: {{.*}} test.c
Index: clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c.expected
===================================================================
--- /dev/null
+++ clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c.expected
@@ -0,0 +1,25 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals --global-value-regex "foo\..*" "bar\..*" --global-hex-value-regex ".*\.hex"
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+//.
+// CHECK: @foo.hex = internal global i32 [[#0x10]], align 4
+// CHECK: @foo.dec = internal global i32 10, align 4
+// CHECK: @bar.hex = internal global i32 [[#0x20]], align 4
+// CHECK: @bar.dec = internal global i32 20, align 4
+//.
+// CHECK-LABEL: @foo(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    ret void
+//
+void foo() {
+  static int hex = 0x10;
+  static int dec = 10;
+}
+// CHECK-LABEL: @bar(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    ret void
+//
+void bar() {
+  static int hex = 0x20;
+  static int dec = 20;
+}
Index: clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c
===================================================================
--- /dev/null
+++ clang/test/utils/update_cc_test_checks/Inputs/global-hex-value-regex.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+void foo() {
+  static int hex = 0x10;
+  static int dec = 10;
+}
+void bar() {
+  static int hex = 0x20;
+  static int dec = 20;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104743.353771.patch
Type: text/x-patch
Size: 4157 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210622/f98aee0d/attachment.bin>


More information about the cfe-commits mailing list