Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SEH][CodeGen] Add test to track CFG optimization bug for SEH #77441

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

HaohaiWen
Copy link
Contributor

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 9, 2024

@llvm/pr-subscribers-backend-x86

Author: None (HaohaiWen)

Changes

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.


Full diff: https://github.com/llvm/llvm-project/pull/77441.diff

1 Files Affected:

  • (added) llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll (+81)
diff --git a/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
new file mode 100644
index 00000000000000..bd6743f7c414b4
--- /dev/null
+++ b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
@@ -0,0 +1,81 @@
+; XFAIL: *
+; RUN: llc -mtriple=x86_64-pc-windows-msvc %s
+define dso_local void @main(ptr %addr, ptr %src, ptr %dst) personality ptr @__CxxFrameHandler3 !dbg !11 {
+entry:
+  %tmp0 = load float, ptr %src
+  %src1 = getelementptr inbounds float, ptr %src, i64 1
+  %tmp1 = load float, ptr %src1
+  %src2 = getelementptr inbounds float, ptr %src, i64 2
+  %tmp2 = load float, ptr %src2
+  %src3 = getelementptr inbounds float, ptr %src, i64 3
+  %tmp3 = load float, ptr %src3
+  %src4 = getelementptr inbounds float, ptr %src, i64 4
+  %tmp4 = load float, ptr %src4
+  %src5 = getelementptr inbounds float, ptr %src, i64 5
+  %tmp5 = load float, ptr %src5
+  %src6 = getelementptr inbounds float, ptr %src, i64 6
+  %tmp6 = load float, ptr %src6
+  invoke void @foo(ptr %addr)
+          to label %scope_begin unwind label %ehcleanup1, !dbg !13
+
+scope_begin:
+  invoke void @llvm.seh.scope.begin()
+          to label %scope_end unwind label %ehcleanup, !dbg !13
+
+scope_end:
+  invoke void @llvm.seh.scope.end()
+          to label %finish unwind label %ehcleanup, !dbg !13
+
+ehcleanup:
+  %0 = cleanuppad within none [], !dbg !13
+  call void @llvm.dbg.value(metadata ptr %addr, metadata !12, metadata !DIExpression()), !dbg !13
+  call void @foo(ptr %addr) [ "funclet"(token %0) ], !dbg !13
+  cleanupret from %0 unwind label %ehcleanup1, !dbg !13
+
+ehcleanup1:
+  %1 = cleanuppad within none [], !dbg !13
+  call void @foo(ptr %addr) [ "funclet"(token %1) ], !dbg !13
+  cleanupret from %1 unwind to caller, !dbg !13
+
+finish:
+  store float %tmp0, ptr %dst
+  %dst1 = getelementptr inbounds float, ptr %dst, i64 1
+  store float %tmp1, ptr %dst1
+  %dst2 = getelementptr inbounds float, ptr %dst, i64 2
+  store float %tmp2, ptr %dst2
+  %dst3 = getelementptr inbounds float, ptr %dst, i64 3
+  store float %tmp3, ptr %dst3
+  %dst4 = getelementptr inbounds float, ptr %dst, i64 4
+  store float %tmp4, ptr %dst4
+  %dst5 = getelementptr inbounds float, ptr %dst, i64 5
+  store float %tmp5, ptr %dst5
+  %dst6 = getelementptr inbounds float, ptr %dst, i64 6
+  store float %tmp6, ptr %dst6
+  ret void
+}
+
+declare dso_local void @llvm.seh.scope.begin()
+declare dso_local void @llvm.seh.scope.end()
+declare dso_local i32 @__CxxFrameHandler3(...)
+declare dso_local void @foo(ptr %addr)
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.dbg.cu = !{!14}
+
+!0 = !{i32 2, !"eh-asynch", i32 1}
+!1 = !{i32 2, !"CodeView", i32 1}
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !{i32 7, !"uwtable", i32 2}
+
+!4 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64)
+!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64)
+!8 = !DISubroutineType(types: !9)
+!9 = !{null, !7, !5, !5}
+!10 = !DIFile(filename: "c:/main.cpp", directory: "")
+!11 = distinct !DISubprogram(name: "main", scope: !10, file: !10, line: 5, type: !8, scopeLine: 11, unit: !14)
+!12 = !DILocalVariable(name: "addr", scope: !11, file: !10, line: 5, type: !7)
+!13 = !DILocation(line: 7, scope: !11)
+!14 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !10, isOptimized: true, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)

@HaohaiWen
Copy link
Contributor Author

$llc -mtriple=x86_64-pc-windows-msvc < llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
        .text
        .def    @feat.00;
        .scl    3;
        .type   0;
        .endef
        .globl  @feat.00
.set @feat.00, 0
        .file   "<stdin>"
