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

[RISCV] [Relax] J instr is not relaxed to auipc + jalr in RISCVAsmBackend which is not handled before in BranchRelax Pass

Open zixuan-wu opened this issue 2 years ago • 11 comments

getInstSizeInBytes estimate the size of compressed instruction incorrectly because the instructions between the branchrelax source and target may be relaxed in assembler later. In such case, unconditional jump(J instr) would be kept to assembler without being handled in BranchRelax Pass. But in the end, target addresss of J instr is out of range.

unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {

...

if (MI.getParent() && MI.getParent()->getParent()) {
    if (isCompressibleInst(MI, STI))
      return 2;
 }
...

So it also needs to relax J/JAL in assembler to fix up the output of branchrelax process.

zixuan-wu avatar Jun 14 '24 08:06 zixuan-wu

@llvm/issue-subscribers-bug

Author: Zi Xuan Wu (Zeson) (zixuan-wu)

```

if (MI.getParent() && MI.getParent()->getParent()) { if (isCompressibleInst(MI, STI)) return 2; }


Because the compressed instruction can be relaxed in assembler later, so is this right to estimate the size of compressed instruction?
</details>

llvmbot avatar Jun 14 '24 08:06 llvmbot

@llvm/issue-subscribers-backend-risc-v

Author: Zi Xuan Wu (Zeson) (zixuan-wu)

```

if (MI.getParent() && MI.getParent()->getParent()) { if (isCompressibleInst(MI, STI)) return 2; }


Because the compressed instruction can be relaxed in assembler later, so is this right to estimate the size of compressed instruction?
</details>

llvmbot avatar Jun 14 '24 08:06 llvmbot

This ticket https://github.com/llvm/llvm-project/issues/56448 says we over estimate the size of branch instructions in getInstSizeInBytes.

topperc avatar Jun 14 '24 16:06 topperc

So it also needs to relax J/JAL in assembler to fix up the output of branchrelax process.

This isn't possible because its need a temporary register.

topperc avatar Jun 14 '24 16:06 topperc

This ticket #56448 says we over estimate the size of branch instructions in getInstSizeInBytes.

What do you mean? Is it a known issue which has't been fixed?

zixuan-wu avatar Jun 17 '24 02:06 zixuan-wu

This ticket #56448 says we over estimate the size of branch instructions in getInstSizeInBytes.

What do you mean? Is it a known issue which has't been fixed?

According to #56448, isCompressibleInst returns false for compressed branches in getInstSizeInBytes. So we should be counting branched instructions as 4 bytes during branch relaxation.

The only instructions that should be relaxed during assembler should be compressed branches/jumps to uncompressed branches/jumpes. Everything else should be relaxed by BranchRelaxation.

So the bug in this ticket shouldn't be possible.

topperc avatar Jun 17 '24 03:06 topperc

Is it the order that compressing instruction to compressed instruction before relaxing the compressed instruction to uncompressed instruction?


J DstJ (1)
...
BEQ DstBEQ (2)
...
DstJ:
...
DstBEQ:

For example, In BranchRelaxtion Pass, (2) is predicated as compressed instruction, but it's uncompressed in assembler phase. So it influences the offset calculation of (1). In the end, (1) would be out of range in linker phase.

zixuan-wu avatar Jun 17 '24 06:06 zixuan-wu

Is it the order that compressing instruction to compressed instruction before relaxing the compressed instruction to uncompressed instruction?




J DstJ (1)

...

BEQ DstBEQ (2)

...

DstJ:

...

DstBEQ:



For example, In BranchRelaxtion Pass, (2) is predicated as compressed instruction, but it's uncompressed in assembler phase. So it influences the offset calculation of (1). In the end, (1) would be out of range in linker phase.

I think getInstSizeInBytes will always return 4 for the BEQ. The code in is isCompressibleInst checks isImm which returns false because the operand is a basic block label not an immediate.

Do you have a test that shows otherwise?

topperc avatar Jun 17 '24 19:06 topperc

Is it the order that compressing instruction to compressed instruction before relaxing the compressed instruction to uncompressed instruction?




J DstJ (1)

...

BEQ DstBEQ (2)

...

DstJ:

...

DstBEQ:

For example, In BranchRelaxtion Pass, (2) is predicated as compressed instruction, but it's uncompressed in assembler phase. So it influences the offset calculation of (1). In the end, (1) would be out of range in linker phase.

I think getInstSizeInBytes will always return 4 for the BEQ. The code in is isCompressibleInst checks isImm which returns false because the operand is a basic block label not an immediate.

Do you have a test that shows otherwise?

OK, I got it, thank you.

I met an issue that branch target is out range in linker phase. There is no special instruction between branch inst and target table. I think there must be some offset change after branchrelax and before emit to binary. Like inserting nop or block alignment adjustment, or...etc.

zixuan-wu avatar Jun 18 '24 03:06 zixuan-wu

The real issue is fixed at https://github.com/llvm/llvm-project/pull/90013

zixuan-wu avatar Jun 24 '24 02:06 zixuan-wu

@llvm/issue-subscribers-clang-driver

Author: Zi Xuan Wu (Zeson) (zixuan-wu)

getInstSizeInBytes estimate the size of compressed instruction incorrectly because the instructions between the branchrelax source and target may be relaxed in assembler later. In such case, unconditional jump(J instr) would be kept to assembler without being handled in BranchRelax Pass. But in the end, target addresss of J instr is out of range.
unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &amp;MI) const {

...

if (MI.getParent() &amp;&amp; MI.getParent()-&gt;getParent()) {
    if (isCompressibleInst(MI, STI))
      return 2;
 }
...

So it also needs to relax J/JAL in assembler to fix up the output of branchrelax process.

llvmbot avatar Jun 24 '24 03:06 llvmbot