Skip to content

Commit 70e1391

Browse files
abidhSterling-Augustine
authored andcommitted
[flang][debug] Allow variable length for dummy char arguments. (llvm#109448)
As pointed out by @jeanPerier [here](llvm#108283 (comment)), we don't need to restrict the length of the dummy character argument location to `fir.unboxchar`. This PR removes that restriction.
1 parent a6d5647 commit 70e1391

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,24 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertCharacterType(
295295
// variable that will contain that length. This variable is used as
296296
// 'stringLength' in DIStringTypeAttr.
297297
if (declOp && !declOp.getTypeparams().empty()) {
298-
mlir::Operation *op = declOp.getTypeparams()[0].getDefiningOp();
299-
if (auto unbox = mlir::dyn_cast_or_null<fir::UnboxCharOp>(op)) {
300-
auto name =
301-
mlir::StringAttr::get(context, "." + declOp.getUniqName().str());
302-
mlir::OpBuilder builder(context);
303-
builder.setInsertionPoint(declOp);
304-
mlir::Type i64Ty = builder.getIntegerType(64);
305-
auto convOp = builder.create<fir::ConvertOp>(unbox.getLoc(), i64Ty,
306-
unbox.getResult(1));
307-
mlir::LLVM::DITypeAttr Ty = convertType(i64Ty, fileAttr, scope, declOp);
308-
auto lvAttr = mlir::LLVM::DILocalVariableAttr::get(
309-
context, scope, name, fileAttr, /*line=*/0, /*argNo=*/0,
310-
/*alignInBits=*/0, Ty, mlir::LLVM::DIFlags::Artificial);
311-
builder.create<mlir::LLVM::DbgValueOp>(convOp.getLoc(), convOp, lvAttr,
312-
nullptr);
313-
varAttr = mlir::cast<mlir::LLVM::DIVariableAttr>(lvAttr);
298+
auto name =
299+
mlir::StringAttr::get(context, "." + declOp.getUniqName().str());
300+
mlir::OpBuilder builder(context);
301+
builder.setInsertionPoint(declOp);
302+
mlir::Value sizeVal = declOp.getTypeparams()[0];
303+
mlir::Type type = sizeVal.getType();
304+
if (!mlir::isa<mlir::IntegerType>(type) || !type.isSignlessInteger()) {
305+
type = builder.getIntegerType(64);
306+
sizeVal =
307+
builder.create<fir::ConvertOp>(declOp.getLoc(), type, sizeVal);
314308
}
309+
mlir::LLVM::DITypeAttr Ty = convertType(type, fileAttr, scope, declOp);
310+
auto lvAttr = mlir::LLVM::DILocalVariableAttr::get(
311+
context, scope, name, fileAttr, /*line=*/0, /*argNo=*/0,
312+
/*alignInBits=*/0, Ty, mlir::LLVM::DIFlags::Artificial);
313+
builder.create<mlir::LLVM::DbgValueOp>(declOp.getLoc(), sizeVal, lvAttr,
314+
nullptr);
315+
varAttr = mlir::cast<mlir::LLVM::DIVariableAttr>(lvAttr);
315316
}
316317
}
317318

flang/test/Transforms/debug-107988.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
1717
// CHECK: func.func @test
1818
// CHECK: %[[V1:.*]]:2 = fir.unboxchar{{.*}}
1919
// CHECK: %[[V2:.*]] = fir.convert %[[V1]]#1 : (index) -> i64
20-
// CHECK: llvm.intr.dbg.value #di_local_variable = %[[V2]] : i64
20+
// CHECK: llvm.intr.dbg.value #[[VAR]] = %[[V2]] : i64
2121
// CHECK: #[[STR_TY:.*]] = #llvm.di_string_type<tag = DW_TAG_string_type, name = "", stringLength = #[[VAR]], encoding = DW_ATE_ASCII>
2222
// CHECK: #llvm.di_local_variable<{{.*}}name = "str"{{.*}}type = #[[STR_TY]]>
2323

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s -o - | FileCheck %s
2+
3+
module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
4+
func.func @foo(%arg0: !fir.ref<!fir.char<1,?>> {fir.bindc_name = "str1"} , %arg1: !fir.ref<i64> {fir.bindc_name = "len1"} loc("/home/haqadeer/work/fortran/t1/../str.f90":1:1), %arg2: i64) {
5+
%0 = fir.emboxchar %arg0, %arg2 : (!fir.ref<!fir.char<1,?>>, i64) -> !fir.boxchar<1>
6+
%c4_i32 = arith.constant 4 : i32
7+
%c6_i32 = arith.constant 6 : i32
8+
%c0_i64 = arith.constant 0 : i64
9+
%1 = fir.undefined !fir.dscope
10+
%2 = fircg.ext_declare %arg1 dummy_scope %1 {uniq_name = "_QFfooElen1"} : (!fir.ref<i64>, !fir.dscope) -> !fir.ref<i64> loc(#loc1)
11+
%3:2 = fir.unboxchar %0 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
12+
%4 = fir.load %2 : !fir.ref<i64>
13+
%5 = arith.cmpi sgt, %4, %c0_i64 : i64
14+
%6 = arith.select %5, %4, %c0_i64 : i64
15+
%7 = fircg.ext_declare %3#0 typeparams %6 dummy_scope %1 {uniq_name = "_QFfooEstr1"} : (!fir.ref<!fir.char<1,?>>, i64, !fir.dscope) -> !fir.ref<!fir.char<1,?>> loc(#loc2)
16+
return
17+
} loc(#loc3)
18+
}
19+
20+
21+
#loc1 = loc("test.f90":18:1)
22+
#loc2 = loc("test.f90":17:1)
23+
#loc3 = loc("test.f90":15:1)
24+
25+
// CHECK: #[[VAR:.*]] = #llvm.di_local_variable<{{.*}}name = "._QFfooEstr1"{{.*}}flags = Artificial>
26+
// CHECK: func.func @foo
27+
// CHECK: llvm.intr.dbg.value #[[VAR]]
28+
// CHECK: return
29+
// CHECK: #[[STR_TY:.*]] = #llvm.di_string_type<tag = DW_TAG_string_type, name = "", stringLength = #[[VAR]], encoding = DW_ATE_ASCII>
30+
// CHECK: #llvm.di_local_variable<{{.*}}name = "str1"{{.*}}type = #[[STR_TY]]>
31+

0 commit comments

Comments
 (0)