Skip to content

Commit 08b46d7

Browse files
authored
Fix failures for imports with LLVM-17 (#1414)
* Fix failures for imports with LLVM-17 Symptom: Compilation of any Fortran program with imports (USE statement) fails with assert in compiler backend. Diagnosis: LLVM-17 expects imports list in retainedNode field of DISubprogram wihch while Flang is still generating it as imports field of DICompilationUnit. Fix: Flang is now updated to new IR as expected by LLVM-17. * Incorporated comment from @bryan.
1 parent 2be8555 commit 08b46d7

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

test/debug_info/use_only_test.f90

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
! REQUIRES: llvm-17
2+
13
!RUN: %flang -g -S -emit-llvm %s_mod.f90 %s
24
!RUN: cat use_only_test.ll | FileCheck %s
35

46
!CHECK: [[MOD_VAR:![0-9]+]] = distinct !DIGlobalVariable(name: "mod_var1"
57
!CHECK: distinct !DICompileUnit
68
!CHECK-SAME: imports: [[IMPLIST:![0-9]+]]
7-
!CHECK: [[IMPLIST]] = !{[[IMPORT:![0-9]+]]
8-
!CHECK: [[IMPORT]] = !DIImportedEntity(tag: DW_TAG_imported_declaration
9+
!CHECK: [[IMPLIST]] = !{}
10+
!CHECK: !DISubprogram(name: "main"
11+
!CHECK-SAME: retainedNodes: [[RETAINED:![0-9]+]]
12+
!CHECK: [[RETAINED]] = !{[[RETAIN1:![0-9]+]]}
13+
!CHECK: [[RETAIN1]] = !DIImportedEntity(tag: DW_TAG_imported_declaration
914
!CHECK-SAME: entity: [[MOD_VAR]]
1015
!CHECK-NOT: !DIImportedEntity(tag: DW_TAG_imported_module
1116

test/debug_info/usemodule.f90

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1+
! REQUIRES: llvm-17
2+
13
!RUN: %flang -g -S -emit-llvm %s -o - | FileCheck %s
24

3-
!CHECK: [[VAR1:![0-9]+]] = distinct !DIGlobalVariable(name: "var1"
4-
!CHECK: [[MYMOD:![0-9]+]] = !DIModule(scope: {{![0-9]+}}, name: "mymod"
5-
!CHECK: [[VAR2:![0-9]+]] = distinct !DIGlobalVariable(name: "var2"
6-
!CHECK: [[VAR3:![0-9]+]] = distinct !DIGlobalVariable(name: "var3"
75

8-
!CHECK: !DIImportedEntity(tag: DW_TAG_imported_module, scope: [[USE_ALL:![0-9]+]], entity: [[MYMOD]]
9-
!CHECK: [[USE_ALL]] = distinct !DISubprogram(name: "use_all"
6+
!CHECK-DAG: [[VAR1:![0-9]+]] = distinct !DIGlobalVariable(name: "var1"
7+
!CHECK-DAG: [[MYMOD:![0-9]+]] = !DIModule(scope: {{![0-9]+}}, name: "mymod"
8+
!CHECK-DAG: [[VAR2:![0-9]+]] = distinct !DIGlobalVariable(name: "var2"
9+
!CHECK-DAG: [[VAR3:![0-9]+]] = distinct !DIGlobalVariable(name: "var3"
10+
11+
!CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, scope: [[USE_ALL:![0-9]+]], entity: [[MYMOD]]
12+
!CHECK-DAG: [[USE_ALL]] = distinct !DISubprogram(name: "use_all"
1013

11-
!CHECK: !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: [[USE_RESTRICTED:![0-9]+]], entity: [[VAR1]]
12-
!CHECK: [[USE_RESTRICTED]] = distinct !DISubprogram(name: "use_restricted"
14+
!CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: [[USE_RESTRICTED:![0-9]+]], entity: [[VAR1]]
15+
!CHECK-DAG: [[USE_RESTRICTED]] = distinct !DISubprogram(name: "use_restricted"
1316

14-
!CHECK: !DIImportedEntity(tag: DW_TAG_imported_module, scope: [[USE_RENAMED:![0-9]+]], entity: [[MYMOD]]
17+
!CHECK: [[USE_RENAMED:![0-9]+]] = distinct !DISubprogram(name: "use_renamed"
18+
!CHECK-SAME: retainedNodes: [[RETAINED:![0-9]+]]
19+
!CHECK: [[RETAINED]] = !{[[RETAIN1:![0-9]+]]
20+
!CHECK: [[RETAIN1]] = !DIImportedEntity(tag: DW_TAG_imported_module, scope: [[USE_RENAMED]], entity: [[MYMOD]]
1521
!CHECK-SAME: elements: [[RENAMES:![0-9]+]]
16-
!CHECK: [[USE_RENAMED]] = distinct !DISubprogram(name: "use_renamed"
1722
!CHECK: [[RENAMES]] = !{[[RENAME1:![0-9]+]]}
18-
!CHECK: [[RENAME1]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "var4", scope: [[USE_RENAMED]], entity: [[VAR1]]
23+
!CHECK: [[RENAME1]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "var4", scope: [[USE_RENAMED:![0-9]+]], entity: [[VAR1]]
1924

20-
!CHECK: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "var4", scope: [[USE_RESTRICTED_RENAMED:![0-9]+]], entity: [[VAR1]]
21-
!CHECK: [[USE_RESTRICTED_RENAMED]] = distinct !DISubprogram(name: "use_restricted_renamed"
25+
!CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "var4", scope: [[USE_RESTRICTED_RENAMED:![0-9]+]], entity: [[VAR1]]
26+
!CHECK-DAG: [[USE_RESTRICTED_RENAMED]] = distinct !DISubprogram(name: "use_restricted_renamed"
2227

2328
module mymod
2429
integer :: var1 = 11

tools/flang2/flang2exe/ll_structure.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef enum LL_IRVersion {
149149
LL_Version_11_0 = 110,
150150
LL_Version_12_0 = 120,
151151
LL_Version_13_0 = 130,
152+
LL_Version_17_0 = 170,
152153
LL_Version_trunk = 1023
153154
} LL_IRVersion;
154155

@@ -413,6 +414,13 @@ ll_feature_debug_info_ver13(const LL_IRFeatures *feature)
413414
return feature->version >= LL_Version_13_0;
414415
}
415416

417+
/**
418+
\brief Version 17.0 debug metadata
419+
*/
420+
INLINE static bool ll_feature_debug_info_ver17(const LL_IRFeatures *feature)
421+
{
422+
return feature->version >= LL_Version_17_0;
423+
}
416424
/**
417425
\brief Version 9.0 onwards uses 3 field syntax for constructors
418426
and destructors
@@ -515,6 +523,7 @@ ll_feature_no_file_in_namespace(const LL_IRFeatures *feature)
515523
#define ll_feature_debug_info_ver11(f) ((f)->version >= LL_Version_11_0)
516524
#define ll_feature_debug_info_ver12(f) ((f)->version >= LL_Version_12_0)
517525
#define ll_feature_debug_info_ver13(f) ((f)->version >= LL_Version_13_0)
526+
#define ll_feature_debug_info_ver17(f) ((f)->version >= LL_Version_17_0)
518527
#define ll_feature_three_argument_ctor_and_dtor(f) \
519528
((f)->version >= LL_Version_9_0)
520529
#define ll_feature_use_distinct_metadata(f) ((f)->version >= LL_Version_3_8)

tools/flang2/flang2exe/lldebug.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ static LL_MDRef lldbg_fwd_local_variable(LL_DebugInfo *db, int sptr, int findex,
188188
static LL_MDRef lldbg_create_imported_entity(LL_DebugInfo *db, SPTR entity_sptr,
189189
SPTR func_sptr,
190190
IMPORT_TYPE entity_type,
191-
LL_MDRef elements_mdnode);
191+
LL_MDRef elements_mdnode,
192+
LL_MDRef imported_list);
192193
static void lldbg_emit_imported_entity(LL_DebugInfo *db, SPTR entity_sptr,
193194
SPTR func_sptr, IMPORT_TYPE entity_type,
194-
LL_MDRef elements);
195+
LL_MDRef elements, LL_MDRef imported_list);
195196
static LL_MDRef lldbg_create_subrange_mdnode(LL_DebugInfo *db, LL_MDRef count,
196197
LL_MDRef lb, LL_MDRef ub,
197198
LL_MDRef st);
@@ -530,6 +531,7 @@ lldbg_create_subprogram_mdnode(
530531
llmd_add_md(mdb, lv_list_mdnode);
531532
} else if (ll_feature_debug_info_ver13(&db->module->ir))
532533
llmd_add_md(mdb, lv_list_mdnode);
534+
533535
llmd_add_i32(mdb, scope);
534536

535537
/* Request a distinct mdnode so that it can be updated with a function pointer
@@ -2417,6 +2419,7 @@ lldbg_emit_subprogram(LL_DebugInfo *db, SPTR sptr, DTYPE ret_dtype, int findex,
24172419
LL_MDRef lv_list_mdnode;
24182420
LL_MDRef context_mdnode;
24192421
LL_MDRef scope;
2422+
LL_MDRef imported_list;
24202423
const char *mips_linkage_name = "";
24212424
const char *func_name;
24222425
int virtuality = 0;
@@ -2471,6 +2474,8 @@ lldbg_emit_subprogram(LL_DebugInfo *db, SPTR sptr, DTYPE ret_dtype, int findex,
24712474
ll_get_md_null(), lv_list_mdnode, lineno);
24722475
if (!db->subroutine_mdnodes)
24732476
db->subroutine_mdnodes = hashmap_alloc(hash_functions_direct);
2477+
imported_list = ll_feature_debug_info_ver17(&db->module->ir) ?
2478+
lv_list_mdnode : db->llvm_dbg_imported;
24742479
scopeData = (hash_data_t)(unsigned long)db->cur_subprogram_mdnode;
24752480
hashmap_replace(db->subroutine_mdnodes, INT2HKEY(sptr), &scopeData);
24762481
while (db->import_entity_list) {
@@ -2480,10 +2485,12 @@ lldbg_emit_subprogram(LL_DebugInfo *db, SPTR sptr, DTYPE ret_dtype, int findex,
24802485
/* There are pending entities to be imported into this func */
24812486
lldbg_emit_imported_entity(db, db->import_entity_list->entity, sptr,
24822487
db->import_entity_list->entity_type,
2483-
elements_mdnode);
2488+
elements_mdnode, imported_list);
24842489
while (child) {
24852490
LL_MDRef element = lldbg_create_imported_entity(db, child->entity, sptr,
2486-
child->entity_type, (LL_MDRef) NULL);
2491+
child->entity_type,
2492+
(LL_MDRef) NULL,
2493+
(LL_MDRef) NULL);
24872494
ll_extend_md_node(db->module, elements_mdnode, element);
24882495
child = child->next;
24892496
}
@@ -4107,7 +4114,8 @@ lldbg_function_end(LL_DebugInfo *db, int func)
41074114

41084115
static LL_MDRef
41094116
lldbg_create_imported_entity(LL_DebugInfo *db, SPTR entity_sptr, SPTR func_sptr,
4110-
IMPORT_TYPE entity_type, LL_MDRef elements_mdnode)
4117+
IMPORT_TYPE entity_type, LL_MDRef elements_mdnode,
4118+
LL_MDRef imported_list)
41114119
{
41124120
LLMD_Builder mdb;
41134121
LL_MDRef entity_mdnode, scope_mdnode = 0, file_mdnode, cur_mdnode;
@@ -4164,14 +4172,16 @@ lldbg_create_imported_entity(LL_DebugInfo *db, SPTR entity_sptr, SPTR func_sptr,
41644172
llmd_add_md(mdb, elements_mdnode); // elements
41654173

41664174
cur_mdnode = llmd_finish(mdb);
4167-
ll_extend_md_node(db->module, db->llvm_dbg_imported, cur_mdnode);
4175+
if (imported_list)
4176+
ll_extend_md_node(db->module, imported_list, cur_mdnode);
41684177
return cur_mdnode;
41694178
}
41704179

41714180
static void
41724181
lldbg_emit_imported_entity(LL_DebugInfo *db, SPTR entity_sptr,
41734182
SPTR func_sptr, IMPORT_TYPE entity_type,
4174-
LL_MDRef elements_mdnode)
4183+
LL_MDRef elements_mdnode,
4184+
LL_MDRef imported_list)
41754185
{
41764186
static hashset_t entity_func_added;
41774187
const char *entity_func;
@@ -4185,7 +4195,7 @@ lldbg_emit_imported_entity(LL_DebugInfo *db, SPTR entity_sptr,
41854195
return;
41864196
hashset_insert(entity_func_added, entity_func);
41874197
lldbg_create_imported_entity(db, entity_sptr, func_sptr, entity_type,
4188-
elements_mdnode);
4198+
elements_mdnode, imported_list);
41894199
}
41904200

41914201
void

0 commit comments

Comments
 (0)