Fixes 61932.
The core issue I found is that DISubprogram operations usually use getUnit, while its DIE uses getScope. Once these two are associated with different CUs, associated DIEs may use another CU.
I found that even under llc version 15.0.7, DW_AT_low_pc and DW_AT_high_pc are still placed under the wrong CU. The D136039 just reflected this error to other locations. And this "Cannot represent a difference across sections" error only occurs on x86/x86-64.
$ llc -O0 -mtriple=x86_64-unknown-unknown -filetype=obj test.ll $ llvm-dwarfdump --debug-info --verbose test.o 0x0000000b: DW_TAG_compile_unit [1] * ... DW_AT_low_pc [DW_FORM_addr] (0x0000000000000070 ".text") DW_AT_high_pc [DW_FORM_data4] (0x00000000) 0x00000080: DW_TAG_namespace [5] * (0x0000004b) ... 0x00000085: DW_TAG_structure_type [6] * (0x00000080) ... 0x0000008c: DW_TAG_subprogram [9] * (0x00000085) DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000 ".text") DW_AT_high_pc [DW_FORM_data4] (0x00000062) ... 0x000000a5: DW_TAG_lexical_block [10] * (0x0000008c) DW_AT_low_pc [DW_FORM_addr] (0x0000000000000030 ".text") DW_AT_high_pc [DW_FORM_data4] (0x0000001e) ... 0x000000fe: DW_TAG_compile_unit [1] * ... DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000 ".text") <--- This should be in another CU. DW_AT_high_pc [DW_FORM_data4] (0x00000062) <--- This should be in another CU. ...
OK, so from the description in the other inline comment - I take it you're dealing with the situation here, where:
!23 (DISubprogram for alloc::alloc::Global::alloc_impl) is defined in CU !1
!8 that also, indirectly, references the type is in CU !4
Yeah, I think the bug here is that you shouldn't have member function definitions directly associated with DICompositeTypes - they should indirect through a declaration of the function.
Otherwise we'll inevitably end up in impossible situations - where we can't define the type in every translation unit that defines one of the member functions (or other references) to the type.
Types are intentionally not attached to a CU because they exist across the whole program - so we need to be able to reference them from anywhere/lazily create them in whatever CU we need, generally.
If you've got similar metadata from Swift, I'd like to take a look/talk to the Apple/Swift folks about this.
But, yeah, generally, I don't think this IR is OK/supportable as-is. It should have a declaration of the function - so that the function definition can appear in a CU distinct from the definition of the type. So the function definition can stay in the CU that defined it.