Skip to content

[MLIR][LLVM] Improve inlining debug information #123520

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

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

gysit
Copy link
Contributor

@gysit gysit commented Jan 19, 2025

This commit improves the debug information for alloca and memcpy operations generated by the LLVM dialect inlining interface.

When inlining by value parameters, the inliner creates alloca and memcpy operations. This revision sets the location of these created operations to the respective argument locations instead of the function location. This change enables users to better identify the source code location of the copied variables.

@llvmbot
Copy link
Member

llvmbot commented Jan 19, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Tobias Gysi (gysit)

Changes

This commit improves the debug information for alloca and memcpy operations generated by the LLVM dialect inlining interface.

When inlining by value parameters, the inliner creates alloca and memcpy operations. This revision sets the location of these created operations to the respective argument locations instead of the function location. This change enables users to better identify the source code location of the copied variables.


Full diff: https://github.com/llvm/llvm-project/pull/123520.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp (+3-3)
  • (added) mlir/test/Dialect/LLVMIR/inlining-debuginfo.mlir (+21)
diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
index 79dd3e30696482..aab8d037cd8d21 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
@@ -643,9 +643,9 @@ static Value handleByValArgument(OpBuilder &builder, Operation *callable,
       return argument;
   }
   uint64_t targetAlignment = std::max(requestedAlignment, minimumAlignment);
-  return handleByValArgumentInit(builder, func.getLoc(), argument, elementType,
-                                 dataLayout.getTypeSize(elementType),
-                                 targetAlignment);
+  return handleByValArgumentInit(
+      builder, argument.getLoc(), argument, elementType,
+      dataLayout.getTypeSize(elementType), targetAlignment);
 }
 
 namespace {
diff --git a/mlir/test/Dialect/LLVMIR/inlining-debuginfo.mlir b/mlir/test/Dialect/LLVMIR/inlining-debuginfo.mlir
new file mode 100644
index 00000000000000..10b06d987e4498
--- /dev/null
+++ b/mlir/test/Dialect/LLVMIR/inlining-debuginfo.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt %s -inline -mlir-print-debuginfo | FileCheck %s
+
+llvm.func @foo() -> !llvm.ptr
+
+llvm.func @with_byval_arg(%ptr : !llvm.ptr { llvm.byval = f64 }) {
+  llvm.return
+}
+
+// CHECK-LABEL: llvm.func @test_byval
+llvm.func @test_byval() {
+  // CHECK: %[[COPY:.+]] = llvm.alloca %{{.+}} x f64
+  // CHECK-SAME: loc(#[[LOC:.+]])
+  // CHECK: %[[ORIG:.+]] = llvm.call @foo() : () -> !llvm.ptr loc(#[[LOC]])
+  %0 = llvm.call @foo() : () -> !llvm.ptr loc("inlining-debuginfo.mlir":14:2)
+  // CHECK: "llvm.intr.memcpy"(%[[COPY]], %[[ORIG]]
+  // CHECK-SAME: loc(#[[LOC]])
+  llvm.call @with_byval_arg(%0) : (!llvm.ptr) -> ()
+  llvm.return
+}
+
+// CHECK: #[[LOC]] = loc("inlining-debuginfo.mlir":14:2)
\ No newline at end of file

This commit improves the debug information for `alloca` and `memcpy`
operations generated by the LLVM dialect inlining interface.

When inlining by value parameters, the inliner creates `alloca` and
`memcpy` operations. This revision sets the location of these created
operations to the respective argument locations instead of the function
location. This change enables users to better identify the source code
location of the copied variables.
@gysit gysit force-pushed the improve-inlining-loc branch from f3d6693 to ee21acd Compare January 19, 2025 17:18
@gysit gysit requested review from Dinistro and definelicht January 19, 2025 17:19
Copy link
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@Dinistro Dinistro left a 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 fix.

@gysit gysit merged commit d0c9e70 into llvm:main Jan 20, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants