[Mlir-commits] [mlir] 0ab6b20 - [MLIR] Add DIExpression to LLVM dialect (#72462)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Nov 16 11:32:06 PST 2023
Author: Billy Zhu
Date: 2023-11-16T11:32:02-08:00
New Revision: 0ab6b20c36ab90395a60b6ff109207c1eb5436f1
URL: https://github.com/llvm/llvm-project/commit/0ab6b20c36ab90395a60b6ff109207c1eb5436f1
DIFF: https://github.com/llvm/llvm-project/commit/0ab6b20c36ab90395a60b6ff109207c1eb5436f1.diff
LOG: [MLIR] Add DIExpression to LLVM dialect (#72462)
Add initial support for DIExpression in LLVM dialect.
Similar to LLVM IR, DI Expression is encoded as a list of uint64. The
difference is that LLVM IR has helpers for understanding the expression
(e.g. for verification and pretty printing), whereas the current support
added by this PR treats the expression elements as opaque.
Added:
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
mlir/lib/Target/LLVMIR/ModuleImport.cpp
mlir/test/Dialect/LLVMIR/debuginfo.mlir
mlir/test/Target/LLVMIR/Import/debug-info.ll
mlir/test/Target/LLVMIR/Import/import-failure.ll
mlir/test/Target/LLVMIR/llvmir-debug.mlir
mlir/test/mlir-translate/import-diagnostics.ll
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 0d9af88157a8ae1..9e35bf1ba977725 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -261,6 +261,17 @@ def LLVM_DITagParameter : LLVM_DIParameter<
"tag", /*default=*/"", "Tag"
>;
+//===----------------------------------------------------------------------===//
+// DIExpressionAttr
+//===----------------------------------------------------------------------===//
+
+// TODO: Implement custom printer/parser for elements so that operators are
+// dumped in textual form.
+def LLVM_DIExpressionAttr : ArrayOfAttr<LLVM_Dialect, "DIExpression",
+ "di_expr", "uint64_t"> {
+ let assemblyFormat = "`<` `[` (`]` `>`) : ($value^ `]` `>`)?";
+}
+
//===----------------------------------------------------------------------===//
// DINullTypeAttr
//===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
index 1123466b7a75e3f..1d6936ed6c2bf0f 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
@@ -538,7 +538,7 @@ class LLVM_DbgIntrOp<string name, string argName, list<Trait> traits = []>
llvm::MetadataAsValue::get(ctx,
llvm::ValueAsMetadata::get(moduleTranslation.lookupValue(opInst.getOperand(0)))),
llvm::MetadataAsValue::get(ctx, moduleTranslation.translateDebugInfo($varInfo)),
- llvm::MetadataAsValue::get(ctx, llvm::DIExpression::get(ctx, std::nullopt)),
+ llvm::MetadataAsValue::get(ctx, llvm::DIExpression::get(ctx, $locationExpr)),
});
}];
let mlirBuilder = [{
@@ -548,7 +548,7 @@ class LLVM_DbgIntrOp<string name, string argName, list<Trait> traits = []>
return success();
}];
let assemblyFormat = [{
- qualified($varInfo) `=` $}] # argName #
+ qualified($varInfo) (qualified($locationExpr)^)? `=` $}] # argName #
[{ `:` qualified(type($}] # argName # [{)) attr-dict
}];
}
@@ -556,13 +556,21 @@ class LLVM_DbgIntrOp<string name, string argName, list<Trait> traits = []>
def LLVM_DbgDeclareOp : LLVM_DbgIntrOp<"dbg.declare", "addr",
[DeclareOpInterfaceMethods<PromotableOpInterface>]> {
let summary = "Describes how the address relates to a source language variable.";
- let arguments = (ins LLVM_AnyPointer:$addr, LLVM_DILocalVariableAttr:$varInfo);
+ let arguments = (ins
+ LLVM_AnyPointer:$addr,
+ LLVM_DILocalVariableAttr:$varInfo,
+ DefaultValuedAttr<LLVM_DIExpressionAttr, "std::nullopt">:$locationExpr
+ );
}
def LLVM_DbgValueOp : LLVM_DbgIntrOp<"dbg.value", "value",
[DeclareOpInterfaceMethods<PromotableOpInterface>]> {
let summary = "Describes how the value relates to a source language variable.";
- let arguments = (ins LLVM_Type:$value, LLVM_DILocalVariableAttr:$varInfo);
+ let arguments = (ins
+ LLVM_Type:$value,
+ LLVM_DILocalVariableAttr:$varInfo,
+ DefaultValuedAttr<LLVM_DIExpressionAttr, "std::nullopt">:$locationExpr
+ );
}
def LLVM_DbgLabelOp : LLVM_IntrOp<"dbg.label", [], [], [], 0> {
@@ -660,8 +668,8 @@ class LLVM_VecReductionI<string mnem>
// LLVM vector reduction over a single vector, with an initial value,
// and with permission to reassociate the reduction operations.
class LLVM_VecReductionAccBase<string mnem, Type element>
- : LLVM_OneResultIntrOp</*mnem=*/"vector.reduce." # mnem,
- /*overloadedResults=*/[],
+ : LLVM_OneResultIntrOp</*mnem=*/"vector.reduce." # mnem,
+ /*overloadedResults=*/[],
/*overloadedOperands=*/[1],
/*traits=*/[Pure, SameOperandsAndResultElementType],
/*equiresFastmath=*/1>,
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
index cc2a917e58f6394..43349a26ad11276 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
@@ -46,7 +46,8 @@ void LLVM::AllocaOp::handleBlockArgument(const MemorySlot &slot,
for (Operation *user : getOperation()->getUsers())
if (auto declareOp = llvm::dyn_cast<LLVM::DbgDeclareOp>(user))
rewriter.create<LLVM::DbgValueOp>(declareOp.getLoc(), argument,
- declareOp.getVarInfo());
+ declareOp.getVarInfo(),
+ declareOp.getLocationExpr());
}
void LLVM::AllocaOp::handlePromotionComplete(const MemorySlot &slot,
@@ -174,7 +175,8 @@ DeletionKind LLVM::StoreOp::removeBlockingUses(
for (Operation *user : slot.ptr.getUsers())
if (auto declareOp = dyn_cast<LLVM::DbgDeclareOp>(user))
rewriter.create<LLVM::DbgValueOp>(declareOp->getLoc(), getValue(),
- declareOp.getVarInfo());
+ declareOp.getVarInfo(),
+ declareOp.getLocationExpr());
return DeletionKind::Delete;
}
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 75e806650f3117e..d9e039e75e9ef24 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1819,9 +1819,9 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
emitWarning(loc) << "dropped intrinsic: " << diag(*dbgIntr);
return success();
};
- // Drop debug intrinsics with a non-empty debug expression.
- // TODO: Support debug intrinsics that evaluate a debug expression.
- if (dbgIntr->hasArgList() || dbgIntr->getExpression()->getNumElements() != 0)
+ // Drop debug intrinsics with arg lists.
+ // TODO: Support debug intrinsics that have arg lists.
+ if (dbgIntr->hasArgList())
return emitUnsupportedWarning();
// Kill locations can have metadata nodes as location operand. This
// cannot be converted to poison as the type cannot be reconstructed.
@@ -1855,15 +1855,17 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
}
DILocalVariableAttr localVariableAttr =
matchLocalVariableAttr(dbgIntr->getArgOperand(1));
+ auto locationExprAttr =
+ DIExpressionAttr::get(context, dbgIntr->getExpression()->getElements());
Operation *op =
llvm::TypeSwitch<llvm::DbgVariableIntrinsic *, Operation *>(dbgIntr)
.Case([&](llvm::DbgDeclareInst *) {
- return builder.create<LLVM::DbgDeclareOp>(loc, *argOperand,
- localVariableAttr);
+ return builder.create<LLVM::DbgDeclareOp>(
+ loc, *argOperand, localVariableAttr, locationExprAttr);
})
.Case([&](llvm::DbgValueInst *) {
- return builder.create<LLVM::DbgValueOp>(loc, *argOperand,
- localVariableAttr);
+ return builder.create<LLVM::DbgValueOp>(
+ loc, *argOperand, localVariableAttr, locationExprAttr);
});
mapNoResultOp(dbgIntr, op);
setNonDebugMetadataAttrs(dbgIntr, op);
diff --git a/mlir/test/Dialect/LLVMIR/debuginfo.mlir b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
index 5f7b1acff6bbe92..cdfcb7f9e32e5c4 100644
--- a/mlir/test/Dialect/LLVMIR/debuginfo.mlir
+++ b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
@@ -162,8 +162,8 @@ llvm.func @addr(%arg: i64) {
// CHECK: llvm.func @value(%[[ARG1:.*]]: i32, %[[ARG2:.*]]: i32)
llvm.func @value(%arg1: i32, %arg2: i32) {
- // CHECK: llvm.intr.dbg.value #[[VAR1]] = %[[ARG1]]
- llvm.intr.dbg.value #var1 = %arg1 : i32
+ // CHECK: llvm.intr.dbg.value #[[VAR1]] #llvm.di_expr<[2, 4096, 0, 4]> = %[[ARG1]]
+ llvm.intr.dbg.value #var1 #llvm.di_expr<[2, 4096, 0, 4]> = %arg1 : i32
// CHECK: llvm.intr.dbg.value #[[VAR2]] = %[[ARG2]]
llvm.intr.dbg.value #var2 = %arg2 : i32
// CHECK: llvm.intr.dbg.label #[[LABEL1]]
diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll
index 08e60deb699d8d1..51445f25dc400f0 100644
--- a/mlir/test/Target/LLVMIR/Import/debug-info.ll
+++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll
@@ -257,8 +257,8 @@ source_filename = "debug-info.ll"
; CHECK-SAME: %[[ARG1:[a-zA-Z0-9]+]]
define void @intrinsic(i64 %0, ptr %1) {
; CHECK: llvm.intr.dbg.declare #[[$VAR1]] = %[[ARG1]] : !llvm.ptr loc(#[[LOC1:.+]])
- ; CHECK: llvm.intr.dbg.value #[[$VAR0]] = %[[ARG0]] : i64 loc(#[[LOC0:.+]])
- call void @llvm.dbg.value(metadata i64 %0, metadata !5, metadata !DIExpression()), !dbg !7
+ ; CHECK: llvm.intr.dbg.value #[[$VAR0]] #llvm.di_expr<[4096, 0, 8]> = %[[ARG0]] : i64 loc(#[[LOC0:.+]])
+ call void @llvm.dbg.value(metadata i64 %0, metadata !5, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8)), !dbg !7
call void @llvm.dbg.declare(metadata ptr %1, metadata !6, metadata !DIExpression()), !dbg !9
; CHECK: llvm.intr.dbg.label #[[$LABEL]] loc(#[[LOC1:.+]])
call void @llvm.dbg.label(metadata !10), !dbg !9
diff --git a/mlir/test/Target/LLVMIR/Import/import-failure.ll b/mlir/test/Target/LLVMIR/Import/import-failure.ll
index 19837072f676376..09621346656636f 100644
--- a/mlir/test/Target/LLVMIR/Import/import-failure.ll
+++ b/mlir/test/Target/LLVMIR/Import/import-failure.ll
@@ -61,14 +61,11 @@ define void @unhandled_intrinsic() gc "example" {
declare void @llvm.dbg.value(metadata, metadata, metadata)
-; CHECK: import-failure.ll
-; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata i64 %arg1, metadata !3, metadata !DIExpression(DW_OP_plus_uconst, 42, DW_OP_stack_value)), !dbg !5
; CHECK: import-failure.ll
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !DIArgList(i64 %arg1, i64 undef), metadata !3, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 1, DW_OP_mul, DW_OP_plus, DW_OP_stack_value)), !dbg !5
; CHECK: import-failure.ll
; CHECK-SAME: warning: dropped intrinsic: call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
define void @dropped_instruction(i64 %arg1) {
- call void @llvm.dbg.value(metadata i64 %arg1, metadata !3, metadata !DIExpression(DW_OP_plus_uconst, 42, DW_OP_stack_value)), !dbg !5
call void @llvm.dbg.value(metadata !DIArgList(i64 %arg1, i64 undef), metadata !3, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 1, DW_OP_mul, DW_OP_plus, DW_OP_stack_value)), !dbg !5
call void @llvm.dbg.value(metadata !6, metadata !3, metadata !DIExpression()), !dbg !5
ret void
diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
index ea962c66cb8eff9..e088374ec0eb8e1 100644
--- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
@@ -74,7 +74,7 @@ llvm.func @func_no_debug() {
#variableAddr = #llvm.di_local_variable<scope = #blockScope, name = "alloc">
#noNameVariable = #llvm.di_local_variable<scope = #blockScope>
#module = #llvm.di_module<
- file = #file, scope = #file, name = "module",
+ file = #file, scope = #file, name = "module",
configMacros = "bar", includePath = "/",
apinotes = "/", line = 42, isDecl = true
>
@@ -91,12 +91,12 @@ llvm.func @func_with_debug(%arg: i64) {
%allocCount = llvm.mlir.constant(1 : i32) : i32
%alloc = llvm.alloca %allocCount x i64 : (i32) -> !llvm.ptr
- // CHECK: call void @llvm.dbg.value(metadata i64 %[[ARG]], metadata ![[VAR_LOC:[0-9]+]], metadata !DIExpression())
- // CHECK: call void @llvm.dbg.declare(metadata ptr %[[ALLOC]], metadata ![[ADDR_LOC:[0-9]+]], metadata !DIExpression())
+ // CHECK: call void @llvm.dbg.value(metadata i64 %[[ARG]], metadata ![[VAR_LOC:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_fragment, 8, 8))
+ // CHECK: call void @llvm.dbg.declare(metadata ptr %[[ALLOC]], metadata ![[ADDR_LOC:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8))
// CHECK: call void @llvm.dbg.value(metadata i64 %[[ARG]], metadata ![[NO_NAME_VAR:[0-9]+]], metadata !DIExpression())
- llvm.intr.dbg.value #variable = %arg : i64
- llvm.intr.dbg.declare #variableAddr = %alloc : !llvm.ptr
- llvm.intr.dbg.value #noNameVariable= %arg : i64
+ llvm.intr.dbg.value #variable #llvm.di_expr<[4096, 8, 8]> = %arg : i64
+ llvm.intr.dbg.declare #variableAddr #llvm.di_expr<[4096, 0, 8]> = %alloc : !llvm.ptr
+ llvm.intr.dbg.value #noNameVariable = %arg : i64
// CHECK: call void @func_no_debug(), !dbg ![[CALLSITE_LOC:[0-9]+]]
llvm.call @func_no_debug() : () -> () loc(callsite("mysource.cc":3:4 at "mysource.cc":5:6))
diff --git a/mlir/test/mlir-translate/import-diagnostics.ll b/mlir/test/mlir-translate/import-diagnostics.ll
index dd72f07745b4ba9..acf45eb1c7c5dd5 100644
--- a/mlir/test/mlir-translate/import-diagnostics.ll
+++ b/mlir/test/mlir-translate/import-diagnostics.ll
@@ -37,7 +37,7 @@ declare void @llvm.dbg.value(metadata, metadata, metadata)
; DEFAULT-NOT: warning:
; EXPENSIVE: warning:
define void @dropped_instruction(i64 %arg1) {
- call void @llvm.dbg.value(metadata i64 %arg1, metadata !3, metadata !DIExpression(DW_OP_plus_uconst, 42, DW_OP_stack_value)), !dbg !5
+ call void @llvm.dbg.value(metadata !DIArgList(i64 %arg1, i64 undef), metadata !3, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value)), !dbg !5
ret void
}
More information about the Mlir-commits
mailing list