[Mlir-commits] [mlir] 7d5b4eb - [mlir][LLVM] Stop importing module location for all unknown locs

Christian Ulmann llvmlistbot at llvm.org
Tue Jul 4 07:04:45 PDT 2023


Author: Christian Ulmann
Date: 2023-07-04T14:03:07Z
New Revision: 7d5b4ebb22732535c0f3af926805428686ec71d3

URL: https://github.com/llvm/llvm-project/commit/7d5b4ebb22732535c0f3af926805428686ec71d3
DIFF: https://github.com/llvm/llvm-project/commit/7d5b4ebb22732535c0f3af926805428686ec71d3.diff

LOG: [mlir][LLVM] Stop importing module location for all unknown locs

This commit changes the LLVM IR import to use UnkownLoc for missing
debug locations. This change ensures that we do not accidentially
introduce faulty locations that can influence debugging post export.

This behavior change is not applied to locations of global metadata
operations, as their location will not be exported.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D154416

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/DebugImporter.cpp
    mlir/lib/Target/LLVMIR/DebugImporter.h
    mlir/lib/Target/LLVMIR/ModuleImport.cpp
    mlir/test/Target/LLVMIR/Import/basic.ll
    mlir/test/Target/LLVMIR/Import/debug-info.ll
    mlir/test/Target/LLVMIR/Import/import-failure.ll

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
index 7d4c927fa25448..fbd2324fd9ed31 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
@@ -23,14 +23,14 @@ using namespace mlir;
 using namespace mlir::LLVM;
 using namespace mlir::LLVM::detail;
 
-void DebugImporter::translate(llvm::Function *func, LLVMFuncOp funcOp) {
+Location DebugImporter::translateFuncLocation(llvm::Function *func) {
   if (!func->getSubprogram())
-    return;
+    return UnknownLoc::get(context);
 
   // Add a fused location to link the subprogram information.
   StringAttr name = StringAttr::get(context, func->getSubprogram()->getName());
-  funcOp->setLoc(FusedLocWith<DISubprogramAttr>::get(
-      {NameLoc::get(name)}, translate(func->getSubprogram()), context));
+  return FusedLocWith<DISubprogramAttr>::get(
+      {NameLoc::get(name)}, translate(func->getSubprogram()), context);
 }
 
 //===----------------------------------------------------------------------===//
@@ -237,7 +237,7 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
 
 Location DebugImporter::translateLoc(llvm::DILocation *loc) {
   if (!loc)
-    return mlirModule.getLoc();
+    return UnknownLoc::get(context);
 
   // Get the file location of the instruction.
   Location result = FileLineColLoc::get(context, loc->getFilename(),

diff  --git a/mlir/lib/Target/LLVMIR/DebugImporter.h b/mlir/lib/Target/LLVMIR/DebugImporter.h
index 5028e22694a765..f75f09e3357d9e 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.h
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.h
@@ -35,8 +35,9 @@ class DebugImporter {
   /// Translates the given LLVM debug location to an MLIR location.
   Location translateLoc(llvm::DILocation *loc);
 
-  /// Translates the debug information for the given function.
-  void translate(llvm::Function *func, LLVMFuncOp funcOp);
+  /// Translates the debug information for the given function into a Location.
+  /// Returns UnknownLoc if `func` has no debug information attached to it.
+  Location translateFuncLocation(llvm::Function *func);
 
   /// Translates the given LLVM debug metadata to MLIR.
   DINodeAttr translate(llvm::DINode *node);

diff  --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index f23dad679e0ae8..e877ff29bfd400 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -594,13 +594,13 @@ LogicalResult ModuleImport::convertGlobals() {
     if (globalVar.getName() == getGlobalCtorsVarName() ||
         globalVar.getName() == getGlobalDtorsVarName()) {
       if (failed(convertGlobalCtorsAndDtors(&globalVar))) {
-        return emitError(mlirModule.getLoc())
+        return emitError(UnknownLoc::get(context))
                << "unhandled global variable: " << diag(globalVar);
       }
       continue;
     }
     if (failed(convertGlobal(&globalVar))) {
-      return emitError(mlirModule.getLoc())
+      return emitError(UnknownLoc::get(context))
              << "unhandled global variable: " << diag(globalVar);
     }
   }
@@ -1019,7 +1019,7 @@ ModuleImport::getConstantsToConvert(llvm::Constant *constant) {
 }
 
 FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
-  Location loc = mlirModule.getLoc();
+  Location loc = UnknownLoc::get(context);
 
   // Convert constants that can be represented as attributes.
   if (Attribute attr = getConstantAsAttr(constant)) {
@@ -1188,7 +1188,7 @@ FailureOr<Value> ModuleImport::convertValue(llvm::Value *value) {
   if (auto *constant = dyn_cast<llvm::Constant>(value))
     return convertConstantExpr(constant);
 
-  Location loc = mlirModule.getLoc();
+  Location loc = UnknownLoc::get(context);
   if (auto *inst = dyn_cast<llvm::Instruction>(value))
     loc = translateLoc(inst->getDebugLoc());
   return emitError(loc) << "unhandled value: " << diag(*value);
@@ -1721,13 +1721,11 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
   OpBuilder::InsertionGuard guard(builder);
   builder.setInsertionPoint(mlirModule.getBody(), mlirModule.getBody()->end());
 
+  Location loc = debugImporter->translateFuncLocation(func);
   LLVMFuncOp funcOp = builder.create<LLVMFuncOp>(
-      mlirModule.getLoc(), func->getName(), functionType,
+      loc, func->getName(), functionType,
       convertLinkageFromLLVM(func->getLinkage()), dsoLocal, cconv);
 
-  // Set the function debug information if available.
-  debugImporter->translate(func, funcOp);
-
   convertParameterAttributes(func, funcOp, builder);
 
   if (FlatSymbolRefAttr personality = getPersonalityAsAttr(func))

diff  --git a/mlir/test/Target/LLVMIR/Import/basic.ll b/mlir/test/Target/LLVMIR/Import/basic.ll
index 19e4543f8010da..a059425d978067 100644
--- a/mlir/test/Target/LLVMIR/Import/basic.ll
+++ b/mlir/test/Target/LLVMIR/Import/basic.ll
@@ -1,7 +1,7 @@
 ; RUN: mlir-translate -import-llvm %s | FileCheck %s
 ; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s --check-prefix=CHECK-DBG
 
-; CHECK-DBG: #[[MODULELOC:.+]] = loc({{.*}}basic.ll{{.*}}:0:0)
+; CHECK-DBG: #[[UNKNOWN_LOC:.+]] = loc(unknown)
 
 @global = external global double, align 8
 
@@ -9,7 +9,7 @@
 declare float @fe(i32)
 
 ; CHECK-LABEL: llvm.func internal @f1(%arg0: i64) -> i32 attributes {dso_local, passthrough = ["norecurse"]} {
-; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc({{.*}}basic.ll{{.*}}:0:0)) -> i32 attributes {dso_local, passthrough = ["norecurse"]} {
+; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc(unknown)) -> i32 attributes {dso_local, passthrough = ["norecurse"]} {
 ; CHECK: %[[c2:[0-9]+]] = llvm.mlir.constant(2 : i32) : i32
 ; CHECK: %[[c1:[0-9]+]] = llvm.mlir.constant(true) : i1
 ; CHECK: %[[c43:[0-9]+]] = llvm.mlir.constant(43 : i32) : i32
@@ -18,7 +18,7 @@ define internal dso_local i32 @f1(i64 %a) norecurse {
 entry:
 ; CHECK: %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr
   %aa = inttoptr i64 %a to ptr
-; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr loc(#[[MODULELOC]])
+; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr loc(#[[UNKNOWN_LOC]])
 ; %[[addrof:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr
 ; %[[addrof2:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr
 ; %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr
@@ -27,13 +27,12 @@ entry:
   %bb = ptrtoint ptr @global to i64
   %cc = getelementptr double, ptr @global, i32 3
 ; CHECK: %[[b:[0-9]+]] = llvm.trunc %arg0 : i64 to i32
-; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[MODULELOC]])
+; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[UNKNOWN_LOC]])
   %b = trunc i64 %a to i32
 ; CHECK: %[[c:[0-9]+]] = llvm.call @fe(%[[b]]) : (i32) -> f32
   %c = call float @fe(i32 %b)
 ; CHECK: %[[d:[0-9]+]] = llvm.fptosi %[[c]] : f32 to i32
   %d = fptosi float %c to i32
-; FIXME: icmp should return i1.
 ; CHECK: %[[e:[0-9]+]] = llvm.icmp "ne" %[[d]], %[[c2]] : i32
   %e = icmp ne i32 %d, 2
 ; CHECK: llvm.cond_br %[[e]], ^bb1, ^bb2
@@ -51,7 +50,7 @@ if.end:
 ; CHECK: llvm.return %[[c43]]
   ret i32 43
 }
-; CHECK-DBG: } loc(#[[MODULELOC]])
+; CHECK-DBG: } loc(#[[UNKNOWN_LOC]])
 
 ; CHECK-LABEL: @hasGCFunction
 ; CHECK-SAME: garbageCollector = "statepoint-example"
@@ -59,7 +58,7 @@ define void @hasGCFunction() gc "statepoint-example" {
     ret void
 }
 
-;CHECK-LABEL: @useFreezeOp
+; CHECK-LABEL: @useFreezeOp
 define i32 @useFreezeOp(i32 %x) {
   ;CHECK: %{{[0-9]+}} = llvm.freeze %{{[0-9a-z]+}} : i32
   %1 = freeze i32 %x

diff  --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll
index d39873b6d0d526..4f20fa92f4aef9 100644
--- a/mlir/test/Target/LLVMIR/Import/debug-info.ll
+++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll
@@ -1,17 +1,17 @@
 ; RUN: mlir-translate -import-llvm -mlir-print-debuginfo -split-input-file %s | FileCheck %s
 
-; CHECK: #[[$MODULELOC:.+]] = loc({{.*}}debug-info.ll{{.*}}:0:0)
+; CHECK: #[[$UNKNOWN_LOC:.+]] = loc(unknown)
 
 ; CHECK-LABEL: @module_loc(
 define i32 @module_loc(i32 %0) {
 entry:
   br label %next
 end:
-  ; CHECK: ^{{.*}}(%{{.+}}: i32 loc({{.*}}debug-info.ll{{.*}}:0:0)):
+  ; CHECK: ^{{.*}}(%{{.+}}: i32 loc(unknown)):
   %1 = phi i32 [ %2, %next ]
   ret i32 %1
 next:
-  ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$MODULELOC]])
+  ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$UNKNOWN_LOC]])
   %2 = mul i32 %0, %0
   br label %end
 }

diff  --git a/mlir/test/Target/LLVMIR/Import/import-failure.ll b/mlir/test/Target/LLVMIR/Import/import-failure.ll
index 51a4fe0cb93538..dbb1d10599da45 100644
--- a/mlir/test/Target/LLVMIR/Import/import-failure.ll
+++ b/mlir/test/Target/LLVMIR/Import/import-failure.ll
@@ -1,6 +1,6 @@
 ; RUN: not mlir-translate -import-llvm -emit-expensive-warnings -split-input-file %s 2>&1 | FileCheck %s
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled instruction: indirectbr ptr %dst, [label %bb1, label %bb2]
 define i32 @unhandled_instruction(ptr %dst) {
   indirectbr ptr %dst, [label %bb1, label %bb2]
@@ -12,7 +12,7 @@ bb2:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled value: ptr asm "bswap $0", "=r,r"
 define i32 @unhandled_value(i32 %arg1) {
   %1 = call i32 asm "bswap $0", "=r,r"(i32 %arg1)
@@ -21,9 +21,9 @@ define i32 @unhandled_value(i32 %arg1) {
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: unhandled constant: ptr blockaddress(@unhandled_constant, %bb1) since blockaddress(...) is unsupported
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled instruction: ret ptr blockaddress(@unhandled_constant, %bb1)
 define ptr @unhandled_constant() {
   br label %bb1
@@ -33,9 +33,9 @@ bb1:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: unhandled constant: ptr blockaddress(@unhandled_global, %bb1) since blockaddress(...) is unsupported
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled global variable: @private = private global ptr blockaddress(@unhandled_global, %bb1)
 @private = private global ptr blockaddress(@unhandled_global, %bb1)
 
@@ -49,7 +49,7 @@ bb1:
 
 declare void @llvm.gcroot(ptr %arg1, ptr %arg2)
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled intrinsic: call void @llvm.gcroot(ptr %arg1, ptr null)
 define void @unhandled_intrinsic() gc "example" {
   %arg1 = alloca ptr
@@ -83,7 +83,7 @@ define void @dropped_instruction(i64 %arg1) {
 ; // -----
 
 ; global_dtors with non-null data fields cannot be represented in MLIR.
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: error: unhandled global variable: @llvm.global_dtors
 @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @foo, ptr @foo }]
 
@@ -132,9 +132,9 @@ define void @access_group(ptr %arg1) {
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected all loop properties to be either debug locations or metadata nodes
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, i32 42}
 define void @invalid_loop_node(i64 %n, ptr %A) {
 entry:
@@ -147,9 +147,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: cannot import empty loop property
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @invalid_loop_node(i64 %n, ptr %A) {
 entry:
@@ -163,9 +163,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: cannot import loop property without a name
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @invalid_loop_node(i64 %n, ptr %A) {
 entry:
@@ -179,9 +179,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: cannot import loop properties with duplicated names llvm.loop.disable_nonforced
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -195,9 +195,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata node llvm.loop.disable_nonforced to hold no value
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -211,9 +211,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata nodes llvm.loop.unroll.enable and llvm.loop.unroll.disable to be mutually exclusive
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !2}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -228,9 +228,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.enable to hold a boolean value
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -244,9 +244,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.width to hold an i32 value
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -260,9 +260,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.followup_all to hold an MDNode
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -276,9 +276,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected metadata node llvm.loop.parallel_accesses to hold one or multiple MDNodes
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -292,9 +292,9 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unknown loop annotation llvm.loop.typo
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !2}
 define void @unsupported_loop_annotation(i64 %n, ptr %A) {
 entry:
@@ -309,7 +309,7 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: could not lookup access group
 define void @unused_access_group(ptr %arg) {
 entry:
@@ -326,7 +326,7 @@ end:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: expected function_entry_count to be attached to a function
 ; CHECK:      warning: unhandled metadata: !0 = !{!"function_entry_count", i64 42}
 define void @cond_br(i1 %arg) {
@@ -342,7 +342,7 @@ bb2:
 
 ; // -----
 
-; CHECK:      import-failure.ll
+; CHECK:      <unknown>
 ; CHECK-SAME: warning: dropped instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0)
 define void @unused_scope() {
   call void @llvm.experimental.noalias.scope.decl(metadata !0)


        


More information about the Mlir-commits mailing list