bcc: Introduce new pass manager 31/294731/1
authorHengqi Chen <chenhengqi@outlook.com>
Thu, 28 Apr 2022 11:02:47 +0000 (11:02 +0000)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Fri, 23 Jun 2023 20:06:04 +0000 (22:06 +0200)
There are two pass managers in LLVM. Currently BCC uses the legacy one.
Switch to the new pass manager because the legacy one will be removed
in upcoming releases of LLVM.

Running the following script:
```

from bcc import BPF

bpf_text = '''
static int foobar()
{
    bpf_trace_printk("enter vfs_read");
    return 0;
}

KFUNC_PROBE(vfs_read)
{
    return foobar();
}
'''

BPF(text=bpf_text, debug=1)
```

The IR output is the same with or without this change using LLVM 15:

    ; ModuleID = 'sscanf'
    source_filename = "sscanf"
    ; ModuleID = '/virtual/main.c'
    source_filename = "/virtual/main.c"
    target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
    target triple = "bpf-pc-linux"

    @_version = dso_local global i32 332032, section "version", align 4, !dbg !0
    @_license = dso_local global [4 x i8] c"GPL\00", section "license", align 1, !dbg !5
    @__const.foobar._fmt = private unnamed_addr constant [15 x i8] c"enter vfs_read\00", align 1
    @llvm.compiler.used = appending global [2 x ptr] [ptr @_license, ptr @_version], section "llvm.metadata"

    ; Function Attrs: alwaysinline nounwind
    define dso_local i32 @kfunc__vfs_read(ptr nocapture noundef readnone %0) local_unnamed_addr #0 section ".bpf.fn.kfunc__vfs_read" !dbg !33 {
    %2 = alloca [15 x i8], align 1
    call void @llvm.dbg.value(metadata ptr %0, metadata !39, metadata !DIExpression()), !dbg !41
    call void @llvm.dbg.value(metadata ptr undef, metadata !42, metadata !DIExpression()) #4, !dbg !45
    call void @llvm.lifetime.start.p0(i64 15, ptr nonnull %2) #4, !dbg !47
    call void @llvm.dbg.declare(metadata ptr %2, metadata !53, metadata !DIExpression()) #4, !dbg !58
    call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(15) %2, ptr noundef nonnull align 1 dereferenceable(15) @__const.foobar._fmt, i64 15, i1 false) #4, !dbg !58
    %3 = call i32 (ptr, i64, ...) inttoptr (i64 6 to ptr)(ptr noundef nonnull %2, i64 noundef 15) #4, !dbg !59
    call void @llvm.lifetime.end.p0(i64 15, ptr nonnull %2) #4, !dbg !60
    call void @llvm.dbg.value(metadata i32 0, metadata !40, metadata !DIExpression()), !dbg !41
    ret i32 0, !dbg !61
    }

    ; Function Attrs: alwaysinline mustprogress nocallback nofree nosync nounwind readnone speculatable willreturn
    declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

    ; Function Attrs: alwaysinline argmemonly mustprogress nocallback nofree nosync nounwind willreturn
    declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

    ; Function Attrs: alwaysinline argmemonly mustprogress nocallback nofree nosync nounwind willreturn
    declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

    ; Function Attrs: alwaysinline argmemonly mustprogress nofree nounwind willreturn
    declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

    ; Function Attrs: alwaysinline mustprogress nocallback nofree nosync nounwind readnone speculatable willreturn
    declare void @llvm.dbg.value(metadata, metadata, metadata) #1

    attributes #0 = { alwaysinline nounwind "frame-pointer"="none" "min-legal-vector-width"="0" "no-jump-tables"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
    attributes #1 = { alwaysinline mustprogress nocallback nofree nosync nounwind readnone speculatable willreturn }
    attributes #2 = { alwaysinline argmemonly mustprogress nocallback nofree nosync nounwind willreturn }
    attributes #3 = { alwaysinline argmemonly mustprogress nofree nounwind willreturn }
    attributes #4 = { nounwind }

    !llvm.dbg.cu = !{!2}
    !llvm.module.flags = !{!27, !28, !29, !30, !31}
    !llvm.ident = !{!32}

    !0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
    !1 = distinct !DIGlobalVariable(name: "_version", scope: !2, file: !14, line: 526, type: !26, isLocal: false, isDefinition: true)
    !2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "Ubuntu clang version 15.0.0-++20220426083628+d738d4717f6d-1~exp1~20220426203725.435", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
    !3 = !DIFile(filename: "/virtual/main.c", directory: "/home/ubuntu/sources/bpf-next")
    !4 = !{!0, !5, !12}
    !5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
    !6 = distinct !DIGlobalVariable(name: "_license", scope: !2, file: !7, line: 26, type: !8, isLocal: false, isDefinition: true)
    !7 = !DIFile(filename: "/virtual/include/bcc/footer.h", directory: "")
    !8 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, size: 32, elements: !10)
    !9 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
    !10 = !{!11}
    !11 = !DISubrange(count: 4)
    !12 = !DIGlobalVariableExpression(var: !13, expr: !DIExpression())
    !13 = distinct !DIGlobalVariable(name: "bpf_trace_printk_", scope: !2, file: !14, line: 542, type: !15, isLocal: true, isDefinition: true)
    !14 = !DIFile(filename: "/virtual/include/bcc/helpers.h", directory: "")
    !15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64)
    !16 = !DISubroutineType(types: !17)
    !17 = !{!18, !19, !21, null}
    !18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
    !19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
    !20 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !9)
    !21 = !DIDerivedType(tag: DW_TAG_typedef, name: "u64", file: !22, line: 23, baseType: !23)
    !22 = !DIFile(filename: "include/asm-generic/int-ll64.h", directory: "/home/ubuntu/sources/bpf-next")
    !23 = !DIDerivedType(tag: DW_TAG_typedef, name: "__u64", file: !24, line: 31, baseType: !25)
    !24 = !DIFile(filename: "include/uapi/asm-generic/int-ll64.h", directory: "/home/ubuntu/sources/bpf-next")
    !25 = !DIBasicType(name: "unsigned long long", size: 64, encoding: DW_ATE_unsigned)
    !26 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
    !27 = !{i32 7, !"Dwarf Version", i32 4}
    !28 = !{i32 2, !"Debug Info Version", i32 3}
    !29 = !{i32 1, !"wchar_size", i32 4}
    !30 = !{i32 7, !"PIC Level", i32 2}
    !31 = !{i32 7, !"PIE Level", i32 2}
    !32 = !{!"Ubuntu clang version 15.0.0-++20220426083628+d738d4717f6d-1~exp1~20220426203725.435"}
    !33 = distinct !DISubprogram(name: "kfunc__vfs_read", scope: !34, file: !34, line: 23, type: !35, scopeLine: 23, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !38)
    !34 = !DIFile(filename: "/virtual/main.c", directory: "")
    !35 = !DISubroutineType(types: !36)
    !36 = !{!18, !37}
    !37 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !25, size: 64)
    !38 = !{!39, !40}
    !39 = !DILocalVariable(name: "ctx", arg: 1, scope: !33, file: !34, line: 23, type: !37)
    !40 = !DILocalVariable(name: "__ret", scope: !33, file: !34, line: 23, type: !18)
    !41 = !DILocation(line: 0, scope: !33)
    !42 = !DILocalVariable(name: "ctx", arg: 1, scope: !43, file: !34, line: 23, type: !37)
    !43 = distinct !DISubprogram(name: "____kfunc__vfs_read", scope: !34, file: !34, line: 23, type: !35, scopeLine: 24, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !44)
    !44 = !{!42}
    !45 = !DILocation(line: 0, scope: !43, inlinedAt: !46)
    !46 = distinct !DILocation(line: 23, column: 1, scope: !33)
    !47 = !DILocation(line: 15, column: 5, scope: !48, inlinedAt: !57)
    !48 = distinct !DILexicalBlock(scope: !49, file: !34, line: 15, column: 3)
    !49 = distinct !DISubprogram(name: "foobar", scope: !34, file: !34, line: 13, type: !50, scopeLine: 14, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !52)
    !50 = !DISubroutineType(types: !51)
    !51 = !{!18}
    !52 = !{!53}
    !53 = !DILocalVariable(name: "_fmt", scope: !48, file: !34, line: 15, type: !54)
    !54 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, size: 120, elements: !55)
    !55 = !{!56}
    !56 = !DISubrange(count: 15)
    !57 = distinct !DILocation(line: 25, column: 9, scope: !43, inlinedAt: !46)
    !58 = !DILocation(line: 15, column: 10, scope: !48, inlinedAt: !57)
    !59 = !DILocation(line: 15, column: 37, scope: !48, inlinedAt: !57)
    !60 = !DILocation(line: 15, column: 76, scope: !49, inlinedAt: !57)
    !61 = !DILocation(line: 23, column: 1, scope: !33)

