Skip to content

Commit 2d321ce

Browse files
committed
Revert "Relocate and use llvm::omp::getDeviceKernels"
This reverts commit bb9d5c2. This will facilitate merging main due to 07ed818 (PR llvm#122320), which changes llvm::omp::getDeviceKernels. Will rewrite and reapply after merging main.
1 parent 0a347cf commit 2d321ce

File tree

5 files changed

+39
-74
lines changed

5 files changed

+39
-74
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "llvm-c/Types.h"
1818
#include "llvm/ADT/STLExtras.h"
19-
#include "llvm/ADT/SetVector.h"
2019
#include "llvm/ADT/StringMap.h"
2120
#include "llvm/ADT/StringRef.h"
2221
#include "llvm/ADT/iterator_range.h"
@@ -514,9 +513,6 @@ class LLVM_ABI Module {
514513
/// Remove the given NamedMDNode from this module and delete it.
515514
void eraseNamedMetadata(NamedMDNode *NMD);
516515

517-
/// Get device kernels in the module.
518-
SetVector<Function *> getDeviceKernels();
519-
520516
/// @}
521517
/// @name Comdat Accessors
522518
/// @{

llvm/lib/Analysis/KernelInfo.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class KernelInfo {
7878

7979
} // end anonymous namespace
8080

81+
static bool isKernelFunction(Function &F) {
82+
// TODO: Is this general enough? Consider languages beyond OpenMP.
83+
return F.hasFnAttribute("kernel");
84+
}
85+
8186
static void identifyCallee(OptimizationRemark &R, const Module *M,
8287
const Value *V, StringRef Kind = "") {
8388
SmallString<100> Name; // might be function name or asm expression
@@ -287,8 +292,7 @@ void KernelInfo::emitKernelInfo(Function &F, FunctionAnalysisManager &FAM,
287292
KI.FlatAddrspace = TheTTI.getFlatAddressSpace();
288293

289294
// Record function properties.
290-
KI.ExternalNotKernel =
291-
F.hasExternalLinkage() && !F.getParent()->getDeviceKernels().contains(&F);
295+
KI.ExternalNotKernel = F.hasExternalLinkage() && !isKernelFunction(F);
292296
for (StringRef Name : {"omp_target_num_teams", "omp_target_thread_limit"}) {
293297
if (auto Val = parseFnAttrAsInteger(F, Name))
294298
KI.LaunchBounds.push_back({Name, *Val});

llvm/lib/IR/Module.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,6 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) {
322322
eraseNamedMDNode(NMD);
323323
}
324324

325-
SetVector<Function *> Module::getDeviceKernels() {
326-
// TODO: Create a more cross-platform way of determining device kernels.
327-
NamedMDNode *MD = getNamedMetadata("nvvm.annotations");
328-
SetVector<Function *> Kernels;
329-
330-
if (!MD)
331-
return Kernels;
332-
333-
for (auto *Op : MD->operands()) {
334-
if (Op->getNumOperands() < 2)
335-
continue;
336-
MDString *KindID = dyn_cast<MDString>(Op->getOperand(1));
337-
if (!KindID || KindID->getString() != "kernel")
338-
continue;
339-
340-
Function *KernelFn =
341-
mdconst::dyn_extract_or_null<Function>(Op->getOperand(0));
342-
if (!KernelFn)
343-
continue;
344-
345-
Kernels.insert(KernelFn);
346-
}
347-
348-
return Kernels;
349-
}
350-
351325
bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
352326
if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
353327
uint64_t Val = Behavior->getLimitedValue();

llvm/lib/Transforms/IPO/OpenMPOpt.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5910,16 +5910,34 @@ bool llvm::omp::isOpenMPKernel(Function &Fn) {
59105910
}
59115911

59125912
KernelSet llvm::omp::getDeviceKernels(Module &M) {
5913+
// TODO: Create a more cross-platform way of determining device kernels.
5914+
NamedMDNode *MD = M.getNamedMetadata("nvvm.annotations");
59135915
KernelSet Kernels;
5914-
for (Kernel K : M.getDeviceKernels()) {
5916+
5917+
if (!MD)
5918+
return Kernels;
5919+
5920+
for (auto *Op : MD->operands()) {
5921+
if (Op->getNumOperands() < 2)
5922+
continue;
5923+
MDString *KindID = dyn_cast<MDString>(Op->getOperand(1));
5924+
if (!KindID || KindID->getString() != "kernel")
5925+
continue;
5926+
5927+
Function *KernelFn =
5928+
mdconst::dyn_extract_or_null<Function>(Op->getOperand(0));
5929+
if (!KernelFn)
5930+
continue;
5931+
59155932
// We are only interested in OpenMP target regions. Others, such as kernels
59165933
// generated by CUDA but linked together, are not interesting to this pass.
5917-
if (isOpenMPKernel(*K)) {
5934+
if (isOpenMPKernel(*KernelFn)) {
59185935
++NumOpenMPTargetRegionKernels;
5919-
Kernels.insert(K);
5936+
Kernels.insert(KernelFn);
59205937
} else
59215938
++NumNonOpenMPTargetRegionKernels;
59225939
}
5940+
59235941
return Kernels;
59245942
}
59255943

llvm/test/Analysis/KernelInfo/linkage.ll

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,32 @@
77
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
88
target triple = "nvptx64-nvidia-cuda"
99

10-
; CHECK: remark: test.c:3:0: in function 'extNotKer', ExternalNotKernel = 1
11-
define external void @extNotKer() !dbg !10 {
10+
; CHECK: remark: test.c:3:0: in function 'f', ExternalNotKernel = 1
11+
define external void @f() !dbg !10 {
1212
entry:
1313
ret void
1414
}
1515

16-
; CHECK: remark: test.c:13:0: in artificial function 'impNotKer', ExternalNotKernel = 1
17-
define void @impNotKer() !dbg !20 {
16+
; CHECK: remark: test.c:13:0: in artificial function 'g', ExternalNotKernel = 1
17+
define void @g() !dbg !20 {
1818
entry:
1919
ret void
2020
}
2121

22-
; CHECK: remark: test.c:23:0: in artificial function 'weakNotKer', ExternalNotKernel = 0
23-
define weak void @weakNotKer() !dbg !30 {
22+
; CHECK: remark: test.c:23:0: in function 'h', ExternalNotKernel = 0
23+
define external void @h() #0 !dbg !30 {
2424
entry:
2525
ret void
2626
}
2727

28-
; CHECK: remark: test.c:33:0: in function 'extKerAttr', ExternalNotKernel = 0
29-
define external void @extKerAttr() #0 !dbg !40 {
30-
entry:
31-
ret void
32-
}
33-
34-
; CHECK: remark: test.c:43:0: in function 'extKer', ExternalNotKernel = 0
35-
define external void @extKer() !dbg !50 {
36-
entry:
37-
ret void
38-
}
39-
40-
; CHECK: remark: test.c:53:0: in artificial function 'weakKer', ExternalNotKernel = 0
41-
define weak void @weakKer() !dbg !60 {
28+
; CHECK: remark: test.c:33:0: in artificial function 'i', ExternalNotKernel = 0
29+
define weak void @i() !dbg !40 {
4230
entry:
4331
ret void
4432
}
4533

4634
attributes #0 = { "kernel" }
4735

48-
!nvvm.annotations = !{!42, !52, !62}
49-
5036
!llvm.module.flags = !{!0}
5137
!llvm.dbg.cu = !{!1}
5238

@@ -55,24 +41,11 @@ attributes #0 = { "kernel" }
5541
!2 = !DIFile(filename: "test.c", directory: "/tmp")
5642
!3 = !{null}
5743
!4 = !{}
58-
59-
!10 = distinct !DISubprogram(name: "extNotKer", scope: !2, file: !2, line: 3, type: !11, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !1, retainedNodes: !4)
44+
!10 = distinct !DISubprogram(name: "f", scope: !2, file: !2, line: 3, type: !11, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !1, retainedNodes: !4)
6045
!11 = !DISubroutineType(types: !3)
61-
62-
!20 = distinct !DISubprogram(name: "impNotKer", scope: !2, file: !2, line: 13, type: !21, scopeLine: 13, flags: DIFlagArtificial | DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !1, retainedNodes: !4)
46+
!20 = distinct !DISubprogram(name: "g", scope: !2, file: !2, line: 13, type: !21, scopeLine: 13, flags: DIFlagArtificial | DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !1, retainedNodes: !4)
6347
!21 = distinct !DISubroutineType(types: !3)
64-
65-
!30 = distinct !DISubprogram(name: "weakNotKer", scope: !2, file: !2, line: 23, type: !31, scopeLine: 23, flags: DIFlagArtificial | DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !1, retainedNodes: !4)
48+
!30 = distinct !DISubprogram(name: "h", scope: !2, file: !2, line: 23, type: !31, scopeLine: 23, spFlags: DISPFlagDefinition, unit: !1, retainedNodes: !4)
6649
!31 = distinct !DISubroutineType(types: !3)
67-
68-
!40 = distinct !DISubprogram(name: "extKerAttr", scope: !2, file: !2, line: 33, type: !41, scopeLine: 33, spFlags: DISPFlagDefinition, unit: !1, retainedNodes: !4)
50+
!40 = distinct !DISubprogram(name: "i", scope: !2, file: !2, line: 33, type: !41, scopeLine: 33, flags: DIFlagArtificial | DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !1, retainedNodes: !4)
6951
!41 = distinct !DISubroutineType(types: !3)
70-
!42 = !{ptr @extKerAttr, !"kernel", i32 1}
71-
72-
!50 = distinct !DISubprogram(name: "extKer", scope: !2, file: !2, line: 43, type: !51, scopeLine: 43, spFlags: DISPFlagDefinition, unit: !1, retainedNodes: !4)
73-
!51 = distinct !DISubroutineType(types: !3)
74-
!52 = !{ptr @extKer, !"kernel", i32 1}
75-
76-
!60 = distinct !DISubprogram(name: "weakKer", scope: !2, file: !2, line: 53, type: !61, scopeLine: 53, flags: DIFlagArtificial | DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !1, retainedNodes: !4)
77-
!61 = distinct !DISubroutineType(types: !3)
78-
!62 = !{ptr @weakKer, !"kernel", i32 1}

0 commit comments

Comments
 (0)