llc: /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/SmallVector.h:294: T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::operator[](llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type) [with T =
LiveDebugValues::ValueIDNum; <template-parameter-1-2> = void; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::reference = LiveDebugValues::ValueIDNum&; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type = long unsigne
d int]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llc -mtriple=x86_64-pc-windows-msvc
1.      Running pass 'Function Pass Manager' on module '<stdin>'.
2.      Running pass 'Live DEBUG_VALUE analysis' on function '@main'
 #0 0x0000000001b056d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/Support/Unix/Signals.inc:727:3
 #1 0x0000000001b0318c llvm::sys::RunSignalHandlers() /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #2 0x0000000001b034c6 SignalHandler(int) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007fe7cc7b5db0 __restore_rt (/lib64/libc.so.6+0x59db0)
 #4 0x00007fe7cc80242c __pthread_kill_implementation (/lib64/libc.so.6+0xa642c)
 #5 0x00007fe7cc7b5d06 gsignal (/lib64/libc.so.6+0x59d06)
 #6 0x00007fe7cc7887d3 abort (/lib64/libc.so.6+0x2c7d3)
 #7 0x00007fe7cc7886fb _nl_load_domain.cold (/lib64/libc.so.6+0x2c6fb)
 #8 0x00007fe7cc7aec86 (/lib64/libc.so.6+0x52c86)
 #9 0x0000000001213b93 (/export/users2/haohaiwe/compilers/llvm-project/builds/bin/llc+0x1213b93)
