[OpenMP] Ensure to remove noinline from all runtime functions eventually
authorJohannes Doerfert <johannes@jdoerfert.de>
Fri, 21 Jan 2022 21:47:56 +0000 (15:47 -0600)
committerJohannes Doerfert <johannes@jdoerfert.de>
Tue, 1 Feb 2022 07:07:50 +0000 (01:07 -0600)
We used to remove noinline from known OpenMP runtime functions (which
are declared in OMPKinds.td). Now we remove noinline from all functions
with the proper prefixes: __kmpc, _ZN4_OMP (= namespace omp), omp_

llvm/lib/Transforms/IPO/OpenMPOpt.cpp
llvm/test/Transforms/OpenMP/remove_noinline_attributes.ll [new file with mode: 0644]

index 68f3341..2004550 100644 (file)
@@ -458,7 +458,6 @@ struct OMPInformationCache : public InformationCache {
     RTLFunctions.insert(F);                                                    \
     if (declMatchesRTFTypes(F, OMPBuilder._ReturnType, ArgsTypes)) {           \
       RuntimeFunctionIDMap[F] = _Enum;                                         \
-      F->removeFnAttr(Attribute::NoInline);                                    \
       auto &RFI = RFIs[_Enum];                                                 \
       RFI.Kind = _Enum;                                                        \
       RFI.Name = _Name;                                                        \
@@ -480,6 +479,15 @@ struct OMPInformationCache : public InformationCache {
   }
 #include "llvm/Frontend/OpenMP/OMPKinds.def"
 
+    // Remove the `noinline` attribute from `__kmpc`, `_OMP::` and `omp_`
+    // functions, except if `optnone` is present.
+    for (Function &F : M) {
+      for (StringRef Prefix : {"__kmpc", "_ZN4_OMP", "omp_"})
+        if (F.getName().startswith(Prefix) &&
+            !F.hasFnAttribute(Attribute::OptimizeNone))
+          F.removeFnAttr(Attribute::NoInline);
+    }
+
     // TODO: We should attach the attributes defined in OMPKinds.def.
   }
 
diff --git a/llvm/test/Transforms/OpenMP/remove_noinline_attributes.ll b/llvm/test/Transforms/OpenMP/remove_noinline_attributes.ll
new file mode 100644 (file)
index 0000000..76c5063
--- /dev/null
@@ -0,0 +1,98 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes
+; RUN: opt < %s -S -openmp-opt-cgscc        | FileCheck %s
+; RUN: opt < %s -S -passes=openmp-opt-cgscc | FileCheck %s
+
+declare void @unknown()
+
+; __kmpc functions
+define void @__kmpc_noinline() noinline nounwind {
+; CHECK: Function Attrs: nounwind
+; CHECK-LABEL: @__kmpc_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+; omp_X functions
+define void @omp_noinline() noinline nounwind {
+; CHECK: Function Attrs: nounwind
+; CHECK-LABEL: @omp_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+; _OMP namespace
+define void @_ZN4_OMP_noinline() noinline nounwind {
+; CHECK: Function Attrs: nounwind
+; CHECK-LABEL: @_ZN4_OMP_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+
+; Negative tests:
+
+define void @__kmpc_noinline_optnone() noinline optnone nounwind {
+; CHECK: Function Attrs: noinline nounwind optnone
+; CHECK-LABEL: @__kmpc_noinline_optnone(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+define void @omp_noinline_optnone() noinline optnone nounwind {
+; CHECK: Function Attrs: noinline nounwind optnone
+; CHECK-LABEL: @omp_noinline_optnone(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+; _OMP namespace
+define void @_ZN4_OMP_noinline_optnone() noinline optnone nounwind {
+; CHECK: Function Attrs: noinline nounwind optnone
+; CHECK-LABEL: @_ZN4_OMP_noinline_optnone(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+define void @a___kmpc_noinline() noinline nounwind {
+; CHECK: Function Attrs: noinline nounwind
+; CHECK-LABEL: @a___kmpc_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+define void @a_omp_noinline() noinline nounwind {
+; CHECK: Function Attrs: noinline nounwind
+; CHECK-LABEL: @a_omp_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+define void @a__ZN4_OMP_noinline() noinline nounwind {
+; CHECK: Function Attrs: noinline nounwind
+; CHECK-LABEL: @a__ZN4_OMP_noinline(
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    ret void
+;
+  call void @unknown()
+  ret void
+}
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 7, !"openmp", i32 50}