llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang generates invalid assembly on ppc64le

Open llvmbot opened this issue 10 years ago • 31 comments

Bugzilla Link 28102
Version 3.5
OS Linux
Attachments Test case, Faulty assembler output
Reporter LLVM Bugzilla Contributor
CC @osyrez,@DougGregor,@hfinkel,@lei137,@nemanjai

Extended Description

When building a large, proprietary codebase against ppc64le for the first time, clang generated an invalid intermediate assembly file.

Reducing the failing code as much as possible yielded the attached test case. It generates a significant amount of assembler output, but the most critical section is:

.L0_0_set_4294967295 = ".LBB0_-1"-.LJTI0_0 .L0_0_set_3 = .LBB0_3-.LJTI0_0 .L0_0_set_4 = .LBB0_4-.LJTI0_0 .L0_0_set_5 = .LBB0_5-.LJTI0_0

There appears to be integer wrap-around causing an invalid .LBB label to be generated. Reducing the number of the case statements in the switch block resolved the failure, and the failure remained when adding additional statements, implying clang is not handling anything over 5 statements correctly in this case.

clang arguments were: -fPIC -Wno-switch -Wno-unused-value -fvisibility=hidden -DNDEBUG -g -O2 -v -save-temps -std=c++11

The relevant sections of the generated assembler file are attached. I (likely) cannot post code outside of the provided sections due to license restrictions on the codebase.

Please let me know if you need any further information!

llvmbot avatar Jun 13 '16 08:06 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#39124

lei137 avatar Nov 27 '21 01:11 lei137

*** Bug llvm/llvm-bugzilla-archive#39124 has been marked as a duplicate of this bug. ***

lei137 avatar Oct 15 '18 15:10 lei137

Tim,

llvm/llvm-bugzilla-archive#39124 seem to be failing due to the same issue as here. I was able to generate a reduced IR for their issue which is fixed by Chris's patch. Can you please give his patch a try to see if it will fix your original issue?

Chris,

Can you please post your patch up for review in phabricator? The reduced IR you have below doesn't fail for ppc, but I've attached a reduced IR in llvm/llvm-bugzilla-archive#39124 that asserts with ToT which your patch fixes.

Thanks!

lei137 avatar Oct 15 '18 15:10 lei137

This was traced to unreachable BB pruning not deleting the associated BB from the jump tables. After several false starts it doesn't appear possible and/or a good idea to remove the BB from the jump table list during pruning, so I ended up modifying the assembler output to ignore unreachable BBs that remain in the jump table lists after pruning.

Please let me know if this is the right approach or if I should be doing this some other way.

Thanks!

I'm curious what problems were encountered with removing the MBB references from the jump tables, or any other issues to this approach? I ask because this is the approach I used in a local fix and have yet to see any issues.

osyrez avatar May 12 '18 00:05 osyrez

I think this is still an issue as I've encountered it on x86 and arm64 architectures as well. As mentioned in comment #​20, I found that the unreachable-mbb-elimination pass deletes MBBs referenced by a jump table but doesn't remove those references from the jump table. I've attached a testcase and potential and would love to get some feedback on this whether or not this is a viable approach.

Testcase: attachment 20297 It sounds like there may have been some difficulty using the existing testcase so I have attached a minimal bitcode testcase. It should reproduce the error by simply passing the testcase bitcode file to llc.

Potential Fix: attachment 20298 I've attached a patch containing my local fix and would welcome any comments or criticism. In short, it removes the MBB being deleted from the jump tables right before its deletion in MachineFunction::DeleteMachineBasicBlock().

I didn't extend this change to MBBs that were just removed (not deleted) from the MF since I wasn't sure if I could guarantee that MBB wouldn't be re-added to the MF, in which case it could theoretically still be valid.

I chose the removal point right before MBB deletion (as opposed to within the unreachable-mbb-elimination pass) since it seemed like a more generic solution that would prevent the MachineFunction data structure from getting into an invalid state in the first case (assuming a jump table reference to a deleted MBB is an invalid state).

I did try the previously proposed fix but it didn't end up working for my case since my code path through AsmPrinter::EmitJumpTableInfo() was a bit different and ended up still running into the error when MachineBasicBlock::getSymbol() was called.

Let me know if there are any questions, and what I can do to help move this forward.

Thanks

osyrez avatar May 12 '18 00:05 osyrez

Potential fix

osyrez avatar May 12 '18 00:05 osyrez

Bitcode test case

osyrez avatar May 12 '18 00:05 osyrez

If this is still a problem, I'd like to revisit it to try to find the solution.

nemanjai avatar Mar 09 '18 15:03 nemanjai

Uploaded at https://reviews.llvm.org/D22435

llvmbot avatar Jul 16 '16 05:07 llvmbot

Your description seems reasonable and the fix for this specific issue seems reasonable (should certainly work). However, I don't know enough about this part of the code to state whether this is a safe thing to do. Namely, I am not certain that this will not hide some sort of bug.

I recommend posting this on Phabricator for review so someone more familiar with this code can comment.

nemanjai avatar Jul 15 '16 19:07 nemanjai

This was traced to unreachable BB pruning not deleting the associated BB from the jump tables. After several false starts it doesn't appear possible and/or a good idea to remove the BB from the jump table list during pruning, so I ended up modifying the assembler output to ignore unreachable BBs that remain in the jump table lists after pruning.

Please let me know if this is the right approach or if I should be doing this some other way.

Thanks!

llvmbot avatar Jul 13 '16 23:07 llvmbot

I've narrowed this down to the jump table entry being removed from the main basic block list, but somehow remaining in the jump table list. Still working on it.

llvmbot avatar Jul 13 '16 20:07 llvmbot

I think the question that Hal asked is pertinent in this case. Namely, the basic blocks are uniquely numbered per-function. We need to know if the function being compiled somehow ends up with more than 2 billion basic block labels causing the numbering to wrap around or if a jump table somehow mentions a basic block that is not part of a function.

My apologies for not providing this earlier; I've been picking through the clang source trying to locate the error myself.

It doesn't look like there are anywhere near 2 billion block labels; the largest one I see is .LBB2559_22. From what I can see this far it looks like a basic block is "lost" somehow and left at the default number of -1.

llvmbot avatar Jul 12 '16 18:07 llvmbot

I forgot to ask the actual question... How many of these labels are in the assembly?

nemanjai avatar Jul 11 '16 22:07 nemanjai

I think the question that Hal asked is pertinent in this case. Namely, the basic blocks are uniquely numbered per-function. We need to know if the function being compiled somehow ends up with more than 2 billion basic block labels causing the numbering to wrap around or if a jump table somehow mentions a basic block that is not part of a function.

nemanjai avatar Jul 11 '16 22:07 nemanjai

Never mind, debug mode just took much longer than I expected. I do see an assert now.

While I cannot post the source file in question due to licensing of the codebase, I have attached the clang assertion output for analysis.

llvmbot avatar Jul 10 '16 09:07 llvmbot

clang trunk hangs as well. The hang is in the backend; I have attached a backtrace from attaching to the hung clang llvm process.

I'm going to let this run overnight to see if clang might recover at some point.

llvmbot avatar Jul 10 '16 09:07 llvmbot