#10 0x0000000001225b2b LiveDebugValues::MLocTracker::readMLoc(LiveDebugValues::LocIdx) /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/DenseMap.h:1278:5
#11 0x0000000001225b2b TransferTracker::transferMlocs(LiveDebugValues::LocIdx, LiveDebugValues::LocIdx, llvm::MachineInstrBundleIterator<llvm::MachineInstr, false>) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRefB
asedImpl.cpp:884:51
#12 0x0000000001231e3d operator() /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp:2058:32
#13 0x0000000001231e3d LiveDebugValues::InstrRefBasedLDV::transferSpillOrRestoreInst(llvm::MachineInstr&) (.part.0) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp:2074:15
#14 0x00000000012448f3 LiveDebugValues::InstrRefBasedLDV::process(llvm::MachineInstr&, LiveDebugValues::FuncValueTable const*, LiveDebugValues::FuncValueTable const*) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRe
fBasedImpl.cpp:2256:3
#15 0x00000000012448f3 LiveDebugValues::InstrRefBasedLDV::process(llvm::MachineInstr&, LiveDebugValues::FuncValueTable const*, LiveDebugValues::FuncValueTable const*) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRe
fBasedImpl.cpp:2242:6
#16 0x0000000001244baa LiveDebugValues::InstrRefBasedLDV::depthFirstVLocAndEmit(unsigned int, llvm::DenseMap<llvm::LexicalScope const*, llvm::DILocation const*, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail::DenseMapPair<llvm::Lexical
Scope const*, llvm::DILocation const*>> const&, llvm::DenseMap<llvm::LexicalScope const*, llvm::SmallSet<llvm::DebugVariable, 4u, std::less<llvm::DebugVariable>>, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail::DenseMapPair<llvm::Lexic
alScope const*, llvm::SmallSet<llvm::DebugVariable, 4u, std::less<llvm::DebugVariable>>>> const&, llvm::DenseMap<llvm::LexicalScope const*, llvm::SmallPtrSet<llvm::MachineBasicBlock*, 4u>, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail
::DenseMapPair<llvm::LexicalScope const*, llvm::SmallPtrSet<llvm::MachineBasicBlock*, 4u>>>&, llvm::SmallVector<llvm::SmallVector<std::pair<llvm::DebugVariable, LiveDebugValues::DbgValue>, 8u>, 8u>&, LiveDebugValues::FuncValueTable&, LiveDebugValues::Fun
cValueTable&, llvm::SmallVectorImpl<LiveDebugValues::VLocTracker>&, llvm::MachineFunction&, llvm::DenseMap<llvm::DebugVariable, unsigned int, llvm::DenseMapInfo<llvm::DebugVariable, void>, llvm::detail::DenseMapPair<llvm::DebugVariable, unsigned int>>&,
llvm::TargetPassConfig const&)::'lambda'(llvm::MachineBasicBlock&)::operator()(llvm::MachineBasicBlock&) const /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp:3506:5
#17 0x0000000001245b68 llvm::SmallPtrSetIterator<llvm::MachineBasicBlock const*>::operator++() /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/SmallPtrSet.h:296:5
#18 0x0000000001245b68 LiveDebugValues::InstrRefBasedLDV::depthFirstVLocAndEmit(unsigned int, llvm::DenseMap<llvm::LexicalScope const*, llvm::DILocation const*, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail::DenseMapPair<llvm::Lexical
Scope const*, llvm::DILocation const*>> const&, llvm::DenseMap<llvm::LexicalScope const*, llvm::SmallSet<llvm::DebugVariable, 4u, std::less<llvm::DebugVariable>>, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail::DenseMapPair<llvm::Lexic
alScope const*, llvm::SmallSet<llvm::DebugVariable, 4u, std::less<llvm::DebugVariable>>>> const&, llvm::DenseMap<llvm::LexicalScope const*, llvm::SmallPtrSet<llvm::MachineBasicBlock*, 4u>, llvm::DenseMapInfo<llvm::LexicalScope const*, void>, llvm::detail
::DenseMapPair<llvm::LexicalScope const*, llvm::SmallPtrSet<llvm::MachineBasicBlock*, 4u>>>&, llvm::SmallVector<llvm::SmallVector<std::pair<llvm::DebugVariable, LiveDebugValues::DbgValue>, 8u>, 8u>&, LiveDebugValues::FuncValueTable&, LiveDebugValues::Fun
cValueTable&, llvm::SmallVectorImpl<LiveDebugValues::VLocTracker>&, llvm::MachineFunction&, llvm::DenseMap<llvm::DebugVariable, unsigned int, llvm::DenseMapInfo<llvm::DebugVariable, void>, llvm::detail::DenseMapPair<llvm::DebugVariable, unsigned int>>&,
llvm::TargetPassConfig const&) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp:3571:30
#19 0x000000000124ad2c LiveDebugValues::InstrRefBasedLDV::ExtendRanges(llvm::MachineFunction&, llvm::MachineDominatorTree*, llvm::TargetPassConfig*, unsigned int, unsigned int) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/LiveDebugValu
es/InstrRefBasedImpl.cpp:3786:36
#20 0x0000000000ef4397 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:93:33
#21 0x000000000143082e llvm::FPPassManager::runOnFunction(llvm::Function&) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1453:7
#22 0x0000000001430a51 llvm::ilist_node_base<true>::getNext() const /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/ilist_node_base.h:43:45
#23 0x0000000001430a51 llvm::ilist_node_impl<llvm::ilist_detail::node_options<llvm::Function, true, false, void, false>>::getNext() /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/ilist_node.h:94:66
#24 0x0000000001430a51 llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, true, false, void, false>, false, false>::operator++() /export/users2/haohaiwe/compilers/llvm-project/llvm/include/llvm/ADT/ilist_iterator.h:157:25
#25 0x0000000001430a51 llvm::FPPassManager::runOnModule(llvm::Module&) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1488:22
#26 0x000000000143129e runOnModule /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1565:7
#27 0x000000000143129e llvm::legacy::PassManagerImpl::run(llvm::Module&) /export/users2/haohaiwe/compilers/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:541:55
#28 0x00000000006c948d compileModule(char**, llvm::LLVMContext&) /export/users2/haohaiwe/compilers/llvm-project/llvm/tools/llc/llc.cpp:751:34
#29 0x0000000000652226 main /export/users2/haohaiwe/compilers/llvm-project/llvm/tools/llc/llc.cpp:425:35
#30 0x00007fe7cc7a0e50 __libc_start_call_main (/lib64/libc.so.6+0x44e50)
#31 0x00007fe7cc7a0efc __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x44efc)
#32 0x00000000006c09c5 _start (/export/users2/haohaiwe/compilers/llvm-project/builds/bin/llc+0x6c09c5)
Aborted (core dumped)

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@HaohaiWen HaohaiWen merged commit a2dba0c into llvm:main Jan 9, 2024
5 checks passed
@HaohaiWen HaohaiWen deleted the seh-cfg branch January 9, 2024 14:30
HaohaiWen added a commit that referenced this pull request Jan 10, 2024
HaohaiWen added a commit to HaohaiWen/llvm-project that referenced this pull request Jan 10, 2024
This test tracks BranchFolding pass which removes fall through jump and
leaves landing-pad to be machine basic block of no predecessors. It
would raise bug as introduced in llvm#77441.
HaohaiWen added a commit that referenced this pull request Jan 10, 2024
This test tracks BranchFolding pass which removes fall through jump and
leaves landing-pad to be machine basic block of no predecessors. It
would raise bug as introduced in #77441.
HaohaiWen added a commit to HaohaiWen/llvm-project that referenced this pull request Jan 11, 2024
…7441)

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.
HaohaiWen added a commit to HaohaiWen/llvm-project that referenced this pull request Jan 11, 2024
HaohaiWen added a commit to HaohaiWen/llvm-project that referenced this pull request Jan 11, 2024
Those not reachable blocks was not analyzed by LiveDebugValues and may
raise out of bound access to VarLocs as case in llvm#77441.
HaohaiWen added a commit that referenced this pull request Jan 11, 2024
When removing an empty machine basic block, all of its successors should
be inherited by its fall through MBB. This keeps CFG as only have one
entry which is required by LiveDebugValues.

Reland #77441 as LiveDebugValues test.
HaohaiWen added a commit that referenced this pull request Jan 11, 2024
Those not reachable blocks was not analyzed by LiveDebugValues and may
raise out of bound access to VarLocs as case in #77441.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…7441)

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
This test tracks BranchFolding pass which removes fall through jump and
leaves landing-pad to be machine basic block of no predecessors. It
would raise bug as introduced in llvm#77441.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
When removing an empty machine basic block, all of its successors should
be inherited by its fall through MBB. This keeps CFG as only have one
entry which is required by LiveDebugValues.

Reland llvm#77441 as LiveDebugValues test.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…7725)

Those not reachable blocks was not analyzed by LiveDebugValues and may
raise out of bound access to VarLocs as case in llvm#77441.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants