This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Remove isTerminator for TRAP instruction
Needs ReviewPublic

Authored by qiucf on Aug 9 2020, 12:14 AM.

Details

Reviewers
nemanjai
ahatanak
dblaikie
ZhangKang
vitalybuka
Group Reviewers
Restricted Project
Summary

In this patch https://github.ibm.com/compiler/llvm-project/commit/30e3db2 , the TRAP instruction for
PowerPC has been set to isTerminator. In fact, the TRAP instruction should not add isTerminator property,
because when the TRAP has been handled, the program should be continued.

This bug has caused 6 lit cases error on PPC.

For below case,

void test_builtin_trap() {
  volatile int i = 0;
  __builtin_trap();
  volatile int j = i;
}

we use clang sim.c -target powerpc-unknown-unknown -c to build it, we will get below error:

*** Bad machine code: Non-terminator instruction after the first terminator ***
- function:    test_builtin_trap
- basic block: %bb.0 entry (0x1002c61f1f8)
- instruction: %1:gprc = LWZ 0, %stack.0.i :: (volatile dereferenceable load 4 from %ir.i)
First terminator was:	TRAP

*** Bad machine code: Non-terminator instruction after the first terminator ***
- function:    test_builtin_trap
- basic block: %bb.0 entry (0x1002c61f1f8)
- instruction: STW killed %1:gprc, 0, %stack.1.j :: (volatile store 4 into %ir.j)
First terminator was:	TRAP
fatal error: error in backend: Found 2 machine code errors.

This patch is to fix above bug.

Diff Detail

Event Timeline

ZhangKang created this revision.Aug 9 2020, 12:14 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 9 2020, 12:14 AM
Herald added subscribers: Restricted Project, cfe-commits, steven.zhang and 3 others. · View Herald Transcript
ZhangKang requested review of this revision.Aug 9 2020, 12:14 AM
ZhangKang updated this revision to Diff 284250.Aug 9 2020, 8:10 PM

Fix the case.

Fixing a MachineVerifier issue by patching clang is generally wrong; if the IR is valid, the backend should do something sane with it. If the IR isn't valid, the IR Verifier should complain.

Here, I'd say the IR is valid; adding a special case to the IR rules here would be a pain. So the right fix involves changing isel and/or the definitions of the the instructions in question.

Remoeve isTerminator for TRAP instruction.

ZhangKang retitled this revision from [Clang] Consider __builtin_trap() and __builtin_debugtrap() as terminator to [PowerPC] Remove isTerminator for TRAP instruction.Aug 10 2020, 10:18 AM
ZhangKang edited the summary of this revision. (Show Details)

Fixing a MachineVerifier issue by patching clang is generally wrong; if the IR is valid, the backend should do something sane with it. If the IR isn't valid, the IR Verifier should complain.

Here, I'd say the IR is valid; adding a special case to the IR rules here would be a pain. So the right fix involves changing isel and/or the definitions of the the instructions in question.

Yes, after the TRAP has been handled, the program should be continued. So I have removed the isTerminator for TRAP on PPC.

shchenz added a comment.EditedAug 10 2020, 7:39 PM

With this change, we get verify failure for below case:

int test_builtin_trap(int num) {
  volatile int i = 0;
  if (num > 0)
    __builtin_unreachable();
  return i;
}
clang t.c -target powerpc-unknown-unknown  -c -mllvm -verify-machineinstrs -mllvm -trap-unreachable=true
*** Bad machine code: MBB exits via unconditional fall-through but ends with a barrier instruction! *** 
- function:    test_builtin_trap
- basic block: %bb.1 if.then (0x1001f837b50)
fatal error: error in backend: Found 1 machine code errors.

IR unreachable instruction is an IR level terminator, and it will be selected to TRAP in selectiondagbuilder, is it ok to change TRAP to non-terminator on PowerPC. If above case is valid, I think the answer is no.

There is no TRAP in IR level, I guess this is the reason why IR verifier does not complain. Setting llvm.trap as terminator seems also not right, because on some targets, it sets ISD::TRAP to non-terminator on purpose. See https://reviews.llvm.org/rL278144.

When selecting Intrinsic::trap to ISD::TRAP, updating the BB info according to TRAP is a terminator or not on specific target? If TRAP is terminator, add a new BB, if it is not, do nothing? I am not sure.

vitalybuka resigned from this revision.Aug 12 2020, 9:21 PM
qiucf commandeered this revision.Sep 9 2020, 11:34 PM
qiucf added a reviewer: ZhangKang.

I think we should handle this similarly to SITargetLowering::splitKillBlock().

Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2022, 11:04 AM