Skip to content

Commit cc374d8

Browse files
authored
[OpenMP] Remove register_requires global constructor (#80460)
Summary: Currently, OpenMP handles the `omp requires` clause by emitting a global constructor into the runtime for every translation unit that requires it. However, this is not a great solution because it prevents us from having a defined order in which the runtime is accessed and used. This patch changes the approach to no longer use global constructors, but to instead group the flag with the other offloading entires that we already handle. This has the effect of still registering each flag per requires TU, but now we have a single constructor that handles everything. This function removes support for the old `__tgt_register_requires` and replaces it with a warning message. We just had a recent release, and the OpenMP policy for the past four releases since we switched to LLVM is that we do not provide strict backwards compatibility between major LLVM releases now that the library is versioned. This means that a user will need to recompile if they have an old binary that relied on `register_requires` having the old behavior. It is important that we actively deprecate this, as otherwise it would not solve the problem of having no defined init and shutdown order for `libomptarget`. The problem of `libomptarget` not having a define init and shutdown order cascades into a lot of other issues so I have a strong incentive to be rid of it. It is worth noting that the current `__tgt_offload_entry` only has space for a 32-bit integer here. I am planning to overhaul these at some point as well.
1 parent 58f45d9 commit cc374d8

File tree

175 files changed

+3622
-7044
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+3622
-7044
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10100,44 +10100,6 @@ bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) {
1010010100
return !AlreadyEmittedTargetDecls.insert(D).second;
1010110101
}
1010210102

