Skip to content

Commit f97de4c

Browse files
committed
[OpenMPOpt] Move dedup runtime calls after init for target regions
Deduplication in OpenMPOpt finds redundant OpenMP runtime calls and replaces them with a single call placed in the earliest safe location in the IR. When deduplication happens in a target region this patch makes sure replacement calls are put after target_init. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D106556
1 parent 0e64a52 commit f97de4c

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

llvm/lib/Transforms/IPO/OpenMPOpt.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,27 @@ struct OpenMPOpt {
15731573
if (!CanBeMoved(*CI))
15741574
continue;
15751575

1576-
CI->moveBefore(&*F.getEntryBlock().getFirstInsertionPt());
1576+
// If the function is a kernel, dedup will move
1577+
// the runtime call right after the kernel init callsite. Otherwise,
1578+
// it will move it to the beginning of the caller function.
1579+
if (isKernel(F)) {
1580+
auto &KernelInitRFI = OMPInfoCache.RFIs[OMPRTL___kmpc_target_init];
1581+
auto *KernelInitUV = KernelInitRFI.getUseVector(F);
1582+
1583+
if (KernelInitUV->empty())
1584+
continue;
1585+
1586+
assert(KernelInitUV->size() == 1 &&
1587+
"Expected a single __kmpc_target_init in kernel\n");
1588+
1589+
CallInst *KernelInitCI =
1590+
getCallIfRegularCall(*KernelInitUV->front(), &KernelInitRFI);
1591+
assert(KernelInitCI &&
1592+
"Expected a call to __kmpc_target_init in kernel\n");
1593+
1594+
CI->moveAfter(KernelInitCI);
1595+
} else
1596+
CI->moveBefore(&*F.getEntryBlock().getFirstInsertionPt());
15771597
ReplVal = CI;
15781598
break;
15791599
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes
2+
; RUN: opt -openmp-opt-cgscc -S < %s | FileCheck %s
3+
; RUN: opt -passes=openmp-opt-cgscc -S < %s | FileCheck %s
4+
5+
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
6+
target triple = "nvptx64"
7+
8+
%struct.ident_t = type { i32, i32, i32, i32, i8* }
9+
10+
@0 = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1
11+
@1 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @0, i32 0, i32 0) }, align 8
12+
@2 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 1, i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @0, i32 0, i32 0) }, align 8
13+
@__omp_offloading_50_a3e09bf8_foo_l2_exec_mode = weak constant i8 0
14+
@llvm.compiler.used = appending global [1 x i8*] [i8* @__omp_offloading_50_a3e09bf8_foo_l2_exec_mode], section "llvm.metadata"
15+
16+
declare void @use(i32)
17+
18+
define weak void @__omp_offloading_50_a3e09bf8_foo_l2() #0 {
19+
; CHECK-LABEL: define {{[^@]+}}@__omp_offloading_50_a3e09bf8_foo_l2
20+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
21+
; CHECK-NEXT: entry:
22+
; CHECK-NEXT: [[CAPTURED_VARS_ADDRS:%.*]] = alloca [0 x i8*], align 8
23+
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @__kmpc_target_init(%struct.ident_t* @[[GLOB1:[0-9]+]], i1 true, i1 false, i1 true)
24+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @__kmpc_global_thread_num(%struct.ident_t* @[[GLOB2:[0-9]+]])
25+
; CHECK-NEXT: [[EXEC_USER_CODE:%.*]] = icmp eq i32 [[TMP0]], -1
26+
; CHECK-NEXT: br i1 [[EXEC_USER_CODE]], label [[USER_CODE_ENTRY:%.*]], label [[WORKER_EXIT:%.*]]
27+
; CHECK: user_code.entry:
28+
; CHECK-NEXT: call void @__kmpc_target_deinit(%struct.ident_t* @[[GLOB1]], i1 true, i1 true)
29+
; CHECK-NEXT: ret void
30+
; CHECK: worker.exit:
31+
; CHECK-NEXT: ret void
32+
;
33+
entry:
34+
%captured_vars_addrs = alloca [0 x i8*], align 8
35+
%0 = call i32 @__kmpc_target_init(%struct.ident_t* @1, i1 true, i1 false, i1 true)
36+
%exec_user_code = icmp eq i32 %0, -1
37+
br i1 %exec_user_code, label %user_code.entry, label %worker.exit
38+
39+
user_code.entry: ; preds = %entry
40+
%1 = call i32 @__kmpc_global_thread_num(%struct.ident_t* @2)
41+
%2 = call i32 @__kmpc_global_thread_num(%struct.ident_t* @2)
42+
call void @__kmpc_target_deinit(%struct.ident_t* @1, i1 true, i1 true)
43+
ret void
44+
45+
worker.exit: ; preds = %entry
46+
ret void
47+
}
48+
49+
declare i32 @__kmpc_target_init(%struct.ident_t*, i1, i1, i1)
50+
51+
declare i32 @__kmpc_global_thread_num(%struct.ident_t*) #1
52+
53+
declare void @__kmpc_target_deinit(%struct.ident_t*, i1, i1)
54+
55+
attributes #0 = { convergent noinline norecurse nounwind "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+ptx32,+sm_20" }
56+
attributes #1 = { nounwind }
57+
58+
!omp_offload.info = !{!0}
59+
!nvvm.annotations = !{!1}
60+
!llvm.module.flags = !{!2, !3, !4}
61+
62+
!0 = !{i32 0, i32 80, i32 -1545561096, !"foo", i32 2, i32 0}
63+
!1 = !{void ()* @__omp_offloading_50_a3e09bf8_foo_l2, !"kernel", i32 1}
64+
!2 = !{i32 1, !"wchar_size", i32 4}
65+
!3 = !{i32 7, !"openmp", i32 50}
66+
!4 = !{i32 7, !"openmp-device", i32 50}

0 commit comments

Comments
 (0)