Skip to content

Commit 860a3b5

Browse files
committed
Address feedback
1 parent 1c7df13 commit 860a3b5

File tree

3 files changed

+83
-101
lines changed

3 files changed

+83
-101
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class ModuleImport {
283283
void addDebugIntrinsic(llvm::CallInst *intrinsic);
284284

285285
/// Similar to `addDebugIntrinsic`, but for debug records.
286-
void addDebugRecord(llvm::DbgRecord *dr);
286+
void addDebugRecord(llvm::DbgRecord *debugRecord);
287287

288288
/// Converts the LLVM values for an intrinsic to mixed MLIR values and
289289
/// attributes for LLVM_IntrOpBase. Attributes correspond to LLVM immargs. The
@@ -350,15 +350,15 @@ class ModuleImport {
350350
LogicalResult processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
351351
DominanceInfo &domInfo);
352352
/// Converts a single debug record.
353-
LogicalResult processDebugRecord(llvm::DbgRecord &dr, DominanceInfo &domInfo);
353+
LogicalResult processDebugRecord(llvm::DbgRecord &debugRecord,
354+
DominanceInfo &domInfo);
354355
/// Process arguments for declare/value operation insertion. `localVarAttr`
355356
/// and `localExprAttr` are the attained attributes after importing the debug
356357
/// variable and expressions. This also sets the builder insertion point to be
357358
/// used by these operations.
358-
void processDebugOpArgumentsAndInsertionPt(
359-
Location loc, DILocalVariableAttr &localVarAttr,
360-
DIExpressionAttr &localExprAttr, Value &locVal, bool hasArgList,
361-
bool isKillLocation,
359+
std::tuple<DILocalVariableAttr, DIExpressionAttr, Value>
360+
processDebugOpArgumentsAndInsertionPt(
361+
Location loc, bool hasArgList, bool isKillLocation,
362362
llvm::function_ref<FailureOr<Value>()> convertArgOperandToValue,
363363
llvm::Value *address,
364364
llvm::PointerUnion<llvm::Value *, llvm::DILocalVariable *> variable,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ void ModuleImport::addDebugIntrinsic(llvm::CallInst *intrinsic) {
524524
debugIntrinsics.insert(intrinsic);
525525
}
526526

527-
void ModuleImport::addDebugRecord(llvm::DbgRecord *dr) {
528-
if (!debugRecords.contains(dr))
529-
debugRecords.insert(dr);
527+
void ModuleImport::addDebugRecord(llvm::DbgRecord *debugRecord) {
528+
if (!debugRecords.contains(debugRecord))
529+
debugRecords.insert(debugRecord);
530530
}
531531

532532
static Attribute convertCGProfileModuleFlagValue(ModuleOp mlirModule,
@@ -2558,10 +2558,9 @@ LogicalResult ModuleImport::processInstruction(llvm::Instruction *inst) {
25582558
return convertIntrinsic(intrinsic);
25592559

25602560
// Capture instruction with attached debug markers for later processing.
2561-
if (inst->DebugMarker) {
2562-
for (llvm::DbgRecord &dr : inst->DebugMarker->getDbgRecordRange())
2563-
addDebugRecord(&dr);
2564-
}
2561+
if (inst->DebugMarker)
2562+
for (llvm::DbgRecord &debugRecord : inst->DebugMarker->getDbgRecordRange())
2563+
addDebugRecord(&debugRecord);
25652564

25662565
// Convert all remaining LLVM instructions to MLIR operations.
25672566
return convertInstruction(inst);
@@ -3026,7 +3025,7 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
30263025
if (failed(processDebugIntrinsics()))
30273026
return failure();
30283027

3029-
// Process the debug r that require a delayed conversion after
3028+
// Process the debug records that require a delayed conversion after
30303029
// everything else was converted.
30313030
if (failed(processDebugRecords()))
30323031
return failure();
@@ -3044,8 +3043,8 @@ static bool isMetadataKillLocation(bool isKillLocation, llvm::Value *value) {
30443043
return !isa<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
30453044
}
30463045

3047-
/// Ensure that the debug intrinsic is inserted right after its operand is
3048-
/// defined. Otherwise, the operand might not necessarily dominate the
3046+
/// Ensure that the debug intrinsic is inserted right after the operand
3047+
/// definition. Otherwise, the operand might not necessarily dominate the
30493048
/// intrinsic. If the defining operation is a terminator, insert the intrinsic
30503049
/// into a dominated block.
30513050
static LogicalResult setDebugIntrinsicBuilderInsertionPoint(
@@ -3080,41 +3079,40 @@ static LogicalResult setDebugIntrinsicBuilderInsertionPoint(
30803079
return success();
30813080
}
30823081

3083-
void ModuleImport::processDebugOpArgumentsAndInsertionPt(
3084-
Location loc, DILocalVariableAttr &localVarAttr,
3085-
DIExpressionAttr &localExprAttr, Value &locVal, bool hasArgList,
3086-
bool isKillLocation,
3082+
std::tuple<DILocalVariableAttr, DIExpressionAttr, Value>
3083+
ModuleImport::processDebugOpArgumentsAndInsertionPt(
3084+
Location loc, bool hasArgList, bool isKillLocation,
30873085
llvm::function_ref<FailureOr<Value>()> convertArgOperandToValue,
30883086
llvm::Value *address,
30893087
llvm::PointerUnion<llvm::Value *, llvm::DILocalVariable *> variable,
30903088
llvm::DIExpression *expression, DominanceInfo &domInfo) {
30913089
// Drop debug intrinsics with arg lists.
30923090
// TODO: Support debug intrinsics that have arg lists.
30933091
if (hasArgList)
3094-
return;
3092+
return {};
30953093
// Kill locations can have metadata nodes as location operand. This
30963094
// cannot be converted to poison as the type cannot be reconstructed.
30973095
// TODO: find a way to support this case.
30983096
if (isMetadataKillLocation(isKillLocation, address))
3099-
return;
3097+
return {};
31003098
// Drop debug intrinsics if the associated variable information cannot be
31013099
// translated due to cyclic debug metadata.
31023100
// TODO: Support cyclic debug metadata.
3103-
localVarAttr = matchLocalVariableAttr(variable);
3101+
DILocalVariableAttr localVarAttr = matchLocalVariableAttr(variable);
31043102
if (!localVarAttr)
3105-
return;
3103+
return {};
31063104
FailureOr<Value> argOperand = convertArgOperandToValue();
31073105
if (failed(argOperand)) {
31083106
emitError(loc) << "failed to convert a debug operand: " << diag(*address);
3109-
return;
3107+
return {};
31103108
}
31113109

31123110
if (setDebugIntrinsicBuilderInsertionPoint(builder, domInfo, *argOperand)
31133111
.failed())
3114-
return;
3112+
return {};
31153113

3116-
localExprAttr = debugImporter->translateExpression(expression);
3117-
locVal = *argOperand;
3114+
return {localVarAttr, debugImporter->translateExpression(expression),
3115+
*argOperand};
31183116
}
31193117

31203118
LogicalResult
@@ -3127,99 +3125,92 @@ ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
31273125
return success();
31283126
};
31293127

3130-
DILocalVariableAttr localVariableAttr;
3131-
DIExpressionAttr locationExprAttr;
3132-
Value locVal;
31333128
OpBuilder::InsertionGuard guard(builder);
31343129
auto convertArgOperandToValue = [&]() {
31353130
return convertMetadataValue(dbgIntr->getArgOperand(0));
31363131
};
31373132

3138-
processDebugOpArgumentsAndInsertionPt(
3139-
loc, localVariableAttr, locationExprAttr, locVal, dbgIntr->hasArgList(),
3140-
dbgIntr->isKillLocation(), convertArgOperandToValue,
3141-
dbgIntr->getArgOperand(0), dbgIntr->getArgOperand(1),
3142-
dbgIntr->getExpression(), domInfo);
3133+
auto [localVariableAttr, locationExprAttr, locVal] =
3134+
processDebugOpArgumentsAndInsertionPt(
3135+
loc, dbgIntr->hasArgList(), dbgIntr->isKillLocation(),
3136+
convertArgOperandToValue, dbgIntr->getArgOperand(0),
3137+
dbgIntr->getArgOperand(1), dbgIntr->getExpression(), domInfo);
31433138

31443139
if (!localVariableAttr)
31453140
return emitUnsupportedWarning();
31463141

31473142
if (!locVal) // Expected if localVariableAttr is present.
31483143
return failure();
31493144

3150-
Operation *op =
3151-
llvm::TypeSwitch<llvm::DbgVariableIntrinsic *, Operation *>(dbgIntr)
3152-
.Case([&](llvm::DbgDeclareInst *) {
3153-
return LLVM::DbgDeclareOp::create(
3154-
builder, loc, locVal, localVariableAttr, locationExprAttr);
3155-
})
3156-
.Case([&](llvm::DbgValueInst *) {
3157-
return LLVM::DbgValueOp::create(
3158-
builder, loc, locVal, localVariableAttr, locationExprAttr);
3159-
});
3145+
Operation *op = nullptr;
3146+
if (isa<llvm::DbgDeclareInst>(dbgIntr))
3147+
op = LLVM::DbgDeclareOp::create(builder, loc, locVal, localVariableAttr,
3148+
locationExprAttr);
3149+
else if (isa<llvm::DbgValueInst>(dbgIntr))
3150+
op = LLVM::DbgValueOp::create(builder, loc, locVal, localVariableAttr,
3151+
locationExprAttr);
3152+
else
3153+
return emitUnsupportedWarning();
3154+
31603155
mapNoResultOp(dbgIntr, op);
31613156
setNonDebugMetadataAttrs(dbgIntr, op);
31623157
return success();
31633158
}
31643159

3165-
LogicalResult ModuleImport::processDebugRecord(llvm::DbgRecord &dr,
3160+
LogicalResult ModuleImport::processDebugRecord(llvm::DbgRecord &debugRecord,
31663161
DominanceInfo &domInfo) {
3167-
Location loc = translateLoc(dr.getDebugLoc());
3162+
Location loc = translateLoc(debugRecord.getDebugLoc());
31683163
auto emitUnsupportedWarning = [&]() {
31693164
if (!emitExpensiveWarnings)
31703165
return success();
31713166
std::string options;
31723167
llvm::raw_string_ostream optionsStream(options);
3173-
dr.print(optionsStream);
3168+
debugRecord.print(optionsStream);
31743169
emitWarning(loc) << "unhandled debug record " << optionsStream.str();
31753170
return success();
31763171
};
31773172

31783173
OpBuilder::InsertionGuard guard(builder);
3179-
if (auto *dbgVar = dyn_cast<llvm::DbgVariableRecord>(&dr)) {
3180-
DILocalVariableAttr localVariableAttr;
3181-
DIExpressionAttr locationExprAttr;
3182-
Value locVal;
3183-
auto convertArgOperandToValue = [&]() -> FailureOr<Value> {
3184-
llvm::Value *value = dbgVar->getAddress();
3185-
3186-
// Return the mapped value if it has been converted before.
3187-
auto it = valueMapping.find(value);
3188-
if (it != valueMapping.end())
3189-
return it->getSecond();
3190-
3191-
// Convert constants such as immediate values that have no mapping yet.
3192-
if (auto *constant = dyn_cast<llvm::Constant>(value))
3193-
return convertConstantExpr(constant);
3194-
return failure();
3195-
};
3174+
auto *dbgVar = dyn_cast<llvm::DbgVariableRecord>(&debugRecord);
3175+
if (!dbgVar)
3176+
return emitUnsupportedWarning();
31963177

3197-
processDebugOpArgumentsAndInsertionPt(
3198-
loc, localVariableAttr, locationExprAttr, locVal, dbgVar->hasArgList(),
3199-
dbgVar->isKillLocation(), convertArgOperandToValue,
3200-
dbgVar->getAddress(), dbgVar->getVariable(), dbgVar->getExpression(),
3201-
domInfo);
3178+
auto convertArgOperandToValue = [&]() -> FailureOr<Value> {
3179+
llvm::Value *value = dbgVar->getAddress();
32023180

3203-
if (!localVariableAttr)
3204-
return emitUnsupportedWarning();
3181+
// Return the mapped value if it has been converted before.
3182+
auto it = valueMapping.find(value);
3183+
if (it != valueMapping.end())
3184+
return it->getSecond();
32053185

3206-
if (!locVal) // Expected if localVariableAttr is present.
3207-
return failure();
3186+
// Convert constants such as immediate values that have no mapping yet.
3187+
if (auto *constant = dyn_cast<llvm::Constant>(value))
3188+
return convertConstantExpr(constant);
3189+
return failure();
3190+
};
32083191

3209-
if (dbgVar->isDbgDeclare())
3210-
LLVM::DbgDeclareOp::create(builder, loc, locVal, localVariableAttr,
3211-
locationExprAttr);
3212-
else if (dbgVar->isDbgValue())
3213-
LLVM::DbgValueOp::create(builder, loc, locVal, localVariableAttr,
3214-
locationExprAttr);
3215-
else // isDbgAssign
3216-
return emitUnsupportedWarning();
3192+
auto [localVariableAttr, locationExprAttr, locVal] =
3193+
processDebugOpArgumentsAndInsertionPt(
3194+
loc, dbgVar->hasArgList(), dbgVar->isKillLocation(),
3195+
convertArgOperandToValue, dbgVar->getAddress(), dbgVar->getVariable(),
3196+
dbgVar->getExpression(), domInfo);
32173197

3218-
// FIXME: Nothing to map given the source is an LLVM attribute?
3219-
return success();
3220-
}
3198+
if (!localVariableAttr)
3199+
return emitUnsupportedWarning();
32213200

3222-
return emitUnsupportedWarning();
3201+
if (!locVal) // Expected if localVariableAttr is present.
3202+
return failure();
3203+
3204+
if (dbgVar->isDbgDeclare())
3205+
LLVM::DbgDeclareOp::create(builder, loc, locVal, localVariableAttr,
3206+
locationExprAttr);
3207+
else if (dbgVar->isDbgValue())
3208+
LLVM::DbgValueOp::create(builder, loc, locVal, localVariableAttr,
3209+
locationExprAttr);
3210+
else // isDbgAssign
3211+
return emitUnsupportedWarning();
3212+
3213+
return success();
32233214
}
32243215

32253216
LogicalResult ModuleImport::processDebugIntrinsics() {
@@ -3234,8 +3225,8 @@ LogicalResult ModuleImport::processDebugIntrinsics() {
32343225

32353226
LogicalResult ModuleImport::processDebugRecords() {
32363227
DominanceInfo domInfo;
3237-
for (llvm::DbgRecord *dr : debugRecords) {
3238-
if (failed(processDebugRecord(*dr, domInfo)))
3228+
for (llvm::DbgRecord *debugRecord : debugRecords) {
3229+
if (failed(processDebugRecord(*debugRecord, domInfo)))
32393230
return failure();
32403231
}
32413232
debugRecords.clear();

mlir/test/Target/LLVMIR/Import/debug-info-records.ll

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ define void @func_no_debug() {
1818
; CHECK: llvm.func @func_with_debug(%[[ARG0:.*]]: i64
1919
define void @func_with_debug(i64 %0) !dbg !3 {
2020

21-
; CHECK: llvm.intr.dbg.value #di_local_variable = %[[ARG0]] : i64
22-
; CHECK: llvm.intr.dbg.value #di_local_variable1 #llvm.di_expression<[DW_OP_LLVM_fragment(0, 1)]> = %[[ARG0]] : i64
21+
; CHECK: llvm.intr.dbg.value #di_local_variable{{.*}} = %[[ARG0]] : i64
22+
; CHECK: llvm.intr.dbg.value #di_local_variable{{.*}} #llvm.di_expression<[DW_OP_LLVM_fragment(0, 1)]> = %[[ARG0]] : i64
2323
; CHECK: %[[CST:.*]] = llvm.mlir.constant(1 : i32) : i32
2424
; CHECK: %[[ADDR:.*]] = llvm.alloca %[[CST]] x i64
25-
; CHECK: llvm.intr.dbg.declare #di_local_variable2 #llvm.di_expression<[DW_OP_deref, DW_OP_LLVM_convert(4, DW_ATE_signed)]> = %[[ADDR]] : !llvm.ptr
25+
; CHECK: llvm.intr.dbg.declare #di_local_variable{{.*}} #llvm.di_expression<[DW_OP_deref, DW_OP_LLVM_convert(4, DW_ATE_signed)]> = %[[ADDR]] : !llvm.ptr
2626
%2 = alloca i64, align 8, !dbg !19
2727
#dbg_value(i64 %0, !20, !DIExpression(DW_OP_LLVM_fragment, 0, 1), !22)
2828
#dbg_declare(ptr %2, !23, !DIExpression(DW_OP_deref, DW_OP_LLVM_convert, 4, DW_ATE_signed), !25)
2929
#dbg_value(i64 %0, !26, !DIExpression(), !27)
3030
call void @func_no_debug(), !dbg !28
31-
call void @func_no_debug(), !dbg !28
32-
call void @func_no_debug(), !dbg !29
33-
call void @func_no_debug(), !dbg !30
34-
call void @func_no_debug(), !dbg !30
35-
call void @func_no_debug(), !dbg !31
36-
call void @func_no_debug(), !dbg !32
3731
%3 = add i64 %0, %0, !dbg !32
3832
ret void, !dbg !37
3933
}
@@ -74,9 +68,6 @@ define void @empty_types() !dbg !38 {
7468
!26 = !DILocalVariable(scope: !24)
7569
!27 = !DILocation(line: 109, column: 3, scope: !3)
7670
!28 = !DILocation(line: 1, column: 2, scope: !3)
77-
!29 = !DILocation(line: 10, column: 10, scope: !3)
78-
!30 = !DILocation(line: 5, column: 6, scope: !3)
79-
!31 = !DILocation(line: 1, column: 1, scope: !3)
8071
!32 = !DILocation(line: 2, column: 4, scope: !33, inlinedAt: !36)
8172
!33 = distinct !DISubprogram(name: "callee", scope: !13, file: !1, type: !34, spFlags: DISPFlagDefinition, unit: !0)
8273
!34 = !DISubroutineType(types: !35)

0 commit comments

Comments
 (0)