10103-
llvm::Function *CGOpenMPRuntime::emitRequiresDirectiveRegFun() {
10104-
// If we don't have entries or if we are emitting code for the device, we
10105-
// don't need to do anything.
10106-
if (CGM.getLangOpts().OMPTargetTriples.empty() ||
10107-
CGM.getLangOpts().OpenMPSimd || CGM.getLangOpts().OpenMPIsTargetDevice ||
10108-
(OMPBuilder.OffloadInfoManager.empty() &&
10109-
!HasEmittedDeclareTargetRegion && !HasEmittedTargetRegion))
10110-
return nullptr;
10111-
10112-
// Create and register the function that handles the requires directives.
10113-
ASTContext &C = CGM.getContext();
10114-
10115-
llvm::Function *RequiresRegFn;
10116-
{
10117-
CodeGenFunction CGF(CGM);
10118-
const auto &FI = CGM.getTypes().arrangeNullaryFunction();
10119-
llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
10120-
std::string ReqName = getName({"omp_offloading", "requires_reg"});
10121-
RequiresRegFn = CGM.CreateGlobalInitOrCleanUpFunction(FTy, ReqName, FI);
10122-
CGF.StartFunction(GlobalDecl(), C.VoidTy, RequiresRegFn, FI, {});
10123-
// TODO: check for other requires clauses.
10124-
// The requires directive takes effect only when a target region is
10125-
// present in the compilation unit. Otherwise it is ignored and not
10126-
// passed to the runtime. This avoids the runtime from throwing an error
10127-
// for mismatching requires clauses across compilation units that don't
10128-
// contain at least 1 target region.
10129-
assert((HasEmittedTargetRegion || HasEmittedDeclareTargetRegion ||
10130-
!OMPBuilder.OffloadInfoManager.empty()) &&
10131-
"Target or declare target region expected.");
10132-
CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
10133-
CGM.getModule(), OMPRTL___tgt_register_requires),
10134-
llvm::ConstantInt::get(
10135-
CGM.Int64Ty, OMPBuilder.Config.getRequiresFlags()));
10136-
CGF.FinishFunction();
10137-
}
10138-
return RequiresRegFn;
10139-
}
10140-
1014110103
void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF,
1014210104
const OMPExecutableDirective &D,
1014310105
SourceLocation Loc,

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,10 +1407,6 @@ class CGOpenMPRuntime {
14071407
/// \param GD Global to scan.
14081408
virtual bool emitTargetGlobal(GlobalDecl GD);
14091409

1410-
/// Creates and returns a registration function for when at least one
1411-
/// requires directives was used in the current module.
1412-
llvm::Function *emitRequiresDirectiveRegFun();
1413-
14141410
/// Creates all the offload entries in the current compilation unit
14151411
/// along with the associated metadata.
14161412
void createOffloadEntriesAndInfoMetadata();

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,10 +838,6 @@ void CodeGenModule::Release() {
838838
AddGlobalCtor(CudaCtorFunction);
839839
}
840840
if (OpenMPRuntime) {
841-
if (llvm::Function *OpenMPRequiresDirectiveRegFun =
842-
OpenMPRuntime->emitRequiresDirectiveRegFun()) {
843-
AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
844-
}
845841
OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
846842
OpenMPRuntime->clear();
847843
}

clang/test/OpenMP/bug60602.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,3 @@ int kernel_within_loop(int *a, int *b, int N, int num_iters) {
569569
// CHECK: omp.precond.end:
570570
// CHECK-NEXT: ret void
571571
//
572-
//
573-
// CHECK-LABEL: define internal void @.omp_offloading.requires_reg
574-
// CHECK-SAME: () #[[ATTR4:[0-9]+]] {
575-
// CHECK-NEXT: entry:
576-
// CHECK-NEXT: call void @__tgt_register_requires(i64 1)
577-
// CHECK-NEXT: ret void
578-
//

clang/test/OpenMP/distribute_codegen.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,13 +1037,6 @@ int fint(void) { return ftemplate<int>(); }
10371037
// CHECK1-NEXT: ret void
10381038
//
10391039
//
1040-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1041-
// CHECK1-SAME: () #[[ATTR3:[0-9]+]] {
1042-
// CHECK1-NEXT: entry:
1043-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
1044-
// CHECK1-NEXT: ret void
1045-
//
1046-
//
10471040
// CHECK3-LABEL: define {{[^@]+}}@_Z23without_schedule_clausePfS_S_S_
10481041
// CHECK3-SAME: (ptr noundef [[A:%.*]], ptr noundef [[B:%.*]], ptr noundef [[C:%.*]], ptr noundef [[D:%.*]]) #[[ATTR0:[0-9]+]] {
10491042
// CHECK3-NEXT: entry:
@@ -1953,13 +1946,6 @@ int fint(void) { return ftemplate<int>(); }
19531946
// CHECK3-NEXT: ret void
19541947
//
19551948
//
1956-
// CHECK3-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1957-
// CHECK3-SAME: () #[[ATTR3:[0-9]+]] {
1958-
// CHECK3-NEXT: entry:
1959-
// CHECK3-NEXT: call void @__tgt_register_requires(i64 1)
1960-
// CHECK3-NEXT: ret void
1961-
//
1962-
//
19631949
// CHECK17-LABEL: define {{[^@]+}}@{{__omp_offloading_[0-9a-z]+_[0-9a-z]+}}__Z23without_schedule_clausePfS_S_S__l56
19641950
// CHECK17-SAME: (ptr noalias noundef [[DYN_PTR:%.*]], ptr noundef [[A:%.*]], ptr noundef [[B:%.*]], ptr noundef [[C:%.*]], ptr noundef [[D:%.*]]) #[[ATTR0:[0-9]+]] {
19651951
// CHECK17-NEXT: entry:

clang/test/OpenMP/distribute_firstprivate_codegen.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,6 @@ int main() {
304304
// CHECK1-NEXT: ret void
305305
//
306306
//
307-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
308-
// CHECK1-SAME: () #[[ATTR4:[0-9]+]] {
309-
// CHECK1-NEXT: entry:
310-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
311-
// CHECK1-NEXT: ret void
312-
//
313-
//
314307
// CHECK3-LABEL: define {{[^@]+}}@main
315308
// CHECK3-SAME: () #[[ATTR0:[0-9]+]] {
316309
// CHECK3-NEXT: entry:
@@ -476,13 +469,6 @@ int main() {
476469
// CHECK3-NEXT: ret void
477470
//
478471
//
479-
// CHECK3-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
480-
// CHECK3-SAME: () #[[ATTR4:[0-9]+]] {
481-
// CHECK3-NEXT: entry:
482-
// CHECK3-NEXT: call void @__tgt_register_requires(i64 1)
483-
// CHECK3-NEXT: ret void
484-
//
485-
//
486472
// CHECK9-LABEL: define {{[^@]+}}@main
487473
// CHECK9-SAME: () #[[ATTR0:[0-9]+]] {
488474
// CHECK9-NEXT: entry:
@@ -794,7 +780,7 @@ int main() {
794780
//
795781
//
796782
// CHECK9-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
797-
// CHECK9-SAME: () #[[ATTR5:[0-9]+]] comdat {
783+
// CHECK9-SAME: () #[[ATTR1]] comdat {
798784
// CHECK9-NEXT: entry:
799785
// CHECK9-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
800786
// CHECK9-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -1148,13 +1134,6 @@ int main() {
11481134
// CHECK9-NEXT: ret void
11491135
//
11501136
//
1151-
// CHECK9-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1152-
// CHECK9-SAME: () #[[ATTR6:[0-9]+]] {
1153-
// CHECK9-NEXT: entry:
1154-
// CHECK9-NEXT: call void @__tgt_register_requires(i64 1)
1155-
// CHECK9-NEXT: ret void
1156-
//
1157-
//
11581137
// CHECK11-LABEL: define {{[^@]+}}@main
11591138
// CHECK11-SAME: () #[[ATTR0:[0-9]+]] {
11601139
// CHECK11-NEXT: entry:
@@ -1464,7 +1443,7 @@ int main() {
14641443
//
14651444
//
14661445
// CHECK11-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
1467-
// CHECK11-SAME: () #[[ATTR5:[0-9]+]] comdat {
1446+
// CHECK11-SAME: () #[[ATTR1]] comdat {
14681447
// CHECK11-NEXT: entry:
14691448
// CHECK11-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
14701449
// CHECK11-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -1815,10 +1794,3 @@ int main() {
18151794
// CHECK11-NEXT: [[THIS1:%.*]] = load ptr, ptr [[THIS_ADDR]], align 4
18161795
// CHECK11-NEXT: ret void
18171796
//
1818-
//
1819-
// CHECK11-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1820-
// CHECK11-SAME: () #[[ATTR6:[0-9]+]] {
1821-
// CHECK11-NEXT: entry:
1822-
// CHECK11-NEXT: call void @__tgt_register_requires(i64 1)
1823-
// CHECK11-NEXT: ret void
1824-
//

clang/test/OpenMP/distribute_lastprivate_codegen.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,6 @@ int main() {
291291
// CHECK1-NEXT: ret void
292292
//
293293
//
294-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
295-
// CHECK1-SAME: () #[[ATTR4:[0-9]+]] {
296-
// CHECK1-NEXT: entry:
297-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
298-
// CHECK1-NEXT: ret void
299-
//
300-
//
301294
// CHECK3-LABEL: define {{[^@]+}}@main
302295
// CHECK3-SAME: () #[[ATTR0:[0-9]+]] {
303296
// CHECK3-NEXT: entry:
@@ -460,13 +453,6 @@ int main() {
460453
// CHECK3-NEXT: ret void
461454
//
462455
//
463-
// CHECK3-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
464-
// CHECK3-SAME: () #[[ATTR4:[0-9]+]] {
465-
// CHECK3-NEXT: entry:
466-
// CHECK3-NEXT: call void @__tgt_register_requires(i64 1)
467-
// CHECK3-NEXT: ret void
468-
//
469-
//
470456
// CHECK9-LABEL: define {{[^@]+}}@main
471457
// CHECK9-SAME: () #[[ATTR0:[0-9]+]] {
472458
// CHECK9-NEXT: entry:
@@ -797,7 +783,7 @@ int main() {
797783
//
798784
//
799785
// CHECK9-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
800-
// CHECK9-SAME: () #[[ATTR5:[0-9]+]] comdat {
786+
// CHECK9-SAME: () #[[ATTR1]] comdat {
801787
// CHECK9-NEXT: entry:
802788
// CHECK9-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
803789
// CHECK9-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -1169,13 +1155,6 @@ int main() {
11691155
// CHECK9-NEXT: ret void
11701156
//
11711157
//
1172-
// CHECK9-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1173-
// CHECK9-SAME: () #[[ATTR6:[0-9]+]] {
1174-
// CHECK9-NEXT: entry:
1175-
// CHECK9-NEXT: call void @__tgt_register_requires(i64 1)
1176-
// CHECK9-NEXT: ret void
1177-
//
1178-
//
11791158
// CHECK11-LABEL: define {{[^@]+}}@main
11801159
// CHECK11-SAME: () #[[ATTR0:[0-9]+]] {
11811160
// CHECK11-NEXT: entry:
@@ -1504,7 +1483,7 @@ int main() {
15041483
//
15051484
//
15061485
// CHECK11-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
1507-
// CHECK11-SAME: () #[[ATTR5:[0-9]+]] comdat {
1486+
// CHECK11-SAME: () #[[ATTR1]] comdat {
15081487
// CHECK11-NEXT: entry:
15091488
// CHECK11-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
15101489
// CHECK11-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -1873,10 +1852,3 @@ int main() {
18731852
// CHECK11-NEXT: [[THIS1:%.*]] = load ptr, ptr [[THIS_ADDR]], align 4
18741853
// CHECK11-NEXT: ret void
18751854
//
1876-
//
1877-
// CHECK11-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1878-
// CHECK11-SAME: () #[[ATTR6:[0-9]+]] {
1879-
// CHECK11-NEXT: entry:
1880-
// CHECK11-NEXT: call void @__tgt_register_requires(i64 1)
1881-
// CHECK11-NEXT: ret void
1882-
//

clang/test/OpenMP/distribute_parallel_for_codegen.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,13 +2538,6 @@ int main() {
25382538
// CHECK1-NEXT: ret void
25392539
//
25402540
//
2541-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
2542-
// CHECK1-SAME: () #[[ATTR4:[0-9]+]] {
2543-
// CHECK1-NEXT: entry:
2544-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
2545-
// CHECK1-NEXT: ret void
2546-
//
2547-
//
25482541
// CHECK3-LABEL: define {{[^@]+}}@main
25492542
// CHECK3-SAME: () #[[ATTR0:[0-9]+]] {
25502543
// CHECK3-NEXT: entry:
@@ -4265,13 +4258,6 @@ int main() {
42654258
// CHECK3-NEXT: ret void
42664259
//
42674260
//
4268-
// CHECK3-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
4269-
// CHECK3-SAME: () #[[ATTR4:[0-9]+]] {
4270-
// CHECK3-NEXT: entry:
4271-
// CHECK3-NEXT: call void @__tgt_register_requires(i64 1)
4272-
// CHECK3-NEXT: ret void
4273-
//
4274-
//
42754261
// CHECK9-LABEL: define {{[^@]+}}@main
42764262
// CHECK9-SAME: () #[[ATTR0:[0-9]+]] {
42774263
// CHECK9-NEXT: entry:
@@ -8886,13 +8872,6 @@ int main() {
88868872
// CHECK9-NEXT: ret void
88878873
//
88888874
//
8889-
// CHECK9-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
8890-
// CHECK9-SAME: () #[[ATTR4:[0-9]+]] {
8891-
// CHECK9-NEXT: entry:
8892-
// CHECK9-NEXT: call void @__tgt_register_requires(i64 1)
8893-
// CHECK9-NEXT: ret void
8894-
//
8895-
//
88968875
// CHECK11-LABEL: define {{[^@]+}}@main
88978876
// CHECK11-SAME: () #[[ATTR0:[0-9]+]] {
88988877
// CHECK11-NEXT: entry:
@@ -13404,10 +13383,3 @@ int main() {
1340413383
// CHECK11: omp.precond.end:
1340513384
// CHECK11-NEXT: ret void
1340613385
//
13407-
//
13408-
// CHECK11-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
13409-
// CHECK11-SAME: () #[[ATTR4:[0-9]+]] {
13410-
// CHECK11-NEXT: entry:
13411-
// CHECK11-NEXT: call void @__tgt_register_requires(i64 1)
13412-
// CHECK11-NEXT: ret void
13413-
//

clang/test/OpenMP/distribute_parallel_for_firstprivate_codegen.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,6 @@ int main() {
504504
// CHECK1-NEXT: ret void
505505
//
506506
//
507-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
508-
// CHECK1-SAME: () #[[ATTR4:[0-9]+]] {
509-
// CHECK1-NEXT: entry:
510-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
511-
// CHECK1-NEXT: ret void
512-
//
513-
//
514507
// CHECK3-LABEL: define {{[^@]+}}@main
515508
// CHECK3-SAME: () #[[ATTR0:[0-9]+]] {
516509
// CHECK3-NEXT: entry:
@@ -759,13 +752,6 @@ int main() {
759752
// CHECK3-NEXT: ret void
760753
//
761754
//
762-
// CHECK3-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
763-
// CHECK3-SAME: () #[[ATTR4:[0-9]+]] {
764-
// CHECK3-NEXT: entry:
765-
// CHECK3-NEXT: call void @__tgt_register_requires(i64 1)
766-
// CHECK3-NEXT: ret void
767-
//
768-
//
769755
// CHECK8-LABEL: define {{[^@]+}}@main
770756
// CHECK8-SAME: () #[[ATTR0:[0-9]+]] {
771757
// CHECK8-NEXT: entry:
@@ -1207,7 +1193,7 @@ int main() {
12071193
//
12081194
//
12091195
// CHECK8-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
1210-
// CHECK8-SAME: () #[[ATTR5:[0-9]+]] comdat {
1196+
// CHECK8-SAME: () #[[ATTR1]] comdat {
12111197
// CHECK8-NEXT: entry:
12121198
// CHECK8-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
12131199
// CHECK8-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -1685,13 +1671,6 @@ int main() {
16851671
// CHECK8-NEXT: ret void
16861672
//
16871673
//
1688-
// CHECK8-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1689-
// CHECK8-SAME: () #[[ATTR6:[0-9]+]] {
1690-
// CHECK8-NEXT: entry:
1691-
// CHECK8-NEXT: call void @__tgt_register_requires(i64 1)
1692-
// CHECK8-NEXT: ret void
1693-
//
1694-
//
16951674
// CHECK10-LABEL: define {{[^@]+}}@main
16961675
// CHECK10-SAME: () #[[ATTR0:[0-9]+]] {
16971676
// CHECK10-NEXT: entry:
@@ -2127,7 +2106,7 @@ int main() {
21272106
//
21282107
//
21292108
// CHECK10-LABEL: define {{[^@]+}}@_Z5tmainIiET_v
2130-
// CHECK10-SAME: () #[[ATTR5:[0-9]+]] comdat {
2109+
// CHECK10-SAME: () #[[ATTR1]] comdat {
21312110
// CHECK10-NEXT: entry:
21322111
// CHECK10-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
21332112
// CHECK10-NEXT: [[TEST:%.*]] = alloca [[STRUCT_S_0:%.*]], align 4
@@ -2598,10 +2577,3 @@ int main() {
25982577
// CHECK10-NEXT: [[THIS1:%.*]] = load ptr, ptr [[THIS_ADDR]], align 4
25992578
// CHECK10-NEXT: ret void
26002579
//
2601-
//
2602-
// CHECK10-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
2603-
// CHECK10-SAME: () #[[ATTR6:[0-9]+]] {
2604-
// CHECK10-NEXT: entry:
2605-
// CHECK10-NEXT: call void @__tgt_register_requires(i64 1)
2606-
// CHECK10-NEXT: ret void
2607-
//

clang/test/OpenMP/distribute_parallel_for_if_codegen.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,3 @@ int main() {
16091609
// CHECK1-NEXT: call void @__kmpc_for_static_fini(ptr @[[GLOB1]], i32 [[TMP3]])
16101610
// CHECK1-NEXT: ret void
16111611
//
1612-
//
1613-
// CHECK1-LABEL: define {{[^@]+}}@.omp_offloading.requires_reg
1614-
// CHECK1-SAME: () #[[ATTR5:[0-9]+]] {
1615-
// CHECK1-NEXT: entry:
1616-
// CHECK1-NEXT: call void @__tgt_register_requires(i64 1)
1617-
// CHECK1-NEXT: ret void
1618-
//

0 commit comments

Comments
 (0)