You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用llc作为编译器后端时遭遇scope points into type hierarchy错误

解决LLVM llc调试信息错误:scope points into the type hierarchy

你碰到的这个错误,根源是DILocation的scope直接引用了DISubprogram,不符合LLVM调试元数据的规范要求——尤其是当编译单元开启优化(isOptimized: true)时,LLVM会严格校验作用域的层级关系。

修复步骤

  • 给DISubprogram添加一个顶层DILexicalBlock作为词法作用域节点
  • 将DILocation的scope指向这个新的DILexicalBlock

修改后的完整代码

; ModuleID = 'foo'
source_filename = "foo.src"

define i32 @foo() {
EntryBlock:
  ret i32 0, !dbg !4
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3}

!0 = distinct !DICompileUnit(language: 9999, file: !1, producer: "mycompiler", isOptimized: true, runtimeVersion: 1, emissionKind: FullDebug)
!1 = !DIFile(filename: "foo.src", directory: "")
!2 = !{i32 2, !"Dwarf Version", i32 3}
!3 = !{i32 1, !"Debug Info Version", i32 3}
!4 = !DILocation(line: 35, column: 8, scope: !5)
!5 = !DILexicalBlock(scope: !6, file: !1, line: 28, column: 0)  ; 新增的顶层词法作用域
!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !0, file: !1, line: 28, type: !7, spFlags: 0, retainedNodes: !10)
!7 = !DISubroutineType(types: !8) 
!8 = !{!9}
!9 = !DIBasicType(name: "u32", size: 32, encoding: DW_ATE_unsigned)
!10 = !{}

关键说明

  • DILexicalBlock作为函数的顶层词法作用域,它的scope指向对应的DISubprogram,建立了正确的层级链:DICompileUnit → DISubprogram → DILexicalBlock → DILocation
  • 开启优化时,LLVM会假设代码结构可能被重排,因此要求调试位置必须关联到具体的词法块,而非直接关联到函数本身,避免出现作用域混淆的校验错误

内容的提问来源于stack exchange,提问作者Chris Dodd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 21:41:03