Commit 0e5633f
authored
[OMPIRBuilder] always leave PARALLEL via the same barrier (#164586)
A barrier will pause execution until all threads reach it. If some go to
a different barrier then we deadlock. This manifests in that the
finalization callback must only be run once. Fix by ensuring we always
go through the same finalization block whether the thread in cancelled
or not and no matter which cancellation point causes the cancellation.
The old callback only affected PARALLEL, so it has been moved into the
code generating PARALLEL. For this reason, we don't need similar changes
for other cancellable constructs. We need to create the barrier on the
shared exit from the outlined function instead of only on the cancelled
branch to make sure that threads exiting normally (without cancellation)
meet the same barriers as those which were cancelled. For example,
previously we might have generated code like
```
...
%ret = call i32 @__kmpc_cancel(...)
%cond = icmp eq i32 %ret, 0
br i1 %cond, label %continue, label %cancel
continue:
// do the rest of the callback, eventually branching to %fini
br label %fini
cancel:
// Populated by the callback:
// unsafe: if any thread makes it to the end without being cancelled
// it won't reach this barrier and then the program will deadlock
%unused = call i32 @__kmpc_cancel_barrier(...)
br label %fini
fini:
// run destructors etc
ret
```
In the new version the barrier is moved into fini. I generate it *after*
the destructors because the standard describes the barrier as occurring
after the end of the parallel region.
```
...
%ret = call i32 @__kmpc_cancel(...)
%cond = icmp eq i32 %ret, 0
br i1 %cond, label %continue, label %cancel
continue:
// do the rest of the callback, eventually branching to %fini
br label %fini
cancel:
br label %fini
fini:
// run destructors etc
// safe so long as every exit from the function happens via this block:
%unused = call i32 @__kmpc_cancel_barrier(...)
ret
```
To achieve this, the barrier is now generated alongside the finalization
code instead of in the callback. This is the reason for the changes to
the unit test.
I'm unsure if I should keep the incorrect barrier generation callback
only on the cancellation branch in clang with the OMPIRBuilder backend
because that would match clang's ordinary codegen. Right now I have
opted to remove it entirely because it is a deadlock waiting to happen.1 parent 4394aa6 commit 0e5633f
File tree
23 files changed
+369
-262
lines changed- clang/test/OpenMP
- flang/test/Integration/OpenMP
- llvm
- include/llvm/Frontend/OpenMP
- lib/Frontend/OpenMP
- test/Transforms/OpenMP
- unittests/Frontend
- mlir
- lib/Target/LLVMIR/Dialect/OpenMP
- test/Target/LLVMIR
23 files changed
+369
-262
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
774 | 774 | | |
775 | 775 | | |
776 | 776 | | |
777 | | - | |
778 | | - | |
779 | 777 | | |
780 | 778 | | |
781 | 779 | | |
| |||
811 | 809 | | |
812 | 810 | | |
813 | 811 | | |
814 | | - | |
| 812 | + | |
815 | 813 | | |
816 | 814 | | |
817 | 815 | | |
818 | 816 | | |
819 | | - | |
| 817 | + | |
820 | 818 | | |
821 | | - | |
| 819 | + | |
822 | 820 | | |
823 | | - | |
| 821 | + | |
824 | 822 | | |
825 | 823 | | |
826 | 824 | | |
| |||
833 | 831 | | |
834 | 832 | | |
835 | 833 | | |
836 | | - | |
837 | | - | |
838 | 834 | | |
839 | 835 | | |
840 | 836 | | |
| |||
894 | 890 | | |
895 | 891 | | |
896 | 892 | | |
897 | | - | |
898 | | - | |
| 893 | + | |
| 894 | + | |
899 | 895 | | |
900 | 896 | | |
901 | 897 | | |
| |||
967 | 963 | | |
968 | 964 | | |
969 | 965 | | |
970 | | - | |
971 | | - | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
972 | 970 | | |
973 | 971 | | |
974 | 972 | | |
| |||
984 | 982 | | |
985 | 983 | | |
986 | 984 | | |
987 | | - | |
| 985 | + | |
988 | 986 | | |
989 | 987 | | |
990 | 988 | | |
991 | 989 | | |
992 | 990 | | |
993 | 991 | | |
994 | | - | |
995 | | - | |
996 | | - | |
| 992 | + | |
997 | 993 | | |
998 | 994 | | |
999 | 995 | | |
| |||
1089 | 1085 | | |
1090 | 1086 | | |
1091 | 1087 | | |
1092 | | - | |
| 1088 | + | |
1093 | 1089 | | |
1094 | 1090 | | |
1095 | 1091 | | |
| |||
1100 | 1096 | | |
1101 | 1097 | | |
1102 | 1098 | | |
1103 | | - | |
| 1099 | + | |
1104 | 1100 | | |
1105 | 1101 | | |
1106 | 1102 | | |
| |||
1153 | 1149 | | |
1154 | 1150 | | |
1155 | 1151 | | |
| 1152 | + | |
| 1153 | + | |
1156 | 1154 | | |
1157 | 1155 | | |
1158 | 1156 | | |
| |||
1162 | 1160 | | |
1163 | 1161 | | |
1164 | 1162 | | |
1165 | | - | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
1166 | 1166 | | |
1167 | | - | |
| 1167 | + | |
1168 | 1168 | | |
1169 | 1169 | | |
1170 | 1170 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
0 commit comments