Closes #3947.

References:
  [0]: https://llvm.org/docs/NewPassManager.html
  [1]: https://blog.llvm.org/posts/2021-03-26-the-new-pass-manager/

Change-Id: I6477250b7e9952ea098113785a42c5b0f645b6f0
Origin: upstream, https://github.com/iovisor/bcc/commit/c9635f8aa5e974bb30d5a7935054878a6eb5af53
Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
src/cc/bpf_module.cc

index 36f9582a562e3e95cd62de4a29c503b30d403daa..97f4518c838ec7f86753b0ae00a9652c94c5f97d 100644 (file)
 #include <llvm/ExecutionEngine/MCJIT.h>
 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
 #include <llvm/IR/IRPrintingPasses.h>
-#include <llvm/IR/LegacyPassManager.h>
 #include <llvm/IR/LLVMContext.h>
 #include <llvm/IR/Module.h>
+
+#if LLVM_MAJOR_VERSION >= 15
+#include <llvm/Pass.h>
+#include <llvm/IR/PassManager.h>
+#include <llvm/Passes/PassBuilder.h>
+#include <llvm/Transforms/IPO/AlwaysInliner.h>
+#else
+#include <llvm/IR/LegacyPassManager.h>
+#endif
+
 #include <llvm/IR/Verifier.h>
 #include <llvm/Support/TargetSelect.h>
 #include <llvm/Transforms/IPO.h>
@@ -196,9 +205,30 @@ void BPFModule::annotate_light() {
 }
 
 void BPFModule::dump_ir(Module &mod) {
+#if LLVM_MAJOR_VERSION >= 15
+  // Create the analysis managers
+  LoopAnalysisManager LAM;
+  FunctionAnalysisManager FAM;
+  CGSCCAnalysisManager CGAM;
+  ModuleAnalysisManager MAM;
+
+  // Create the pass manager
+  PassBuilder PB;
+  PB.registerModuleAnalyses(MAM);
+  PB.registerCGSCCAnalyses(CGAM);
+  PB.registerFunctionAnalyses(FAM);
+  PB.registerLoopAnalyses(LAM);
+  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
+  auto MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O2);
+
+  // Add passes and run
+  MPM.addPass(PrintModulePass(errs()));
+  MPM.run(mod, MAM);
+#else
   legacy::PassManager PM;
   PM.add(createPrintModulePass(errs()));
   PM.run(mod);
+#endif
 }
 
 int BPFModule::run_pass_manager(Module &mod) {
@@ -208,6 +238,28 @@ int BPFModule::run_pass_manager(Module &mod) {
     return -1;
   }
 
+#if LLVM_MAJOR_VERSION >= 15
+  // Create the analysis managers
+  LoopAnalysisManager LAM;
+  FunctionAnalysisManager FAM;
+  CGSCCAnalysisManager CGAM;
+  ModuleAnalysisManager MAM;
+
+  // Create the pass manager
+  PassBuilder PB;
+  PB.registerModuleAnalyses(MAM);
+  PB.registerCGSCCAnalyses(CGAM);
+  PB.registerFunctionAnalyses(FAM);
+  PB.registerLoopAnalyses(LAM);
+  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
+  auto MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O3);
+
+  // Add passes and run
+  MPM.addPass(AlwaysInlinerPass());
+  if (flags_ & DEBUG_LLVM_IR)
+    MPM.addPass(PrintModulePass(outs()));
+  MPM.run(mod, MAM);
+#else
   legacy::PassManager PM;
   PassManagerBuilder PMB;
   PMB.OptLevel = 3;
@@ -224,6 +276,8 @@ int BPFModule::run_pass_manager(Module &mod) {
   if (flags_ & DEBUG_LLVM_IR)
     PM.add(createPrintModulePass(outs()));
   PM.run(mod);
+#endif
+
   return 0;
 }