Skip to content

Commit fede947

Browse files
authored
[mlir][LLVMIR] Handle missing functions in CGProfile module flags (#169517)
This commit extends the CGProfile module flags export with support for missing function references. Previously, this caused a crash and now it's properly exported to `null` values in the metadata node. Fixes: #160717
1 parent 4099121 commit fede947

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,16 @@ convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr,
243243

244244
if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
245245
for (auto entry : arrayAttr.getAsRange<ModuleFlagCGProfileEntryAttr>()) {
246-
llvm::Metadata *fromMetadata =
247-
entry.getFrom()
248-
? llvm::ValueAsMetadata::get(moduleTranslation.lookupFunction(
249-
entry.getFrom().getValue()))
250-
: nullptr;
251-
llvm::Metadata *toMetadata =
252-
entry.getTo()
253-
? llvm::ValueAsMetadata::get(
254-
moduleTranslation.lookupFunction(entry.getTo().getValue()))
255-
: nullptr;
246+
auto getFuncMetadata = [&](FlatSymbolRefAttr sym) -> llvm::Metadata * {
247+
if (!sym)
248+
return nullptr;
249+
if (llvm::Function *fn =
250+
moduleTranslation.lookupFunction(sym.getValue()))
251+
return llvm::ValueAsMetadata::get(fn);
252+
return nullptr;
253+
};
254+
llvm::Metadata *fromMetadata = getFuncMetadata(entry.getFrom());
255+
llvm::Metadata *toMetadata = getFuncMetadata(entry.getTo());
256256

257257
llvm::Metadata *vals[] = {
258258
fromMetadata, toMetadata,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: mlir-translate %s -mlir-to-llvmir | FileCheck %s
2+
// CHECK: !llvm.module.flags = !{![[CG_FLAG:[0-9]+]], ![[DBG_FLAG:[0-9]+]]}
3+
// CHECK: ![[CG_FLAG]] = !{i32 5, !"CG Profile", ![[CG_LIST:[0-9]+]]}
4+
// CHECK: ![[CG_LIST]] = distinct !{![[CG_ENTRY:[0-9]+]], ![[CG_ENTRY]], ![[CG_ENTRY]]}
5+
// CHECK: ![[CG_ENTRY]] = !{null, null, i64 222}
6+
// CHECK: ![[DBG_FLAG]] = !{i32 2, !"Debug Info Version", i32 3}
7+
8+
module {
9+
llvm.module_flags [#llvm.mlir.module_flag<append, "CG Profile", [
10+
#llvm.cgprofile_entry<from = @from, to = @to, count = 222>,
11+
#llvm.cgprofile_entry<from = @from, count = 222>,
12+
#llvm.cgprofile_entry<from = @to, to = @from, count = 222>
13+
]>]
14+
}

0 commit comments

Comments
 (0)