llvm-project/mlir/test/Target/LLVMIR/Import/metadata-profiling.ll
Slava Zakharin 70343c8d44
[mlir][flang] Added Weighted[Region]BranchOpInterface's. (#142079)
The new interfaces provide getters and setters for the weight
information about the branches of BranchOpInterface and
RegionBranchOpInterface operations.

These interfaces are done the same way as LLVM dialect's
BranchWeightOpInterface.

The plan is to produce this information in Flang, e.g. mark
most probably "cold" code as such and allow LLVM to order
basic blocks accordingly. An example of such a code is
copy loops generated for arrays repacking - we can mark it
as "cold" assuming that the copy will not happen dynamically.
If the copy actually happens the overhead of the copy is probably high
enough so that we may not care about the little overhead
of jumping to the "cold" code and fetching it.
2025-06-17 16:14:13 -07:00

72 lines
1.5 KiB
LLVM

; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
; CHECK-LABEL: @cond_br
define i64 @cond_br(i1 %arg1, i64 %arg2) {
entry:
; CHECK: llvm.cond_br
; CHECK-SAME: weights([0, 3])
br i1 %arg1, label %bb1, label %bb2, !prof !0
bb1:
ret i64 %arg2
bb2:
ret i64 %arg2
}
!0 = !{!"branch_weights", i32 0, i32 3}
; // -----
; CHECK-LABEL: @simple_switch(
define i32 @simple_switch(i32 %arg1) {
; CHECK: llvm.switch
; CHECK: {branch_weights = array<i32: 42, 3, 5>}
switch i32 %arg1, label %bbd [
i32 0, label %bb1
i32 9, label %bb2
], !prof !0
bb1:
ret i32 %arg1
bb2:
ret i32 %arg1
bbd:
ret i32 %arg1
}
!0 = !{!"branch_weights", i32 42, i32 3, i32 5}
; // -----
; Verify that a single weight attached to a call is not translated.
; The MLIR WeightedBranchOpInterface does not support this case.
; CHECK: llvm.func @fn()
declare i32 @fn()
; CHECK-LABEL: @call_branch_weights
define i32 @call_branch_weights() {
; CHECK: llvm.call @fn() : () -> i32
%1 = call i32 @fn(), !prof !0
ret i32 %1
}
!0 = !{!"branch_weights", i32 42}
; // -----
declare void @foo()
declare i32 @__gxx_personality_v0(...)
; CHECK-LABEL: @invoke_branch_weights
define i32 @invoke_branch_weights() personality ptr @__gxx_personality_v0 {
; CHECK: llvm.invoke @foo() to ^bb2 unwind ^bb1 {branch_weights = array<i32: 42, 99>} : () -> ()
invoke void @foo() to label %bb2 unwind label %bb1, !prof !0
bb1:
%1 = landingpad { ptr, i32 } cleanup
br label %bb2
bb2:
ret i32 1
}
!0 = !{!"branch_weights", i32 42, i32 99}