-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][LLVM] Extend DIScopeForLLVMFuncOp to handle cross-file operatio… #167844
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
base: main
Are you sure you want to change the base?
Conversation
…ons without CallSiteLoc
|
@llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: Zichen Lu (MikaOvO) ChangesThe current Scenario: # a.py
@<!-- -->cute.kernel
def kernel_a(tensor):
cute.print("a: {}", tensor) # a.py:3
jit_func_b(tensor) # Calls b.py code
# b.py
@<!-- -->cute.jit
def jit_func_b(tensor):
cute.print("b: {}", tensor) # b.py:7The DSL executes Python at compile-time and directly inserts operations from cuda.kernel @<!-- -->kernel_a(...) {
cute.print("a: {}", %arg0) loc(#loc_a) // a.py:3
cute.print("b: {}", %arg0) loc(#loc_b) // b.py:7 <- FileLineColLoc, not CallSiteLoc
} loc(#loc_kernel) // a.py:1
#loc1 = loc("a.py":3:.)
#loc2 = loc("b.py":7:.)
#loc_a = loc("cute.printf"(#loc1))
#loc_b = loc("cute.printf"(#loc2))!6 = !DIFile(filename: "a.py", directory: "...")
!9 = distinct !DISubprogram(name: "...", linkageName: "...", scope: !6, file: !6, line: 13, ...)
!10 = !DILocation(line: 7, column: ., scope: !9) // Points to kernel's DISubprogram, not correctFull diff: https://github.com/llvm/llvm-project/pull/167844.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
index 67573c4ee6061..8862cf8e70366 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
@@ -122,7 +122,44 @@ static void setLexicalBlockFileAttr(Operation *op) {
op->setLoc(
CallSiteLoc::get(getNestedLoc(op, scopeAttr, calleeLoc), callerLoc));
}
+
+ return;
}
+
+ auto funcOp = op->getParentOfType<LLVM::LLVMFuncOp>();
+ if (!funcOp)
+ return;
+
+ FileLineColLoc opFileLoc = extractFileLoc(opLoc);
+ if (!opFileLoc)
+ return;
+
+ FileLineColLoc funcFileLoc = extractFileLoc(funcOp.getLoc());
+ if (!funcFileLoc)
+ return;
+
+ StringRef opFile = opFileLoc.getFilename().getValue();
+ StringRef funcFile = funcFileLoc.getFilename().getValue();
+
+ if (opFile != funcFile) {
+ auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc());
+ if (!funcOpLoc)
+ return;
+ auto scopeAttr = dyn_cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
+ if (!scopeAttr)
+ return;
+
+ auto *context = op->getContext();
+ LLVM::DIFileAttr opFileAttr =
+ LLVM::DIFileAttr::get(context, llvm::sys::path::filename(opFile),
+ llvm::sys::path::parent_path(opFile));
+
+ auto lexicalBlockFileAttr = LLVM::DILexicalBlockFileAttr::get(
+ context, scopeAttr, opFileAttr, 0);
+
+ Location newLoc = FusedLoc::get(context, {opLoc}, lexicalBlockFileAttr);
+ op->setLoc(newLoc);
+ }
}
namespace {
|
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp -- mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
index 8862cf8e7..3ba289fbd 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
@@ -136,14 +136,14 @@ static void setLexicalBlockFileAttr(Operation *op) {
FileLineColLoc funcFileLoc = extractFileLoc(funcOp.getLoc());
if (!funcFileLoc)
- return;
+ return;
StringRef opFile = opFileLoc.getFilename().getValue();
StringRef funcFile = funcFileLoc.getFilename().getValue();
if (opFile != funcFile) {
auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc());
- if (!funcOpLoc)
+ if (!funcOpLoc)
return;
auto scopeAttr = dyn_cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
if (!scopeAttr)
@@ -154,12 +154,12 @@ static void setLexicalBlockFileAttr(Operation *op) {
LLVM::DIFileAttr::get(context, llvm::sys::path::filename(opFile),
llvm::sys::path::parent_path(opFile));
- auto lexicalBlockFileAttr = LLVM::DILexicalBlockFileAttr::get(
- context, scopeAttr, opFileAttr, 0);
+ auto lexicalBlockFileAttr =
+ LLVM::DILexicalBlockFileAttr::get(context, scopeAttr, opFileAttr, 0);
Location newLoc = FusedLoc::get(context, {opLoc}, lexicalBlockFileAttr);
op->setLoc(newLoc);
- }
+ }
}
namespace {
|
The current
DIScopeForLLVMFuncOppass handles debug information for inlined code by processingCallSiteLocattributes. However, some compilation scenarios (particularly DSL frameworks) compose code from multiple source files directly into a single function without generatingCallSiteLoc.Scenario:
The DSL executes Python at compile-time and directly inserts operations from
b.pyinto the kernel function, resulting in MLIR like: