-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][LLVMIR] Handle missing functions in CGProfile module flags #169517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-llvm Author: Men-cotton (Men-cotton) ChangesFixes: #160717 Now Full diff: https://github.com/llvm/llvm-project/pull/169517.diff 2 Files Affected:
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
index 7d3486acaf82a..f11b9a6c61690 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
@@ -243,16 +243,17 @@ convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr,
if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
for (auto entry : arrayAttr.getAsRange<ModuleFlagCGProfileEntryAttr>()) {
- llvm::Metadata *fromMetadata =
- entry.getFrom()
- ? llvm::ValueAsMetadata::get(moduleTranslation.lookupFunction(
- entry.getFrom().getValue()))
- : nullptr;
- llvm::Metadata *toMetadata =
- entry.getTo()
- ? llvm::ValueAsMetadata::get(
- moduleTranslation.lookupFunction(entry.getTo().getValue()))
- : nullptr;
+ const auto getFuncMetadata =
+ [&](FlatSymbolRefAttr sym) -> llvm::Metadata * {
+ if (!sym)
+ return nullptr;
+ if (llvm::Function *fn =
+ moduleTranslation.lookupFunction(sym.getValue()))
+ return llvm::ValueAsMetadata::get(fn);
+ return nullptr;
+ };
+ llvm::Metadata *const fromMetadata = getFuncMetadata(entry.getFrom());
+ llvm::Metadata *const toMetadata = getFuncMetadata(entry.getTo());
llvm::Metadata *vals[] = {
fromMetadata, toMetadata,
diff --git a/mlir/test/Dialect/LLVMIR/invalid-cg-profile.mlir b/mlir/test/Dialect/LLVMIR/invalid-cg-profile.mlir
new file mode 100644
index 0000000000000..d7a0f386303e0
--- /dev/null
+++ b/mlir/test/Dialect/LLVMIR/invalid-cg-profile.mlir
@@ -0,0 +1,26 @@
+// RUN: mlir-translate %s -mlir-to-llvmir | FileCheck %s
+// CHECK: !llvm.module.flags
+
+module {
+ llvm.module_flags [#llvm.mlir.module_flag<error, "wchar_size", 4 : i32>,
+ #llvm.mlir.module_flag<min, "PIC Level", 2 : i32>,
+ #llvm.mlir.module_flag<max, "PIE Level", 2 : i32>,
+ #llvm.mlir.module_flag<max, "uwtable", 2 : i32>,
+ #llvm.mlir.module_flag<max, "frame-pointer", 1 : i32>,
+ #llvm.mlir.module_flag<override, "probe-stack", "inline-asm">,
+ #llvm.mlir.module_flag<append, "CG Profile", [
+ #llvm.cgprofile_entry<from = @from, to = @to, count = 222>,
+ #llvm.cgprofile_entry<from = @from, count = 222>,
+ #llvm.cgprofile_entry<from = @to, to = @from, count = 222>
+ ]>,
+ #llvm.mlir.module_flag<error, "ProfileSummary",
+ #llvm.profile_summary<format = InstrProf, total_count = 263646, max_count = 86427,
+ max_internal_count = 86427, max_function_count = 4691,
+ num_counts = 3712, num_functions = 796,
+ is_partial_profile = 0,
+ partial_profile_ratio = 0.000000e+00 : f64,
+ detailed_summary =
+ <cut_off = 10000, min_count = 86427, num_counts = 1>,
+ <cut_off = 100000, min_count = 86427, num_counts = 1>
+ >>]
+}
|
Dinistro
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if it's legal that the cgprofile entries have missing functions?
mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
Outdated
Show resolved
Hide resolved
mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
Outdated
Show resolved
Hide resolved
@Men-cotton can you give some context on this? I did not perform a deep search but so far did not encounter any documentation that states anything about this. |
I think it's legal. llvm-project/llvm/lib/IR/Verifier.cpp Lines 2033 to 2048 in cca66a2
llvm-project/llvm/lib/Target/TargetLoweringObjectFile.cpp Lines 177 to 192 in cca66a2
|
|
One more relevant spot: llvm-project/llvm/lib/Transforms/IPO/StripSymbols.cpp Lines 303 to 318 in cca66a2
So verifier allows nulls, object emission skips them, and this pass proactively strips them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo the @Dinistro 's comment!
Thanks for the pointers!
Dinistro
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the clarifications and addressing the comments.
…vm#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: llvm#160717
Fixes: #160717
Now
build/bin/mlir-translate -mlir-to-llvmir mlir/test/Dialect/LLVMIR/module-roundtrip.mlirpasses.