Repository: compiler-explorer/compiler-explorer
Pull request #6618 (2024-06-17 15:34:09)

Add support for filtering debug records in LLVM IR
In LLVM, debug records (`#dbg_value`) are now being printed by default in LLVM IR instead of debug intrinsics (`call void @llvm.dbg.value`). As the debug info filters don't recognize debug records, this means [IR output from clang-19 (trunk)](https://godbolt.org/z/fPbdEhYsn) and onwards will always show debug variable output.

This patch adds debug records to the filters to fix this.


Patch:
diff --git a/lib/llvm-ir.ts b/lib/llvm-ir.ts
index 161fc623a..7f81c66e8 100644
--- a/lib/llvm-ir.ts
+++ b/lib/llvm-ir.ts
@@ -42,7 +42,8 @@ export class LlvmIrParser {
     private otherMetaDirective: RegExp;
     private namedMetaDirective: RegExp;
     private metaNodeOptionsRe: RegExp;
-    private llvmDebugLine: RegExp;
+    private llvmDebugIntrinsicLine: RegExp;
+    private llvmDebugRecordLine: RegExp;
     private llvmDebugAnnotation: RegExp;
     private otherMetadataAnnotation: RegExp;
     private attributeAnnotation: RegExp;
@@ -67,7 +68,8 @@ export class LlvmIrParser {
         this.namedMetaDirective = /^(![.A-Z_a-z-]+) = (?:distinct )?!{.*}/;
         this.metaNodeOptionsRe = /(\w+): (!?\d+|\w+|""|"(?:[^"]|\\")*[^\\]")/gi;
 
-        this.llvmDebugLine = /^\s*(tail\s)?call void @llvm\.dbg\..*$/;
+        this.llvmDebugIntrinsicLine = /^\s*(tail\s)?call void @llvm\.dbg\..*$/;
+        this.llvmDebugRecordLine = /^\s*#dbg_.*$/;
         this.llvmDebugAnnotation = /,? !dbg !\d+/;
         this.otherMetadataAnnotation = /,? !(?!dbg)[\w.]+ (!\d+)/;
         this.attributeAnnotation = /,? #\d+(?= )/;
@@ -165,7 +167,7 @@ export class LlvmIrParser {
         const lineFilters: RegExp[] = [];
 
         if (options.filterDebugInfo) {
-            filters.push(this.llvmDebugLine);
+            filters.push(this.llvmDebugIntrinsicLine, this.llvmDebugRecordLine);
             lineFilters.push(this.llvmDebugAnnotation);
         }
         if (options.filterIRMetadata) {
diff --git a/lib/parsers/llvm-pass-dump-parser.ts b/lib/parsers/llvm-pass-dump-parser.ts
index 9f67b8cdf..7c2d5fb53 100644
--- a/lib/parsers/llvm-pass-dump-parser.ts
+++ b/lib/parsers/llvm-pass-dump-parser.ts
@@ -100,6 +100,7 @@ export class LlvmPassDumpParser {
         // Additional filters conditionally enabled by `filterDebugInfo`
         this.debugInfoFilters = [
             /^\s+(tail\s)?call void @llvm\.dbg.+$/, // dbg calls
+            /^\s+#dbg_.+$/, // dbg records
             /^\s+DBG_.+$/, // dbg pseudo-instructions
             /^(!\d+) = (?:distinct )?!DI([A-Za-z]+)\(([^)]+?)\)/, // meta
             /^(!\d+) = (?:distinct )?!{.*}/, // meta


Test patch:
diff --git a/test/llvm-pass-dump-parser-tests.ts b/test/llvm-pass-dump-parser-tests.ts
index 30eb37317..4dd41b529 100644
--- a/test/llvm-pass-dump-parser-tests.ts
+++ b/test/llvm-pass-dump-parser-tests.ts
@@ -54,6 +54,8 @@ describe('llvm-pass-dump-parser filter', () => {
         { text: '  call void @llvm.dbg.value(metadata %struct.S1* %s1, metadata !30, metadata !DIExpression()), !dbg !32' },
         { text: '  tail call void @llvm.dbg.declare(metadata i16* %p.addr, metadata !24, metadata !DIExpression()), !dbg !12' },
         { text: '  tail call void @llvm.dbg.value(metadata i32 0, metadata !20, metadata !DIExpression()), !dbg !21' },
+        { text: '    #dbg_declare(i16* %x.addr, !44, !DIExpression(), !42)' },
+        { text: '    #dbg_value(i32 10, !40, !DIExpression(), !41)' },
         { text: '  DBG_VALUE $rdi, $noreg, !"s1", !DIExpression(), debug-location !32; example.cpp:0 line no:7' },
         { text: '  store %struct.S2* %s2, %struct.S2** %s2.addr, align 8, !tbaa !32' },
         { text: '  %0 = load %struct.S2*, %struct.S2** %s2.addr, align 8, !dbg !38, !tbaa !32' },
@@ -111,6 +113,8 @@ describe('llvm-pass-dump-parser filter', () => {
                     { text: '  call void @llvm.dbg.value(metadata %struct.S1* %s1, metadata !30, metadata !DIExpression())' },
                     { text: '  tail call void @llvm.dbg.declare(metadata i16* %p.addr, metadata !24, metadata !DIExpression())' },
                     { text: '  tail call void @llvm.dbg.value(metadata i32 0, metadata !20, metadata !DIExpression())' },
+                    { text: '    #dbg_declare(i16* %x.addr, !44, !DIExpression(), !42)' },
+                    { text: '    #dbg_value(i32 10, !40, !DIExpression(), !41)' },
                     { text: '  DBG_VALUE $rdi, $noreg, !"s1", !DIExpression(), debug-location !32; example.cpp:0 line no:7' },
                     { text: '  store %struct.S2* %s2, %struct.S2** %s2.addr, align 8' },
                     { text: '  %0 = load %struct.S2*, %struct.S2** %s2.addr, align 8' },